baseUtils.js 13 KB

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