feat: 培训小程序(Taro+React,OPC AI 移动端)
- 页面:首页/活动/报名/测评/政策/流程/调研/我的 - 自定义 tabBar、骨架屏、后端驱动测评
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import Taro from '@tarojs/taro';
|
||||
import { request } from './api';
|
||||
|
||||
/* 用户端认证:手机号 + 短信验证码(与 web 端一致,见 小程序同步约束.md) */
|
||||
export async function sendCode(phone) {
|
||||
return request('POST', '/api/auth/send-code', { phone });
|
||||
}
|
||||
|
||||
export async function phoneLogin(phone, code) {
|
||||
const data = await request('POST', '/api/auth/phone-login', { phone, code });
|
||||
if (data.token) {
|
||||
Taro.setStorageSync('pine_token', data.token);
|
||||
// 手机号登录账号即以手机号为账号,天然已绑定手机号(phoneBound=true),保持一致避免再次提示绑定
|
||||
Taro.setStorageSync('pine_user', {
|
||||
username: data.username,
|
||||
name: data.name || data.username,
|
||||
avatar: '',
|
||||
phone: data.username,
|
||||
phoneBound: true
|
||||
});
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export function isAuthed() {
|
||||
return !!Taro.getStorageSync('pine_token');
|
||||
}
|
||||
|
||||
export function getUser() {
|
||||
return Taro.getStorageSync('pine_user') || null;
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
Taro.removeStorageSync('pine_token');
|
||||
Taro.removeStorageSync('pine_user');
|
||||
}
|
||||
|
||||
/* ---------------- 微信登录 / 手机绑定 / 我的资料 ---------------- */
|
||||
/** 微信一键登录:wx.login 拿 code(演示后端用 code 哈希做伪 openid),可携带昵称/头像 */
|
||||
export async function wxLogin({ code, nickName, avatarUrl }) {
|
||||
const data = await request('POST', '/api/auth/wx-login', { code, nickName, avatarUrl });
|
||||
if (data.token) {
|
||||
Taro.setStorageSync('pine_token', data.token);
|
||||
Taro.setStorageSync('pine_user', {
|
||||
username: data.username,
|
||||
name: data.name || '微信用户',
|
||||
avatar: data.avatar || '',
|
||||
phone: '',
|
||||
phoneBound: !!data.phoneBound
|
||||
});
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/** 绑定手机:验证码校验后把手机号挂到当前账号 */
|
||||
export async function bindPhone(phone, code) {
|
||||
const data = await request('POST', '/api/auth/bind-phone', { phone, code }, true);
|
||||
if (data.ok) {
|
||||
const u = getUser() || {};
|
||||
Taro.setStorageSync('pine_user', { ...u, phone, phoneBound: true });
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/** 拉取当前账号资料(刷新昵称/头像/手机绑定状态) */
|
||||
export async function getMe() {
|
||||
const data = await request('GET', '/api/auth/me', {}, true);
|
||||
if (data.ok && data.user) {
|
||||
const u = getUser() || {};
|
||||
Taro.setStorageSync('pine_user', { ...u, ...data.user });
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/** 微信官方手机号一键绑定(getPhoneNumber 回调 code) */
|
||||
export async function wxPhoneBind(code) {
|
||||
const data = await request('POST', '/api/auth/wx-phone', { code }, true);
|
||||
if (data.ok && data.phone) {
|
||||
const u = getUser() || {};
|
||||
Taro.setStorageSync('pine_user', { ...u, phone: data.phone, phoneBound: true });
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/** 更新当前用户资料(昵称/头像 + 报名资料 status/topics/source) */
|
||||
export async function updateProfile(data) {
|
||||
const r = await request('POST', '/api/auth/update-profile', data, true);
|
||||
if (r.ok && r.user) {
|
||||
const u = getUser() || {};
|
||||
// 用后端返回的完整 user(含 nickname/avatar/phone/phoneBound/status/topics/source)覆盖内存资料
|
||||
Taro.setStorageSync('pine_user', { ...u, ...r.user });
|
||||
}
|
||||
return r;
|
||||
}
|
||||
Reference in New Issue
Block a user