index.vue 14 KB

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