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. <button class="btn btn-orange" open-type="share" :data-share-type="'product'" :data-title="currentProduct.bookName" :data-isbn="currentProduct.isbn" :data-image="currentProduct.cover" @click="handleShareAction">
  66. <text class="price">¥{{ displayReducePrice }}</text>
  67. <text class="desc">分享一人可降 {{ currentProduct.reducePrice }} 元</text>
  68. </button>
  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 > 1) {
  147. this.currentQuality = this.qualityOptions[1].conditionType || 2;
  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. } else {
  256. this.$u.toast(res.msg || '加入购物车失败');
  257. }
  258. }).finally(() => {
  259. this.isSubmitting = false; // 重置提交状态
  260. });
  261. },
  262. handleScan(bookInfo) {
  263. this.$refs.reduceQrcode.open(bookInfo);
  264. },
  265. formatPrice(value) {
  266. if (value === undefined || value === null) return '0.00';
  267. const num = Number(value);
  268. return Number.isFinite(num) ? num.toFixed(2) : '0.00';
  269. }
  270. }
  271. };
  272. </script>
  273. <style lang="scss" scoped>
  274. .popup-content {
  275. padding: 30rpx 30rpx 20rpx;
  276. background-color: #fff;
  277. position: relative;
  278. }
  279. .header {
  280. display: flex;
  281. justify-content: center;
  282. align-items: center;
  283. position: relative;
  284. margin-bottom: 30rpx;
  285. padding-bottom: 30rpx;
  286. border-bottom: 2rpx dashed #eee;
  287. .title {
  288. font-size: 34rpx;
  289. font-weight: bold;
  290. color: #333;
  291. }
  292. .close-icon {
  293. position: absolute;
  294. right: 0;
  295. top: 0;
  296. width: 24rpx;
  297. height: 24rpx;
  298. padding: 10rpx;
  299. box-sizing: content-box;
  300. }
  301. }
  302. .product-info {
  303. display: flex;
  304. margin-bottom: 30rpx;
  305. .book-cover {
  306. width: 140rpx;
  307. height: 180rpx;
  308. border-radius: 8rpx;
  309. margin-right: 24rpx;
  310. background-color: #f5f5f5;
  311. }
  312. .info-right {
  313. flex: 1;
  314. display: flex;
  315. flex-direction: column;
  316. justify-content: space-between;
  317. padding: 10rpx 0;
  318. .price-row {
  319. display: flex;
  320. align-items: center;
  321. .currency {
  322. font-size: 36rpx;
  323. color: #D81A00;
  324. font-weight: bold;
  325. }
  326. .price {
  327. font-size: 40rpx;
  328. color: #D81A00;
  329. font-weight: bold;
  330. margin-right: 20rpx;
  331. line-height: 1;
  332. }
  333. .drop-tag {
  334. background: #E8F9EA;
  335. padding: 4rpx 12rpx;
  336. border-radius: 4rpx;
  337. text {
  338. color: #38C248;
  339. font-size: 24rpx;
  340. font-weight: 500;
  341. }
  342. }
  343. }
  344. .tag-row {
  345. .quality-tag {
  346. display: inline-block;
  347. background: linear-gradient(0deg, #FFAB26 0%, #FFD426 100%);
  348. border-radius: 21rpx 0px 21rpx 0px;
  349. padding: 2rpx 24rpx;
  350. margin-bottom: 24rpx;
  351. text {
  352. color: #fff;
  353. font-size: 24rpx;
  354. font-weight: 500;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. .tips-row {
  361. display: flex;
  362. align-items: center;
  363. margin-bottom: 16rpx;
  364. text {
  365. font-size: 26rpx;
  366. color: #8D8D8D;
  367. margin-right: 10rpx;
  368. }
  369. .tips-icon {
  370. width: 36rpx;
  371. height: 36rpx;
  372. }
  373. }
  374. .promo-note {
  375. margin-bottom: 40rpx;
  376. text {
  377. font-size: 26rpx;
  378. color: #8D8D8D;
  379. }
  380. }
  381. .options-list {
  382. margin-bottom: 40rpx;
  383. .option-item {
  384. position: relative;
  385. display: flex;
  386. justify-content: space-between;
  387. align-items: center;
  388. background: #ffffff;
  389. border-radius: 12rpx;
  390. padding: 24rpx 30rpx;
  391. margin-bottom: 24rpx;
  392. border: 2rpx solid #dfdfdf;
  393. transition: all 0.2s;
  394. &.active {
  395. border-color: transparent;
  396. }
  397. &.disabled {
  398. background: #F0F0F0;
  399. border-color: #E0E0E0;
  400. opacity: 0.7;
  401. .opt-name,
  402. .right text {
  403. color: #999;
  404. }
  405. .opt-discount {
  406. background: #CCCCCC;
  407. }
  408. }
  409. .bg-image {
  410. position: absolute;
  411. top: -6rpx;
  412. left: -1%;
  413. width: 102%;
  414. height: 110rpx;
  415. z-index: 0;
  416. }
  417. .left,
  418. .right {
  419. position: relative;
  420. z-index: 1;
  421. }
  422. .left {
  423. display: flex;
  424. align-items: center;
  425. .opt-name {
  426. font-size: 30rpx;
  427. font-weight: bold;
  428. color: #333;
  429. margin-right: 16rpx;
  430. }
  431. .opt-discount {
  432. background: #D8D8D8;
  433. padding: 0 20rpx;
  434. border-radius: 0 20rpx 0 20rpx;
  435. text {
  436. color: #fff;
  437. font-size: 24rpx;
  438. display: inline-block;
  439. }
  440. &.active {
  441. background: linear-gradient(0deg, #30E030 0%, #28C445 100%);
  442. }
  443. }
  444. }
  445. .right {
  446. text {
  447. font-size: 28rpx;
  448. color: #333;
  449. }
  450. }
  451. }
  452. }
  453. .quantity-row {
  454. display: flex;
  455. justify-content: space-between;
  456. align-items: center;
  457. margin-bottom: 50rpx;
  458. .label {
  459. font-size: 30rpx;
  460. font-weight: bold;
  461. color: #333;
  462. }
  463. }
  464. .footer-btns {
  465. display: flex;
  466. justify-content: space-between;
  467. padding-bottom: 10rpx;
  468. .btn {
  469. flex: 1;
  470. height: 100rpx;
  471. display: flex;
  472. flex-direction: column;
  473. align-items: center;
  474. justify-content: center;
  475. font-family: Source Han Sans SC;
  476. font-weight: 500;
  477. border: none;
  478. outline: none;
  479. padding: 0;
  480. margin: 0;
  481. -webkit-tap-highlight-color: transparent;
  482. &::after {
  483. border: none;
  484. }
  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>