feat: 支持邀请好友功能
This commit is contained in:
@@ -1,26 +1,40 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import type { InviteActivitySummary } from '@mp-pilates/shared'
|
||||
import { get } from '../utils/request'
|
||||
import { get, post } from '../utils/request'
|
||||
|
||||
export const useInviteStore = defineStore('invite', () => {
|
||||
const activity = ref<InviteActivitySummary | null>(null)
|
||||
const loading = ref(false)
|
||||
|
||||
async function fetchActivity() {
|
||||
loading.value = true
|
||||
const code = ref('')
|
||||
const eligible = ref(false)
|
||||
const pendingCode = ref('')
|
||||
let revision = 0
|
||||
let confirming = false
|
||||
async function refresh() {
|
||||
if (confirming) return
|
||||
const requestRevision = ++revision
|
||||
const result = await get<{ inviteCode: string; discountEligible: boolean }>('/invite/code')
|
||||
if (requestRevision !== revision) return
|
||||
code.value = result.inviteCode
|
||||
eligible.value = result.discountEligible
|
||||
}
|
||||
async function confirm(value: string) {
|
||||
const requestRevision = ++revision
|
||||
confirming = true
|
||||
try {
|
||||
activity.value = await get<InviteActivitySummary>('/invite/activity')
|
||||
return activity.value
|
||||
const result = await post<{ inviteCode: string; discountEligible: boolean }>('/invite/confirm', { code: value })
|
||||
if (requestRevision !== revision) throw new Error('登录状态已变化,请重试')
|
||||
code.value = result.inviteCode
|
||||
eligible.value = result.discountEligible
|
||||
pendingCode.value = ''
|
||||
} finally {
|
||||
loading.value = false
|
||||
confirming = false
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
activity,
|
||||
loading,
|
||||
fetchActivity,
|
||||
function reset() {
|
||||
revision++
|
||||
code.value = ''
|
||||
eligible.value = false
|
||||
pendingCode.value = ''
|
||||
}
|
||||
function price(value: number) { return eligible.value ? Math.round(value * 95 / 100) : value }
|
||||
return { code, eligible, pendingCode, refresh, confirm, reset, price }
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user