jifen.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <div>
  3. <div class="nav-bar">
  4. <div class="back-btn" @click="$router.back()">
  5. <i class="arrow-left"></i>
  6. </div>
  7. <div class="title">积分商城</div>
  8. </div>
  9. <div class="jifen-container">
  10. <!-- 头部积分显示 -->
  11. <div class="header-section">
  12. <div class="points-display">
  13. <h2>兑换国家物资</h2>
  14. <div class="current-points">{{ userPoints }}</div>
  15. <div class="points-label">我的积分</div>
  16. </div>
  17. </div>
  18. <!-- 商品网格 -->
  19. <div class="products-grid">
  20. <div
  21. v-for="product in products"
  22. :key="product.id"
  23. class="product-card"
  24. :class="{ 'large-card': product.isLarge }"
  25. >
  26. <div class="product-image">
  27. <img :src="product.image" :alt="product.name" />
  28. </div>
  29. <div class="product-info">
  30. <div class="product-info-content">
  31. <div class="product-points">{{ product.probability }}积分</div>
  32. <div class="product-name">{{ product.name }}</div>
  33. </div>
  34. <button
  35. class="exchange-btn"
  36. @click="handleExchange(product)"
  37. >
  38. 立即兑换
  39. </button>
  40. </div>
  41. </div>
  42. </div>
  43. <!-- 兑换确认弹窗 -->
  44. <div v-if="showConfirmDialog" class="confirm-overlay">
  45. <div class="confirm-dialog">
  46. <div class="confirm-content">
  47. <h3>确认兑换</h3>
  48. <div class="confirm-product">
  49. <img :src="selectedProduct.image" :alt="selectedProduct.name" />
  50. <div class="confirm-info">
  51. <div class="confirm-name">{{ selectedProduct.name }}</div>
  52. <div class="confirm-points">需要 {{ selectedProduct.probability }} 积分</div>
  53. </div>
  54. </div>
  55. <p>确认要兑换此商品吗?</p>
  56. <div class="confirm-buttons">
  57. <button class="btn-cancel" @click="cancelExchange">取消</button>
  58. <button class="btn-confirm" @click="confirmExchange">确认兑换</button>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. <!-- Toast 提示 -->
  64. <Toast ref="toast" />
  65. </div>
  66. </div>
  67. </template>
  68. <script>
  69. import Toast from '@/components/Toast.vue';
  70. import { getLotteryPrize, exchangePrize } from '@/api/profile';
  71. export default {
  72. name: 'JifenPage',
  73. components: {
  74. Toast
  75. },
  76. data() {
  77. return {
  78. showConfirmDialog: false,
  79. selectedProduct: null,
  80. products: []
  81. }
  82. },
  83. mounted() {
  84. this.loadProducts();
  85. },
  86. methods: {
  87. // 加载商品列表
  88. async loadProducts() {
  89. try {
  90. const res = await getLotteryPrize();
  91. this.products = res.data;
  92. // 当前使用模拟数据,实际开发时替换为API调用
  93. console.log('商品列表加载完成');
  94. } catch (error) {
  95. console.error('加载商品列表失败:', error);
  96. this.$refs.toast.show('加载商品失败', 'error');
  97. }
  98. },
  99. // 处理兑换点击
  100. handleExchange(product) {
  101. this.selectedProduct = product;
  102. this.showConfirmDialog = true;
  103. },
  104. // 取消兑换
  105. cancelExchange() {
  106. this.showConfirmDialog = false;
  107. this.selectedProduct = null;
  108. },
  109. // 确认兑换
  110. async confirmExchange() {
  111. try {
  112. const res = await exchangePrize({
  113. prize_id: this.selectedProduct.id,
  114. });
  115. console.log(res);
  116. if(res.code == 1){
  117. this.$refs.toast.show('兑换成功!', 'success');
  118. this.userPoints -= this.selectedProduct.points;
  119. this.showConfirmDialog = false;
  120. this.selectedProduct = null;
  121. }else{
  122. this.$refs.toast.show('兑换失败,请重试', 'error');
  123. }
  124. this.showConfirmDialog = false;
  125. this.selectedProduct = null;
  126. } catch (error) {
  127. console.error('兑换失败:', error);
  128. this.$refs.toast.show('兑换失败,请重试', 'error');
  129. }
  130. }
  131. }
  132. }
  133. </script>
  134. <style scoped>
  135. .jifen-container {
  136. min-height: 100vh;
  137. background: linear-gradient(135deg, #c94545 0%, #b43a39 100%);
  138. padding: 20px;
  139. padding-bottom: 100px;
  140. }
  141. /* 头部积分显示 */
  142. .header-section {
  143. text-align: center;
  144. margin-bottom: 30px;
  145. }
  146. .points-display h2 {
  147. color: white;
  148. font-size: 20px;
  149. font-weight: 600;
  150. margin-bottom: 10px;
  151. }
  152. .current-points {
  153. color: white;
  154. font-size: 48px;
  155. font-weight: bold;
  156. margin-bottom: 5px;
  157. text-shadow: 0 2px 4px rgba(0,0,0,0.3);
  158. }
  159. .points-label {
  160. color: white;
  161. font-size: 16px;
  162. opacity: 0.9;
  163. }
  164. /* 商品网格 */
  165. .products-grid {
  166. display: grid;
  167. grid-template-columns: 1fr 1fr;
  168. gap: 15px;
  169. margin-bottom: 20px;
  170. }
  171. .product-card {
  172. display: flex;
  173. flex-direction: column;
  174. justify-content: space-between;
  175. background: white;
  176. border-radius: 12px;
  177. padding: 15px;
  178. box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  179. transition: transform 0.2s ease;
  180. }
  181. .product-card:active {
  182. transform: scale(0.98);
  183. }
  184. .product-card.large-card {
  185. grid-column: span 2;
  186. }
  187. .product-image {
  188. width: 100%;
  189. height: 140px;
  190. background: #f5f5f5;
  191. border-radius: 8px;
  192. display: flex;
  193. align-items: center;
  194. justify-content: center;
  195. margin-bottom: 12px;
  196. overflow: hidden;
  197. }
  198. .large-card .product-image {
  199. height: 120px;
  200. }
  201. .product-image img {
  202. width: 80%;
  203. height: 80%;
  204. object-fit: contain;
  205. }
  206. .product-info {
  207. text-align: center;
  208. }
  209. .product-info-content {
  210. display: flex;
  211. justify-content: space-between;
  212. align-items: center;
  213. }
  214. .product-points {
  215. font-size: 14px;
  216. color: #000;
  217. font-weight: 500;
  218. }
  219. .product-name {
  220. font-size: 14px;
  221. color: #000;
  222. font-weight: 500;
  223. }
  224. .exchange-btn {
  225. margin-top: 10px;
  226. width: 100%;
  227. background: #c94545;
  228. color: white;
  229. border: none;
  230. border-radius: 20px;
  231. padding: 8px 0;
  232. font-size: 14px;
  233. font-weight: 500;
  234. cursor: pointer;
  235. transition: all 0.3s ease;
  236. }
  237. .exchange-btn:hover {
  238. background: #b43a39;
  239. }
  240. .exchange-btn:disabled {
  241. background: #ccc;
  242. cursor: not-allowed;
  243. }
  244. .exchange-btn:active {
  245. transform: scale(0.95);
  246. }
  247. /* 确认弹窗 */
  248. .confirm-overlay {
  249. position: fixed;
  250. top: 0;
  251. left: 0;
  252. width: 100%;
  253. height: 100%;
  254. background: rgba(0,0,0,0.6);
  255. display: flex;
  256. align-items: center;
  257. justify-content: center;
  258. z-index: 1000;
  259. }
  260. .confirm-dialog {
  261. background: white;
  262. border-radius: 12px;
  263. width: 90%;
  264. max-width: 400px;
  265. overflow: hidden;
  266. }
  267. .confirm-content {
  268. padding: 25px 20px;
  269. text-align: center;
  270. }
  271. .confirm-content h3 {
  272. margin: 0 0 20px 0;
  273. font-size: 18px;
  274. color: #333;
  275. }
  276. .confirm-product {
  277. display: flex;
  278. align-items: center;
  279. background: #f8f8f8;
  280. border-radius: 8px;
  281. padding: 15px;
  282. margin-bottom: 20px;
  283. }
  284. .confirm-product img {
  285. width: 60px;
  286. height: 60px;
  287. object-fit: contain;
  288. margin-right: 15px;
  289. background: white;
  290. border-radius: 6px;
  291. }
  292. .confirm-info {
  293. flex: 1;
  294. text-align: left;
  295. }
  296. .confirm-name {
  297. font-size: 16px;
  298. font-weight: 500;
  299. color: #333;
  300. margin-bottom: 5px;
  301. }
  302. .confirm-points {
  303. font-size: 14px;
  304. color: #c94545;
  305. font-weight: 500;
  306. }
  307. .confirm-content p {
  308. margin: 0 0 25px 0;
  309. color: #666;
  310. font-size: 15px;
  311. }
  312. .confirm-buttons {
  313. display: flex;
  314. gap: 12px;
  315. }
  316. .btn-cancel,
  317. .btn-confirm {
  318. flex: 1;
  319. padding: 12px 0;
  320. border: none;
  321. border-radius: 8px;
  322. font-size: 16px;
  323. font-weight: 500;
  324. cursor: pointer;
  325. transition: all 0.3s ease;
  326. }
  327. .btn-cancel {
  328. background: #f5f5f5;
  329. color: #666;
  330. }
  331. .btn-cancel:hover {
  332. background: #e8e8e8;
  333. }
  334. .btn-confirm {
  335. background: #c94545;
  336. color: white;
  337. }
  338. .btn-confirm:hover {
  339. background: #b43a39;
  340. }
  341. .btn-cancel:active,
  342. .btn-confirm:active {
  343. transform: scale(0.98);
  344. }
  345. /* 响应式优化 */
  346. @media screen and (max-width: 375px) {
  347. .jifen-container {
  348. padding: 15px;
  349. }
  350. .products-grid {
  351. gap: 12px;
  352. }
  353. .product-card {
  354. padding: 12px;
  355. }
  356. .current-points {
  357. font-size: 42px;
  358. }
  359. .points-display h2 {
  360. font-size: 18px;
  361. }
  362. }
  363. @media screen and (max-width: 320px) {
  364. .current-points {
  365. font-size: 36px;
  366. }
  367. .product-image {
  368. height: 80px;
  369. }
  370. .large-card .product-image {
  371. height: 100px;
  372. }
  373. }
  374. .nav-bar {
  375. height: 44px;
  376. background-color: #fff;
  377. color: #000;
  378. display: flex;
  379. align-items: center;
  380. position: relative;
  381. padding: 0 15px;
  382. font-weight: bold;
  383. }
  384. .back-btn {
  385. width: 24px;
  386. height: 44px;
  387. display: flex;
  388. align-items: center;
  389. cursor: pointer;
  390. }
  391. .arrow-left {
  392. width: 12px;
  393. height: 12px;
  394. border-left: 2px solid #000;
  395. border-bottom: 2px solid #000;
  396. transform: rotate(45deg);
  397. }
  398. .title {
  399. position: absolute;
  400. left: 50%;
  401. transform: translateX(-50%);
  402. font-size: 16px;
  403. }
  404. </style>