buyComponent.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import { enidfilter } from '~utils/baseUtils'
  9. export default {
  10. props: ['item', 'disabledFlag'],
  11. methods: {
  12. buyNow: function (isBuy, event) {
  13. event.stopPropagation()
  14. if (!this.$store.state.option.user.logged) {
  15. this.$http.get('/login/page', {params: {returnUrl: window.location.href}}).then(response => {
  16. if (response.data) {
  17. window.location.href = response.data.content + '&baseUrl=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + response.data.baseUrl)
  18. }
  19. })
  20. } else {
  21. if (this.item && !this.disabledFlag) {
  22. if (isBuy) {
  23. this.$http.post('/trade/order/buyNow', [{
  24. uuid: this.item.uuid,
  25. batchCode: this.item.batchCode,
  26. number: this.item.minBuyQty,
  27. storeid: this.item.storeid ? this.item.storeid : this.item.storeId,
  28. storeUuid: this.item.storeid ? this.item.storeid : this.item.storeId,
  29. currencyName: this.item.currencyName,
  30. minPackQty: this.item.minPackQty
  31. }])
  32. .then(response => {
  33. if (response.data.success) {
  34. if (response.data.message) {
  35. this.$message({
  36. message: response.data.message,
  37. type: 'success'
  38. })
  39. window.setTimeout(function () {
  40. window.location.href = '/user#/order/pay/' + enidfilter(response.data.data.orderid)
  41. }, 1000)
  42. } else {
  43. window.location.href = '/user#/order/pay/' + 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.$http.post('/trade/cart/add', {
  60. uuid: this.item.uuid,
  61. batchCode: this.item.batchCode,
  62. number: this.item.minBuyQty,
  63. storeid: this.item.storeid ? this.item.storeid : this.item.storeId,
  64. storeUuid: this.item.storeid ? this.item.storeid : this.item.storeId,
  65. currencyName: this.item.currencyName,
  66. minPackQty: this.item.minPackQty
  67. })
  68. .then(response => {
  69. if (response.data.success) {
  70. if (response.data.message) {
  71. this.$message({
  72. message: '添加购物车成功,但商品信息有更新',
  73. type: 'success'
  74. })
  75. } else {
  76. this.$message({
  77. message: '添加购物车成功',
  78. type: 'success'
  79. })
  80. }
  81. } else {
  82. if (response.data.code === 2) {
  83. this.$message.error('库存已不满足起订量')
  84. } else if (response.data.message === '该产品已失效') {
  85. this.$message.error(response.data.message + ',请刷新页面')
  86. } else {
  87. this.$message.error(response.data.message)
  88. }
  89. }
  90. })
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. </script>
  98. <style scoped>
  99. /* 物品列表按钮 */
  100. .btn-buy-now {
  101. background-color: #5078CB;
  102. color: #fff;
  103. width: 80px;
  104. height: 30px;
  105. font-size: 12px;
  106. border: 1px solid #5078cb;
  107. }
  108. .btn-add-cart {
  109. margin-top: 10px;
  110. color: #214797;
  111. width: 80px;
  112. height: 30px;
  113. font-size: 12px;
  114. background-color: #fff;
  115. border: 1px solid #e8e8e8;
  116. }
  117. .btn-buy-now:hover{
  118. background: #214797;
  119. }
  120. .btn-add-cart:hover{
  121. background-color: #5078CB;
  122. color: #fff;
  123. }
  124. .btn-buy-now.disabled,
  125. .btn-buy-now.disabled:focus,
  126. .btn-add-cart.disabled,
  127. .btn-add-cart.disabled:focus{
  128. background-color: #d1d2d3!important;
  129. border: none!important;
  130. outline: none;
  131. color: #fff!important;
  132. cursor: not-allowed;
  133. }
  134. </style>