23de8bc86a
- 后端: VerificationCode模型+send-code API+register增加verifyCode校验 - 前端: 注册表单增加验证码输入框+60s倒计时获取按钮 - 前端: 登录表单增加用户协议/隐私政策勾选 - 前端: 登录按钮增加disabled状态(未勾选时不可点击) - 后端: nodemailer邮件发送工具(可配置SMTP,未配置时返回devCode)
202 lines
4.7 KiB
JavaScript
202 lines
4.7 KiB
JavaScript
/**
|
|
* 宇之然AI维度 — API 工具
|
|
* 后端地址:wdkj.yuzhiran.com.cn (生产) / 127.0.0.1:3001 (开发)
|
|
*/
|
|
|
|
const BASE_URL = 'https://wdkj.yuzhiran.com.cn'
|
|
const DEV_URL = 'http://127.0.0.1:3001'
|
|
|
|
// 生产环境使用域名(SSL 已续期)
|
|
const API_BASE = 'https://wdkj.yuzhiran.com.cn'
|
|
|
|
/**
|
|
* 通用请求封装
|
|
*/
|
|
async function request(method, path, data = null, token = null) {
|
|
const url = `${API_BASE}${path}`
|
|
const headers = {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`
|
|
}
|
|
|
|
try {
|
|
const res = await uni.request({
|
|
url,
|
|
method,
|
|
data,
|
|
header: headers,
|
|
timeout: 15000
|
|
})
|
|
|
|
const body = res.data
|
|
if (body.success) {
|
|
return body.data
|
|
}
|
|
throw new Error(body.error || '请求失败')
|
|
} catch (err) {
|
|
console.error(`[API] ${method} ${path} failed:`, err)
|
|
throw err
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 认证相关
|
|
*/
|
|
export const authApi = {
|
|
/** 微信小程序登录 */
|
|
login: (code, userInfo) =>
|
|
request('POST', '/api/auth/login', { code, userInfo }),
|
|
|
|
/** H5 密码登录 */
|
|
h5Login: (loginData) =>
|
|
request('POST', '/api/auth/user/login', loginData),
|
|
|
|
/** H5 注册 */
|
|
register: (email, password, nickName, verifyCode) =>
|
|
request('POST', '/api/auth/user/register', { email, password, nickName, verifyCode }),
|
|
|
|
/** 获取当前用户 */
|
|
me: (token) =>
|
|
request('GET', '/api/auth/me', null, token),
|
|
}
|
|
|
|
/**
|
|
* 知识库
|
|
*/
|
|
export const knowledgeApi = {
|
|
/** 获取知识列表 */
|
|
list: (params = {}) => {
|
|
const query = Object.entries(params)
|
|
.filter(([_, v]) => v !== undefined && v !== null)
|
|
.map(([k, v]) => `${k}=${v}`)
|
|
.join('&')
|
|
return request('GET', `/api/knowledge${query ? '?' + query : ''}`)
|
|
},
|
|
|
|
/** 获取知识详情 */
|
|
detail: (id) =>
|
|
request('GET', `/api/knowledge/${id}`),
|
|
|
|
/** 收藏知识 */
|
|
collect: (id, token) =>
|
|
request('POST', `/api/knowledge/${id}/collect`, null, token),
|
|
|
|
/** 取消收藏 */
|
|
uncollect: (id, token) =>
|
|
request('DELETE', `/api/knowledge/collect/${id}`, null, token),
|
|
|
|
/** 获取收藏列表 */
|
|
collected: (token) =>
|
|
request('GET', '/api/knowledge/collected', null, token),
|
|
|
|
/** 获取维度问答 */
|
|
questions: (dim) =>
|
|
request('GET', `/api/knowledge/${dim}/questions`),
|
|
|
|
/** 搜索知识 */
|
|
search: (q, options = {}) => {
|
|
const params = { q, ...options }
|
|
const query = Object.entries(params)
|
|
.filter(([_, v]) => v !== undefined && v !== null)
|
|
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
|
|
.join('&')
|
|
return request('GET', `/api/knowledge/search${query ? '?' + query : ''}`)
|
|
},
|
|
}
|
|
|
|
/**
|
|
* AI 问答
|
|
*/
|
|
export const aiChatApi = {
|
|
/** 获取配额 */
|
|
quota: (token) =>
|
|
request('GET', '/api/ai-chat/quota', null, token),
|
|
|
|
/** 提问 */
|
|
ask: (question, dimension = 'general', token) =>
|
|
request('POST', '/api/ai-chat/ask', { question, dimension }, token),
|
|
|
|
/** 分享获得次数 */
|
|
shareGain: (token) =>
|
|
request('POST', '/api/ai-chat/share-gain', null, token),
|
|
}
|
|
|
|
/**
|
|
* 用户相关
|
|
*/
|
|
export const userApi = {
|
|
/** 获取用户进度 */
|
|
progress: (token) =>
|
|
request('GET', '/api/user/progress', null, token),
|
|
|
|
/** 保存进度 */
|
|
saveProgress: (data, token) =>
|
|
request('POST', '/api/user/progress', data, token),
|
|
|
|
/** 排行榜 */
|
|
leaderboard: (dim = null) =>
|
|
request('GET', `/api/user/leaderboard${dim ? '?dim=' + dim : ''}`),
|
|
|
|
/** 获取成就 */
|
|
achievements: (token) =>
|
|
request('GET', '/api/user/achievements', null, token),
|
|
|
|
/** 用户统计 */
|
|
stats: (token) =>
|
|
request('GET', '/api/user/stats', null, token),
|
|
}
|
|
|
|
/**
|
|
* 支付相关
|
|
*/
|
|
export const paymentApi = {
|
|
/** 获取商品列表 */
|
|
products: () =>
|
|
request('GET', '/api/payment/products'),
|
|
|
|
/** 创建订单 */
|
|
createOrder: (productId, token) =>
|
|
request('POST', '/api/payment/create-order', { productId }, token),
|
|
}
|
|
|
|
/**
|
|
* 趋势资讯
|
|
*/
|
|
export const trendApi = {
|
|
/** 获取趋势列表 */
|
|
list: (params = {}) => {
|
|
const query = Object.entries(params)
|
|
.filter(([_, v]) => v !== undefined && v !== null)
|
|
.map(([k, v]) => `${k}=${v}`)
|
|
.join('&')
|
|
return request('GET', `/api/trend${query ? '?' + query : ''}`)
|
|
},
|
|
|
|
/** 获取趋势详情 */
|
|
detail: (id) =>
|
|
request('GET', `/api/trend/${id}`),
|
|
}
|
|
|
|
/**
|
|
* 意见反馈
|
|
*/
|
|
export const feedbackApi = {
|
|
/** 提交反馈 */
|
|
create: (data, token) =>
|
|
request('POST', '/api/feedback', data, token),
|
|
/** 我的反馈列表 */
|
|
my: (token) =>
|
|
request('GET', '/api/feedback/my', null, token),
|
|
}
|
|
|
|
export default {
|
|
auth: authApi,
|
|
knowledge: knowledgeApi,
|
|
aiChat: aiChatApi,
|
|
user: userApi,
|
|
payment: paymentApi,
|
|
trend: trendApi,
|
|
feedback: feedbackApi
|
|
} |