buyComponent.vue 7.4 KB

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