| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- // import errorCode from './errorCode'
- // import baseInfo from './baseInfo'
- // import {PRICE_IS_ROUND,SERVER_API_HTTP_TYPE,SERVER_API_DOMAIN,CDN_BASE_URL} from './config'
- import usersInfo from '@/utils/usersInfo'
- import errorCode from 'core-js/internals/internal-state'
- const tool = {
- /**
- * 设置JS存在客户端的Storage值
- * @param key
- * @param value
- */
- setStorage(key, value) {
- localStorage.setItem(key, value)
- },
- /**
- * 获取Storage值
- * @param key
- * @returns {string | null}
- */
- getStorage(key) {
- return localStorage.getItem(key)
- },
- /**
- * 移除Storage值
- * @param key
- */
- removeStorage(key) {
- localStorage.removeItem(key)
- },
- /**
- * 获取当前时间戳(精确到秒)
- * @returns {number}
- */
- getTimestamp(date = null) {
- const days = usersInfo.daysDiff()
- let dateObj
- if (date !== null) {
- dateObj = new Date(date)
- } else {
- dateObj = new Date()
- }
- return +Math.round(dateObj.getTime() / 1000 + (days * 86400))
- },
- formatDate(timestamp, withTime = true) {
- const newDate = new Date()
- timestamp = parseInt(timestamp)
- if (timestamp) {
- newDate.setTime(timestamp * 1000)
- } else {
- return ''
- }
- const Y = newDate.getFullYear() + '-'
- const M = (newDate.getMonth() + 1 < 10 ? '0' + (newDate.getMonth() + 1) : newDate.getMonth() + 1) + '-'
- const D = (newDate.getDate() < 10 ? '0' + (newDate.getDate()) : newDate.getDate()) + ' '
- const h = (newDate.getHours() < 10 ? '0' + newDate.getHours() : newDate.getHours()) + ':'
- const m = (newDate.getMinutes() < 10 ? '0' + newDate.getMinutes() : newDate.getMinutes()) + ':'
- const s = (newDate.getSeconds() < 10 ? '0' + newDate.getSeconds() : newDate.getSeconds())
- if (withTime) return Y + M + D + h + m + s
- return Y + M + D
- },
- /**
- * 处理错误结果
- * @param error
- * @returns {{message: string, todo: *}}
- */
- errorHandle(error) {
- let message = ''
- let status = 0
- let todo
- if (errorCode.has(error)) {
- const errorResult = errorCode.get(error)
- message = errorResult.message
- status = errorResult.status ? errorResult.status : 0
- todo = errorResult.todo
- } else {
- message = error.message
- todo = null
- status = error.data.status ? error.data.status : 0
- }
- if (todo || status === 401) {
- message = todo ? '未获得授权,请重新登录' : (message === 'Your request was made with invalid credentials.' ? '未获得授权,请重新登录' : message)
- usersInfo.logout()
- }
- if (todo || status === 402) {
- message = todo ? '长时间未进行操作,请重新登录' : (message === 'Connection not operated for too long' ? '长时间未进行操作,请重新登录' : message)
- usersInfo.logout()
- }
- return { message, todo, status }
- },
- /**
- * 解析URL地址
- * @param url
- * var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
- myURL.file; // = 'index.html'
- myURL.hash; // = 'top'
- myURL.host; // = 'abc.com'
- myURL.query; // = '?id=255&m=hello'
- myURL.params; // = Object = { id: 255, m: hello }
- myURL.path; // = '/dir/index.html'
- myURL.segments; // = Array = ['dir', 'index.html']
- myURL.port; // = '8080'
- myURL.protocol; // = 'http'
- myURL.source; // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'
- * @returns {{source: *, protocol: string, host: string, port: string, query: string, params, file: string | *, hash: string, path: string, relative: string | *, segments: string[]}}
- */
- parseURL(url) {
- const a = document.createElement('a')
- a.href = url
- return {
- source: url,
- protocol: a.protocol.replace(':', ''),
- host: a.hostname,
- port: a.port,
- query: a.search,
- params: (function() {
- const waitUrl = a.hash.replace(/^#\/[\-_a-zA-Z0-9]+\?/, '\?') || a.search
- const ret = {}
- const seg = waitUrl.replace(/^\?/, '').split('&')
- const len = seg.length
- let i = 0
- let s
- for (; i < len; i++) {
- if (!seg[i]) {
- continue
- }
- s = seg[i].split('=')
- ret[s[0]] = s[1]
- }
- return ret
- })(),
- file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
- hash: a.hash.replace('#', ''),
- path: a.pathname.replace(/^([^\/])/, '/$1'),
- relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
- segments: a.pathname.replace(/^\//, '').split('/')
- }
- },
- removeFromArr(arr, item) {
- for (let i = arr.length - 1; i >= 0; i--) {
- if (arr[i] === item) {
- arr.splice(i, 1)
- return arr
- }
- }
- },
- /**
- * 是否空对象
- * @param obj
- * @returns {boolean}
- */
- isEmptyObject(obj) {
- const objArr = Object.keys(obj)
- return objArr.length === 0
- },
- isInArray(arr, value) {
- for (const i in arr) {
- if (value === arr[i]) {
- return true
- }
- }
- return false
- },
- isString(val) {
- return typeof val === 'string' || val instanceof String
- },
- /**
- * 格式化金额数值为两位小数,是否四舍五入
- * @param val
- * @returns {string}
- */
- formatPrice(val) {
- val = Number.parseFloat(val)
- return val.toFixed(2)
- },
- /**
- * 根据状态返回颜色
- * @param val
- * @returns {string}
- */
- statusType(val) {
- switch (val) {
- case '0':
- return 'info'
- case '1':
- return ''
- case '2':
- return 'danger'
- case '3':
- return 'warning'
- case '4':
- return 'success'
- default:
- return 'info'
- }
- },
- sum(arr) { // 求数组总和
- var s = 0
- for (var i = arr.length - 1; i >= 0; i--) {
- s += arr[i]
- }
- return s
- },
- /**
- * 获取table显示高度
- * @param hasStatusBar
- * @returns {number}
- */
- getTableHeight(hasStatusBar = false) {
- if (hasStatusBar) return window.innerHeight - 320
- return window.innerHeight - 260
- },
- // /**
- // * 拼装图片地址.
- // * @param imageUrl
- // * @param path
- // * @returns {string}
- // */
- // getLocaleLink(imageUrl, path = '') {
- // return imageUrl.indexOf('http') > -1 ? imageUrl : `${process.env.VUE_APP_CDN_API}${path}${imageUrl}`
- // },
- // 计算商品税额
- calculateTax(amount, taxRate, count = 1) {
- const taxAmount = (amount - amount / (1 + taxRate / 100)) * count
- return Math.round(taxAmount * 100) / 100
- },
- // 计算商品BV
- calculateBV(amount, count = 1) {
- return Math.round(amount * count)
- },
- /**
- * 拼装图片
- * @param imageUrl 图片
- * @param path 路径
- * @returns {string}
- */
- getArImage(imageUrl, path = null) {
- return process.env.VUE_APP_CDN_API + `/${path}/${imageUrl}`
- },
- getEmpLv(id) {
- // return id.length > 0 ? getBaseInfo().empLevels : 'No Rank';
- },
- /**
- * 向地址来看
- * @param paramName
- * @param value
- */
- insertUrlParam(paramName, value) {
- if (typeof value === 'object') {
- if (tool.isEmptyObject(value)) {
- return
- }
- value = JSON.stringify(value)
- }
- if (window.location.href.indexOf(paramName) === -1) {
- if (window.location.href.indexOf('?') !== -1) {
- window.location.href += ('&' + paramName + '=' + value)
- } else {
- window.location.href += ('?' + paramName + '=' + value)
- }
- } else {
- const searchStr = new RegExp('(\\?|&)(' + paramName + '=)([^&]*)(&|$)')
- window.location.href = window.location.href.replace(searchStr, '$1$2' + value + '$4')
- }
- }
- }
- export default tool
|