index.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. // getWay: 0-扫码(A) 1-被分享(B);被分享领取后不再二次分享,直接回首页
  328. const isSharedReceive = Number(res.data.getWay) === 1;
  329. setTimeout(() => {
  330. if (isSharedReceive) {
  331. this.goHome();
  332. return;
  333. }
  334. uni.redirectTo({
  335. url: '/packet/pages/get?bianhao=' + this.bianhao
  336. });
  337. }, 1500);
  338. } else {
  339. uni.showToast({
  340. title: res.msg || '领取失败',
  341. icon: 'none'
  342. });
  343. }
  344. } catch (e) {
  345. uni.hideLoading();
  346. console.error('领取红包失败:', e);
  347. uni.showToast({
  348. title: '网络错误',
  349. icon: 'none'
  350. });
  351. }
  352. },
  353. //执行微信确认收款操作
  354. handleConfirmReceipt(data) {
  355. if (wx.canIUse('requestMerchantTransfer')) {
  356. wx.requestMerchantTransfer({
  357. mchId: data.mchId,
  358. appId: data.appId,
  359. package: data.packageInfo,
  360. success: (res) => {
  361. // res.err_msg将在页面展示成功后返回应用时返回ok,并不代表付款成功
  362. uni.showToast({
  363. title: '确认收款成功',
  364. icon: 'none'
  365. });
  366. //跳转到首页
  367. uni.switchTab({
  368. url: '/pages/sell/index'
  369. });
  370. },
  371. fail: (res) => {
  372. console.log('fail:', res);
  373. }
  374. });
  375. } else {
  376. wx.showModal({
  377. content: '你的微信版本过低,请更新至最新版本。',
  378. showCancel: false
  379. });
  380. }
  381. },
  382. handleShare() {
  383. // Share logic
  384. }
  385. }
  386. }
  387. </script>
  388. <style lang="scss" scoped>
  389. .packet-page {
  390. height: 100vh;
  391. position: relative;
  392. display: flex;
  393. flex-direction: column;
  394. align-items: center;
  395. overflow: hidden;
  396. background: #FFFFFF;
  397. .bg-image {
  398. position: absolute;
  399. top: 0;
  400. left: 0;
  401. width: 100%;
  402. height: 100%;
  403. z-index: 0;
  404. }
  405. .content-wrapper {
  406. position: relative;
  407. z-index: 1;
  408. width: 100%;
  409. padding: 40rpx 30rpx;
  410. box-sizing: border-box;
  411. .headline-image {
  412. width: 80%;
  413. margin-left: 10%;
  414. margin-bottom: 30rpx;
  415. margin-top: 30rpx;
  416. }
  417. .envelope-section {
  418. position: relative;
  419. .envelope-image {
  420. width: 90%;
  421. display: block;
  422. margin: 0 auto;
  423. }
  424. .amount-wrapper {
  425. position: absolute;
  426. top: 12%;
  427. left: 50%;
  428. transform: translateX(-50%);
  429. text-align: center;
  430. .amount-display {
  431. display: flex;
  432. align-items: baseline;
  433. justify-content: center;
  434. .currency {
  435. font-size: 78rpx;
  436. color: #FF4444;
  437. font-weight: bold;
  438. margin-right: 4rpx;
  439. }
  440. .amount-value {
  441. font-size: 156rpx;
  442. color: #FF4444;
  443. font-weight: bold;
  444. }
  445. }
  446. }
  447. .receive-btn-wrapper {
  448. position: absolute;
  449. bottom: 15%;
  450. left: 52%;
  451. transform: translateX(-50%);
  452. width: 84%;
  453. .btn-image {
  454. width: 100%;
  455. height: auto;
  456. }
  457. .btn-text {
  458. display: inline-block;
  459. position: absolute;
  460. top: 45%;
  461. left: 50%;
  462. transform: translate(-50%, -50%);
  463. font-size: 32rpx;
  464. color: #D73800;
  465. font-weight: 500;
  466. white-space: nowrap;
  467. word-break: keep-all;
  468. }
  469. }
  470. }
  471. .info-card-section {
  472. position: relative;
  473. margin-bottom: 20rpx;
  474. .info-frame {
  475. width: 100%;
  476. display: block;
  477. }
  478. .info-content {
  479. position: absolute;
  480. top: 20%;
  481. left: 0;
  482. right: 0;
  483. bottom: 0;
  484. padding: 30rpx;
  485. box-sizing: border-box;
  486. .info-list {
  487. padding: 0 20rpx;
  488. .info-item {
  489. font-family: Fotor_HelloFont_GongYiTi;
  490. font-size: 40rpx;
  491. color: #000000;
  492. line-height: 64rpx;
  493. text-align: center;
  494. .spacer {
  495. flex: 1;
  496. margin: 0 16rpx;
  497. }
  498. .star {
  499. color: #FFB300;
  500. margin-left: 8rpx;
  501. }
  502. }
  503. }
  504. .rule-footer {
  505. display: flex;
  506. justify-content: center;
  507. align-items: center;
  508. margin-top: 30rpx;
  509. cursor: pointer;
  510. .rule-text {
  511. font-size: 26rpx;
  512. color: #666;
  513. text-decoration: underline;
  514. }
  515. .share-btn-wrapper {
  516. position: absolute;
  517. bottom: 20rpx;
  518. right: 20rpx;
  519. width: 120rpx;
  520. height: 120rpx;
  521. .share-btn-image {
  522. width: 100%;
  523. height: 100%;
  524. }
  525. .share-text {
  526. position: absolute;
  527. top: 50%;
  528. left: 50%;
  529. transform: translate(-50%, -50%);
  530. font-size: 22rpx;
  531. color: #fff;
  532. font-weight: bold;
  533. text-align: center;
  534. line-height: 1.3;
  535. }
  536. }
  537. }
  538. }
  539. }
  540. }
  541. }
  542. .fail-wrapper {
  543. width: 100%;
  544. min-height: 100vh;
  545. padding: 120rpx 48rpx 80rpx;
  546. box-sizing: border-box;
  547. display: flex;
  548. justify-content: center;
  549. align-items: flex-start;
  550. background: #FFFFFF;
  551. }
  552. .fail-card {
  553. width: 100%;
  554. background: transparent;
  555. border-radius: 0;
  556. padding: 0;
  557. box-sizing: border-box;
  558. display: flex;
  559. flex-direction: column;
  560. align-items: center;
  561. box-shadow: none;
  562. }
  563. .fail-icon {
  564. width: 144rpx;
  565. height: 144rpx;
  566. border-radius: 72rpx;
  567. display: flex;
  568. align-items: center;
  569. justify-content: center;
  570. margin-bottom: 36rpx;
  571. border: 6rpx solid #FF5A5F;
  572. box-sizing: border-box;
  573. &--ban {
  574. border-color: #FF9F1A;
  575. background: rgba(255, 159, 26, 0.08);
  576. }
  577. &--warn {
  578. border-color: #F34A45;
  579. background: rgba(243, 74, 69, 0.06);
  580. }
  581. &__text {
  582. font-size: 76rpx;
  583. font-weight: 600;
  584. line-height: 1;
  585. color: #F34A45;
  586. text-transform: uppercase;
  587. }
  588. &--ban &__text {
  589. color: #FF9F1A;
  590. }
  591. }
  592. .fail-title {
  593. font-size: 40rpx;
  594. line-height: 56rpx;
  595. font-weight: 600;
  596. color: #222222;
  597. text-align: center;
  598. margin-bottom: 24rpx;
  599. }
  600. .fail-desc {
  601. font-size: 30rpx;
  602. line-height: 46rpx;
  603. color: #666666;
  604. text-align: center;
  605. margin-bottom: 72rpx;
  606. white-space: pre-wrap;
  607. }
  608. .fail-btn {
  609. width: 100%;
  610. max-width: 520rpx;
  611. height: 88rpx;
  612. border-radius: 44rpx;
  613. background: linear-gradient(90deg, #FF453A 0%, #F5221E 100%);
  614. display: flex;
  615. align-items: center;
  616. justify-content: center;
  617. text {
  618. color: #FFFFFF;
  619. font-size: 32rpx;
  620. font-weight: 500;
  621. }
  622. }
  623. .rule-popup {
  624. width: 640rpx;
  625. background: #FFFFFF;
  626. border-radius: 24rpx;
  627. padding: 36rpx 32rpx 30rpx;
  628. box-sizing: border-box;
  629. &__header {
  630. position: relative;
  631. display: flex;
  632. justify-content: center;
  633. align-items: center;
  634. margin-bottom: 28rpx;
  635. }
  636. &__title {
  637. font-size: 36rpx;
  638. font-weight: 600;
  639. color: #222222;
  640. }
  641. &__close {
  642. position: absolute;
  643. right: 0;
  644. top: 50%;
  645. transform: translateY(-50%);
  646. width: 44rpx;
  647. height: 44rpx;
  648. line-height: 44rpx;
  649. text-align: center;
  650. border-radius: 22rpx;
  651. background: #F2F3F5;
  652. color: #666666;
  653. font-size: 26rpx;
  654. }
  655. &__body {
  656. max-height: 720rpx;
  657. }
  658. &__item {
  659. font-size: 28rpx;
  660. color: #333333;
  661. line-height: 46rpx;
  662. margin-bottom: 20rpx;
  663. word-break: break-all;
  664. }
  665. &__footer {
  666. display: flex;
  667. justify-content: center;
  668. margin-top: 16rpx;
  669. }
  670. &__btn {
  671. min-width: 240rpx;
  672. height: 72rpx;
  673. line-height: 72rpx;
  674. text-align: center;
  675. background: linear-gradient(90deg, #6ADD83 0%, #38C148 100%);
  676. border-radius: 36rpx;
  677. color: #FFFFFF;
  678. font-size: 30rpx;
  679. font-weight: 500;
  680. padding: 0 30rpx;
  681. box-sizing: border-box;
  682. }
  683. }
  684. </style>