继续完善购物逻辑闭环,consumer模块完成度75%

This commit is contained in:
2026-01-26 17:15:51 +08:00
parent be90f1213b
commit f2f208f258
21 changed files with 2516 additions and 217 deletions

View File

@@ -1,5 +1,5 @@
import supa, { supaReady } from '@/components/supadb/aksupainstance.uts'
import type { UserProfile } from '@/pages/user/types.uts'
import { supabase, ensureSupabaseReady } from '@/components/supadb/aksupainstance.uts'
import type { UserProfile } from '@/types/mall-types.uts'
/**
* 确保用户资料存在,如果不存在则创建基础资料
@@ -8,7 +8,7 @@ import type { UserProfile } from '@/pages/user/types.uts'
*/
export async function ensureUserProfile(sessionUser: UTSJSONObject): Promise<UserProfile | null> {
try {
await supaReady
await ensureSupabaseReady()
// 从 sessionUser 中获取用户ID和邮箱
const userId = sessionUser.getString('id')
@@ -20,7 +20,7 @@ export async function ensureUserProfile(sessionUser: UTSJSONObject): Promise<Use
}
// 检查用户是否已存在
const checkRes = await supa.from('ak_users')
const checkRes = await supabase.from('ak_users')
.select('*', {})
.eq('id', userId)
.single()
@@ -30,7 +30,7 @@ export async function ensureUserProfile(sessionUser: UTSJSONObject): Promise<Use
// 用户已存在,返回现有资料
const existingUser = checkRes.data as UTSJSONObject
return {
id: existingUser.getString('id'),
id: existingUser.getString('id') ?? '',
username: existingUser.getString('username') ?? '',
email: existingUser.getString('email') ?? email,
gender: existingUser.getString('gender'),
@@ -40,10 +40,9 @@ export async function ensureUserProfile(sessionUser: UTSJSONObject): Promise<Use
bio: existingUser.getString('bio'),
avatar_url: existingUser.getString('avatar_url'),
preferred_language: existingUser.getString('preferred_language'),
role: existingUser.getString('role'),
school_id: existingUser.getString('school_id'),
grade_id: existingUser.getString('grade_id'),
class_id: existingUser.getString('class_id')
role: existingUser.getString('role') ?? 'consumer',
created_at: existingUser.getString('created_at'),
updated_at: existingUser.getString('updated_at')
} as UserProfile
}
@@ -53,7 +52,7 @@ export async function ensureUserProfile(sessionUser: UTSJSONObject): Promise<Use
newUserData.set('email', email)
newUserData.set('username', email.split('@')[0] ?? 'user') // 默认用户名为邮箱前缀
const insertRes = await supa.from('ak_users')
const insertRes = await supabase.from('ak_users')
.insert(newUserData)
.select('*', {})
.single()
@@ -62,7 +61,7 @@ export async function ensureUserProfile(sessionUser: UTSJSONObject): Promise<Use
if (insertRes.status >= 200 && insertRes.status < 300 && insertRes.data != null) {
const newUser = insertRes.data as UTSJSONObject
return {
id: newUser.getString('id'),
id: newUser.getString('id') ?? '',
username: newUser.getString('username') ?? '',
email: newUser.getString('email') ?? email,
gender: newUser.getString('gender'),
@@ -72,10 +71,9 @@ export async function ensureUserProfile(sessionUser: UTSJSONObject): Promise<Use
bio: newUser.getString('bio'),
avatar_url: newUser.getString('avatar_url'),
preferred_language: newUser.getString('preferred_language'),
role: newUser.getString('role'),
school_id: newUser.getString('school_id'),
grade_id: newUser.getString('grade_id'),
class_id: newUser.getString('class_id')
role: newUser.getString('role') ?? 'consumer',
created_at: newUser.getString('created_at'),
updated_at: newUser.getString('updated_at')
} as UserProfile
} else {
console.error('创建用户资料失败:', insertRes.status)

View File

@@ -1,4 +1,4 @@
import supa, { supaReady } from '@/components/supadb/aksupainstance.uts'
import { supabase as supa, ensureSupabaseReady } from '@/components/supadb/aksupainstance.uts'
import type { UserProfile, UserStats } from '@/pages/user/types.uts'
import type { DeviceInfo } from '@/pages/sense/types.uts'
import { SenseDataService, type DeviceParams } from '@/pages/sense/senseDataService.uts'
@@ -50,7 +50,7 @@ export const setUserProfile = (profile : UserProfile) => {
// 获取当前用户信息(含补全 profile
export async function getCurrentUser() : Promise<UserProfile | null> {
try {
await supaReady
await ensureSupabaseReady()
} catch (_) {}
const sessionInfo = supa.getSession()