index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <template>
  2. <view @click.stop>
  3. <u-popup v-model="visible" mode="bottom" border-radius="24" :safe-area-inset-bottom="true" :mask-close-able="true"
  4. @close="close">
  5. <view class="popup-content" @click.stop>
  6. <!-- Header -->
  7. <view class="header">
  8. <text class="title">选择商品品相</text>
  9. <image src="/pages-sell/static/select-good/icon-close.png" class="close-icon" @click="close"></image>
  10. </view>
  11. <!-- Product Info -->
  12. <view class="product-info">
  13. <image :src="currentProduct.cover" class="book-cover" mode="aspectFill"></image>
  14. <view class="info-right">
  15. <view class="price-row">
  16. <text class="currency">¥</text>
  17. <text class="price">{{ displayUnitPrice }}</text>
  18. <view class="drop-tag">
  19. <text>↓可降至 ¥{{ displayMinPrice }}</text>
  20. </view>
  21. </view>
  22. <view class="tag-row">
  23. <view class="quality-tag">
  24. <text>品相{{ currentQualityName }}</text>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. <!-- Tips -->
  30. <view class="tips-row">
  31. <text>不同品相有什么区别</text>
  32. <image src="/pages-sell/static/select-good/icon-tips.png" class="tips-icon"></image>
  33. </view>
  34. <!-- Promo Note -->
  35. <view class="promo-note">
  36. <text>下单时用购书余额支付可享余额价 ( 售价 8 折优惠 )</text>
  37. </view>
  38. <!-- Options -->
  39. <view class="options-list">
  40. <view class="option-item" v-for="(opt, index) in qualityOptions" :key="index"
  41. :class="{ active: currentQuality === opt.conditionType, disabled: isOptionDisabled(opt) }"
  42. @click="selectQuality(opt)">
  43. <image v-if="currentQuality === opt.conditionType && !isOptionDisabled(opt)"
  44. src="/pages-sell/static/select-good/selected.png" class="bg-image"></image>
  45. <view class="left">
  46. <text class="opt-name">{{ opt.conditionType | conditionText }}</text>
  47. <view class="opt-discount"
  48. :class="{ active: currentQuality === opt.conditionType && !isOptionDisabled(opt) }">
  49. <text>{{ opt.discount }}折</text>
  50. </view>
  51. </view>
  52. <view class="right">
  53. <text v-if="!isOptionDisabled(opt)">¥{{ opt.price }} ( 余额价 ¥{{ opt.balanceMoney }} )</text>
  54. <text v-else>无货</text>
  55. </view>
  56. </view>
  57. </view>
  58. <!-- Quantity -->
  59. <view class="quantity-row">
  60. <text class="label">数量</text>
  61. <u-number-box v-model="quantity" :min="1" :max="99"></u-number-box>
  62. </view>
  63. <!-- Footer Buttons -->
  64. <view class="footer-btns">
  65. <view class="btn btn-orange" @click="handleShareAction">
  66. <text class="price">¥{{ displayReducePrice }}</text>
  67. <text class="desc">分享一人可降 {{ currentProduct.reducePrice }} 元</text>
  68. </view>
  69. <view class="btn btn-green" @click="handleAction" :class="{ 'btn-gray': !hasStock && currentProduct.hasArrivalNotice === 1 }">
  70. <text v-if="hasStock" class="price">¥{{ displayTotalPrice }}</text>
  71. <text class="desc" :class="{ 'notice': !hasStock }">{{ hasStock ? '加入购物车' : (currentProduct.hasArrivalNotice === 1 ? '取消到货通知' : '到货通知') }}</text>
  72. </view>
  73. </view>
  74. </view>
  75. </u-popup>
  76. <price-reduction-popup ref="reducePopup" @scan="handleScan"></price-reduction-popup>
  77. <reduce-qrcode ref="reduceQrcode"></reduce-qrcode>
  78. </view>
  79. </template>
  80. <script>
  81. import PriceReductionPopup from '@/components/price-reduction-popup/price-reduction-popup.vue';
  82. import ReduceQrcode from '@/components/reduce-qrcode/reduce-qrcode.vue';
  83. export default {
  84. name: 'SelectGoodPopup',
  85. components: {
  86. PriceReductionPopup,
  87. ReduceQrcode
  88. },
  89. data() {
  90. return {
  91. visible: false,
  92. quantity: 1,
  93. currentQuality: 1,
  94. currentProduct: {},
  95. qualityOptions: [],
  96. sourceFrom: 0,
  97. isSubmitting: false, // 防重复提交标志
  98. };
  99. },
  100. computed: {
  101. currentQualityName() {
  102. const opt = this.$conditionMap[this.currentQuality];
  103. return opt || '未知';
  104. },
  105. selectedOption() {
  106. return this.qualityOptions.find(o => o.conditionType === this.currentQuality) || {};
  107. },
  108. displayUnitPrice() {
  109. return this.formatPrice(this.selectedOption.price);
  110. },
  111. displayMinPrice() {
  112. return this.formatPrice(this.selectedOption.price - this.currentProduct.maxReducePrice || 0);
  113. },
  114. displayTotalPrice() {
  115. const total = Number(this.selectedOption.price || 0) * this.quantity;
  116. return this.formatPrice(total);
  117. },
  118. displayReducePrice() {
  119. const currentPrice = Number(this.selectedOption.price || 0);
  120. const maxReduce = Number(this.currentProduct.maxReducePrice || 0);
  121. return this.formatPrice(currentPrice - maxReduce);
  122. },
  123. hasStock() {
  124. const stock = this.selectedOption.stockNum;
  125. if (stock === 0) return false;
  126. if (stock === undefined || stock === null || stock === '') return true;
  127. return Number(stock) > 0;
  128. }
  129. },
  130. methods: {
  131. open(product, sourceFrom) {
  132. this.visible = true;
  133. this.quantity = 1;
  134. this.sourceFrom = sourceFrom || 0;
  135. if (product.isbn) {
  136. this.getGoodQualityInfo(product.isbn);
  137. }
  138. },
  139. //根据 isbn 获取商品品相信息
  140. getGoodQualityInfo(isbn) {
  141. uni.$u.http.get('/token/shop/bookDetail', { isbn }).then(res => {
  142. console.log(res);
  143. if (res.code === 200) {
  144. this.currentProduct = res.data || {};
  145. this.qualityOptions = res.data.skuList || [];
  146. if (this.qualityOptions.length > 0) {
  147. this.currentQuality = this.qualityOptions[0].conditionType
  148. }
  149. }
  150. });
  151. },
  152. close() {
  153. this.visible = false;
  154. },
  155. isOptionDisabled(opt) {
  156. const stock = opt.stockNum;
  157. if (stock === 0) return true;
  158. if (stock === undefined || stock === null || stock === '') return false;
  159. return Number(stock) <= 0;
  160. },
  161. selectQuality(opt) {
  162. if (this.isOptionDisabled(opt)) {
  163. this.$u.toast('该品相暂无库存');
  164. return;
  165. }
  166. this.currentQuality = opt.conditionType;
  167. },
  168. handleAction() {
  169. if (!this.hasStock) {
  170. this.handleNotify();
  171. return;
  172. }
  173. this.handleConfirm();
  174. },
  175. handleNotify() {
  176. const isCancel = this.currentProduct.hasArrivalNotice === 1;
  177. const apiUrl = isCancel ? '/token/shop/user/noticeArrivalCancel' : '/token/shop/user/noticeArrival';
  178. uni.$u.http.post(apiUrl, {
  179. isbn: this.currentProduct.isbn,
  180. }).then(res => {
  181. if (res.code === 200) {
  182. const newValue = isCancel ? 0 : 1;
  183. this.$set(this.currentProduct, 'hasArrivalNotice', newValue);
  184. this.$emit('notice-change', newValue);
  185. this.$u.toast(isCancel ? '已取消到货通知' : '到货通知设置成功');
  186. }
  187. }).finally(() => {
  188. this.close();
  189. });
  190. },
  191. handleConfirm() {
  192. if (this.isSubmitting) return; // 防止重复提交
  193. if (!this.currentProduct.isbn) {
  194. this.$u.toast('商品信息缺失');
  195. return;
  196. }
  197. const selectedOption = this.selectedOption;
  198. const conditionType = selectedOption.conditionType || this.currentQuality;
  199. if (!conditionType) {
  200. this.$u.toast('请选择品相');
  201. return;
  202. }
  203. this.isSubmitting = true; // 设置提交中状态
  204. this.$u.api.addShopCartAjax({
  205. isbn: this.currentProduct.isbn,
  206. quantity: this.quantity,
  207. conditionType: conditionType,
  208. sourceFrom: this.sourceFrom
  209. }).then(async res => {
  210. if (res.code === 200) {
  211. await this.$updateCartBadge();
  212. this.$u.toast('加入购物车成功');
  213. this.$emit('confirm', {
  214. product: this.currentProduct,
  215. quality: this.currentQuality,
  216. quantity: this.quantity
  217. });
  218. this.close();
  219. } else {
  220. this.$u.toast(res.msg || '加入购物车失败');
  221. }
  222. }).finally(() => {
  223. this.isSubmitting = false; // 重置提交状态
  224. });
  225. },
  226. handleShareAction() {
  227. if (this.isSubmitting) return; // 防止重复提交
  228. if (!this.currentProduct.isbn) {
  229. this.$u.toast('商品信息缺失');
  230. return;
  231. }
  232. const selectedOption = this.selectedOption;
  233. const conditionType = selectedOption.conditionType || this.currentQuality;
  234. if (!conditionType) {
  235. this.$u.toast('请选择品相');
  236. return;
  237. }
  238. this.isSubmitting = true; // 设置提交中状态
  239. this.$u.api.addShopCartAjax({
  240. isbn: this.currentProduct.isbn,
  241. quantity: this.quantity,
  242. conditionType: conditionType,
  243. sourceFrom: this.sourceFrom
  244. }).then(async res => {
  245. if (res.code === 200) {
  246. await this.$updateCartBadge();
  247. this.$u.toast('加入购物车成功');
  248. this.$emit('confirm', {
  249. product: this.currentProduct,
  250. quality: this.currentQuality,
  251. quantity: this.quantity
  252. });
  253. this.close();
  254. // 打开减价弹窗
  255. setTimeout(() => {
  256. if (this.$refs.reducePopup) {
  257. this.$refs.reducePopup.open({
  258. isbn: this.currentProduct.isbn,
  259. conditionType: conditionType
  260. });
  261. }
  262. }, 300);
  263. } else {
  264. this.$u.toast(res.msg || '加入购物车失败');
  265. }
  266. }).finally(() => {
  267. this.isSubmitting = false; // 重置提交状态
  268. });
  269. },
  270. handleScan(bookInfo) {
  271. this.$refs.reduceQrcode.open(bookInfo);
  272. },
  273. formatPrice(value) {
  274. if (value === undefined || value === null) return '0.00';
  275. const num = Number(value);
  276. return Number.isFinite(num) ? num.toFixed(2) : '0.00';
  277. }
  278. }
  279. };
  280. </script>
  281. <style lang="scss" scoped>
  282. .popup-content {
  283. padding: 30rpx 30rpx 20rpx;
  284. background-color: #fff;
  285. position: relative;
  286. }
  287. .header {
  288. display: flex;
  289. justify-content: center;
  290. align-items: center;
  291. position: relative;
  292. margin-bottom: 30rpx;
  293. padding-bottom: 30rpx;
  294. border-bottom: 2rpx dashed #eee;
  295. .title {
  296. font-size: 34rpx;
  297. font-weight: bold;
  298. color: #333;
  299. }
  300. .close-icon {
  301. position: absolute;
  302. right: 0;
  303. top: 0;
  304. width: 24rpx;
  305. height: 24rpx;
  306. padding: 10rpx;
  307. box-sizing: content-box;
  308. }
  309. }
  310. .product-info {
  311. display: flex;
  312. margin-bottom: 30rpx;
  313. .book-cover {
  314. width: 140rpx;
  315. height: 180rpx;
  316. border-radius: 8rpx;
  317. margin-right: 24rpx;
  318. background-color: #f5f5f5;
  319. }
  320. .info-right {
  321. flex: 1;
  322. display: flex;
  323. flex-direction: column;
  324. justify-content: space-between;
  325. padding: 10rpx 0;
  326. .price-row {
  327. display: flex;
  328. align-items: center;
  329. .currency {
  330. font-size: 36rpx;
  331. color: #D81A00;
  332. font-weight: bold;
  333. }
  334. .price {
  335. font-size: 40rpx;
  336. color: #D81A00;
  337. font-weight: bold;
  338. margin-right: 20rpx;
  339. line-height: 1;
  340. }
  341. .drop-tag {
  342. background: #E8F9EA;
  343. padding: 4rpx 12rpx;
  344. border-radius: 4rpx;
  345. text {
  346. color: #38C248;
  347. font-size: 24rpx;
  348. font-weight: 500;
  349. }
  350. }
  351. }
  352. .tag-row {
  353. .quality-tag {
  354. display: inline-block;
  355. background: linear-gradient(0deg, #FFAB26 0%, #FFD426 100%);
  356. border-radius: 21rpx 0px 21rpx 0px;
  357. padding: 2rpx 24rpx;
  358. margin-bottom: 24rpx;
  359. text {
  360. color: #fff;
  361. font-size: 24rpx;
  362. font-weight: 500;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. .tips-row {
  369. display: flex;
  370. align-items: center;
  371. margin-bottom: 16rpx;
  372. text {
  373. font-size: 26rpx;
  374. color: #8D8D8D;
  375. margin-right: 10rpx;
  376. }
  377. .tips-icon {
  378. width: 36rpx;
  379. height: 36rpx;
  380. }
  381. }
  382. .promo-note {
  383. margin-bottom: 40rpx;
  384. text {
  385. font-size: 26rpx;
  386. color: #8D8D8D;
  387. }
  388. }
  389. .options-list {
  390. margin-bottom: 40rpx;
  391. .option-item {
  392. position: relative;
  393. display: flex;
  394. justify-content: space-between;
  395. align-items: center;
  396. background: #ffffff;
  397. border-radius: 12rpx;
  398. padding: 24rpx 30rpx;
  399. margin-bottom: 24rpx;
  400. border: 2rpx solid #dfdfdf;
  401. transition: all 0.2s;
  402. &.active {
  403. border-color: transparent;
  404. }
  405. &.disabled {
  406. background: #F0F0F0;
  407. border-color: #E0E0E0;
  408. opacity: 0.7;
  409. .opt-name,
  410. .right text {
  411. color: #999;
  412. }
  413. .opt-discount {
  414. background: #CCCCCC;
  415. }
  416. }
  417. .bg-image {
  418. position: absolute;
  419. top: -6rpx;
  420. left: -1%;
  421. width: 102%;
  422. height: 110rpx;
  423. z-index: 0;
  424. }
  425. .left,
  426. .right {
  427. position: relative;
  428. z-index: 1;
  429. }
  430. .left {
  431. display: flex;
  432. align-items: center;
  433. .opt-name {
  434. font-size: 30rpx;
  435. font-weight: bold;
  436. color: #333;
  437. margin-right: 16rpx;
  438. }
  439. .opt-discount {
  440. background: #D8D8D8;
  441. padding: 0 20rpx;
  442. border-radius: 0 20rpx 0 20rpx;
  443. text {
  444. color: #fff;
  445. font-size: 24rpx;
  446. display: inline-block;
  447. }
  448. &.active {
  449. background: linear-gradient(0deg, #30E030 0%, #28C445 100%);
  450. }
  451. }
  452. }
  453. .right {
  454. text {
  455. font-size: 28rpx;
  456. color: #333;
  457. }
  458. }
  459. }
  460. }
  461. .quantity-row {
  462. display: flex;
  463. justify-content: space-between;
  464. align-items: center;
  465. margin-bottom: 50rpx;
  466. .label {
  467. font-size: 30rpx;
  468. font-weight: bold;
  469. color: #333;
  470. }
  471. }
  472. .footer-btns {
  473. display: flex;
  474. justify-content: space-between;
  475. padding-bottom: 10rpx;
  476. .btn {
  477. flex: 1;
  478. height: 100rpx;
  479. display: flex;
  480. flex-direction: column;
  481. align-items: center;
  482. justify-content: center;
  483. font-family: Source Han Sans SC;
  484. font-weight: 500;
  485. border: none;
  486. outline: none;
  487. padding: 0;
  488. margin: 0;
  489. -webkit-tap-highlight-color: transparent;
  490. .price {
  491. font-size: 38rpx;
  492. color: #fff;
  493. line-height: 32rpx;
  494. }
  495. .desc {
  496. font-size: 24rpx;
  497. color: #fff;
  498. line-height: 32rpx;
  499. }
  500. .notice {
  501. font-size: 32rpx;
  502. color: #fff;
  503. }
  504. &.btn-orange {
  505. background: linear-gradient(0deg, #EFA941 0%, #FFB84F 100%);
  506. width: 340rpx;
  507. border-radius: 50rpx 0 0 50rpx;
  508. }
  509. &.btn-green {
  510. width: 340rpx;
  511. background: linear-gradient(0deg, #38C248 0%, #5FEA6F 100%);
  512. border-radius: 0 50rpx 50rpx 0;
  513. }
  514. &.btn-gray {
  515. background: #ccc !important;
  516. }
  517. }
  518. }
  519. </style>