const NULL_ARR = ['空', '没', '无', '-', '—', 'null', '#N/A'] /* * isBuy: 是否是立即购买 * event: 触发事件 * $this: 当前对象 * item: 操作对象 * */ const _buyOrCar = function (isBuy, event, $this, item, url = '/user#/order/pay/') { if (event) event.stopPropagation() if (!$this.$store.state.option.user.logged) { $this.$http.get('/login/page', {params: {returnUrl: window.location.href}}).then(response => { if (response.data) { window.location.href = response.data.content + '&baseUrl=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + response.data.baseUrl) } }) } else { if (item && !$this.disabledFlag) { if (isBuy) { $this.$http.post('/trade/order/buyNow', [{ uuid: item.uuid, batchCode: item.batchCode, number: item.minBuyQty, storeid: item.storeid ? item.storeid : item.storeId, storeUuid: item.storeid ? item.storeid : item.storeId, currencyName: item.currencyName, minPackQty: item.minPackQty }]) .then(response => { // window.location.href = '/user#/order/pay/' + $this.enidfilter(response.data.orderid) if (response.data.success) { if (response.data.message) { $this.$message({ message: response.data.message, type: 'success' }) window.setTimeout(function () { window.location.href = url + _enidfilter(response.data.data.orderid) }, 1000) } else { window.location.href = url + _enidfilter(response.data.data.orderid) } } else { if (response.data.data && response.data.data.unvailable === 1) { _setMessage($this, '产品信息已失效,请刷新页面', $this.isMobile, false) } else { _setMessage($this, response.data.message, $this.isMobile, false) } } }, err => { console.log(err) if (item.minBuyQty > item.reserve) { _setMessage($this, '商品' + item.code + '的库存已经不满足起订量', $this.isMobile, false) } }) } else { // $this.$store.dispatch('user/addCar', {uuid: item.uuid, batchCode: item.batchCode, number: item.minBuyQty}) $this.$http.post('/trade/cart/add', { uuid: item.uuid, batchCode: item.batchCode, number: item.minBuyQty, storeid: item.storeid ? item.storeid : item.storeId, storeUuid: item.storeid ? item.storeid : item.storeId, currencyName: item.currencyName, minPackQty: item.minPackQty }) .then(response => { if (response.data.success) { if (response.data.message) { _setMessage($this, '添加购物车成功,但商品信息有更新', $this.isMobile, true) } else { _setMessage($this, '添加购物车成功', $this.isMobile, true) } } else { // if (response.data.code === 2) { // $this.$message.error('库存已不满足起订量') // } else if (response.data.message === '该产品已失效') { _setMessage($this, response.data.message + ',请刷新页面', $this.isMobile, false) } else { _setMessage($this, response.data.message, $this.isMobile, false) } } }) } } } // window.location.href = 'user#/order/pay/' + $this.enidfilter($this.buy_info.orderid) } const _enidfilter = (str) => { if (str) { let encryptStr = '' // 最终返回的加密后的字符串 // 产生三位随机数 let num = '' for (let i = 0; i < 3; i++) { num += Math.floor(Math.random() * 10) } encryptStr += num // 产生3位随机数 // 16位加密 let tempspit = '' let strspit = str.toString().toLowerCase() if (strspit.match(/^[-+]?\d*$/) === null) { // 非整数字符,对每一个字符都转换成16进制,然后拼接 /** * Unicode汉字、英文字母、数字的unicode范围 *汉字:[0x4e00,0x9fa5](或十进制[19968,40869]) *数字:[0x30,0x39](或十进制[48, 57]) *小写字母:[0x61,0x7a](或十进制[97, 122]) *大写字母:[0x41,0x5a](或十进制[65, 90] * 'a'的Unicode编码:'a',charCodeAt()的值是97 * '码'的Unicode编码:'\u7801', new String('码').charCodeAt()的值是30721,30721的16进制表示是7801 */ let s = strspit.split('') for (let i = 0; i < s.length; i++) { s[i] = s[i].charCodeAt() // 先转换成Unicode编码 s[i] = s[i].toString(16) // 因为在服务器是每两位当做一个字符进行解析的,所以这里每个字符的Unicode编码范围必须在0——255之间。数字和大小写满足该要求,特殊字符则不一定,如果后续有特殊字符的要求,需要重写编码器和解码器 if (s[i].length === 1) { s[i] = '0' + s[i] } tempspit = tempspit + s[i] } tempspit = tempspit + '{' + 1 // 1代表字符 } else { // 数字直接转换成16进制 strspit = parseInt(strspit) .toString(16) tempspit = strspit + '{' + 0 // 0代表纯数字 } let temp = tempspit.split('{') // 对要加密的字符转换成16进制 let numLength = temp[0].length // 转换后的字符长度 numLength = numLength.toString(16) // 字符长度换算成16进制 if (numLength.length === 1) { // 如果是1,补一个0 numLength = '0' + numLength } else if (numLength.length > 3) { // 转换后的16进制字符长度如果大于2位数,则返回,不支持 return '' } encryptStr += numLength if (temp[1] === '0') { encryptStr += 0 } else if (temp[1] === '1') { encryptStr += 1 } encryptStr += temp[0] if (encryptStr.length < 20) { // 如果小于20位,补上随机数 // 产生三位随机数 let numtwo = '' for (let i = 0; i < 20 - encryptStr.length; i++) { numtwo += Math.floor(Math.random() * 10) } let ran = numtwo // 产生3位随机数 encryptStr += ran } return encryptStr } } /* * @str 字符串 * */ const _getRealLen = function (str) { let len = 0 for (let i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) { len += 2 } else { len++ } } return len } /* * @str 字符串 * @length 需要剪切的字符长度 * */ const _cutOutString = (str, length) => { for (let i = 1; i <= str.length; i++) { if (_getRealLen(str.substr(0, i)) > length) { str = str.substr(0, i - 1) break } } return str } const _spliceStr = (str, length) => { for (let i = 1; i <= str.length; i++) { if (_getRealLen(str.substr(0, i)) > length) { str = str.substr(0, i - 1) + '...' break } } return str } /* * @date 日期,既可以是Date格式,也可是字符串格式 * @fmt 需要转换的格式,如 'yyyy-MM-dd hh:mm:ss' * return 转换后的字符串格式的日期 * */ const _formatDate = (date, fmt) => { if (!date) { return null } if (typeof date === 'string') { date = new Date(Date.parse(date.replace(/-/g, '/'))) } let o = { 'M+': date.getMonth() + 1, // 月份 'd+': date.getDate(), // 日 'h+': 23, // 小时 'm+': 59, // 分 's+': 59, // 秒 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度 'S': date.getMilliseconds() // 毫秒 } if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) } for (let k in o) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) } } return fmt } /* * @str 传入的字符串 * */ const _checkNullStr = (str) => { // NULL_ARR.map(s => { // if (str === s) { // return false // } // }) for (let i = 0; i < NULL_ARR.length; i++) { if (str === NULL_ARR[i]) { return false } } return true } /* * @$this 当前上下文 * @enuu 企业uu * */ const _goLinkUser = ($this, enuu) => { if ($this.user.logged) { // $this.$http.get('/basic/enterprise/' + $this.storeInfo.enUU + '/info').then(response => { // if (response.data.enTel) { // $this.tel = response.data.enTel // $this.showLinkBox = true // } else { // $this.$http.get('/basic/enterprise/' + response.data.uu + '/admin').then(response => { // $this.tel = response.data.userTel // $this.showLinkBox = true // }, err => { // $this.$message.error('获取卖家联系方式失败') // console.log(err) // }) // } // $this.showLinkBox = true // }, err => { // $this.$message.error('获取卖家联系方式失败') // console.log(err) // }) $this.$http.get(`/basic/enterprise/${enuu}/tels`).then(response => { $this.tel = response.data.data.entel || response.data.data.adminTel $this.showLinkBox = true }, err => { $this.$message.error('获取卖家联系方式失败') throw err }) } else { $this.$router.push(`/auth/login?returnUrl=${window.location.href}`) } } /* * @str: 传入的字符串 * @s: 需要判断的字符串 * */ const _startWith = (str, s) => { let reg = new RegExp('^' + s) return reg.test(str) } /* * @path 文件路径地址 * */ const _judgeIsPdf = (path) => { if (path) { return path.slice(path.lastIndexOf('.')).toLowerCase() === '.pdf' } else { return false } } /* * @target 需要拷贝的对象 * */ const _deepCopy = (target) => { if (typeof target !== 'object') return // 判断目标类型,来创建返回值 var newObj = target instanceof Array ? [] : {} for (var item in target) { // 只复制元素自身的属性,不复制原型链上的 if (target.hasOwnProperty(item)) { newObj[item] = typeof target[item] === 'object' ? _deepCopy(target[item]) : target[item] } } return newObj } const _whichTransitionEvent = () => { let t let el = document.createElement('fakeelement') let transitions = { transition: 'transitionend', OTransition: 'oTransitionEnd', MozTransition: 'transitionend', WebkitTransition: 'webkitTransitionEnd' } for (t in transitions) { if (el.style[t] !== undefined) { return transitions[t] } } } /* * @str 剪切的字符串 * @len 长度设置 * */ const _filterStringEllipsis = (str, len) => { return str ? _getRealLen(str) > len ? _cutOutString(str, len) + '...' : str : null } /* * @$this 上下文 * @str 提示信息 * @isMobile 是否移动端 * */ const _setMessage = ($this, str = '系统错误', isMobile, isSuccess = false) => { if (isMobile) { $this.setRemindText(str) } else { if (isSuccess) { $this.$message.success(str) } else { $this.$message.error(str) } } } /* * @$this 上下文 * @url 权限链接 * @callBack 回调 * @isMobile 是否移动端 * */ const _getAuthority = ($this, url, callBack, isMobile) => { return $this.$http.get('/user/authentication/isauthority', {params: {resUrl: url}}).then(res => { if (res.data.status === 'fail') { _setMessage($this, res.data.msg, isMobile) } else { callBack.call($this) } }, err => { console.log(err) _setMessage($this, err.response.data, isMobile) }) } // 店铺类型筛选 const _storeTypeFilter = (type) => { let tmp = '' switch (type) { case 'CONSIGNMENT': tmp = '寄售' break case 'DISTRIBUTION': tmp = '经销' break case 'AGENCY': tmp = '代理' break case 'ORIGIN_FACTORY': tmp = '原厂' break } return tmp } const _getPriceByLevel = (prices, number, currency) => { let prName = currency === 'RMB' ? 'rMBPrice' : 'uSDPrice' for (let i = 0; i < prices.length; i++) { if (number >= prices[i].start && number <= prices[i].end) { return prices[i][prName] } } } const _exchangeArr = (arr, x, y) => { let tmp = arr[x] arr[x] = arr[y] arr[y] = tmp } const _filterDate = (timeStamp, type) => { if (type === 'day') { return Math.floor(timeStamp / (1000 * 60 * 60 * 24)) } else if (type === 'hour') { return Math.floor((timeStamp / (1000 * 60 * 60)) % 24) } else if (type === 'minute') { return Math.floor((timeStamp / (1000 * 60)) % 60) } } const _priceFixed = (price) => { let arr = price.toString().split('.') let adder = 0 let num = price if (arr[1] && arr[1].length > 2) { adder = 0.01 num = Number(arr[0] + '.' + arr[1].substring(0, 2)) } return Number((num + adder).toFixed(2)) } export default { // 获取字符串字符长度 getRealLen: _getRealLen, // 根据字符长度剪切字符 cutOutString: _cutOutString, // 订单号转换 enidfilter: _enidfilter, // 根据字符长度剪切字符串 spliceStr: _spliceStr, // 格式化日期,返回字符串 formatDate: _formatDate, // 检查空字符串或规定状态下空字符 checkNullStr: _checkNullStr, // 联系卖家 goLinkUser: _goLinkUser, // 判断字符串开头 startWith: _startWith, // 根据path文件名来判断文件是否是PDF文件 judgeIsPdf: _judgeIsPdf, // 深拷贝对象 deepCopy: _deepCopy, whichTransitionEvent: _whichTransitionEvent, // 立即购买或加入购物车 buyOrCar: _buyOrCar, // 截取字符长度并补充省略号 filterStringEllipsis: _filterStringEllipsis, // 获取url权限 getAuthority: _getAuthority, // 获得店铺类型中文名 storeTypeFilter: _storeTypeFilter, // 通过分段数量获取分段价格 getPriceByLevel: _getPriceByLevel, // 交换数组元素位置 exchangeArr: _exchangeArr, // 根据时间戳提取天数、时、分 filterDate: _filterDate, // 计算标准订单金额 priceFixed: _priceFixed, setMessage: _setMessage }