buyComponent.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. this.$root.$emit('add_cart', event.target)
  96. } else {
  97. if (response.data.code === 2) {
  98. this.$message.error('库存已不满足最小起订量')
  99. } else if (response.data.message === '该产品已失效') {
  100. this.$message.error(response.data.message + ',请刷新页面')
  101. } else {
  102. this.$message.error(response.data.message)
  103. }
  104. }
  105. })
  106. }
  107. }
  108. }
  109. },
  110. setHoverStyle: function (isShow) {
  111. if (this.btnColor) {
  112. this.$refs.addCartBtn.style.color = isShow ? '#fff' : this.btnColor
  113. this.$refs.addCartBtn.style.background = isShow ? this.btnColor : '#fff'
  114. }
  115. }
  116. }
  117. }
  118. </script>
  119. <style scoped>
  120. /* 物品列表按钮 */
  121. .btn-buy-now {
  122. background-color: #5078CB;
  123. color: #fff;
  124. width: 80px;
  125. height: 30px;
  126. font-size: 12px;
  127. border: 1px solid #5078cb;
  128. position: relative;
  129. }
  130. .btn-add-cart {
  131. margin-top: 10px;
  132. color: #214797;
  133. width: 80px;
  134. height: 30px;
  135. font-size: 12px;
  136. background-color: #fff;
  137. border: 1px solid #e8e8e8;
  138. }
  139. .btn-buy-now:hover{
  140. background: #214797;
  141. }
  142. .btn-add-cart:hover{
  143. background-color: #5078CB;
  144. color: #fff;
  145. }
  146. .btn-buy-now.disabled,
  147. .btn-buy-now.disabled:focus,
  148. .btn-add-cart.disabled,
  149. .btn-add-cart.disabled:focus{
  150. background-color: #d1d2d3!important;
  151. border: none!important;
  152. outline: none;
  153. color: #fff!important;
  154. cursor: not-allowed;
  155. }
  156. </style>