buyComponent.vue 7.7 KB

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