buyComponent.vue 8.4 KB

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