mi-assessment/uniapp/api/auth.js
2026-02-09 14:45:06 +08:00

62 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 认证接口模块
* Requirements: 1.1, 1.2, 1.3
*/
import { post } from './request'
import { setToken, setUserInfo } from '../utils/storage'
/**
* 微信登录
* WHEN a user opens the XiangYi_MiniApp for the first time,
* THE XiangYi_MiniApp SHALL call WeChat login API and obtain authorization code
* Requirements: 1.1, 1.2
*
* @param {string} code - 微信登录授权码
* @returns {Promise<Object>} 登录响应包含token和用户信息
*/
export async function login(code) {
const response = await post('/auth/login', { code }, { needAuth: false })
// 存储token和用户信息
if (response.data) {
const { token, userId, nickname, avatar, xiangQinNo, isProfileCompleted, isMember, memberLevel, isRealName } = response.data
if (token) {
setToken(token)
}
setUserInfo({
userId,
nickname,
avatar,
xiangQinNo,
isProfileCompleted,
isMember,
memberLevel,
isRealName
})
}
return response
}
/**
* 绑定手机号
* WHEN a user needs to bind phone number,
* THE XiangYi_MiniApp SHALL use WeChat getPhoneNumber API and send the code to endpoint
* Requirements: 1.3
*
* @param {string} code - 微信获取手机号的code
* @returns {Promise<Object>} 绑定结果
*/
export async function bindPhone(code) {
const response = await post('/auth/bindPhone', { code })
return response
}
export default {
login,
bindPhone
}