index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <template>
  2. <view class="packet-page">
  3. <!-- Background -->
  4. <image src="/packet/static/index/background.png" class="bg-image" mode="widthFix"></image>
  5. <!-- Main Content -->
  6. <view class="content-wrapper">
  7. <!-- Headline -->
  8. <image src="/packet/static/index/headline.png" class="headline-image" mode="widthFix"></image>
  9. <!-- Red Envelope Section -->
  10. <view class="envelope-section">
  11. <!-- Red Envelope Image -->
  12. <image src="/packet/static/index/red_envelope.png" class="envelope-image" mode="widthFix"></image>
  13. <!-- Amount Display -->
  14. <view class="amount-wrapper">
  15. <view class="amount-display">
  16. <text class="currency">¥</text>
  17. <text class="amount-value">{{ amount }}</text>
  18. </view>
  19. </view>
  20. <!-- Receive Button -->
  21. <view class="receive-btn-wrapper" @click="handleReceive">
  22. <image src="/packet/static/index/btn_receive.png" class="btn-image" mode="widthFix"></image>
  23. <text class="btn-text" v-if="!redBagData.drawStatus">领{{ amount }}元红包 | 买书卖书都能用</text>
  24. <text class="btn-text" v-else>已领取</text>
  25. </view>
  26. </view>
  27. <!-- Info Card -->
  28. <view class="info-card-section">
  29. <image src="/packet/static/index/info_frame.png" class="info-frame" mode="widthFix"></image>
  30. <view class="info-content">
  31. <view class="info-list">
  32. <view class="info-item">
  33. <text>买书立减</text>
  34. <text class="spacer"></text>
  35. <text>卖书加价</text>
  36. </view>
  37. <view class="info-item">
  38. <text>伴你轻松度过大学四年</text>
  39. </view>
  40. <view class="info-item">
  41. <text>官方补贴</text>
  42. <text class="spacer"></text>
  43. <text>无门槛使用</text>
  44. </view>
  45. </view>
  46. <view class="rule-footer">
  47. <text class="rule-text">活动规则</text>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import { silentLogin } from '@/api/auth'
  56. export default {
  57. data() {
  58. return {
  59. amount: '1.58',
  60. bianhao: '', // 红包编号
  61. redBagData: null // 红包数据
  62. }
  63. },
  64. onLoad(options) {
  65. let bianhao = options.bianhao;
  66. // 兼容扫码进入场景,当前页的 scene 里带了 bianhao
  67. if (!bianhao && options.scene) {
  68. const decodeScene = decodeURIComponent(options.scene);
  69. const paramPairs = decodeScene.split('&');
  70. paramPairs.forEach((pair) => {
  71. const [key, value] = pair.split('=');
  72. if (key === 'bianhao') {
  73. bianhao = value;
  74. }
  75. });
  76. }
  77. // 兼容从 App.vue 缓存中获取 (因为如果是扫小程序码直接进入这个页面,App.vue的onLaunch会先执行并存入缓存,然后执行页面的onLoad)
  78. if (!bianhao) {
  79. bianhao = uni.getStorageSync('scene_bianhao');
  80. }
  81. if (bianhao) {
  82. this.bianhao = bianhao;
  83. uni.removeStorageSync('scene_bianhao');
  84. // 调用扫码接口获取红包信息
  85. this.scanRedBag();
  86. }
  87. },
  88. onShow() {
  89. // 如果是从其他页面返回,重新获取红包数据
  90. if (this.bianhao) {
  91. this.scanRedBag();
  92. }
  93. },
  94. methods: {
  95. // 请求前确保 token 可用,避免扫码冷启动时出现 401
  96. async ensureTokenReady() {
  97. const token = uni.getStorageSync('token')
  98. if (token) return true
  99. try {
  100. await silentLogin()
  101. return !!uni.getStorageSync('token')
  102. } catch (e) {
  103. return false
  104. }
  105. },
  106. // 扫码获取红包信息
  107. async scanRedBag() {
  108. if (!this.bianhao) {
  109. // 如果没有编号,提示用户扫码
  110. uni.showModal({
  111. title: '提示',
  112. content: '请扫描红包二维码',
  113. showCancel: false
  114. });
  115. return;
  116. }
  117. const loginReady = await this.ensureTokenReady()
  118. if (!loginReady) {
  119. uni.showToast({
  120. title: '登录状态失效,请稍后重试',
  121. icon: 'none'
  122. });
  123. return;
  124. }
  125. try {
  126. const res = await this.$u.api.scanRedBagAjax(this.bianhao);
  127. if (res.code === 200 || res.code === 0) {
  128. this.redBagData = res.data;
  129. this.amount = res.data.redPrice;
  130. } else {
  131. uni.showToast({
  132. title: res.msg || '获取红包信息失败',
  133. icon: 'none'
  134. });
  135. }
  136. } catch (e) {
  137. console.error('扫码获取红包信息失败:', e);
  138. uni.showToast({
  139. title: '网络错误',
  140. icon: 'none'
  141. });
  142. }
  143. },
  144. // 领取红包
  145. async handleReceive() {
  146. // 如果已领取,提示用户
  147. if (this.redBagData && this.redBagData.drawStatus === 1) {
  148. uni.showToast({
  149. title: '红包已领取',
  150. icon: 'none'
  151. });
  152. return;
  153. }
  154. if (!this.bianhao) {
  155. uni.showToast({
  156. title: '红包编号缺失',
  157. icon: 'none'
  158. });
  159. return;
  160. }
  161. const loginReady = await this.ensureTokenReady()
  162. if (!loginReady) {
  163. uni.showToast({
  164. title: '登录状态失效,请稍后重试',
  165. icon: 'none'
  166. });
  167. return;
  168. }
  169. // 显示领取中提示
  170. uni.showLoading({
  171. title: '领取中...'
  172. });
  173. try {
  174. const res = await this.$u.api.getRedBagAjax(this.bianhao);
  175. uni.hideLoading();
  176. if (res.code === 200) {
  177. // 更新红包数据
  178. this.redBagData = res.data;
  179. uni.showToast({
  180. title: '领取成功',
  181. icon: 'success'
  182. });
  183. // 跳转到 get 页面,并传递红包数据
  184. setTimeout(() => {
  185. uni.redirectTo({
  186. url: '/packet/pages/get?bianhao=' + this.bianhao
  187. });
  188. }, 1500);
  189. } else {
  190. uni.showToast({
  191. title: res.msg || '领取失败',
  192. icon: 'none'
  193. });
  194. }
  195. } catch (e) {
  196. uni.hideLoading();
  197. console.error('领取红包失败:', e);
  198. uni.showToast({
  199. title: '网络错误',
  200. icon: 'none'
  201. });
  202. }
  203. },
  204. handleShare() {
  205. // Share logic
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss" scoped>
  211. .packet-page {
  212. height: 100vh;
  213. position: relative;
  214. display: flex;
  215. flex-direction: column;
  216. align-items: center;
  217. overflow: hidden;
  218. .bg-image {
  219. position: absolute;
  220. top: 0;
  221. left: 0;
  222. width: 100%;
  223. height: 100%;
  224. z-index: 0;
  225. }
  226. .content-wrapper {
  227. position: relative;
  228. z-index: 1;
  229. width: 100%;
  230. padding: 40rpx 30rpx;
  231. box-sizing: border-box;
  232. .headline-image {
  233. width: 80%;
  234. margin-left: 10%;
  235. margin-bottom: 30rpx;
  236. margin-top: 30rpx;
  237. }
  238. .envelope-section {
  239. position: relative;
  240. .envelope-image {
  241. width: 90%;
  242. display: block;
  243. margin: 0 auto;
  244. }
  245. .amount-wrapper {
  246. position: absolute;
  247. top: 12%;
  248. left: 50%;
  249. transform: translateX(-50%);
  250. text-align: center;
  251. .amount-display {
  252. display: flex;
  253. align-items: baseline;
  254. justify-content: center;
  255. .currency {
  256. font-size: 78rpx;
  257. color: #FF4444;
  258. font-weight: bold;
  259. margin-right: 4rpx;
  260. }
  261. .amount-value {
  262. font-size: 156rpx;
  263. color: #FF4444;
  264. font-weight: bold;
  265. }
  266. }
  267. }
  268. .receive-btn-wrapper {
  269. position: absolute;
  270. bottom: 15%;
  271. left: 52%;
  272. transform: translateX(-50%);
  273. width: 84%;
  274. .btn-image {
  275. width: 100%;
  276. height: auto;
  277. }
  278. .btn-text {
  279. position: absolute;
  280. top: 45%;
  281. left: 50%;
  282. transform: translate(-50%, -50%);
  283. font-size: 32rpx;
  284. color: #D73800;
  285. font-weight: 500;
  286. white-space: nowrap;
  287. }
  288. }
  289. }
  290. .info-card-section {
  291. position: relative;
  292. margin-bottom: 20rpx;
  293. .info-frame {
  294. width: 100%;
  295. display: block;
  296. }
  297. .info-content {
  298. position: absolute;
  299. top: 20%;
  300. left: 0;
  301. right: 0;
  302. bottom: 0;
  303. padding: 30rpx;
  304. box-sizing: border-box;
  305. .info-list {
  306. padding: 0 20rpx;
  307. .info-item {
  308. font-family: Fotor_HelloFont_GongYiTi;
  309. font-size: 40rpx;
  310. color: #000000;
  311. line-height: 64rpx;
  312. text-align: center;
  313. .spacer {
  314. flex: 1;
  315. margin: 0 16rpx;
  316. }
  317. .star {
  318. color: #FFB300;
  319. margin-left: 8rpx;
  320. }
  321. }
  322. }
  323. .rule-footer {
  324. display: flex;
  325. justify-content: center;
  326. align-items: center;
  327. margin-top: 30rpx;
  328. .rule-text {
  329. font-size: 26rpx;
  330. color: #666;
  331. text-decoration: underline;
  332. }
  333. .share-btn-wrapper {
  334. position: absolute;
  335. bottom: 20rpx;
  336. right: 20rpx;
  337. width: 120rpx;
  338. height: 120rpx;
  339. .share-btn-image {
  340. width: 100%;
  341. height: 100%;
  342. }
  343. .share-text {
  344. position: absolute;
  345. top: 50%;
  346. left: 50%;
  347. transform: translate(-50%, -50%);
  348. font-size: 22rpx;
  349. color: #fff;
  350. font-weight: bold;
  351. text-align: center;
  352. line-height: 1.3;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. }
  360. </style>