cm-goods-nav.vue 8.5 KB

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