buyComponent.vue 6.5 KB

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