cm-goods-nav.vue 8.5 KB

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