buyComponent.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <div>
  3. <button class="btn btn-primary btn-buy-now" @click="buyNow(true)"><span class="watch">立即购买</span></button>
  4. <button class="btn btn-add-cart" @click="buyNow(false)"><span class="watch">加入购物车</span></button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: ['item'],
  10. methods: {
  11. buyNow: function (isBuy) {
  12. if (!this.$store.state.option.user.logged) {
  13. this.$http.get('/login/page').then(response => {
  14. if (response.data) {
  15. this.$router.push('/auth/login')
  16. }
  17. })
  18. } else {
  19. if (this.item) {
  20. console.log(this.item)
  21. // this.$emit('buyAction', [{uuid: item.uuid, batchCode: item.batchCode, number: item.minBuyQty}])
  22. if (isBuy) {
  23. // this.$store.dispatch('user/getBuyInfo', [{uuid: item.uuid, batchCode: item.batchCode, number: item.minBuyQty}])
  24. this.$http.post('trade/order/buyNow', [{
  25. uuid: this.item.uuid,
  26. batchCode: this.item.batchCode,
  27. number: this.item.minBuyQty
  28. }])
  29. .then(response => {
  30. window.location.href = '/user#/order/pay/' + this.enidfilter(response.data.orderid)
  31. }, err => {
  32. console.log(err)
  33. })
  34. } else {
  35. // this.$store.dispatch('user/addCar', {uuid: item.uuid, batchCode: item.batchCode, number: item.minBuyQty})
  36. this.$http.post('trade/cart/add', {
  37. uuid: this.item.uuid,
  38. batchCode: this.item.batchCode,
  39. number: this.item.minBuyQty
  40. })
  41. .then(response => {
  42. console.log(response.data)
  43. if (response.data.success) {
  44. this.$message({
  45. message: '添加购物车成功',
  46. type: 'success'
  47. })
  48. } else {
  49. this.$message.error(response.data.message)
  50. // console.log(response.data.message)
  51. }
  52. })
  53. }
  54. }
  55. }
  56. // window.location.href = 'user#/order/pay/' + this.enidfilter(this.buy_info.orderid)
  57. },
  58. enidfilter: function (str) {
  59. if (str) {
  60. let encryptStr = '' // 最终返回的加密后的字符串
  61. // 产生三位随机数
  62. let num = ''
  63. for (let i = 0; i < 3; i++) {
  64. num += Math.floor(Math.random() * 10)
  65. }
  66. encryptStr += num // 产生3位随机数
  67. // 16位加密
  68. let tempspit = ''
  69. let strspit = str.toString().toLowerCase()
  70. if (strspit.match(/^[-+]?\d*$/) === null) { // 非整数字符,对每一个字符都转换成16进制,然后拼接
  71. /**
  72. * Unicode汉字、英文字母、数字的unicode范围
  73. *汉字:[0x4e00,0x9fa5](或十进制[19968,40869])
  74. *数字:[0x30,0x39](或十进制[48, 57])
  75. *小写字母:[0x61,0x7a](或十进制[97, 122])
  76. *大写字母:[0x41,0x5a](或十进制[65, 90]
  77. * 'a'的Unicode编码:'&#97;',charCodeAt()的值是97
  78. * '码'的Unicode编码:'\u7801', new String('码').charCodeAt()的值是30721,30721的16进制表示是7801
  79. */
  80. let s = strspit.split('')
  81. for (let i = 0; i < s.length; i++) {
  82. s[i] = s[i].charCodeAt() // 先转换成Unicode编码
  83. s[i] = s[i].toString(16)
  84. // 因为在服务器是每两位当做一个字符进行解析的,所以这里每个字符的Unicode编码范围必须在0——255之间。数字和大小写满足该要求,特殊字符则不一定,如果后续有特殊字符的要求,需要重写编码器和解码器
  85. if (s[i].length === 1) {
  86. s[i] = '0' + s[i]
  87. }
  88. tempspit = tempspit + s[i]
  89. }
  90. tempspit = tempspit + '{' + 1 // 1代表字符
  91. } else { // 数字直接转换成16进制
  92. strspit = parseInt(strspit)
  93. .toString(16)
  94. tempspit = strspit + '{' + 0 // 0代表纯数字
  95. }
  96. let temp = tempspit.split('{') // 对要加密的字符转换成16进制
  97. let numLength = temp[0].length // 转换后的字符长度
  98. numLength = numLength.toString(16) // 字符长度换算成16进制
  99. if (numLength.length === 1) { // 如果是1,补一个0
  100. numLength = '0' + numLength
  101. } else if (numLength.length > 3) { // 转换后的16进制字符长度如果大于2位数,则返回,不支持
  102. return ''
  103. }
  104. encryptStr += numLength
  105. if (temp[1] === '0') {
  106. encryptStr += 0
  107. } else if (temp[1] === '1') {
  108. encryptStr += 1
  109. }
  110. encryptStr += temp[0]
  111. if (encryptStr.length < 20) { // 如果小于20位,补上随机数
  112. // 产生三位随机数
  113. let numtwo = ''
  114. for (let i = 0; i < 20 - encryptStr.length; i++) {
  115. numtwo += Math.floor(Math.random() * 10)
  116. }
  117. let ran = numtwo // 产生3位随机数
  118. encryptStr += ran
  119. }
  120. return encryptStr
  121. }
  122. }
  123. }
  124. }
  125. </script>
  126. <style scoped>
  127. /* 物品列表按钮 */
  128. .btn-buy-now {
  129. background-color: #5078CB;
  130. color: #fff;
  131. width: 80px;
  132. height: 30px;
  133. font-size: 12px;
  134. border: 1px solid #5078cb;
  135. }
  136. .btn-add-cart {
  137. margin-top: 10px;
  138. color: #214797;
  139. width: 80px;
  140. height: 30px;
  141. font-size: 12px;
  142. background-color: #fff;
  143. border: 1px solid #e8e8e8;
  144. }
  145. .btn-buy-now:hover{
  146. background: #214797;
  147. }
  148. .btn-add-cart:hover{
  149. background-color: #5078CB;
  150. color: #fff;
  151. }
  152. </style>