index.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <template>
  2. <view class="packet-page">
  3. <!-- Background -->
  4. <image v-if="!isFailState" src="/packet/static/index/background.png" class="bg-image" mode="widthFix"></image>
  5. <!-- Main Content -->
  6. <view v-if="isFailState" class="fail-wrapper">
  7. <view class="fail-card">
  8. <view class="fail-icon" :class="'fail-icon--' + failState.iconType">
  9. <text class="fail-icon__text">{{ failState.iconText }}</text>
  10. </view>
  11. <view class="fail-title">{{ failState.title }}</view>
  12. <view class="fail-desc">{{ failState.desc }}</view>
  13. <view class="fail-btn" @click="goHome">
  14. <text>进入首页</text>
  15. </view>
  16. </view>
  17. </view>
  18. <view v-else class="content-wrapper">
  19. <!-- Headline -->
  20. <image src="/packet/static/index/headline.png" class="headline-image" mode="widthFix"></image>
  21. <!-- Red Envelope Section -->
  22. <view class="envelope-section">
  23. <!-- Red Envelope Image -->
  24. <image src="/packet/static/index/red_envelope.png" class="envelope-image" mode="widthFix"></image>
  25. <!-- Amount Display -->
  26. <view class="amount-wrapper">
  27. <view class="amount-display">
  28. <text class="currency">¥</text>
  29. <text class="amount-value">{{ amount }}</text>
  30. </view>
  31. </view>
  32. <!-- Receive Button -->
  33. <view class="receive-btn-wrapper" @click="handleReceive">
  34. <image src="/packet/static/index/btn_receive.png" class="btn-image" mode="widthFix"></image>
  35. <text class="btn-text" v-if="!redBagData || !redBagData.drawStatus">领{{ amount }}元红包 |
  36. 买书卖书都能用</text>
  37. <text class="btn-text" v-else>已领取</text>
  38. </view>
  39. </view>
  40. <!-- Info Card -->
  41. <view class="info-card-section">
  42. <image src="/packet/static/index/info_frame.png" class="info-frame" mode="widthFix"></image>
  43. <view class="info-content">
  44. <view class="info-list">
  45. <view class="info-item">
  46. <text>买书立减</text>
  47. <text class="spacer"></text>
  48. <text>卖书加价</text>
  49. </view>
  50. <view class="info-item">
  51. <text>伴你轻松度过大学四年</text>
  52. </view>
  53. <view class="info-item">
  54. <text>官方补贴</text>
  55. <text class="spacer"></text>
  56. <text>无门槛使用</text>
  57. </view>
  58. </view>
  59. <view class="rule-footer" @click="openRulePopup">
  60. <text class="rule-text">活动规则</text>
  61. </view>
  62. </view>
  63. </view>
  64. </view>
  65. <u-popup v-model="showRulePopup" mode="center" border-radius="24" :mask-close-able="true">
  66. <view class="rule-popup">
  67. <view class="rule-popup__header">
  68. <text class="rule-popup__title">活动规则</text>
  69. <view class="rule-popup__close" @click="closeRulePopup">x</view>
  70. </view>
  71. <scroll-view scroll-y class="rule-popup__body">
  72. <view class="rule-popup__item" v-for="(item, index) in ruleList" :key="index">
  73. <text>{{ index + 1 }}.{{ item }}</text>
  74. </view>
  75. </scroll-view>
  76. <view class="rule-popup__footer" @click="closeRulePopup">
  77. <text class="rule-popup__btn">我知道了</text>
  78. </view>
  79. </view>
  80. </u-popup>
  81. </view>
  82. </template>
  83. <script>
  84. import { silentLogin } from '@/api/auth'
  85. export default {
  86. data() {
  87. return {
  88. amount: '1.58',
  89. bianhao: '', // 红包编号
  90. redBagData: null, // 红包数据
  91. failState: null, // 扫码失败态
  92. showRulePopup: false,
  93. ruleList: [
  94. '活动时间:2026年05月01日至2028年05月30日。',
  95. '活动期间,用户扫描书嗨专属红包码,均有机会获得书嗨无门槛红包或现金红包。',
  96. '每位用户每获得一个红包有一次机会成为发起者。每人最多可赠送3位好友,分享者和好友均可获得书嗨无门槛红包或现金红包。',
  97. '助力者仅限新用户。',
  98. '助力红包发放时间:活动时间内,点击领取红包券,会实时到账小程序红包。',
  99. '无门槛红包可用于买书或卖书,买书订单将抵扣消费金额,卖书订单将加于书款上。'
  100. ]
  101. }
  102. },
  103. computed: {
  104. isFailState() {
  105. return !!this.failState;
  106. }
  107. },
  108. onLoad(options) {
  109. let bianhao = options.bianhao;
  110. // 兼容扫码进入场景,当前页的 scene 里带了 bianhao
  111. if (!bianhao && options.scene) {
  112. const decodeScene = decodeURIComponent(options.scene);
  113. const paramPairs = decodeScene.split('&');
  114. paramPairs.forEach((pair) => {
  115. const [key, value] = pair.split('=');
  116. if (key === 'bianhao') {
  117. bianhao = value;
  118. }
  119. });
  120. }
  121. // 兼容从 App.vue 缓存中获取 (因为如果是扫小程序码直接进入这个页面,App.vue的onLaunch会先执行并存入缓存,然后执行页面的onLoad)
  122. if (!bianhao) {
  123. bianhao = uni.getStorageSync('scene_bianhao');
  124. }
  125. if (bianhao) {
  126. this.bianhao = bianhao;
  127. uni.removeStorageSync('scene_bianhao');
  128. // 调用扫码接口获取红包信息
  129. this.scanRedBag();
  130. }
  131. },
  132. onShow() {
  133. // 如果是从其他页面返回,重新获取红包数据
  134. if (this.bianhao) {
  135. this.scanRedBag();
  136. }
  137. },
  138. methods: {
  139. getCantTypeValue(source = {}) {
  140. const rawCantType = source.cantType;
  141. if (rawCantType === undefined || rawCantType === null || rawCantType === '') {
  142. return null;
  143. }
  144. const cantType = Number(rawCantType);
  145. return Number.isNaN(cantType) ? null : cantType;
  146. },
  147. buildFailState(source = {}, fallbackMsg = '') {
  148. const cantType = this.getCantTypeValue(source);
  149. if (cantType === null) {
  150. return null;
  151. }
  152. const scanCount = source.scanNum || source.scanCount || source.usedCount || source.count || '';
  153. const failStateMap = {
  154. 0: {
  155. iconType: 'warn',
  156. iconText: '!',
  157. title: scanCount ? `此二维码已被扫 ${scanCount} 次` : '扫码次数超上限',
  158. desc: scanCount ? '该二维码可参与次数已用完,请更换二维码后重试。' : '该二维码可参与次数已达上限,请更换二维码后重试。'
  159. },
  160. 1: {
  161. iconType: 'ban',
  162. iconText: 'x',
  163. title: '扫码禁用',
  164. desc: '您的扫码功能已被禁用,当前无法领取红包。'
  165. },
  166. 3: {
  167. iconType: 'warn',
  168. iconText: '!',
  169. title: '系统繁忙',
  170. desc: fallbackMsg || '系统开小差了,请稍后再试。'
  171. },
  172. 4: {
  173. iconType: 'warn',
  174. iconText: '!',
  175. title: '活动已结束',
  176. desc: '很抱歉,当前活动已结束,无法继续领取红包。'
  177. },
  178. 5: {
  179. iconType: 'warn',
  180. iconText: '!',
  181. title: '您不是新用户',
  182. desc: '抱歉!您不是新用户,无法领取分享红包。'
  183. },
  184. 6: {
  185. iconType: 'warn',
  186. iconText: '!',
  187. title: '分享超限',
  188. desc: '当前分享次数已达上限,无法继续领取分享红包。'
  189. },
  190. 7: {
  191. iconType: 'warn',
  192. iconText: '!',
  193. title: '二维码已使用',
  194. desc: '当前二维码已被使用,不能重复领取。'
  195. }
  196. };
  197. return failStateMap[cantType] || {
  198. iconType: 'warn',
  199. iconText: '!',
  200. title: '领取失败',
  201. desc: fallbackMsg || '当前无法领取红包,请稍后再试。'
  202. };
  203. },
  204. applyCantTypeState(source = {}, fallbackMsg = '') {
  205. const failState = this.buildFailState(source, fallbackMsg);
  206. if (!failState) {
  207. return false;
  208. }
  209. this.failState = failState;
  210. this.redBagData = null;
  211. uni.hideLoading();
  212. return true;
  213. },
  214. goHome() {
  215. uni.switchTab({
  216. url: '/pages/sell/index'
  217. });
  218. },
  219. openRulePopup() {
  220. this.showRulePopup = true;
  221. },
  222. closeRulePopup() {
  223. this.showRulePopup = false;
  224. },
  225. // 请求前确保 token 可用,避免扫码冷启动时出现 401
  226. async ensureTokenReady() {
  227. const token = uni.getStorageSync('token')
  228. if (token) return true
  229. try {
  230. await silentLogin()
  231. return !!uni.getStorageSync('token')
  232. } catch (e) {
  233. return false
  234. }
  235. },
  236. // 扫码获取红包信息
  237. async scanRedBag() {
  238. if (!this.bianhao) {
  239. // 如果没有编号,提示用户扫码
  240. uni.showModal({
  241. title: '提示',
  242. content: '请扫描红包二维码',
  243. showCancel: false
  244. });
  245. return;
  246. }
  247. const loginReady = await this.ensureTokenReady()
  248. if (!loginReady) {
  249. uni.showToast({
  250. title: '登录状态失效,请稍后重试',
  251. icon: 'none'
  252. });
  253. return;
  254. }
  255. this.$u.api.scanRedBagAjax(this.bianhao).then(res => {
  256. console.log('扫码获取红包信息成功:', res);
  257. if (this.applyCantTypeState(res.data || res, res.msg)) {
  258. return;
  259. }
  260. if (res.code === 200) {
  261. this.failState = null;
  262. this.redBagData = res.data;
  263. this.amount = res.data.redPrice;
  264. } else {
  265. uni.showToast({
  266. title: res.msg || '获取红包信息失败',
  267. icon: 'none'
  268. });
  269. }
  270. })
  271. },
  272. // 领取红包
  273. async handleReceive() {
  274. if (!this.bianhao) {
  275. uni.showToast({
  276. title: '红包编号缺失',
  277. icon: 'none'
  278. });
  279. return;
  280. }
  281. const loginReady = await this.ensureTokenReady()
  282. if (!loginReady) {
  283. uni.showToast({
  284. title: '登录状态失效,请稍后重试',
  285. icon: 'none'
  286. });
  287. return;
  288. }
  289. // 显示领取中提示
  290. uni.showLoading({
  291. title: '领取中...'
  292. });
  293. try {
  294. const res = await this.$u.api.getRedBagAjax(this.bianhao);
  295. uni.hideLoading();
  296. if (this.applyCantTypeState(res.data || res, res.msg)) {
  297. return;
  298. }
  299. if (res.code === 200) {
  300. // 更新红包数据
  301. this.failState = null;
  302. this.redBagData = res.data;
  303. //如果是现金红包,执行确认收款操作
  304. if (res.data.redType == 1) {
  305. return this.handleConfirmReceipt(res.data)
  306. }
  307. uni.showToast({
  308. title: '领取成功',
  309. icon: 'success'
  310. });
  311. // 跳转到 get 页面,并传递红包数据
  312. setTimeout(() => {
  313. uni.redirectTo({
  314. url: '/packet/pages/get?bianhao=' + this.bianhao
  315. });
  316. }, 1500);
  317. } else {
  318. uni.showToast({
  319. title: res.msg || '领取失败',
  320. icon: 'none'
  321. });
  322. }
  323. } catch (e) {
  324. uni.hideLoading();
  325. console.error('领取红包失败:', e);
  326. uni.showToast({
  327. title: '网络错误',
  328. icon: 'none'
  329. });
  330. }
  331. },
  332. //执行微信确认收款操作
  333. handleConfirmReceipt(data) {
  334. if (wx.canIUse('requestMerchantTransfer')) {
  335. wx.requestMerchantTransfer({
  336. mchId: data.mchId,
  337. appId: data.appId,
  338. package: data.packageInfo,
  339. success: (res) => {
  340. // res.err_msg将在页面展示成功后返回应用时返回ok,并不代表付款成功
  341. uni.showToast({
  342. title: '确认收款成功',
  343. icon: 'none'
  344. });
  345. //跳转到首页
  346. uni.switchTab({
  347. url: '/pages/sell/index'
  348. });
  349. },
  350. fail: (res) => {
  351. console.log('fail:', res);
  352. }
  353. });
  354. } else {
  355. wx.showModal({
  356. content: '你的微信版本过低,请更新至最新版本。',
  357. showCancel: false
  358. });
  359. }
  360. },
  361. handleShare() {
  362. // Share logic
  363. }
  364. }
  365. }
  366. </script>
  367. <style lang="scss" scoped>
  368. .packet-page {
  369. height: 100vh;
  370. position: relative;
  371. display: flex;
  372. flex-direction: column;
  373. align-items: center;
  374. overflow: hidden;
  375. background: #FFFFFF;
  376. .bg-image {
  377. position: absolute;
  378. top: 0;
  379. left: 0;
  380. width: 100%;
  381. height: 100%;
  382. z-index: 0;
  383. }
  384. .content-wrapper {
  385. position: relative;
  386. z-index: 1;
  387. width: 100%;
  388. padding: 40rpx 30rpx;
  389. box-sizing: border-box;
  390. .headline-image {
  391. width: 80%;
  392. margin-left: 10%;
  393. margin-bottom: 30rpx;
  394. margin-top: 30rpx;
  395. }
  396. .envelope-section {
  397. position: relative;
  398. .envelope-image {
  399. width: 90%;
  400. display: block;
  401. margin: 0 auto;
  402. }
  403. .amount-wrapper {
  404. position: absolute;
  405. top: 12%;
  406. left: 50%;
  407. transform: translateX(-50%);
  408. text-align: center;
  409. .amount-display {
  410. display: flex;
  411. align-items: baseline;
  412. justify-content: center;
  413. .currency {
  414. font-size: 78rpx;
  415. color: #FF4444;
  416. font-weight: bold;
  417. margin-right: 4rpx;
  418. }
  419. .amount-value {
  420. font-size: 156rpx;
  421. color: #FF4444;
  422. font-weight: bold;
  423. }
  424. }
  425. }
  426. .receive-btn-wrapper {
  427. position: absolute;
  428. bottom: 15%;
  429. left: 52%;
  430. transform: translateX(-50%);
  431. width: 84%;
  432. .btn-image {
  433. width: 100%;
  434. height: auto;
  435. }
  436. .btn-text {
  437. display: inline-block;
  438. position: absolute;
  439. top: 45%;
  440. left: 50%;
  441. transform: translate(-50%, -50%);
  442. font-size: 32rpx;
  443. color: #D73800;
  444. font-weight: 500;
  445. white-space: nowrap;
  446. word-break: keep-all;
  447. }
  448. }
  449. }
  450. .info-card-section {
  451. position: relative;
  452. margin-bottom: 20rpx;
  453. .info-frame {
  454. width: 100%;
  455. display: block;
  456. }
  457. .info-content {
  458. position: absolute;
  459. top: 20%;
  460. left: 0;
  461. right: 0;
  462. bottom: 0;
  463. padding: 30rpx;
  464. box-sizing: border-box;
  465. .info-list {
  466. padding: 0 20rpx;
  467. .info-item {
  468. font-family: Fotor_HelloFont_GongYiTi;
  469. font-size: 40rpx;
  470. color: #000000;
  471. line-height: 64rpx;
  472. text-align: center;
  473. .spacer {
  474. flex: 1;
  475. margin: 0 16rpx;
  476. }
  477. .star {
  478. color: #FFB300;
  479. margin-left: 8rpx;
  480. }
  481. }
  482. }
  483. .rule-footer {
  484. display: flex;
  485. justify-content: center;
  486. align-items: center;
  487. margin-top: 30rpx;
  488. cursor: pointer;
  489. .rule-text {
  490. font-size: 26rpx;
  491. color: #666;
  492. text-decoration: underline;
  493. }
  494. .share-btn-wrapper {
  495. position: absolute;
  496. bottom: 20rpx;
  497. right: 20rpx;
  498. width: 120rpx;
  499. height: 120rpx;
  500. .share-btn-image {
  501. width: 100%;
  502. height: 100%;
  503. }
  504. .share-text {
  505. position: absolute;
  506. top: 50%;
  507. left: 50%;
  508. transform: translate(-50%, -50%);
  509. font-size: 22rpx;
  510. color: #fff;
  511. font-weight: bold;
  512. text-align: center;
  513. line-height: 1.3;
  514. }
  515. }
  516. }
  517. }
  518. }
  519. }
  520. }
  521. .fail-wrapper {
  522. width: 100%;
  523. min-height: 100vh;
  524. padding: 120rpx 48rpx 80rpx;
  525. box-sizing: border-box;
  526. display: flex;
  527. justify-content: center;
  528. align-items: flex-start;
  529. background: #FFFFFF;
  530. }
  531. .fail-card {
  532. width: 100%;
  533. background: transparent;
  534. border-radius: 0;
  535. padding: 0;
  536. box-sizing: border-box;
  537. display: flex;
  538. flex-direction: column;
  539. align-items: center;
  540. box-shadow: none;
  541. }
  542. .fail-icon {
  543. width: 144rpx;
  544. height: 144rpx;
  545. border-radius: 72rpx;
  546. display: flex;
  547. align-items: center;
  548. justify-content: center;
  549. margin-bottom: 36rpx;
  550. border: 6rpx solid #FF5A5F;
  551. box-sizing: border-box;
  552. &--ban {
  553. border-color: #FF9F1A;
  554. background: rgba(255, 159, 26, 0.08);
  555. }
  556. &--warn {
  557. border-color: #F34A45;
  558. background: rgba(243, 74, 69, 0.06);
  559. }
  560. &__text {
  561. font-size: 76rpx;
  562. font-weight: 600;
  563. line-height: 1;
  564. color: #F34A45;
  565. text-transform: uppercase;
  566. }
  567. &--ban &__text {
  568. color: #FF9F1A;
  569. }
  570. }
  571. .fail-title {
  572. font-size: 40rpx;
  573. line-height: 56rpx;
  574. font-weight: 600;
  575. color: #222222;
  576. text-align: center;
  577. margin-bottom: 24rpx;
  578. }
  579. .fail-desc {
  580. font-size: 30rpx;
  581. line-height: 46rpx;
  582. color: #666666;
  583. text-align: center;
  584. margin-bottom: 72rpx;
  585. white-space: pre-wrap;
  586. }
  587. .fail-btn {
  588. width: 100%;
  589. max-width: 520rpx;
  590. height: 88rpx;
  591. border-radius: 44rpx;
  592. background: linear-gradient(90deg, #FF453A 0%, #F5221E 100%);
  593. display: flex;
  594. align-items: center;
  595. justify-content: center;
  596. text {
  597. color: #FFFFFF;
  598. font-size: 32rpx;
  599. font-weight: 500;
  600. }
  601. }
  602. .rule-popup {
  603. width: 640rpx;
  604. background: #FFFFFF;
  605. border-radius: 24rpx;
  606. padding: 36rpx 32rpx 30rpx;
  607. box-sizing: border-box;
  608. &__header {
  609. position: relative;
  610. display: flex;
  611. justify-content: center;
  612. align-items: center;
  613. margin-bottom: 28rpx;
  614. }
  615. &__title {
  616. font-size: 36rpx;
  617. font-weight: 600;
  618. color: #222222;
  619. }
  620. &__close {
  621. position: absolute;
  622. right: 0;
  623. top: 50%;
  624. transform: translateY(-50%);
  625. width: 44rpx;
  626. height: 44rpx;
  627. line-height: 44rpx;
  628. text-align: center;
  629. border-radius: 22rpx;
  630. background: #F2F3F5;
  631. color: #666666;
  632. font-size: 26rpx;
  633. }
  634. &__body {
  635. max-height: 720rpx;
  636. }
  637. &__item {
  638. font-size: 28rpx;
  639. color: #333333;
  640. line-height: 46rpx;
  641. margin-bottom: 20rpx;
  642. word-break: break-all;
  643. }
  644. &__footer {
  645. display: flex;
  646. justify-content: center;
  647. margin-top: 16rpx;
  648. }
  649. &__btn {
  650. min-width: 240rpx;
  651. height: 72rpx;
  652. line-height: 72rpx;
  653. text-align: center;
  654. background: linear-gradient(90deg, #6ADD83 0%, #38C148 100%);
  655. border-radius: 36rpx;
  656. color: #FFFFFF;
  657. font-size: 30rpx;
  658. font-weight: 500;
  659. padding: 0 30rpx;
  660. box-sizing: border-box;
  661. }
  662. }
  663. </style>