index.vue 14 KB

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