cm-goods-nav.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view>
  3. <view class="cm-goods-nav" :class="{ hasBottom: isIphoneX }">
  4. <!-- 导航按钮 -->
  5. <uni-goods-nav
  6. :fill="true"
  7. :options="options"
  8. :buttonGroup="buttonGroup"
  9. @click="onClick"
  10. @buttonClick="buttonClick"
  11. />
  12. </view>
  13. <cm-drawer :visible="countVisible" @close="drawerClose" position="bottom">
  14. <view class="count">
  15. <view class="top">
  16. <image :src="productInfo.mainImage" class="cover"></image>
  17. <view class="right">
  18. <view class="number">
  19. <text>数量:</text>
  20. <number-box :value="count" @change="countChange"></number-box>
  21. </view>
  22. <view class="single-price">
  23. <text>单价:¥</text> <text class="price">{{ buyRetailPrice | priceFormat }}</text>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="btn" @click="confirm">确认</view>
  28. </view>
  29. </cm-drawer>
  30. </view>
  31. </template>
  32. <script>
  33. import CmDrawer from '@/components/cm-module/cm-drawer/cm-drawer.vue'
  34. import NumberBox from '@/components/cm-module/cm-cart-product/number-box.vue'
  35. import { mapGetters, mapActions, mapMutations } from 'vuex'
  36. export default {
  37. components: {
  38. NumberBox,
  39. CmDrawer
  40. },
  41. props: {
  42. productInfo: {
  43. type: Object,
  44. default: () => {}
  45. }
  46. },
  47. filters: {
  48. priceFormat(price) {
  49. return Number(price).toFixed(2)
  50. }
  51. },
  52. data() {
  53. return {
  54. count: 1,
  55. countVisible: false,
  56. buyRetailPrice: 0,
  57. btnClickType: -1,
  58. options: [
  59. {
  60. icon: 'home',
  61. text: '首页'
  62. },
  63. {
  64. icon: 'headphones',
  65. text: '客服',
  66. type: 'contact'
  67. },
  68. {
  69. icon: 'cart',
  70. text: '购物车',
  71. info: 0,
  72. infoBackgroundColor: '#FC464C',
  73. infoColor: '#ffffff'
  74. }
  75. ],
  76. buttonGroup: [
  77. {
  78. text: '加入购物车',
  79. backgroundColor: '#FFEFF4',
  80. color: '#FF457B'
  81. },
  82. {
  83. text: '立即购买',
  84. backgroundColor: 'linear-gradient(90deg, #FC32B4 0%, #F83C6C 100%)',
  85. color: '#fff'
  86. }
  87. ]
  88. }
  89. },
  90. computed: {
  91. ...mapGetters(['isIphoneX', 'kindCount', 'hasLogin'])
  92. },
  93. watch: {
  94. kindCount(newCount) {
  95. this.options[2].info = newCount
  96. },
  97. count() {
  98. if (!this.productInfo) return
  99. this.processActivityPrice()
  100. },
  101. countVisible(newVal) {
  102. if (newVal) this.processActivityPrice()
  103. }
  104. },
  105. created() {
  106. this.options[2].info = this.kindCount
  107. // this.initBuyPrice()
  108. },
  109. methods: {
  110. ...mapActions('cart', ['addToCart', 'getCartNumber']),
  111. countChange(value) {
  112. this.count = value
  113. },
  114. drawerClose() {
  115. this.countVisible = false
  116. this.count = 1
  117. },
  118. // 初始化购买价格
  119. initBuyPrice() {
  120. if (this.productInfo.activeStatus === 1 && this.productInfo.ladderList && this.productInfo.length > 0) {
  121. this.count = this.productInfo.ladderList[0].buyNum
  122. }
  123. },
  124. // 左边按钮
  125. onClick(e) {
  126. const clickFns = {
  127. 0: this.toHome,
  128. 1: this.customer,
  129. 2: this.toCart
  130. }
  131. clickFns[e.index]()
  132. console.log(e.index)
  133. },
  134. // 右边按钮
  135. buttonClick(e) {
  136. this.countVisible = true
  137. this.btnClickType = e.index
  138. this.processActivityPrice()
  139. },
  140. // 确认
  141. confirm() {
  142. this.countVisible = false
  143. const clickFns = {
  144. 0: this.joinCart,
  145. 1: this.buyNow
  146. }
  147. clickFns[this.btnClickType]()
  148. },
  149. // 跳转首页
  150. toHome() {
  151. uni.switchTab({ url: '/pages/tabBar/index/index' })
  152. },
  153. // 客服
  154. customer() {
  155. console.log(this)
  156. },
  157. // 去登陆
  158. toLogin() {
  159. uni.navigateTo({ url: '/pages/login/login' })
  160. },
  161. // 去购物车
  162. toCart() {
  163. if (!this.hasLogin) return this.toLogin()
  164. uni.navigateTo({ url: '/pages/goods/cart' })
  165. },
  166. // 加入购物车
  167. joinCart() {
  168. if (!this.hasLogin) return this.toLogin()
  169. this.addToCart({
  170. productId: this.productInfo.productId,
  171. productCount: this.count
  172. }).finally(() => {
  173. this.countVisible = false
  174. })
  175. },
  176. // 立即购买
  177. buyNow() {
  178. if (!this.hasLogin) return this.toLogin()
  179. let productStp = {
  180. allPrice: this.count * this.buyRetailPrice,
  181. allCount: this.count,
  182. productId: this.productInfo.productId,
  183. productCount: this.count,
  184. heUserId: this.productInfo.heUserId
  185. }
  186. this.$api.navigateTo(
  187. `/pages/user/order/create-order?type=prodcut&data=${JSON.stringify({ data: productStp })}`
  188. )
  189. this.countVisible = false
  190. },
  191. //单独处理活动价格和阶梯价格
  192. processActivityPrice() {
  193. this.buyRetailPrice = this.productInfo.price
  194. if (this.productInfo.activeStatus === 1 && this.productInfo.ladderList) {
  195. this.productInfo.ladderList.forEach((item, index) => {
  196. if (this.count >= item.buyNum) {
  197. this.buyRetailPrice = item.buyPrice
  198. }
  199. })
  200. }
  201. }
  202. }
  203. }
  204. </script>
  205. <style lang="scss" scoped>
  206. .cm-goods-nav {
  207. position: fixed;
  208. width: 100%;
  209. bottom: 0;
  210. left: 0;
  211. background: #fff;
  212. &.hasBottom {
  213. padding-bottom: 44rpx;
  214. }
  215. }
  216. .count {
  217. padding: 60rpx 0;
  218. .top {
  219. display: flex;
  220. justify-content: flex-start;
  221. align-items: center;
  222. padding: 0 60rpx;
  223. .cover {
  224. width: 104rpx;
  225. height: 104rpx;
  226. }
  227. .right {
  228. margin-left: 24rpx;
  229. font-size: 24rpx;
  230. color: #333;
  231. .single-price {
  232. color: #ff457b;
  233. margin-top: 24rpx;
  234. }
  235. .number {
  236. display: flex;
  237. justify-content: flex-start;
  238. align-items: center;
  239. }
  240. }
  241. }
  242. .btn {
  243. width: 100%;
  244. height: 88rpx;
  245. margin-top: 32px;
  246. background: #ff457b;
  247. line-height: 88rpx;
  248. text-align: center;
  249. color: #ffffff;
  250. font-size: 28rpx;
  251. border-radius: 44rpx;
  252. }
  253. }
  254. </style>