buyComponent.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // let $el = this.$refs.ballCard
  28. // let transform = whichTransitionEvent()
  29. // let _shopCar = document.getElementById('shopCar')
  30. // let _eltop = event.currentTarget.getBoundingClientRect().top - 22
  31. // let _elleft = event.currentTarget.getBoundingClientRect().left - 278
  32. // // $el.style = `left: ${_elleft}px;top:${_eltop}px`
  33. // $el.style[transform] = `translate3d(${_elleft}px,${_eltop}px,0)`
  34. // $el.style.transition = 'all 0.5s'
  35. // let _shopCarTop = _shopCar.getBoundingClientRect().top
  36. // let _shopCarLeft = _shopCar.getBoundingClientRect().left
  37. // $el.style[transform] = `translate3d(${_shopCarLeft}px,${_shopCarTop}px,0)`
  38. event.stopPropagation()
  39. if (!this.$store.state.option.user.logged) {
  40. this.$http.get('/login/page', {params: {returnUrl: window.location.href}}).then(response => {
  41. if (response.data) {
  42. window.location.href = response.data.content + '&baseUrl=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + response.data.baseUrl)
  43. }
  44. })
  45. } else {
  46. if (this.item && !this.disabledFlag) {
  47. if (isBuy) {
  48. this.$http.post('/trade/order/buyNow', [{
  49. uuid: this.item.uuid,
  50. batchCode: this.item.batchCode,
  51. number: this.item.minBuyQty,
  52. storeid: this.item.storeid ? this.item.storeid : this.item.storeId,
  53. storeUuid: this.item.storeid ? this.item.storeid : this.item.storeId,
  54. currencyName: this.item.currencyName,
  55. minPackQty: this.item.minPackQty
  56. }])
  57. .then(response => {
  58. if (response.data.success) {
  59. if (response.data.message) {
  60. this.$message({
  61. message: response.data.message,
  62. type: 'success'
  63. })
  64. window.setTimeout(function () {
  65. window.location.href = '/user#/order/pay/' + this.baseUtils.enidfilter(response.data.data.orderid)
  66. }, 1000)
  67. } else {
  68. window.location.href = '/user#/order/pay/' + this.baseUtils.enidfilter(response.data.data.orderid)
  69. }
  70. } else {
  71. if (response.data.data && response.data.data.unvailable === 1) {
  72. this.$message.error('产品信息已失效,请刷新页面')
  73. } else {
  74. this.$message.error(response.data.message)
  75. }
  76. }
  77. }, err => {
  78. console.log(err)
  79. if (this.item.minBuyQty > this.item.reserve) {
  80. this.$message.error('商品' + this.item.code + '的库存已经不满足最小起订量')
  81. }
  82. })
  83. } else {
  84. this.$http.post('/trade/cart/add', {
  85. uuid: this.item.uuid,
  86. batchCode: this.item.batchCode,
  87. number: this.item.minBuyQty,
  88. storeid: this.item.storeid ? this.item.storeid : this.item.storeId,
  89. storeUuid: this.item.storeid ? this.item.storeid : this.item.storeId,
  90. currencyName: this.item.currencyName,
  91. minPackQty: this.item.minPackQty
  92. })
  93. .then(response => {
  94. if (response.data.success) {
  95. if (response.data.message) {
  96. this.$message({
  97. message: '添加购物车成功,但商品信息有更新',
  98. type: 'success'
  99. })
  100. } else {
  101. this.$message({
  102. message: '添加购物车成功',
  103. type: 'success'
  104. })
  105. }
  106. } else {
  107. if (response.data.code === 2) {
  108. this.$message.error('库存已不满足最小起订量')
  109. } else if (response.data.message === '该产品已失效') {
  110. this.$message.error(response.data.message + ',请刷新页面')
  111. } else {
  112. this.$message.error(response.data.message)
  113. }
  114. }
  115. })
  116. }
  117. }
  118. }
  119. },
  120. setHoverStyle: function (isShow) {
  121. if (this.btnColor) {
  122. this.$refs.addCartBtn.style.color = isShow ? '#fff' : this.btnColor
  123. this.$refs.addCartBtn.style.background = isShow ? this.btnColor : '#fff'
  124. }
  125. }
  126. }
  127. }
  128. </script>
  129. <style scoped>
  130. /* 物品列表按钮 */
  131. .btn-buy-now {
  132. background-color: #5078CB;
  133. color: #fff;
  134. width: 80px;
  135. height: 30px;
  136. font-size: 12px;
  137. border: 1px solid #5078cb;
  138. position: relative;
  139. }
  140. .btn-add-cart {
  141. margin-top: 10px;
  142. color: #214797;
  143. width: 80px;
  144. height: 30px;
  145. font-size: 12px;
  146. background-color: #fff;
  147. border: 1px solid #e8e8e8;
  148. }
  149. .btn-buy-now:hover{
  150. background: #214797;
  151. }
  152. .btn-add-cart:hover{
  153. background-color: #5078CB;
  154. color: #fff;
  155. }
  156. .btn-buy-now.disabled,
  157. .btn-buy-now.disabled:focus,
  158. .btn-add-cart.disabled,
  159. .btn-add-cart.disabled:focus{
  160. background-color: #d1d2d3!important;
  161. border: none!important;
  162. outline: none;
  163. color: #fff!important;
  164. cursor: not-allowed;
  165. }
  166. </style>