SVIP.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <template>
  2. <view class="level-container">
  3. <!-- 顶部会员信息区域 -->
  4. <view class="vip-info">
  5. <view class="flex-between vip-box">
  6. <view class="flex_2">
  7. <view class="flex-between">
  8. <view class="vip-text">
  9. <view class="vip-title">SVIP会员</view>
  10. <view class="vip-desc"
  11. >会员等级 {{ appStore?.$userInfo?.level || 0 }}</view
  12. >
  13. </view>
  14. <view class="point-info">
  15. <text class="point-text">{{
  16. appStore?.$userInfo?.integral || 0
  17. }}</text>
  18. <text class="point-desc">会员贝币</text>
  19. </view>
  20. </view>
  21. <view>
  22. <view class="growth-info">
  23. <text>成长值 {{ appStore?.$userInfo?.experience || 0 }}</text>
  24. <text class="growth-progress">
  25. {{ appStore?.$userInfo?.experience || 0 }}/{{
  26. appStore?.$userInfo?.experienceCount || 0
  27. }}
  28. </text>
  29. </view>
  30. <up-line-progress
  31. :percentage="percentExperience"
  32. activeColor="#d6c3a3"
  33. inactiveColor="#808080"
  34. height="6"
  35. :showText="false"
  36. ></up-line-progress>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. <!-- SVIP尊享权益 -->
  42. <view class="vip-benefit">
  43. <view class="task-title">SVIP尊享权益</view>
  44. <view class="task-item" v-for="(task, index) in taskList" :key="index">
  45. <image class="task-icon" :src="task.icon"></image>
  46. <view class="task-desc flex-center-between flex_1">
  47. <text class="task-name">{{ task.name }}</text>
  48. <text class="task-reward flex_1">{{ task.reward }}</text>
  49. </view>
  50. </view>
  51. </view>
  52. <!-- SVIP购买按钮(仅非SVIP用户显示) -->
  53. <view class="svip-price" v-if="!isSvip">
  54. <view class="flex-center" @click="handlePayOpen">
  55. <view class="svip-price-btn">
  56. <text class="svip-price-name">年费会员¥</text>
  57. <text class="svip-price-num">{{ totalPrice }}</text>
  58. </view>
  59. </view>
  60. </view>
  61. <WechatPayment ref="wechatPaymentRef" />
  62. </view>
  63. </template>
  64. <script setup>
  65. import { ref, computed } from "vue";
  66. import { useAppStore } from "@/stores/app";
  67. import { svipPrice, svipsaveAPI } from "@/api/svip";
  68. import { getUserInfo } from "@/api/user";
  69. // 引入封装的微信支付组件(替换原paymentCommon)
  70. import WechatPayment from "@/components/payment/WechatPayment.vue";
  71. // 引入后端接口(按需补充:如“创建会员订单”“标记SVIP身份”的接口)
  72. // import { createVipOrder, markUserSvip } from "@/api/vip";
  73. import { onLoad } from "@dcloudio/uni-app";
  74. // 状态管理
  75. const appStore = useAppStore();
  76. const wechatPaymentRef = ref(null); // 微信支付组件引用
  77. const totalPrice = ref(0); // 会员价格(单位:元,从接口获取)
  78. const orderId = ref(""); // 后端生成的会员订单ID(用于后续标记SVIP)
  79. // 计算是否为SVIP用户
  80. const isSvip = computed(() => {
  81. return appStore.isLogin && !!appStore?.$userInfo?.svip;
  82. });
  83. // 获取用户信息
  84. async function fetchUserInfo() {
  85. const { data } = await getUserInfo();
  86. appStore.UPDATE_USERINFO(data);
  87. }
  88. // 会员权益列表
  89. const taskList = ref([
  90. {
  91. name: "每日福利",
  92. reward: "200成长值",
  93. icon: "https://sb-admin.oss-cn-shenzhen.aliyuncs.com/shuibei-mini/svip/%E8%B7%AF%E5%BE%84733%403x.png",
  94. },
  95. {
  96. name: "回收特权",
  97. reward: "回收价+0.3/克",
  98. icon: "https://sb-admin.oss-cn-shenzhen.aliyuncs.com/shuibei-mini/svip/huishoutequan.png",
  99. },
  100. {
  101. name: "整点秒杀",
  102. reward: "抄底补贴,限时抢购",
  103. icon: "https://sb-admin.oss-cn-shenzhen.aliyuncs.com/shuibei-mini/svip/zhengdianmiaosha.png",
  104. },
  105. {
  106. name: "超级工具",
  107. reward: "自动回收,金价预警使用权限",
  108. icon: "https://sb-admin.oss-cn-shenzhen.aliyuncs.com/shuibei-mini/svip/chaojigongju.png",
  109. },
  110. {
  111. name: "邮费减半",
  112. reward: "邮费全部五折",
  113. icon: "https://sb-admin.oss-cn-shenzhen.aliyuncs.com/shuibei-mini/svip/youfeijianban.png",
  114. },
  115. {
  116. name: "专属服务群",
  117. reward: "专属福利,优先传达",
  118. icon: "https://sb-admin.oss-cn-shenzhen.aliyuncs.com/shuibei-mini/svip/zhuanshufuli.png",
  119. },
  120. ]);
  121. // 成长值进度百分比
  122. const percentExperience = computed(() => {
  123. if (!appStore.isLogin) return 0;
  124. const current = appStore.$userInfo.experience || 0;
  125. const target = appStore.$userInfo.experienceCount || 1; // 避免除以0
  126. const percent = Math.floor((current / target) * 100);
  127. return percent > 100 ? 100 : percent;
  128. });
  129. // 页面加载时获取会员价格
  130. onLoad(() => {
  131. getSvipPrice();
  132. });
  133. /**
  134. * 获取SVIP会员价格(单位:元)
  135. */
  136. const getSvipPrice = async () => {
  137. try {
  138. const res = await svipPrice();
  139. totalPrice.value = Number(res.data) || 99; // 默认99元,防止接口返回异常
  140. } catch (err) {
  141. totalPrice.value = 99; // 兜底价格
  142. }
  143. };
  144. /**
  145. * 点击“开通会员”触发:发起微信支付
  146. */
  147. const handlePayOpen = async () => {
  148. // 1. 前置校验:是否登录、是否已为SVIP
  149. if (!appStore.isLogin) {
  150. uni.showToast({ title: "未登录", icon: "none" });
  151. console.log("未登录");
  152. return;
  153. }
  154. if (isSvip.value) {
  155. uni.showToast({ title: "已经是svip会员", icon: "none" });
  156. console.log("已经是svip会员");
  157. return;
  158. }
  159. console.log("没有openId:", appStore?.$userInfo);
  160. // 2. 校验用户openId(微信支付必需)
  161. const userOpenId = appStore?.$userInfo?.openId; // 假设用户信息中存储了openId
  162. if (!userOpenId) {
  163. uni.showToast({ title: "用户openId获取失败", icon: "none" });
  164. console.log("没有openId:");
  165. return;
  166. }
  167. // 3. 调用微信支付组件的支付方法
  168. wechatPaymentRef.value.createUniPay({
  169. // 支付金额:元转分(微信支付要求单位为“分”)
  170. amount: Math.round(totalPrice.value * 100),
  171. description: `SVIP年费会员(¥${totalPrice.value})`, // 商品描述(显示在微信支付账单中)
  172. openId: userOpenId, // 微信支付必需:用户的微信openId
  173. orderPrefix: "SVIP", // 订单号前缀(与公共组件generateCustomId匹配)
  174. /**
  175. * 回调1:支付订单创建成功(用户尚未支付)
  176. * 作用:记录后端生成的会员订单ID,用于后续支付成功后标记SVIP
  177. */
  178. onUniPayCreate: async (payRes) => {
  179. console.log("会员支付订单创建成功:", payRes);
  180. try {
  181. } catch (err) {
  182. console.error("创建会员订单异常:", err);
  183. }
  184. },
  185. /**
  186. * 回调2:支付成功(核心逻辑)
  187. * 作用:通知后端标记用户为SVIP,刷新用户状态
  188. */
  189. onUniPaySuccess: async (payStatusRes) => {
  190. console.log("SVIP会员支付成功:", payStatusRes);
  191. try {
  192. const res = await svipsaveAPI({
  193. orderId: payStatusRes.out_trade_no,
  194. price: totalPrice.value,
  195. payType: "weixin",
  196. });
  197. appStore.USERINFO();
  198. } catch (err) {
  199. console.error("标记SVIP身份异常:", err);
  200. }
  201. },
  202. /**
  203. * 回调3:用户取消支付
  204. */
  205. onUniPayCancel: () => {
  206. console.log("用户取消SVIP支付");
  207. },
  208. /**
  209. * 回调4:支付失败(如网络异常、余额不足等)
  210. */
  211. onUniPayFail: (err) => {
  212. const errMsg = err.message || "支付失败,请重新尝试";
  213. console.error("SVIP会员支付失败:", err);
  214. },
  215. });
  216. };
  217. </script>
  218. <style scoped lang="scss">
  219. .level-container {
  220. color: black;
  221. position: relative;
  222. top: -130rpx;
  223. }
  224. /* 顶部会员信息 */
  225. .vip-info {
  226. color: #dfd7bc;
  227. margin-bottom: -25rpx;
  228. overflow: hidden;
  229. background-image: url("https://sb-admin.oss-cn-shenzhen.aliyuncs.com/shuibei-mini/svip/svip-bg.jpg");
  230. background-size: 100%;
  231. background-repeat: no-repeat;
  232. height: 450rpx;
  233. .vip-box {
  234. margin: 200rpx 90rpx 100rpx 265rpx;
  235. }
  236. }
  237. .vip-avatar {
  238. width: 151rpx;
  239. height: 161rpx;
  240. margin-right: 70rpx;
  241. margin-top: 50rpx;
  242. }
  243. .vip-title {
  244. font-size: 36rpx;
  245. font-weight: bold;
  246. margin-bottom: 8rpx;
  247. }
  248. .vip-desc {
  249. font-size: 24rpx;
  250. margin-bottom: 12rpx;
  251. }
  252. .growth-info {
  253. display: flex;
  254. justify-content: space-between;
  255. align-items: center;
  256. margin-top: 20rpx;
  257. margin-bottom: 5rpx;
  258. font-size: 20rpx;
  259. }
  260. .point-info {
  261. text-align: right;
  262. }
  263. .point-text {
  264. font-size: 40rpx;
  265. font-weight: bold;
  266. display: block;
  267. }
  268. .point-desc {
  269. font-size: 20rpx;
  270. opacity: 0.8;
  271. }
  272. /* VIP尊享权益 */
  273. .vip-benefit {
  274. padding: 30rpx;
  275. }
  276. .task-title {
  277. font-size: 32rpx;
  278. font-weight: bold;
  279. margin: 32rpx 0 16rpx 0;
  280. }
  281. .task-item {
  282. display: flex;
  283. justify-content: space-between;
  284. align-items: center;
  285. padding: 20rpx 30rpx;
  286. border: 1px solid #f2f2f2;
  287. border-radius: 8rpx;
  288. margin-bottom: 16rpx;
  289. background-color: #f4f3f1;
  290. .task-icon {
  291. width: 70rpx;
  292. height: 70rpx;
  293. margin-right: 30rpx;
  294. }
  295. }
  296. .task-desc {
  297. .task-name {
  298. font-size: 28rpx;
  299. font-weight: bold;
  300. margin-right: 30rpx;
  301. width: 160rpx;
  302. text-align: center;
  303. }
  304. .task-reward {
  305. font-size: 24rpx;
  306. color: #5d5c5a;
  307. }
  308. }
  309. /* SVIP购买按钮 */
  310. .svip-price {
  311. .svip-price-title {
  312. font-size: 32rpx;
  313. font-weight: bold;
  314. text-align: center;
  315. }
  316. .flex-center {
  317. display: flex;
  318. justify-content: center;
  319. align-items: center;
  320. }
  321. .svip-price-btn {
  322. margin-top: 20rpx;
  323. display: inline-block;
  324. border: 1px solid #fdfdf9;
  325. border-radius: 20rpx;
  326. padding: 40rpx 100rpx;
  327. background-color: #fee1a9;
  328. color: #080604;
  329. font-size: 24rpx;
  330. cursor: pointer;
  331. .svip-price-num {
  332. font-size: 40rpx;
  333. font-weight: bold;
  334. margin-left: 10rpx;
  335. }
  336. }
  337. }
  338. /* 工具类(如果项目中没有全局flex工具类,需保留) */
  339. .flex-between {
  340. display: flex;
  341. justify-content: space-between;
  342. align-items: center;
  343. }
  344. .flex-center-between {
  345. display: flex;
  346. justify-content: space-between;
  347. align-items: center;
  348. }
  349. .flex_1 {
  350. flex: 1;
  351. }
  352. .flex_2 {
  353. flex: 2;
  354. }
  355. </style>