index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view class="shop">
  3. <view class="fixtitle">当前积分:{{ userInfo.source || 0 }}</view>
  4. <view v-if="modules && modules.length" class="base-list">
  5. <list-item
  6. v-for="item in modules"
  7. :key="item.id"
  8. :item="item"
  9. :btn="true"
  10. class="base-list-item"
  11. @click="handleDetail(item)"
  12. />
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import login from "@/lib/utils/login";
  18. import { getUserInfo } from "@/lib/api/user";
  19. import { getScoreList, exchangeScore } from "@/lib/api/shop";
  20. import listItem from "@/components/list-item/list-item.vue";
  21. export default {
  22. name: "PageHome",
  23. components: {
  24. listItem,
  25. },
  26. data() {
  27. return {
  28. userInfo: {},
  29. modules: [],
  30. tabStyle: "",
  31. showTask: false,
  32. };
  33. },
  34. onLoad() {
  35. const uinfoStr = uni.getStorageSync("userinfo");
  36. if (uinfoStr) {
  37. this.userInfo = JSON.parse(uinfoStr);
  38. }
  39. uni.showLoading({
  40. title: "加载中",
  41. });
  42. this.loadModules().then(() => {
  43. uni.hideLoading();
  44. });
  45. },
  46. onShow() {
  47. const uinfoStr = uni.getStorageSync("userinfo");
  48. if (uinfoStr) {
  49. this.userInfo = JSON.parse(uinfoStr);
  50. this.getUserInfo();
  51. } else {
  52. this.userInfo = {};
  53. }
  54. },
  55. onShareAppMessage() {
  56. return {
  57. title: "云果国潮会员中心",
  58. imageUrl:
  59. "https://yggc.oss-cn-beijing.aliyuncs.com/images/81d939394aea637ac0f6a3026d1a38e6.jpg",
  60. path: "/pages/index/index",
  61. };
  62. },
  63. onShareTimeline() {
  64. return {
  65. title: "云果国潮会员中心",
  66. imageUrl:
  67. "https://yggc.oss-cn-beijing.aliyuncs.com/images/81d939394aea637ac0f6a3026d1a38e6.jpg",
  68. path: "/pages/index/index",
  69. };
  70. },
  71. async onPullDownRefresh() {
  72. await this.loadModules().catch((e) => console.log(e));
  73. uni.stopPullDownRefresh();
  74. },
  75. // mounted() {},
  76. methods: {
  77. async getUserInfo() {
  78. const res = await getUserInfo();
  79. this.userInfo = { ...this.userInfo, ...res.data };
  80. uni.setStorageSync("userinfo", JSON.stringify(this.userInfo));
  81. },
  82. async loadModules() {
  83. try {
  84. const data = await getScoreList();
  85. if (data.errno == 10000) {
  86. this.modules = data.data;
  87. // this.endTime = data.diff_time;
  88. // this.runCd();
  89. } else {
  90. uni.showToast({
  91. icon: "none",
  92. title: data.errmsg,
  93. duration: 3000,
  94. });
  95. }
  96. } catch (e) {
  97. uni.showToast({
  98. icon: "none",
  99. title: "出错啦,请稍后重试",
  100. duration: 3000,
  101. });
  102. }
  103. },
  104. login() {
  105. return new Promise((resolve, reject) => {
  106. login.wxLogin((userInfo) => {
  107. if (userInfo.session) {
  108. this.userInfo = userInfo;
  109. this.getUserInfo().then(resolve);
  110. } else reject();
  111. });
  112. });
  113. },
  114. async handleDetail(item) {
  115. if (!this.userInfo || !this.userInfo.session) await this.login();
  116. if (item.score > Number(this.userInfo.score)) {
  117. uni.showToast({
  118. icon: "none",
  119. text: "积分不足",
  120. });
  121. return;
  122. }
  123. await new Promise((resolve, reject) => {
  124. uni.showModal({
  125. title: "提示",
  126. content: "是否兑换当前商品?",
  127. success: function (res) {
  128. if (res.confirm) {
  129. resolve();
  130. } else if (res.cancel) {
  131. reject();
  132. }
  133. },
  134. });
  135. });
  136. const res = await exchangeScore({
  137. sku_id: item.id,
  138. });
  139. if (res.errno === 10000) {
  140. uni.showToast({
  141. icon: "none",
  142. text: "兑换成功",
  143. });
  144. }
  145. },
  146. },
  147. };
  148. </script>
  149. <style lang="scss">
  150. @import "./index.scss";
  151. </style>