baseUtils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. const NULL_ARR = ['空', '没', '无', '-', '—', 'null', '#N/A']
  2. /*
  3. * isBuy: 是否是立即购买
  4. * event: 触发事件
  5. * $this: 当前对象
  6. * item: 操作对象
  7. * */
  8. const _buyOrCar = function (isBuy, event, $this, item, url = '/user#/order/pay/') {
  9. if (event) event.stopPropagation()
  10. if (!$this.$store.state.option.user.logged) {
  11. $this.$http.get('/login/page', {params: {returnUrl: window.location.href}}).then(response => {
  12. if (response.data) {
  13. window.location.href = response.data.content + '&baseUrl=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + response.data.baseUrl)
  14. }
  15. })
  16. } else {
  17. if (item && !$this.disabledFlag) {
  18. if (isBuy) {
  19. $this.$http.post('/trade/order/buyNow', [{
  20. uuid: item.uuid,
  21. batchCode: item.batchCode,
  22. number: item.minBuyQty,
  23. storeid: item.storeid ? item.storeid : item.storeId,
  24. storeUuid: item.storeid ? item.storeid : item.storeId,
  25. currencyName: item.currencyName,
  26. minPackQty: item.minPackQty
  27. }])
  28. .then(response => {
  29. // window.location.href = '/user#/order/pay/' + $this.enidfilter(response.data.orderid)
  30. if (response.data.success) {
  31. if (response.data.message) {
  32. $this.$message({
  33. message: response.data.message,
  34. type: 'success'
  35. })
  36. window.setTimeout(function () {
  37. window.location.href = url + _enidfilter(response.data.data.orderid)
  38. }, 1000)
  39. } else {
  40. window.location.href = url + _enidfilter(response.data.data.orderid)
  41. }
  42. } else {
  43. if (response.data.data && response.data.data.unvailable === 1) {
  44. _setMessage($this, '产品信息已失效,请刷新页面', $this.isMobile, false)
  45. } else {
  46. _setMessage($this, response.data.message, $this.isMobile, false)
  47. }
  48. }
  49. }, err => {
  50. console.log(err)
  51. if (item.minBuyQty > item.reserve) {
  52. _setMessage($this, '商品' + item.code + '的库存已经不满足起订量', $this.isMobile, false)
  53. }
  54. })
  55. } else {
  56. // $this.$store.dispatch('user/addCar', {uuid: item.uuid, batchCode: item.batchCode, number: item.minBuyQty})
  57. $this.$http.post('/trade/cart/add', {
  58. uuid: item.uuid,
  59. batchCode: item.batchCode,
  60. number: item.minBuyQty,
  61. storeid: item.storeid ? item.storeid : item.storeId,
  62. storeUuid: item.storeid ? item.storeid : item.storeId,
  63. currencyName: item.currencyName,
  64. minPackQty: item.minPackQty
  65. })
  66. .then(response => {
  67. if (response.data.success) {
  68. if (response.data.message) {
  69. _setMessage($this, '添加购物车成功,但商品信息有更新', $this.isMobile, true)
  70. } else {
  71. _setMessage($this, '添加购物车成功', $this.isMobile, true)
  72. }
  73. } else {
  74. // if (response.data.code === 2) {
  75. // $this.$message.error('库存已不满足起订量')
  76. // } else
  77. if (response.data.message === '该产品已失效') {
  78. _setMessage($this, response.data.message + ',请刷新页面', $this.isMobile, false)
  79. } else {
  80. _setMessage($this, response.data.message, $this.isMobile, false)
  81. }
  82. }
  83. })
  84. }
  85. }
  86. }
  87. // window.location.href = 'user#/order/pay/' + $this.enidfilter($this.buy_info.orderid)
  88. }
  89. const _enidfilter = (str) => {
  90. if (str) {
  91. let encryptStr = '' // 最终返回的加密后的字符串
  92. // 产生三位随机数
  93. let num = ''
  94. for (let i = 0; i < 3; i++) {
  95. num += Math.floor(Math.random() * 10)
  96. }
  97. encryptStr += num // 产生3位随机数
  98. // 16位加密
  99. let tempspit = ''
  100. let strspit = str.toString().toLowerCase()
  101. if (strspit.match(/^[-+]?\d*$/) === null) { // 非整数字符,对每一个字符都转换成16进制,然后拼接
  102. /**
  103. * Unicode汉字、英文字母、数字的unicode范围
  104. *汉字:[0x4e00,0x9fa5](或十进制[19968,40869])
  105. *数字:[0x30,0x39](或十进制[48, 57])
  106. *小写字母:[0x61,0x7a](或十进制[97, 122])
  107. *大写字母:[0x41,0x5a](或十进制[65, 90]
  108. * 'a'的Unicode编码:'&#97;',charCodeAt()的值是97
  109. * '码'的Unicode编码:'\u7801', new String('码').charCodeAt()的值是30721,30721的16进制表示是7801
  110. */
  111. let s = strspit.split('')
  112. for (let i = 0; i < s.length; i++) {
  113. s[i] = s[i].charCodeAt() // 先转换成Unicode编码
  114. s[i] = s[i].toString(16)
  115. // 因为在服务器是每两位当做一个字符进行解析的,所以这里每个字符的Unicode编码范围必须在0——255之间。数字和大小写满足该要求,特殊字符则不一定,如果后续有特殊字符的要求,需要重写编码器和解码器
  116. if (s[i].length === 1) {
  117. s[i] = '0' + s[i]
  118. }
  119. tempspit = tempspit + s[i]
  120. }
  121. tempspit = tempspit + '{' + 1 // 1代表字符
  122. } else { // 数字直接转换成16进制
  123. strspit = parseInt(strspit)
  124. .toString(16)
  125. tempspit = strspit + '{' + 0 // 0代表纯数字
  126. }
  127. let temp = tempspit.split('{') // 对要加密的字符转换成16进制
  128. let numLength = temp[0].length // 转换后的字符长度
  129. numLength = numLength.toString(16) // 字符长度换算成16进制
  130. if (numLength.length === 1) { // 如果是1,补一个0
  131. numLength = '0' + numLength
  132. } else if (numLength.length > 3) { // 转换后的16进制字符长度如果大于2位数,则返回,不支持
  133. return ''
  134. }
  135. encryptStr += numLength
  136. if (temp[1] === '0') {
  137. encryptStr += 0
  138. } else if (temp[1] === '1') {
  139. encryptStr += 1
  140. }
  141. encryptStr += temp[0]
  142. if (encryptStr.length < 20) { // 如果小于20位,补上随机数
  143. // 产生三位随机数
  144. let numtwo = ''
  145. for (let i = 0; i < 20 - encryptStr.length; i++) {
  146. numtwo += Math.floor(Math.random() * 10)
  147. }
  148. let ran = numtwo // 产生3位随机数
  149. encryptStr += ran
  150. }
  151. return encryptStr
  152. }
  153. }
  154. /*
  155. * @str 字符串
  156. * */
  157. const _getRealLen = function (str) {
  158. let len = 0
  159. for (let i = 0; i < str.length; i++) {
  160. if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
  161. len += 2
  162. } else {
  163. len++
  164. }
  165. }
  166. return len
  167. }
  168. /*
  169. * @str 字符串
  170. * @length 需要剪切的字符长度
  171. * */
  172. const _cutOutString = (str, length) => {
  173. for (let i = 1; i <= str.length; i++) {
  174. if (_getRealLen(str.substr(0, i)) > length) {
  175. str = str.substr(0, i - 1)
  176. break
  177. }
  178. }
  179. return str
  180. }
  181. const _spliceStr = (str, length) => {
  182. for (let i = 1; i <= str.length; i++) {
  183. if (_getRealLen(str.substr(0, i)) > length) {
  184. str = str.substr(0, i - 1) + '...'
  185. break
  186. }
  187. }
  188. return str
  189. }
  190. /*
  191. * @date 日期,既可以是Date格式,也可是字符串格式
  192. * @fmt 需要转换的格式,如 'yyyy-MM-dd hh:mm:ss'
  193. * return 转换后的字符串格式的日期
  194. * */
  195. const _formatDate = (date, fmt) => {
  196. if (!date) {
  197. return null
  198. }
  199. if (typeof date === 'string') {
  200. date = new Date(Date.parse(date.replace(/-/g, '/')))
  201. }
  202. let o = {
  203. 'M+': date.getMonth() + 1, // 月份
  204. 'd+': date.getDate(), // 日
  205. 'h+': 23, // 小时
  206. 'm+': 59, // 分
  207. 's+': 59, // 秒
  208. 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
  209. 'S': date.getMilliseconds() // 毫秒
  210. }
  211. if (/(y+)/.test(fmt)) {
  212. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  213. }
  214. for (let k in o) {
  215. if (new RegExp('(' + k + ')').test(fmt)) {
  216. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  217. }
  218. }
  219. return fmt
  220. }
  221. /*
  222. * @str 传入的字符串
  223. * */
  224. const _checkNullStr = (str) => {
  225. // NULL_ARR.map(s => {
  226. // if (str === s) {
  227. // return false
  228. // }
  229. // })
  230. for (let i = 0; i < NULL_ARR.length; i++) {
  231. if (str === NULL_ARR[i]) {
  232. return false
  233. }
  234. }
  235. return true
  236. }
  237. /*
  238. * @$this 当前上下文
  239. * @enuu 企业uu
  240. * */
  241. const _goLinkUser = ($this, enuu) => {
  242. if ($this.user.logged) {
  243. // $this.$http.get('/basic/enterprise/' + $this.storeInfo.enUU + '/info').then(response => {
  244. // if (response.data.enTel) {
  245. // $this.tel = response.data.enTel
  246. // $this.showLinkBox = true
  247. // } else {
  248. // $this.$http.get('/basic/enterprise/' + response.data.uu + '/admin').then(response => {
  249. // $this.tel = response.data.userTel
  250. // $this.showLinkBox = true
  251. // }, err => {
  252. // $this.$message.error('获取卖家联系方式失败')
  253. // console.log(err)
  254. // })
  255. // }
  256. // $this.showLinkBox = true
  257. // }, err => {
  258. // $this.$message.error('获取卖家联系方式失败')
  259. // console.log(err)
  260. // })
  261. $this.$http.get(`/basic/enterprise/${enuu}/tels`).then(response => {
  262. $this.tel = response.data.data.entel || response.data.data.adminTel
  263. $this.showLinkBox = true
  264. }, err => {
  265. $this.$message.error('获取卖家联系方式失败')
  266. throw err
  267. })
  268. } else {
  269. $this.$router.push(`/auth/login?returnUrl=${window.location.href}`)
  270. }
  271. }
  272. /*
  273. * @str: 传入的字符串
  274. * @s: 需要判断的字符串
  275. * */
  276. const _startWith = (str, s) => {
  277. let reg = new RegExp('^' + s)
  278. return reg.test(str)
  279. }
  280. /*
  281. * @path 文件路径地址
  282. * */
  283. const _judgeIsPdf = (path) => {
  284. if (path) {
  285. return path.slice(path.lastIndexOf('.')).toLowerCase() === '.pdf'
  286. } else {
  287. return false
  288. }
  289. }
  290. /*
  291. * @target 需要拷贝的对象
  292. * */
  293. const _deepCopy = (target) => {
  294. if (typeof target !== 'object') return
  295. // 判断目标类型,来创建返回值
  296. var newObj = target instanceof Array ? [] : {}
  297. for (var item in target) {
  298. // 只复制元素自身的属性,不复制原型链上的
  299. if (target.hasOwnProperty(item)) {
  300. newObj[item] = typeof target[item] === 'object' ? _deepCopy(target[item]) : target[item]
  301. }
  302. }
  303. return newObj
  304. }
  305. const _whichTransitionEvent = () => {
  306. let t
  307. let el = document.createElement('fakeelement')
  308. let transitions = {
  309. transition: 'transitionend',
  310. OTransition: 'oTransitionEnd',
  311. MozTransition: 'transitionend',
  312. WebkitTransition: 'webkitTransitionEnd'
  313. }
  314. for (t in transitions) {
  315. if (el.style[t] !== undefined) {
  316. return transitions[t]
  317. }
  318. }
  319. }
  320. /*
  321. * @str 剪切的字符串
  322. * @len 长度设置
  323. * */
  324. const _filterStringEllipsis = (str, len) => {
  325. return str ? _getRealLen(str) > len ? _cutOutString(str, len) + '...' : str : null
  326. }
  327. /*
  328. * @$this 上下文
  329. * @str 提示信息
  330. * @isMobile 是否移动端
  331. * */
  332. const _setMessage = ($this, str = '系统错误', isMobile, isSuccess = false) => {
  333. if (isMobile) {
  334. $this.setRemindText(str)
  335. } else {
  336. if (isSuccess) {
  337. $this.$message.success(str)
  338. } else {
  339. $this.$message.error(str)
  340. }
  341. }
  342. }
  343. /*
  344. * @$this 上下文
  345. * @url 权限链接
  346. * @callBack 回调
  347. * @isMobile 是否移动端
  348. * */
  349. const _getAuthority = ($this, url, callBack, isMobile) => {
  350. return $this.$http.get('/user/authentication/isauthority', {params: {resUrl: url}}).then(res => {
  351. if (res.data.status === 'fail') {
  352. _setMessage($this, res.data.msg, isMobile)
  353. } else {
  354. callBack.call($this)
  355. }
  356. }, err => {
  357. console.log(err)
  358. _setMessage($this, err.response.data, isMobile)
  359. })
  360. }
  361. // 店铺类型筛选
  362. const _storeTypeFilter = (type) => {
  363. let tmp = ''
  364. switch (type) {
  365. case 'CONSIGNMENT':
  366. tmp = '寄售'
  367. break
  368. case 'DISTRIBUTION':
  369. tmp = '经销'
  370. break
  371. case 'AGENCY':
  372. tmp = '代理'
  373. break
  374. case 'ORIGIN_FACTORY':
  375. tmp = '原厂'
  376. break
  377. }
  378. return tmp
  379. }
  380. const _getPriceByLevel = (prices, number, currency) => {
  381. let prName = currency === 'RMB' ? 'rMBPrice' : 'uSDPrice'
  382. for (let i = 0; i < prices.length; i++) {
  383. if (number >= prices[i].start && number <= prices[i].end) {
  384. return prices[i][prName]
  385. }
  386. }
  387. }
  388. const _exchangeArr = (arr, x, y) => {
  389. let tmp = arr[x]
  390. arr[x] = arr[y]
  391. arr[y] = tmp
  392. }
  393. const _filterDate = (timeStamp, type) => {
  394. if (type === 'day') {
  395. return Math.floor(timeStamp / (1000 * 60 * 60 * 24))
  396. } else if (type === 'hour') {
  397. return Math.floor((timeStamp / (1000 * 60 * 60)) % 24)
  398. } else if (type === 'minute') {
  399. return Math.floor((timeStamp / (1000 * 60)) % 60)
  400. }
  401. }
  402. const _priceFixed = (price) => {
  403. let arr = price.toString().split('.')
  404. let adder = 0
  405. let num = price
  406. if (arr[1] && arr[1].length > 2) {
  407. adder = 0.01
  408. num = Number(arr[0] + '.' + arr[1].substring(0, 2))
  409. }
  410. return Number((num + adder).toFixed(2))
  411. }
  412. export default {
  413. // 获取字符串字符长度
  414. getRealLen: _getRealLen,
  415. // 根据字符长度剪切字符
  416. cutOutString: _cutOutString,
  417. // 订单号转换
  418. enidfilter: _enidfilter,
  419. // 根据字符长度剪切字符串
  420. spliceStr: _spliceStr,
  421. // 格式化日期,返回字符串
  422. formatDate: _formatDate,
  423. // 检查空字符串或规定状态下空字符
  424. checkNullStr: _checkNullStr,
  425. // 联系卖家
  426. goLinkUser: _goLinkUser,
  427. // 判断字符串开头
  428. startWith: _startWith,
  429. // 根据path文件名来判断文件是否是PDF文件
  430. judgeIsPdf: _judgeIsPdf,
  431. // 深拷贝对象
  432. deepCopy: _deepCopy,
  433. whichTransitionEvent: _whichTransitionEvent,
  434. // 立即购买或加入购物车
  435. buyOrCar: _buyOrCar,
  436. // 截取字符长度并补充省略号
  437. filterStringEllipsis: _filterStringEllipsis,
  438. // 获取url权限
  439. getAuthority: _getAuthority,
  440. // 获得店铺类型中文名
  441. storeTypeFilter: _storeTypeFilter,
  442. // 通过分段数量获取分段价格
  443. getPriceByLevel: _getPriceByLevel,
  444. // 交换数组元素位置
  445. exchangeArr: _exchangeArr,
  446. // 根据时间戳提取天数、时、分
  447. filterDate: _filterDate,
  448. // 计算标准订单金额
  449. priceFixed: _priceFixed,
  450. setMessage: _setMessage
  451. }