cm-goods-buy-popup.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <template>
  2. <uni-popup type="bottom" class="uni-popup" ref="popup" @change="handleChange">
  3. <view class="close iconfont icon-iconfontguanbi" @click="handleClose"></view>
  4. <view class="popup-content" v-if="currentSku">
  5. <view class="goods-info">
  6. <image class="cover" :src="product.mainImage"></image>
  7. <!-- 普通价格,阶梯价格,拼团价格,限时特价 -->
  8. <view class="info">
  9. <view class="price row">
  10. <text class="label">单价:</text>
  11. <text class="amount" v-text="price"></text>
  12. </view>
  13. <view class="row tags">
  14. <!-- 券后价 -->
  15. <template v-if="couponPriceFlag">
  16. <view class="tag-qh">券后价¥{{ couponPrice }}</view>
  17. </template>
  18. <template v-if="activityType === 'group'">
  19. <view class="tag-pt">{{ collageInfo.memberNum }}人拼团</view>
  20. </template>
  21. <template v-if="activityType === 'activity'">
  22. <view class="tag-hd">活动价</view>
  23. <view class="tag-text">查看活动价</view>
  24. </template>
  25. <template v-if="activityType === 'time-limit'">
  26. <view class="tag-xs">限时特价</view>
  27. </template>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 产品规格 -->
  32. <view class="buy-unit">
  33. <view class="label">规格:</view>
  34. <scroll-view scroll-y="true" class="unit-wrapper">
  35. <view class="unit-list">
  36. <template v-for="item in skusList">
  37. <view
  38. class="unit"
  39. :class="{
  40. disabled: item.stock === 0,
  41. active: item.skuId === currentSkuId && item.stock !== 0
  42. }"
  43. :key="item.skuId"
  44. v-text="item.unit"
  45. @click="handleChooseUtil(item)"
  46. ></view>
  47. </template>
  48. </view>
  49. </scroll-view>
  50. </view>
  51. <!-- 购买数量 -->
  52. <view class="buy-number">
  53. <view class="label">购买数量:</view>
  54. <cm-number-box v-model.lazy="count" :max="limitedNum"></cm-number-box>
  55. </view>
  56. <!-- 优惠券 -->
  57. <view class="coupon-tip">
  58. <template v-if="couponTipFlag">
  59. <text>当前商品可使用</text>
  60. <text class="active" v-text="couponTip">满300减200元</text>
  61. <text>优惠券</text>
  62. </template>
  63. </view>
  64. <!-- 操作 -->
  65. <view class="control" :class="{ disabled: disabled }">
  66. <!-- 加入购物车 -->
  67. <template v-if="useType === 'cart'">
  68. <view class="btn btn-confirm" @click="handleClick('cart')">确定</view>
  69. </template>
  70. <!-- 加入购物车 / 立即购买区域 -->
  71. <template v-if="useType === 'buy'">
  72. <template v-if="groupPriceFlag">
  73. <template v-if="couponPriceFlag">
  74. <view class="btn btn-confirm" @click="handleClick('buy')">领券购买</view>
  75. </template>
  76. <template v-else>
  77. <view class="btn btn-confirm" @click="handleClick('buy')">立即购买</view>
  78. </template>
  79. </template>
  80. <template v-else>
  81. <template v-if="buttonType === 'left'">
  82. <view class="btn btn-confirm" @click="handleClick('cart')">确定</view>
  83. </template>
  84. <template v-else>
  85. <template v-if="couponPriceFlag">
  86. <view class="btn btn-confirm" @click="handleClick('buy')">领券购买</view>
  87. </template>
  88. <template v-else>
  89. <view class="btn btn-confirm" @click="handleClick('buy')">确定</view>
  90. </template>
  91. </template>
  92. </template>
  93. </template>
  94. <!-- 点击规格区域 -->
  95. <template v-if="useType === 'unit'">
  96. <template v-if="groupPriceFlag">
  97. <view class="btn btn-cart" @click="handleClick('buy')">
  98. <text class="tip">{{ leftButton[0] }}</text>
  99. <text class="amount">{{ leftButton[1] }}</text>
  100. </view>
  101. <view class="btn btn-buy" @click="handleClick('buy')">
  102. <text class="tip">{{ rightButton[0] }}</text>
  103. <text class="amount">{{ rightButton[1] }}</text>
  104. </view>
  105. </template>
  106. <template v-else>
  107. <view class="btn btn-cart" @click="handleClick('cart')">{{ leftButton[0] }}</view>
  108. <view class="btn btn-buy" @click="handleClick('buy')">
  109. <template v-if="rightButton[1]">
  110. <text class="tip">{{ rightButton[0] }}</text>
  111. <text class="amount">{{ rightButton[1] }}</text>
  112. </template>
  113. <template v-else>
  114. {{ rightButton[0] }}
  115. </template>
  116. </view>
  117. </template>
  118. </template>
  119. </view>
  120. </view>
  121. </uni-popup>
  122. </template>
  123. <script>
  124. import { generateNavbarButtonText } from '@/common/goods.helper.js'
  125. import { mapGetters } from 'vuex'
  126. export default {
  127. name: 'cm-goods-buy-popup',
  128. props: {
  129. value: {
  130. type: Boolean,
  131. default: false
  132. },
  133. product: {
  134. type: Object,
  135. default: () => {}
  136. },
  137. couponList: {
  138. type: Array,
  139. default: () => []
  140. },
  141. // 组件使用位置
  142. useType: {
  143. type: String,
  144. default: 'buy'
  145. },
  146. // 按钮类型 left 左侧按钮点击 right 右侧按钮点击
  147. buttonType: {
  148. type: String,
  149. default: 'left'
  150. }
  151. },
  152. computed: {
  153. // sku列表
  154. skusList() {
  155. return this.product.skus || []
  156. },
  157. // 选中的sku
  158. currentSku() {
  159. return this.skusList.find(sku => sku.skuId === this.currentSkuId) || {}
  160. },
  161. // 展示价格
  162. price() {
  163. return this.generatePrice()
  164. },
  165. // 活动类型
  166. activityType() {
  167. return this.currentSku.activityType
  168. },
  169. // 价格类型
  170. priceType() {
  171. return this.currentSku.priceType
  172. },
  173. // 券后价标识
  174. couponPriceFlag() {
  175. return ['groupWithCoupon', 'activityWithCoupon', 'normalWithCoupon'].includes(this.currentSku.priceType)
  176. },
  177. // 拼团价标识
  178. groupPriceFlag() {
  179. return ['groupWithCoupon', 'group'].includes(this.currentSku.priceType)
  180. },
  181. // 券后价
  182. couponPrice() {
  183. return this.buttonType === 'left' ? this.currentSku.normalCouponPrice : this.currentSku.couponPrice
  184. },
  185. // 优惠券提示标识
  186. couponTipFlag() {
  187. return this.currentSku.couponStatus > 0 && this.currentSku.couponId
  188. },
  189. // 优惠券提示
  190. couponTip() {
  191. if (this.currentSku.couponId) {
  192. const currentCoupon = this.couponList.find(coupon => this.currentSku.couponId === coupon.couponId)
  193. return currentCoupon.couponTitle
  194. }
  195. if (this.currentSku.couponInfo) {
  196. return this.currentSku.couponInfo.split('|')[1]
  197. }
  198. return ''
  199. },
  200. // 商品拼团信息
  201. collageInfo() {
  202. return this.currentSku.collageProduct
  203. },
  204. limitedNum() {
  205. return this.currentSku.stock === null ? 100 : this.currentSku.stock
  206. },
  207. disabled() {
  208. return this.currentSku.stock === 0
  209. }
  210. },
  211. data() {
  212. return {
  213. count: 1,
  214. currentSkuId: 0,
  215. leftButton: {},
  216. rightButton: {}
  217. }
  218. },
  219. watch: {
  220. value: {
  221. handler: function(val) {
  222. if (val) {
  223. this.currentSkuId = this.product.skuId
  224. this.initNavbarButton()
  225. this.handleOpen()
  226. } else {
  227. this.handleClose()
  228. }
  229. },
  230. immediate: true
  231. }
  232. },
  233. mounted() {
  234. console.log('product', this.product)
  235. },
  236. methods: {
  237. // 导航按钮点击事件
  238. handleClick(type) {
  239. if (this.disabled) {
  240. return
  241. }
  242. this.$emit('confirm', {
  243. type: type,
  244. sku: this.currentSku,
  245. count: this.count
  246. })
  247. },
  248. // 初始化导航按钮文案
  249. initNavbarButton() {
  250. const navbarButton = generateNavbarButtonText(this.currentSku)
  251. this.leftButton = navbarButton.left
  252. this.rightButton = navbarButton.right
  253. },
  254. // 设置价格
  255. generatePrice() {
  256. // 拼团价
  257. if (this.groupPriceFlag) {
  258. // 单独购买价格
  259. if (this.buttonType === 'left') {
  260. return this.currentSku.normalPrice
  261. }
  262. }
  263. // 非阶梯价
  264. if (this.currentSku.activeStatus <= 0) {
  265. return this.currentSku.price
  266. }
  267. // 阶梯价列表为空
  268. if (!this.currentSku.ladderList) {
  269. return this.currentSku.price
  270. }
  271. // 阶梯价
  272. const lastItem = this.currentSku.ladderList
  273. .sort((a, b) => b.buyNum - a.buyNum)
  274. .find(item => this.count >= item.buyNum)
  275. return lastItem ? lastItem.buyPrice : this.currentSku.price
  276. },
  277. // 关闭
  278. handleClose() {
  279. this.$nextTick(() => {
  280. this.$refs.popup.close()
  281. })
  282. },
  283. // 关闭/打开
  284. handleChange(e) {
  285. this.$emit('input', e.show)
  286. },
  287. // 打开
  288. handleOpen() {
  289. this.$nextTick(() => {
  290. this.$refs.popup.open()
  291. })
  292. },
  293. handleChooseUtil(item) {
  294. this.currentSkuId = item.skuId
  295. this.$emit('utilChange', item)
  296. }
  297. }
  298. }
  299. </script>
  300. <style lang="scss" scoped>
  301. .uni-popup {
  302. position: relative;
  303. z-index: 99999;
  304. .close {
  305. position: absolute;
  306. right: 24rpx;
  307. top: 24rpx;
  308. color: #999;
  309. font-size: 32rpx;
  310. }
  311. }
  312. .popup-content {
  313. padding: 40rpx 32rpx;
  314. background-color: #fff;
  315. border-radius: 16rpx 16rpx 0 0;
  316. &::after {
  317. position: absolute;
  318. content: '';
  319. width: 100%;
  320. height: 80rpx;
  321. bottom: -80rpx;
  322. left: 0;
  323. background-color: #fff;
  324. }
  325. .goods-info {
  326. display: flex;
  327. .cover {
  328. flex-shrink: 0;
  329. width: 180rpx;
  330. height: 180rpx;
  331. border: 1px solid #e1e1e1;
  332. }
  333. .info {
  334. margin-left: 24rpx;
  335. margin-top: 24rpx;
  336. .price {
  337. display: flex;
  338. align-items: center;
  339. .label {
  340. font-size: 26rpx;
  341. color: #666666;
  342. }
  343. .amount {
  344. font-size: 26rpx;
  345. color: #ff457b;
  346. font-weight: bold;
  347. &::before {
  348. content: '¥';
  349. }
  350. }
  351. }
  352. .tags {
  353. display: flex;
  354. align-items: center;
  355. flex-wrap: wrap;
  356. margin-top: 24rpx;
  357. gap: 16rpx 24rpx;
  358. .tag-text {
  359. font-size: 22rpx;
  360. color: #ff457b;
  361. }
  362. .tag-qh {
  363. line-height: 40rpx;
  364. height: 40rpx;
  365. background: #ff457b;
  366. border-radius: 25rpx;
  367. padding: 0 15rpx;
  368. font-size: 24rpx;
  369. font-weight: bold;
  370. color: #ffffff;
  371. }
  372. .tag-pt {
  373. height: 32rpx;
  374. line-height: 32rpx;
  375. padding: 0 8rpx;
  376. background: linear-gradient(90deg, #6431f2 0%, #b03bb8 49%, #ff457b 100%);
  377. border-radius: 4rpx;
  378. font-size: 22rpx;
  379. color: #fff;
  380. }
  381. .tag-hd,
  382. .tag-xs {
  383. height: 30rpx;
  384. padding: 0 8rpx;
  385. line-height: 28rpx;
  386. background: #fff3f7;
  387. border: 1rpx solid #ff457b;
  388. border-radius: 4rpx;
  389. font-size: 22rpx;
  390. color: #ff457b;
  391. }
  392. }
  393. }
  394. }
  395. .buy-unit {
  396. margin-top: 32rpx;
  397. .label {
  398. margin-bottom: 24rpx;
  399. }
  400. .unit-wrapper {
  401. max-height: 300rpx;
  402. .unit-list {
  403. display: flex;
  404. flex-wrap: wrap;
  405. gap: 24rpx 16rpx;
  406. padding-top: 24rpx;
  407. .unit {
  408. position: relative;
  409. height: 48rpx;
  410. line-height: 46rpx;
  411. padding: 0 28rpx;
  412. background: #f5f5f5;
  413. color: #666666;
  414. font-size: 24rpx;
  415. box-sizing: border-box;
  416. border: 1rpx solid #f5f5f5;
  417. border-radius: 24rpx;
  418. &.active {
  419. color: #ff457b;
  420. border-color: #ff457b;
  421. background: #fff8fd;
  422. }
  423. &.disabled::after {
  424. content: '缺货';
  425. position: absolute;
  426. height: 32rpx;
  427. line-height: 32rpx;
  428. background: #cccccc;
  429. color: #fff;
  430. padding: 0 10rpx;
  431. right: 0;
  432. top: 0;
  433. transform: translate(30%, -50%);
  434. font-size: 22rpx;
  435. border-radius: 17rpx;
  436. border: 2rpx solid #fff;
  437. }
  438. }
  439. }
  440. }
  441. }
  442. .buy-number,
  443. .buy-unit {
  444. .label {
  445. font-weight: bold;
  446. color: #333333;
  447. font-size: 28rpx;
  448. }
  449. }
  450. .buy-number {
  451. display: flex;
  452. justify-content: space-between;
  453. align-items: center;
  454. margin-top: 60rpx;
  455. }
  456. .coupon-tip {
  457. margin: 70rpx 0 24rpx;
  458. font-size: 0;
  459. text-align: center;
  460. text {
  461. font-size: 24rpx;
  462. color: #666;
  463. }
  464. .active {
  465. color: #ff457b;
  466. }
  467. }
  468. .control {
  469. display: flex;
  470. align-items: center;
  471. justify-content: space-evenly;
  472. &.disabled {
  473. .btn {
  474. background: #ddd !important;
  475. color: #fff !important;
  476. }
  477. }
  478. .btn {
  479. height: 90rpx;
  480. font-size: 30rpx;
  481. border-radius: 45rpx;
  482. display: flex;
  483. justify-content: center;
  484. align-items: center;
  485. flex-direction: column;
  486. position: relative;
  487. overflow: hidden;
  488. &:active::after {
  489. content: '';
  490. position: absolute;
  491. width: 100%;
  492. height: 100%;
  493. left: 0;
  494. top: 0;
  495. background: #999;
  496. opacity: 0.1;
  497. }
  498. &.btn-confirm {
  499. width: 100%;
  500. }
  501. &.btn-confirm,
  502. &.btn-buy {
  503. color: #fff;
  504. background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
  505. }
  506. &.btn-cart {
  507. color: #ff457b;
  508. background: #ffeff4;
  509. }
  510. &.btn-buy,
  511. &.btn-cart {
  512. width: 280rpx;
  513. }
  514. .tip {
  515. font-size: 22rpx;
  516. margin-bottom: 4rpx;
  517. line-height: 24rpx;
  518. }
  519. .amount {
  520. font-size: 30rpx;
  521. font-weight: 600;
  522. line-height: 36rpx;
  523. }
  524. }
  525. }
  526. }
  527. </style>