84 lines
2.1 KiB
JavaScript
84 lines
2.1 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import * as membershipApi from '../api/membership.js'
|
|
import { pay } from '../utils/payment.js'
|
|
|
|
export const useMembershipStore = defineStore('membership', () => {
|
|
// 会员信息
|
|
const membershipInfo = ref(null)
|
|
// 会员商品列表
|
|
const products = ref([])
|
|
// 订阅状态
|
|
const subscriptionStatus = ref(null)
|
|
|
|
/**
|
|
* 获取会员信息
|
|
*/
|
|
async function fetchMembershipInfo() {
|
|
const res = await membershipApi.getMembershipInfo()
|
|
membershipInfo.value = res.data
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* 获取会员商品列表
|
|
*/
|
|
async function fetchProducts() {
|
|
const res = await membershipApi.getProducts()
|
|
products.value = res.data || []
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* 获取订阅状态
|
|
*/
|
|
async function fetchSubscriptionStatus() {
|
|
const res = await membershipApi.getSubscriptionStatus()
|
|
subscriptionStatus.value = res.data
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* 购买单月会员
|
|
* @param {string} productId - 商品ID
|
|
* @param {number} price - 价格
|
|
*/
|
|
async function purchase(productId, price) {
|
|
// 拉起支付
|
|
const payResult = await pay({ productId, price, type: 'purchase' })
|
|
// 支付成功后通知后端
|
|
const res = await membershipApi.purchase(productId, payResult.receipt)
|
|
// 刷新会员信息
|
|
await fetchMembershipInfo()
|
|
await fetchSubscriptionStatus()
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* 订阅会员
|
|
* @param {string} productId - 商品ID
|
|
* @param {number} price - 价格
|
|
*/
|
|
async function subscribe(productId, price) {
|
|
// 拉起订阅支付
|
|
const payResult = await pay({ productId, price, type: 'subscribe' })
|
|
// 订阅成功后通知后端
|
|
const res = await membershipApi.subscribe(productId, payResult.receipt)
|
|
// 刷新会员信息和订阅状态
|
|
await fetchMembershipInfo()
|
|
await fetchSubscriptionStatus()
|
|
return res
|
|
}
|
|
|
|
return {
|
|
membershipInfo,
|
|
products,
|
|
subscriptionStatus,
|
|
fetchMembershipInfo,
|
|
fetchProducts,
|
|
fetchSubscriptionStatus,
|
|
purchase,
|
|
subscribe
|
|
}
|
|
})
|