index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. this.$u.api.scanRedBagAjax(this.bianhao).then(res => {
  126. console.log('扫码获取红包信息成功:', res);
  127. if (res.code === 200) {
  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. })
  137. },
  138. // 领取红包
  139. async handleReceive() {
  140. // 如果已领取,提示用户
  141. if (this.redBagData && this.redBagData.drawStatus === 1) {
  142. uni.showToast({
  143. title: '红包已领取',
  144. icon: 'none'
  145. });
  146. return;
  147. }
  148. if (!this.bianhao) {
  149. uni.showToast({
  150. title: '红包编号缺失',
  151. icon: 'none'
  152. });
  153. return;
  154. }
  155. const loginReady = await this.ensureTokenReady()
  156. if (!loginReady) {
  157. uni.showToast({
  158. title: '登录状态失效,请稍后重试',
  159. icon: 'none'
  160. });
  161. return;
  162. }
  163. // 显示领取中提示
  164. uni.showLoading({
  165. title: '领取中...'
  166. });
  167. try {
  168. const res = await this.$u.api.getRedBagAjax(this.bianhao);
  169. uni.hideLoading();
  170. if (res.code === 200) {
  171. // 更新红包数据
  172. this.redBagData = res.data;
  173. uni.showToast({
  174. title: '领取成功',
  175. icon: 'success'
  176. });
  177. // 跳转到 get 页面,并传递红包数据
  178. setTimeout(() => {
  179. uni.redirectTo({
  180. url: '/packet/pages/get?bianhao=' + this.bianhao
  181. });
  182. }, 1500);
  183. } else {
  184. uni.showToast({
  185. title: res.msg || '领取失败',
  186. icon: 'none'
  187. });
  188. }
  189. } catch (e) {
  190. uni.hideLoading();
  191. console.error('领取红包失败:', e);
  192. uni.showToast({
  193. title: '网络错误',
  194. icon: 'none'
  195. });
  196. }
  197. },
  198. handleShare() {
  199. // Share logic
  200. }
  201. }
  202. }
  203. </script>
  204. <style lang="scss" scoped>
  205. .packet-page {
  206. height: 100vh;
  207. position: relative;
  208. display: flex;
  209. flex-direction: column;
  210. align-items: center;
  211. overflow: hidden;
  212. .bg-image {
  213. position: absolute;
  214. top: 0;
  215. left: 0;
  216. width: 100%;
  217. height: 100%;
  218. z-index: 0;
  219. }
  220. .content-wrapper {
  221. position: relative;
  222. z-index: 1;
  223. width: 100%;
  224. padding: 40rpx 30rpx;
  225. box-sizing: border-box;
  226. .headline-image {
  227. width: 80%;
  228. margin-left: 10%;
  229. margin-bottom: 30rpx;
  230. margin-top: 30rpx;
  231. }
  232. .envelope-section {
  233. position: relative;
  234. .envelope-image {
  235. width: 90%;
  236. display: block;
  237. margin: 0 auto;
  238. }
  239. .amount-wrapper {
  240. position: absolute;
  241. top: 12%;
  242. left: 50%;
  243. transform: translateX(-50%);
  244. text-align: center;
  245. .amount-display {
  246. display: flex;
  247. align-items: baseline;
  248. justify-content: center;
  249. .currency {
  250. font-size: 78rpx;
  251. color: #FF4444;
  252. font-weight: bold;
  253. margin-right: 4rpx;
  254. }
  255. .amount-value {
  256. font-size: 156rpx;
  257. color: #FF4444;
  258. font-weight: bold;
  259. }
  260. }
  261. }
  262. .receive-btn-wrapper {
  263. position: absolute;
  264. bottom: 15%;
  265. left: 52%;
  266. transform: translateX(-50%);
  267. width: 84%;
  268. .btn-image {
  269. width: 100%;
  270. height: auto;
  271. }
  272. .btn-text {
  273. position: absolute;
  274. top: 45%;
  275. left: 50%;
  276. transform: translate(-50%, -50%);
  277. font-size: 32rpx;
  278. color: #D73800;
  279. font-weight: 500;
  280. white-space: nowrap;
  281. }
  282. }
  283. }
  284. .info-card-section {
  285. position: relative;
  286. margin-bottom: 20rpx;
  287. .info-frame {
  288. width: 100%;
  289. display: block;
  290. }
  291. .info-content {
  292. position: absolute;
  293. top: 20%;
  294. left: 0;
  295. right: 0;
  296. bottom: 0;
  297. padding: 30rpx;
  298. box-sizing: border-box;
  299. .info-list {
  300. padding: 0 20rpx;
  301. .info-item {
  302. font-family: Fotor_HelloFont_GongYiTi;
  303. font-size: 40rpx;
  304. color: #000000;
  305. line-height: 64rpx;
  306. text-align: center;
  307. .spacer {
  308. flex: 1;
  309. margin: 0 16rpx;
  310. }
  311. .star {
  312. color: #FFB300;
  313. margin-left: 8rpx;
  314. }
  315. }
  316. }
  317. .rule-footer {
  318. display: flex;
  319. justify-content: center;
  320. align-items: center;
  321. margin-top: 30rpx;
  322. .rule-text {
  323. font-size: 26rpx;
  324. color: #666;
  325. text-decoration: underline;
  326. }
  327. .share-btn-wrapper {
  328. position: absolute;
  329. bottom: 20rpx;
  330. right: 20rpx;
  331. width: 120rpx;
  332. height: 120rpx;
  333. .share-btn-image {
  334. width: 100%;
  335. height: 100%;
  336. }
  337. .share-text {
  338. position: absolute;
  339. top: 50%;
  340. left: 50%;
  341. transform: translate(-50%, -50%);
  342. font-size: 22rpx;
  343. color: #fff;
  344. font-weight: bold;
  345. text-align: center;
  346. line-height: 1.3;
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. </style>