完成consumer端同步
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<template>
|
||||
<template>
|
||||
<view class="subscribe-checkout">
|
||||
<view class="header">
|
||||
<text class="title">确认订阅</text>
|
||||
@@ -47,21 +47,16 @@
|
||||
import { ref } from 'vue'
|
||||
import { onLoad } from '@dcloudio/uni-app'
|
||||
import supaClient from '@/components/supadb/aksupainstance.uts'
|
||||
import { PAYMENT_METHOD } from '@/types/mall-types.uts'
|
||||
import { goToLogin } from '@/utils/utils.uts'
|
||||
|
||||
const planId = ref<string>('')
|
||||
const loading = ref<boolean>(true)
|
||||
const plan = ref<UTSJSONObject | null>(null)
|
||||
const payMethod = ref<number>(PAYMENT_METHOD.WECHAT)
|
||||
const payMethod = ref<number>(1)
|
||||
const trialDays = ref<number>(0)
|
||||
const submitting = ref<boolean>(false)
|
||||
|
||||
onLoad(async (opts: OnLoadOptions) => {
|
||||
planId.value = (opts['planId'] ?? '') as string
|
||||
await loadPlan()
|
||||
})
|
||||
|
||||
const loadPlan = async () => {
|
||||
async function loadPlan(): Promise<void> {
|
||||
try {
|
||||
loading.value = true
|
||||
const res = await supaClient
|
||||
@@ -76,7 +71,7 @@ const loadPlan = async () => {
|
||||
} else {
|
||||
plan.value = res.data as UTSJSONObject
|
||||
}
|
||||
trialDays.value = (plan.value?.['trial_days'] ?? 0) as number
|
||||
trialDays.value = plan.value != null ? (plan.value.getNumber('trial_days') ?? 0) : 0
|
||||
} else {
|
||||
plan.value = null
|
||||
}
|
||||
@@ -87,6 +82,13 @@ const loadPlan = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
onLoad((opts) => {
|
||||
if (opts == null) return
|
||||
const optObj = opts as UTSJSONObject
|
||||
planId.value = optObj.getString('planId') ?? ''
|
||||
loadPlan()
|
||||
})
|
||||
|
||||
const selPay = (v: number) => { payMethod.value = v }
|
||||
|
||||
// 获取当前用户ID(按现有store实现替换)
|
||||
@@ -103,7 +105,7 @@ const confirmSubscribe = async () => {
|
||||
if (plan.value == null) return
|
||||
const userId = getCurrentUserId()
|
||||
if (userId.length === 0) {
|
||||
uni.showToast({ title: '请先登录', icon: 'none' })
|
||||
goToLogin(`/pages/mall/consumer/subscription/subscribe-checkout?planId=${planId.value}`)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -114,30 +116,33 @@ const confirmSubscribe = async () => {
|
||||
const start = now.toISOString()
|
||||
// 简单计算下个扣费日
|
||||
let nextBilling: string | null = null
|
||||
if ((plan.value?.['billing_period'] ?? 'monthly') === 'yearly') {
|
||||
const billingPeriod = plan.value.getString('billing_period') ?? 'monthly'
|
||||
if (billingPeriod === 'yearly') {
|
||||
nextBilling = new Date(now.getFullYear() + 1, now.getMonth(), now.getDate()).toISOString()
|
||||
} else {
|
||||
nextBilling = new Date(now.getFullYear(), now.getMonth() + 1, now.getDate()).toISOString()
|
||||
}
|
||||
const body = {
|
||||
user_id: userId,
|
||||
plan_id: plan.value['id'],
|
||||
status: 'active',
|
||||
start_date: start,
|
||||
end_date: null,
|
||||
next_billing_date: nextBilling,
|
||||
auto_renew: true,
|
||||
metadata: { pay_method: payMethod.value }
|
||||
}
|
||||
|
||||
const metadata = new UTSJSONObject()
|
||||
metadata.set('pay_method', payMethod.value)
|
||||
const body = new UTSJSONObject()
|
||||
body.set('user_id', userId)
|
||||
body.set('plan_id', plan.value.getString('id') ?? '')
|
||||
body.set('status', 'active')
|
||||
body.set('start_date', start)
|
||||
body.set('end_date', null)
|
||||
body.set('next_billing_date', nextBilling)
|
||||
body.set('auto_renew', true)
|
||||
body.set('metadata', metadata)
|
||||
|
||||
const ins = await supaClient
|
||||
.from('ml_user_subscriptions')
|
||||
.insert(body)
|
||||
.single?.()
|
||||
.execute()
|
||||
if (ins != null && ins.error == null) {
|
||||
uni.showToast({ title: '订阅成功', icon: 'success' })
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({ url: '/pages/main/profile' })
|
||||
uni.switchTab({ url: '/pages/main/profile' })
|
||||
}, 600)
|
||||
} else {
|
||||
uni.showToast({ title: ins?.error?.message ?? '订阅失败', icon: 'none' })
|
||||
|
||||
Reference in New Issue
Block a user