buyComponent.vue 5.7 KB

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