buyComponent.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <div>
  3. <button style="z-index: 1000;"
  4. class="btn btn-primary btn-buy-now"
  5. :style="btnColor ? `background:${btnColor};border-color:${btnColor};` : ''"
  6. :class="{'disabled': disabledFlag}"
  7. @click="buyNow(true, $event)">
  8. <span class="watch">立即购买</span>
  9. </button>
  10. <button style="z-index: 1000;"
  11. ref="addCartBtn"
  12. class="btn btn-add-cart"
  13. @mouseenter="setHoverStyle(true)"
  14. @mouseleave="setHoverStyle(false)"
  15. :style="btnColor ? `color:${btnColor};border-color:${btnColor};` : ''"
  16. :class="{'disabled': disabledFlag}"
  17. @click="buyNow(false, $event)">
  18. <span class="watch">加入购物车</span>
  19. </button>
  20. </div>
  21. </template>
  22. <script>
  23. export default {
  24. props: ['item', 'disabledFlag', 'btnColor'],
  25. methods: {
  26. buyNow: function (isBuy, event) {
  27. event.stopPropagation()
  28. if (!this.$store.state.option.user.logged) {
  29. this.$http.get('/login/page', {params: {returnUrl: window.location.href}}).then(response => {
  30. if (response.data) {
  31. window.location.href = response.data.content + '&baseUrl=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + response.data.baseUrl)
  32. }
  33. })
  34. } else {
  35. if (this.item && !this.disabledFlag) {
  36. if (isBuy) {
  37. this.$http.post('/trade/order/buyNow', [{
  38. uuid: this.item.uuid,
  39. batchCode: this.item.batchCode,
  40. number: this.item.minBuyQty,
  41. storeid: this.item.storeid ? this.item.storeid : this.item.storeId,
  42. storeUuid: this.item.storeid ? this.item.storeid : this.item.storeId,
  43. currencyName: this.item.currencyName,
  44. minPackQty: this.item.minPackQty
  45. }])
  46. .then(response => {
  47. if (response.data.success) {
  48. if (response.data.message) {
  49. this.$message({
  50. message: response.data.message,
  51. type: 'success'
  52. })
  53. window.setTimeout(function () {
  54. window.location.href = '/user#/order/pay/' + this.baseUtils.enidfilter(response.data.data.orderid)
  55. }, 1000)
  56. } else {
  57. window.location.href = '/user#/order/pay/' + this.baseUtils.enidfilter(response.data.data.orderid)
  58. }
  59. } else {
  60. if (response.data.data && response.data.data.unvailable === 1) {
  61. this.$message.error('产品信息已失效,请刷新页面')
  62. } else {
  63. this.$message.error(response.data.message)
  64. }
  65. }
  66. }, err => {
  67. console.log(err)
  68. if (this.item.minBuyQty > this.item.reserve) {
  69. this.$message.error('商品' + this.item.code + '的库存已经不满足最小起订量')
  70. }
  71. })
  72. } else {
  73. this.$http.post('/trade/cart/add', {
  74. uuid: this.item.uuid,
  75. batchCode: this.item.batchCode,
  76. number: this.item.minBuyQty,
  77. storeid: this.item.storeid ? this.item.storeid : this.item.storeId,
  78. storeUuid: this.item.storeid ? this.item.storeid : this.item.storeId,
  79. currencyName: this.item.currencyName,
  80. minPackQty: this.item.minPackQty
  81. })
  82. .then(response => {
  83. if (response.data.success) {
  84. if (response.data.message) {
  85. this.$message({
  86. message: '添加购物车成功,但商品信息有更新',
  87. type: 'success'
  88. })
  89. } else {
  90. this.$message({
  91. message: '添加购物车成功',
  92. type: 'success'
  93. })
  94. }
  95. } else {
  96. if (response.data.code === 2) {
  97. this.$message.error('库存已不满足最小起订量')
  98. } else if (response.data.message === '该产品已失效') {
  99. this.$message.error(response.data.message + ',请刷新页面')
  100. } else {
  101. this.$message.error(response.data.message)
  102. }
  103. }
  104. })
  105. }
  106. }
  107. }
  108. },
  109. setHoverStyle: function (isShow) {
  110. if (this.btnColor) {
  111. this.$refs.addCartBtn.style.color = isShow ? '#fff' : this.btnColor
  112. this.$refs.addCartBtn.style.background = isShow ? this.btnColor : '#fff'
  113. }
  114. }
  115. }
  116. }
  117. </script>
  118. <style scoped>
  119. /* 物品列表按钮 */
  120. .btn-buy-now {
  121. background-color: #5078CB;
  122. color: #fff;
  123. width: 80px;
  124. height: 30px;
  125. font-size: 12px;
  126. border: 1px solid #5078cb;
  127. }
  128. .btn-add-cart {
  129. margin-top: 10px;
  130. color: #214797;
  131. width: 80px;
  132. height: 30px;
  133. font-size: 12px;
  134. background-color: #fff;
  135. border: 1px solid #e8e8e8;
  136. }
  137. .btn-buy-now:hover{
  138. background: #214797;
  139. }
  140. .btn-add-cart:hover{
  141. background-color: #5078CB;
  142. color: #fff;
  143. }
  144. .btn-buy-now.disabled,
  145. .btn-buy-now.disabled:focus,
  146. .btn-add-cart.disabled,
  147. .btn-add-cart.disabled:focus{
  148. background-color: #d1d2d3!important;
  149. border: none!important;
  150. outline: none;
  151. color: #fff!important;
  152. cursor: not-allowed;
  153. }
  154. </style>