更新go结构和uniapp

This commit is contained in:
2026-07-15 22:38:33 +08:00
parent bca5170e22
commit 61a937f7a3
23 changed files with 3536 additions and 293 deletions
+147
View File
@@ -0,0 +1,147 @@
/**
* 移动端认证接口 — 对接 Go /app/*
*/
import { request, requestRaw } from '@/api/request.js'
/** 账号密码登录 */
export function login(data) {
return request({
url: '/app/login',
method: 'POST',
data
})
}
/** 发送登录验证码(账号二次校验用,需后台开启验证) */
export function sendLoginCode(data) {
return request({
url: '/app/sendLoginCode',
method: 'POST',
data
})
}
/** 手机号验证码登录 */
export function loginBySms(data) {
return request({
url: '/app/loginBySms',
method: 'POST',
data
})
}
/** 退出登录(后端无状态,失败可忽略;短超时避免卡住) */
export function logoutApi(data = {}) {
return request({
url: '/app/logout',
method: 'POST',
data,
silent: true,
timeout: 5000
})
}
/** 当前用户信息 */
export function getCurrentUser() {
return request({
url: '/app/currentUser',
method: 'GET'
})
}
/** 是否开启登录人机/验证码校验 */
export function getOpenVerify() {
return request({
url: '/app/login/getOpenVerify',
method: 'GET',
silent: true,
timeout: 8000
})
}
/** 极验 3 配置 */
export function getGeetest3Infos() {
return request({
url: '/app/login/getGeetest3Infos',
method: 'GET',
silent: true
})
}
/** 极验 4 配置 */
export function getGeetest4Infos() {
return request({
url: '/app/login/getGeetest4Infos',
method: 'GET',
silent: true,
timeout: 8000
})
}
/** 注册 */
export function register(data) {
return request({
url: '/app/register',
method: 'POST',
data
})
}
/** 发送注册验证码 */
export function sendRegisterCode(data) {
return request({
url: '/app/sendRegisterCode',
method: 'POST',
data
})
}
/** 重置密码 */
export function resetPassword(data) {
return request({
url: '/app/resetPassword',
method: 'POST',
data
})
}
/** 验证租户和账号(找回密码第一步) */
export function verifyAccount(data) {
return request({
url: '/app/verifyAccount',
method: 'POST',
data
})
}
/** 发送找回密码验证码(找回密码第二步) */
export function sendResetCode(data) {
return request({
url: '/app/sendResetCode',
method: 'POST',
data
})
}
/**
* 解析 getOpenVerify 返回的 label/value 列表
* @returns {Promise<{ openVerify: boolean, verifyType: string }>}
*/
export async function fetchVerifyConfig() {
try {
const data = await getOpenVerify()
const list = Array.isArray(data) ? data : []
const map = {}
list.forEach((item) => {
if (item && item.label) map[item.label] = item.value
})
return {
openVerify: map.openVerify === '1' || map.openVerify === 1,
verifyType: map.verifyType || ''
}
} catch {
return { openVerify: false, verifyType: '' }
}
}
export { requestRaw }
+43
View File
@@ -0,0 +1,43 @@
/**
* 从项目根目录 .env 读取接口地址。
* 变量名:VITE_API_BASE_URL
* 调试默认 localhost;正式构建前在 .env 里改成 https://api.yunzer.cn 即可。
*/
/** 极验 4.0 captcha_id(前端展示用;KEY 仅在后端校验) */
export const GEETEST4_CAPTCHA_ID = '75e8a175c43b9ecfa15372c658be05e5'
/**
* @returns {string} 去掉末尾斜杠的 baseURL
*/
export function resolveBaseURL() {
let fromEnv = ''
try {
fromEnv =
(typeof import.meta !== 'undefined' &&
import.meta.env &&
import.meta.env.VITE_API_BASE_URL) ||
''
} catch (e) {
fromEnv = ''
}
if (fromEnv && fromEnv !== 'undefined' && fromEnv !== 'null') {
return String(fromEnv).replace(/\/$/, '')
}
// #ifdef H5
// H5 开发未配置 env 时走 vite 代理(前缀勿用 /api,会与 api/ 源码目录冲突)
if (typeof import.meta !== 'undefined' && import.meta.env && import.meta.env.DEV) {
return '/proxy-api'
}
// #endif
// App / 真机:localhost 指向设备自身,需用 .env.development 配置局域网 IP
return 'http://localhost:9000'
}
/** 与 resolveBaseURL 相同,便于业务侧统一引用 */
export function getBaseURL() {
return resolveBaseURL()
}
+115 -18
View File
@@ -1,23 +1,120 @@
/**
* 统一请求封装,接入后端时在此配置 baseURL、token、错误处理。
*
* 示例:
* export function request({ url, method = 'GET', data }) {
* return new Promise((resolve, reject) => {
* uni.request({
* url: BASE_URL + url,
* method,
* data,
* header: { Authorization: 'Bearer ' + getToken() },
* success: (res) => {
* if (res.statusCode >= 200 && res.statusCode < 300) resolve(res.data)
* else reject(res)
* },
* fail: reject
* })
* })
* }
* 统一请求封装baseURL、Bearer Token、业务 code 处理。
* 接口地址统一从 .env 的 VITE_API_BASE_URL 读取(见 api/config.js)。
*/
import { getToken, logout } from '@/utils/auth.js'
import { resolveBaseURL, getBaseURL } from '@/api/config.js'
const BASE_URL = resolveBaseURL()
export { getBaseURL }
/**
* @param {{ url: string, method?: string, data?: object, header?: object, silent?: boolean }} options
* @returns {Promise<any>} 业务 data 字段(若无 data 则返回整包)
*/
export function request(options = {}) {
const { url, method = 'GET', data, header = {}, silent = false, timeout = 30000 } = options
const token = getToken()
const headers = {
'Content-Type': 'application/json',
...header
}
if (token) {
headers.Authorization = `Bearer ${token}`
}
const fullUrl = BASE_URL.replace(/\/$/, '') + url
return new Promise((resolve, reject) => {
uni.request({
url: fullUrl,
method: method.toUpperCase(),
data: data || {},
header: headers,
timeout,
success(res) {
const status = res.statusCode
const body = res.data || {}
if (status === 401 || body.code === 401) {
logout()
if (!silent) {
uni.showToast({ title: body.msg || '请重新登录', icon: 'none' })
}
setTimeout(() => {
uni.reLaunch({ url: '/pages/login/login' })
}, 400)
reject(new Error(body.msg || '未授权'))
return
}
if (status < 200 || status >= 300) {
const msg = body.msg || `请求失败(${status})`
if (!silent) uni.showToast({ title: msg, icon: 'none' })
reject(new Error(msg))
return
}
// 统一后端 { code, msg, data }
if (typeof body.code !== 'undefined' && body.code !== 200) {
const msg = body.msg || '操作失败'
if (!silent) uni.showToast({ title: msg, icon: 'none' })
const err = new Error(msg)
err.code = body.code
err.response = body
reject(err)
return
}
resolve(typeof body.data !== 'undefined' ? body.data : body)
},
fail(err) {
const detail = (err && err.errMsg) || ''
console.error('[request fail]', fullUrl, detail)
if (!silent) {
uni.showToast({
title: detail.includes('timeout') ? '请求超时' : '网络异常,请检查接口地址',
icon: 'none'
})
}
reject(new Error(detail || '网络异常'))
}
})
})
}
/** 返回完整响应体(含 code/msg),用于需要自行判断 code 的场景 */
export function requestRaw(options = {}) {
const { url, method = 'GET', data, header = {} } = options
const token = getToken()
const headers = {
'Content-Type': 'application/json',
...header
}
if (token) {
headers.Authorization = `Bearer ${token}`
}
const fullUrl = BASE_URL.replace(/\/$/, '') + url
return new Promise((resolve, reject) => {
uni.request({
url: fullUrl,
method: method.toUpperCase(),
data: data || {},
header: headers,
timeout: 30000,
success(res) {
resolve(res.data || {})
},
fail(err) {
console.error('[requestRaw fail]', fullUrl, err)
reject(err)
}
})
})
}
export function generateId() {
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`