index.vue 26 KB

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