cm-goods-nav.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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" mode="center" 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. methods: {
  106. ...mapActions('cart', ['addToCart', 'getCartNumber']),
  107. ...mapMutations('user', ['setInviteUserId']),
  108. countChange(value) {
  109. this.count = value
  110. },
  111. drawerClose() {
  112. this.countVisible = false
  113. this.count = 1
  114. },
  115. // 左边按钮
  116. onClick(e) {
  117. const clickFns = {
  118. 0: this.toHome,
  119. 1: this.customer,
  120. 2: this.toCart
  121. }
  122. clickFns[e.index]()
  123. console.log(e.index)
  124. },
  125. // 右边按钮
  126. buttonClick(e) {
  127. this.countVisible = true
  128. this.btnClickType = e.index
  129. },
  130. // 确认
  131. confirm() {
  132. this.countVisible = false
  133. const clickFns = {
  134. 0: this.joinCart,
  135. 1: this.buyNow
  136. }
  137. clickFns[this.btnClickType]()
  138. },
  139. // 跳转首页
  140. toHome() {
  141. uni.switchTab({ url: '/pages/tabBar/index/index' })
  142. },
  143. // 客服
  144. customer() {
  145. console.log(this)
  146. },
  147. // 去登陆
  148. toLogin(){
  149. uni.navigateTo({ url: '/pages/login/login' })
  150. },
  151. // 去购物车
  152. toCart() {
  153. if(!this.hasLogin) return this.toLogin()
  154. uni.navigateTo({ url: '/pages/goods/cart' })
  155. },
  156. // 加入购物车
  157. joinCart() {
  158. if(!this.hasLogin) return this.toLogin()
  159. this.addToCart({
  160. productId: this.productInfo.productId,
  161. productCount: this.count
  162. }).finally(() => {
  163. this.countVisible = false
  164. })
  165. },
  166. // 立即购买
  167. buyNow() {
  168. if(!this.hasLogin) return this.toLogin()
  169. let productStp = {
  170. allPrice: this.count * this.buyRetailPrice,
  171. allCount: this.count,
  172. productId: this.productInfo.productId,
  173. productCount: this.count,
  174. heUserId: this.productInfo.heUserId
  175. }
  176. this.$api.navigateTo(
  177. `/pages/user/order/create-order?type=prodcut&data=${JSON.stringify({ data: productStp })}`
  178. )
  179. this.countVisible = false
  180. },
  181. //单独处理活动价格和阶梯价格
  182. processActivityPrice() {
  183. if (this.productInfo.activeStatus === 1 && this.productInfo.ladderList) {
  184. this.productInfo.ladderList.forEach((item, index) => {
  185. if (this.count >= item.buyNum) {
  186. this.buyRetailPrice = item.buyPrice
  187. }
  188. })
  189. } else {
  190. this.buyRetailPrice = this.productInfo.price
  191. }
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .cm-goods-nav {
  198. position: fixed;
  199. width: 100%;
  200. bottom: 0;
  201. left: 0;
  202. background: #fff;
  203. &.hasBottom {
  204. padding-bottom: 44rpx;
  205. }
  206. }
  207. .count {
  208. padding: 60rpx 0;
  209. .top {
  210. display: flex;
  211. justify-content: flex-start;
  212. align-items: center;
  213. padding: 0 60rpx;
  214. .cover {
  215. width: 104rpx;
  216. height: 104rpx;
  217. }
  218. .right {
  219. margin-left: 24rpx;
  220. font-size: 24rpx;
  221. color: #333;
  222. .single-price {
  223. color: #ff457b;
  224. margin-top: 24rpx;
  225. }
  226. .number {
  227. display: flex;
  228. justify-content: flex-start;
  229. align-items: center;
  230. }
  231. }
  232. }
  233. .btn {
  234. width: 100%;
  235. height: 88rpx;
  236. margin-top: 32px;
  237. background: #ff457b;
  238. line-height: 88rpx;
  239. text-align: center;
  240. color: #ffffff;
  241. font-size: 28rpx;
  242. border-radius: 44rpx;
  243. }
  244. }
  245. </style>