Initial commit of akmon project

This commit is contained in:
2026-01-20 08:04:15 +08:00
commit 77a2bab985
1309 changed files with 343305 additions and 0 deletions

174
utils/sapi.uts Normal file
View File

@@ -0,0 +1,174 @@
import supa from '@/components/supadb/aksupainstance.uts'
import type { UserProfile } from '@/pages/user/types.uts'
import { AkReqResponse, AkReqUploadOptions, AkReq } from '@/uni_modules/ak-req/index.uts'
/**
* 创建基础用户资料
* 当用户首次登录但在 ak_users 表中没有资料时调用
* @param supaUser - Supabase 认证用户对象
* @returns Promise<UserProfile | null> - 创建的用户资料或null如果失败
*/
export async function createBasicUserProfile(supaUser: UTSJSONObject): Promise<UserProfile | null> { try {
// 从 supaUser 中提取基础信息
const userId = supaUser.getString('id')
const email = supaUser.getString('email')
if (userId == null || email == null) {
console.error('创建用户资料失败缺少用户ID或邮箱')
return null
}
// 从邮箱中提取用户名(@符号前的部分)
const emailParts = email.split('@')
const username = emailParts.length > 0 ? emailParts[0] : 'user'
// 构建基础用户资料
const basicProfile: UTSJSONObject = {
id: userId,
username: username,
email: email,
gender: null,
birthday: null,
height_cm: null,
weight_kg: null,
bio: null,
avatar_url: null,
preferred_language: null, // 默认语言
role: 'student', // 默认角色
created_at: new Date().toISOString(),
updated_at: new Date().toISOString() } as UTSJSONObject
console.log('正在创建用户资料:', basicProfile)
console.log('用户ID:', userId)
console.log('用户邮箱:', email)
console.log('用户名:', username)
// 插入到 ak_users 表
console.log('准备插入数据到 ak_users 表')
console.log('表名: ak_users')
console.log('数据:', JSON.stringify(basicProfile.toMap()))
const result = await supa.from('ak_users').insert(basicProfile).execute()
console.log('插入用户资料结果:', result)
if ((result.status === 201 || result.status === 200) && result.data != null) {
// 插入成功,返回创建的用户资料
let insertedUser: UTSJSONObject | null = null
const data = result.data
if (Array.isArray(data)) {
if (data.length > 0) {
insertedUser = data[0] as UTSJSONObject
}
} else if (data != null) {
insertedUser = data as UTSJSONObject
}
if (insertedUser != null) {
const userProfile: UserProfile = {
id: insertedUser.getString('id') ?? '',
username: insertedUser.getString('username') ?? '',
email: insertedUser.getString('email') ?? '',
gender: insertedUser.getString('gender'),
birthday: insertedUser.getString('birthday'),
height_cm: insertedUser.getNumber('height_cm'),
weight_kg: insertedUser.getNumber('weight_kg'),
bio: insertedUser.getString('bio'),
avatar_url: insertedUser.getString('avatar_url'),
preferred_language: insertedUser.getString('preferred_language'),
role: insertedUser.getString('role')
}
console.log('用户资料创建成功:', userProfile)
return userProfile
} else {
// 如果 insert 返回的数据为空,我们使用原始数据构建 UserProfile
console.log('插入成功但返回数据为空,使用原始数据构建用户资料')
const userProfile: UserProfile = {
id: userId,
username: username,
email: email,
gender: null,
birthday: null,
height_cm: null,
weight_kg: null,
bio: null,
avatar_url: null,
preferred_language: 'zh-CN',
role: 'student'
}
console.log('用户资料创建成功:', userProfile)
return userProfile
}
}
console.error('创建用户资料失败:插入操作未成功')
return null
} catch (error) {
console.error('创建用户资料时发生错误:', error)
return null
}
}
/**
* 检查并创建用户资料
* 如果用户在 ak_users 表中不存在,则创建基础资料
* @param supaUser - Supabase 认证用户对象
* @returns Promise<UserProfile | null> - 用户资料或null
*/
export async function ensureUserProfile(supaUser: UTSJSONObject): Promise<UserProfile | null> { try {
const userId = supaUser.getString('id')
if (userId == null) {
console.error('用户ID不存在')
return null
}
// 首先尝试查询现有用户资料
const existingResult = await supa
.from('ak_users')
.select('*',{})
.eq('id', userId)
.execute()
if (existingResult.status === 200 && existingResult.data != null) {
const data = existingResult.data
let existingUser: UTSJSONObject | null = null
if (Array.isArray(data)) {
if (data.length > 0) {
existingUser = data[0] as UTSJSONObject
}
} else if (data != null) {
existingUser = data as UTSJSONObject
}
// 如果找到现有用户资料,直接返回
if (existingUser != null) {
const userProfile: UserProfile = {
id: existingUser.getString('id'),
username: existingUser.getString('username') ?? '',
email: existingUser.getString('email') ?? '',
gender: existingUser.getString('gender'),
birthday: existingUser.getString('birthday'),
height_cm: existingUser.getNumber('height_cm'),
weight_kg: existingUser.getNumber('weight_kg'),
bio: existingUser.getString('bio'),
avatar_url: existingUser.getString('avatar_url'),
preferred_language: existingUser.getString('preferred_language'),
role: existingUser.getString('role')
}
return userProfile
}
}
// 如果没有找到用户资料,创建新的
console.log('未找到用户资料,正在创建新的基础资料...')
return await createBasicUserProfile(supaUser)
} catch (error) {
console.error('检查用户资料时发生错误:', error)
return null
}
}