baseUtils.js 11 KB

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