baseUtils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. const NULL_ARR = ['空', '没', '无', '-', '—', 'null', '#N/A']
  2. // 立即购买或加入购物车
  3. /*
  4. * isBuy: 是否是立即购买
  5. * event: 触发事件
  6. * $this: 当前对象
  7. * item: 操作对象
  8. * */
  9. const _buyNow = function (isBuy, event, $this, item) {
  10. if (event) event.stopPropagation()
  11. if (!$this.$store.state.option.user.logged) {
  12. $this.$http.get('/login/page', {params: {returnUrl: window.location.href}}).then(response => {
  13. if (response.data) {
  14. window.location.href = response.data.content + '&baseUrl=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + response.data.baseUrl)
  15. }
  16. })
  17. } else {
  18. if (item && !$this.disabledFlag) {
  19. if (isBuy) {
  20. $this.$http.post('/trade/order/buyNow', [{
  21. uuid: item.uuid,
  22. batchCode: item.batchCode,
  23. number: item.minBuyQty,
  24. storeid: item.storeid ? item.storeid : item.storeId,
  25. storeUuid: item.storeid ? item.storeid : item.storeId,
  26. currencyName: item.currencyName,
  27. minPackQty: item.minPackQty
  28. }])
  29. .then(response => {
  30. // window.location.href = '/user#/order/pay/' + $this.enidfilter(response.data.orderid)
  31. if (response.data.success) {
  32. if (response.data.message) {
  33. $this.$message({
  34. message: response.data.message,
  35. type: 'success'
  36. })
  37. window.setTimeout(function () {
  38. window.location.href = '/user#/order/pay/' + _enidfilter(response.data.data.orderid)
  39. }, 1000)
  40. } else {
  41. window.location.href = '/user#/order/pay/' + _enidfilter(response.data.data.orderid)
  42. }
  43. } else {
  44. if (response.data.data && response.data.data.unvailable === 1) {
  45. $this.$message.error('产品信息已失效,请刷新页面')
  46. } else {
  47. $this.$message.error(response.data.message)
  48. }
  49. }
  50. }, err => {
  51. console.log(err)
  52. if (item.minBuyQty > item.reserve) {
  53. $this.$message.error('商品' + item.code + '的库存已经不满足起订量')
  54. }
  55. })
  56. } else {
  57. // $this.$store.dispatch('user/addCar', {uuid: item.uuid, batchCode: item.batchCode, number: item.minBuyQty})
  58. $this.$http.post('/trade/cart/add', {
  59. uuid: item.uuid,
  60. batchCode: item.batchCode,
  61. number: item.minBuyQty,
  62. storeid: item.storeid ? item.storeid : item.storeId,
  63. storeUuid: item.storeid ? item.storeid : item.storeId,
  64. currencyName: item.currencyName,
  65. minPackQty: item.minPackQty
  66. })
  67. .then(response => {
  68. if (response.data.success) {
  69. if (response.data.message) {
  70. $this.$message({
  71. message: '添加购物车成功,但商品信息有更新',
  72. type: 'success'
  73. })
  74. } else {
  75. $this.$message({
  76. message: '添加购物车成功',
  77. type: 'success'
  78. })
  79. }
  80. } else {
  81. // if (response.data.code === 2) {
  82. // $this.$message.error('库存已不满足起订量')
  83. // } else
  84. if (response.data.message === '该产品已失效') {
  85. $this.$message.error(response.data.message + ',请刷新页面')
  86. } else {
  87. $this.$message.error(response.data.message)
  88. }
  89. }
  90. })
  91. }
  92. }
  93. }
  94. // window.location.href = 'user#/order/pay/' + $this.enidfilter($this.buy_info.orderid)
  95. }
  96. // 订单号转换
  97. const _enidfilter = (str) => {
  98. if (str) {
  99. let encryptStr = '' // 最终返回的加密后的字符串
  100. // 产生三位随机数
  101. let num = ''
  102. for (let i = 0; i < 3; i++) {
  103. num += Math.floor(Math.random() * 10)
  104. }
  105. encryptStr += num // 产生3位随机数
  106. // 16位加密
  107. let tempspit = ''
  108. let strspit = str.toString().toLowerCase()
  109. if (strspit.match(/^[-+]?\d*$/) === null) { // 非整数字符,对每一个字符都转换成16进制,然后拼接
  110. /**
  111. * Unicode汉字、英文字母、数字的unicode范围
  112. *汉字:[0x4e00,0x9fa5](或十进制[19968,40869])
  113. *数字:[0x30,0x39](或十进制[48, 57])
  114. *小写字母:[0x61,0x7a](或十进制[97, 122])
  115. *大写字母:[0x41,0x5a](或十进制[65, 90]
  116. * 'a'的Unicode编码:'&#97;',charCodeAt()的值是97
  117. * '码'的Unicode编码:'\u7801', new String('码').charCodeAt()的值是30721,30721的16进制表示是7801
  118. */
  119. let s = strspit.split('')
  120. for (let i = 0; i < s.length; i++) {
  121. s[i] = s[i].charCodeAt() // 先转换成Unicode编码
  122. s[i] = s[i].toString(16)
  123. // 因为在服务器是每两位当做一个字符进行解析的,所以这里每个字符的Unicode编码范围必须在0——255之间。数字和大小写满足该要求,特殊字符则不一定,如果后续有特殊字符的要求,需要重写编码器和解码器
  124. if (s[i].length === 1) {
  125. s[i] = '0' + s[i]
  126. }
  127. tempspit = tempspit + s[i]
  128. }
  129. tempspit = tempspit + '{' + 1 // 1代表字符
  130. } else { // 数字直接转换成16进制
  131. strspit = parseInt(strspit)
  132. .toString(16)
  133. tempspit = strspit + '{' + 0 // 0代表纯数字
  134. }
  135. let temp = tempspit.split('{') // 对要加密的字符转换成16进制
  136. let numLength = temp[0].length // 转换后的字符长度
  137. numLength = numLength.toString(16) // 字符长度换算成16进制
  138. if (numLength.length === 1) { // 如果是1,补一个0
  139. numLength = '0' + numLength
  140. } else if (numLength.length > 3) { // 转换后的16进制字符长度如果大于2位数,则返回,不支持
  141. return ''
  142. }
  143. encryptStr += numLength
  144. if (temp[1] === '0') {
  145. encryptStr += 0
  146. } else if (temp[1] === '1') {
  147. encryptStr += 1
  148. }
  149. encryptStr += temp[0]
  150. if (encryptStr.length < 20) { // 如果小于20位,补上随机数
  151. // 产生三位随机数
  152. let numtwo = ''
  153. for (let i = 0; i < 20 - encryptStr.length; i++) {
  154. numtwo += Math.floor(Math.random() * 10)
  155. }
  156. let ran = numtwo // 产生3位随机数
  157. encryptStr += ran
  158. }
  159. return encryptStr
  160. }
  161. }
  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. export const enidfilter = _enidfilter
  175. const _cutOutString = (str, length) => {
  176. for (let i = 1; i <= str.length; i++) {
  177. if (_getRealLen(str.substr(0, i)) > length) {
  178. str = str.substr(0, i - 1)
  179. break
  180. }
  181. }
  182. return str
  183. }
  184. // 获取字符串字符长度
  185. export const getRealLen = _getRealLen
  186. // 根据字符长度剪切字符
  187. export const cutOutString = _cutOutString
  188. // 根据字符长度剪切字符
  189. export const spliceStr = (str, length) => {
  190. for (let i = 1; i <= str.length; i++) {
  191. if (_getRealLen(str.substr(0, i)) > length) {
  192. str = str.substr(0, i - 1) + '...'
  193. break
  194. }
  195. }
  196. return str
  197. }
  198. // 格式化日期,返回字符串
  199. export const formatDate = (date, fmt) => {
  200. if (!date) {
  201. return null
  202. }
  203. if (typeof date === 'string') {
  204. date = new Date(Date.parse(date.replace(/-/g, '/')))
  205. }
  206. let o = {
  207. 'M+': date.getMonth() + 1, // 月份
  208. 'd+': date.getDate(), // 日
  209. 'h+': 23, // 小时
  210. 'm+': 59, // 分
  211. 's+': 59, // 秒
  212. 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
  213. 'S': date.getMilliseconds() // 毫秒
  214. }
  215. if (/(y+)/.test(fmt)) {
  216. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  217. }
  218. for (let k in o) {
  219. if (new RegExp('(' + k + ')').test(fmt)) {
  220. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  221. }
  222. }
  223. return fmt
  224. }
  225. // 检查空字符串或规定状态下空字符
  226. export const checkNullStr = (str) => {
  227. // NULL_ARR.map(s => {
  228. // if (str === s) {
  229. // return false
  230. // }
  231. // })
  232. for (let i = 0; i < NULL_ARR.length; i++) {
  233. if (str === NULL_ARR[i]) {
  234. return false
  235. }
  236. }
  237. return true
  238. }
  239. // 联系卖家
  240. export const goLinkUser = ($this, enuu) => {
  241. if ($this.user.logged) {
  242. // $this.$http.get('/basic/enterprise/' + $this.storeInfo.enUU + '/info').then(response => {
  243. // if (response.data.enTel) {
  244. // $this.tel = response.data.enTel
  245. // $this.showLinkBox = true
  246. // } else {
  247. // $this.$http.get('/basic/enterprise/' + response.data.uu + '/admin').then(response => {
  248. // $this.tel = response.data.userTel
  249. // $this.showLinkBox = true
  250. // }, err => {
  251. // $this.$message.error('获取卖家联系方式失败')
  252. // console.log(err)
  253. // })
  254. // }
  255. // $this.showLinkBox = true
  256. // }, err => {
  257. // $this.$message.error('获取卖家联系方式失败')
  258. // console.log(err)
  259. // })
  260. $this.$http.get(`/basic/enterprise/${enuu}/tels`).then(response => {
  261. $this.tel = response.data.data.entel || response.data.data.adminTel
  262. $this.showLinkBox = true
  263. }, err => {
  264. $this.$message.error('获取卖家联系方式失败')
  265. throw err
  266. })
  267. } else {
  268. $this.$router.push(`/auth/login?returnUrl=${window.location.href}`)
  269. }
  270. }
  271. // 判断字符串开头
  272. export const startWith = function (str, s) {
  273. let reg = new RegExp('^' + s)
  274. return reg.test(str)
  275. }
  276. // 根据path文件名来判断文件是否是PDF文件
  277. export const judgeIsPdf = function (path) {
  278. if (path) {
  279. return path.slice(path.lastIndexOf('.')).toLowerCase() === '.pdf'
  280. } else {
  281. return false
  282. }
  283. }
  284. // 实现深拷贝
  285. export const deepCopy = function (target) {
  286. if (typeof target !== 'object') return
  287. // 判断目标类型,来创建返回值
  288. var newObj = target instanceof Array ? [] : {}
  289. for (var item in target) {
  290. // 只复制元素自身的属性,不复制原型链上的
  291. if (target.hasOwnProperty(item)) {
  292. newObj[item] = typeof target[item] === 'object' ? deepCopy(target[item]) : target[item]
  293. }
  294. }
  295. return newObj
  296. }
  297. export function whichTransitionEvent() {
  298. var t
  299. var el = document.createElement('fakeelement')
  300. var transitions = {
  301. transition: 'transitionend',
  302. OTransition: 'oTransitionEnd',
  303. MozTransition: 'transitionend',
  304. WebkitTransition: 'webkitTransitionEnd'
  305. }
  306. for (t in transitions) {
  307. if (el.style[t] !== undefined) {
  308. return transitions[t]
  309. }
  310. }
  311. }
  312. export const buyOrCar = _buyNow
  313. export const filterStringEllipsis = (str, len) => {
  314. return str ? _getRealLen(str) > len ? _cutOutString(str, len) + '...' : str : null
  315. }