index.vue 13 KB

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