cm-goods-nav.vue 8.3 KB

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