| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import { defineStore } from 'pinia'
- import { ref } from 'vue'
- import { vipApi } from '@/api/vip'
-
- export const useVipStore = defineStore('vip', () => {
- // VIP状态
- const isVIP = ref(false)
- const vipType = ref(null) // 0-非会员 1-初级 2-中级 3-高级 4-超级
- const expireTime = ref('')
- const privileges = ref([]) // 会员特权列表
-
- // 从Java后端加载会员状态
- const loadVipStatus = async (userId) => {
- try {
- const res = await vipApi.getStatus(userId)
- isVIP.value = res.data.isEffective
- vipType.value = res.data.type
- expireTime.value = res.data.expireTime
- privileges.value = res.data.privileges || [
- '免广告',
- '专属内容',
- '双倍阅读金币'
- ]
- return true
- } catch (err) {
- console.error('VIP状态加载失败', err)
- uni.showToast({ title: '会员信息获取失败', icon: 'none' })
- return false
- }
- }
-
- // 购买VIP(对接Java支付接口)
- const purchaseVip = async (plan) => {
- try {
- uni.showLoading({ title: '支付中...', mask: true })
-
- // 调用Java支付接口
- const payRes = await vipApi.purchase({
- userId: uni.getStorageSync('userId'),
- planId: plan.id,
- amount: plan.price
- })
-
- // 处理支付结果
- if (payRes.data.success) {
- await loadVipStatus() // 刷新状态
- uni.showToast({ title: '开通成功!', icon: 'success' })
- return true
- } else {
- throw new Error(payRes.data.message || '支付失败')
- }
- } catch (err) {
- uni.showToast({ title: err.message, icon: 'none' })
- return false
- } finally {
- uni.hideLoading()
- }
- }
-
- // 会员特权检测(例如广告跳过)
- const hasPrivilege = (privilegeKey) => {
- return isVIP.value && privileges.value.includes(privilegeKey)
- }
-
- return {
- isVIP,
- vipType,
- expireTime,
- privileges,
- loadVipStatus,
- purchaseVip,
- hasPrivilege
- }
- })
|