cm-goods-nav.vue 7.0 KB

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