需求:小程序端代码要同时同步到服务器与 git 远程仓库,此前前端仅存在于本地目录(无 remote,从未推送), 服务器与 gitcode 上只有后端。现并入本仓库,复用现有「push 即部署」链路,一次推送双端落地。 - 新增 uniapp/:小程序端源码全量快照(150 文件 / 1.2M) pages(13 页面) · components(fc-navbar/fc-tabbar/fc-post-card/fc-empty) · common(api·utils·mock·assets·store·config) · static(74 图标 + 3 轮播插画 + LOGO) · docs(01-04 交付文档) · tests(零依赖全链路用例) · tools(图标/主题/轮播配图生成脚本) - 明确排除:AGENTS.md(含后台密码、服务器 root 密码、小程序 AppSecret,切勿入库)、 unpackage/(构建产物,由 uniapp/.gitignore 忽略)、node_modules、.DS_Store、*.log - .gitignore 新增 /uniapp/AGENTS.md 兜底(与根 /AGENTS.md、/fluniapp/ 同一约定) - README.md 增补「本项目结构 + 部署链路」章节,说明 uniapp/ 为源码快照、 小程序真正上线仍需微信开发者工具 cli upload 说明:本次为源码快照,未携带前端本地仓库历史——其历史提交中含 AGENTS.md 凭据, 若做 subtree 合并会把密码永久写入本仓库历史,故采用快照方式。
151 行
5.1 KiB
JavaScript
151 行
5.1 KiB
JavaScript
/**
|
||
* 全链路统一请求层
|
||
* - 统一拼接 baseURL / 版本号
|
||
* - 自动注入 token
|
||
* - 统一响应结构解包:{ code, message, data }
|
||
* - 401 自动清理登录态并跳转登录
|
||
* - 支持 mock 降级(无后端 / 联调前)
|
||
*
|
||
* 该文件为纯 ES Module,Node 测试脚本可直接 import(需先注入 globalThis.uni)。
|
||
*/
|
||
import config from '../config/index.js'
|
||
import storage from './storage.js'
|
||
import { matchMock } from '../mock/index.js'
|
||
|
||
// 便于测试注入的运行时环境(默认使用全局 uni)
|
||
export function getRuntime() {
|
||
return {
|
||
request: typeof uni !== 'undefined' && uni.request ? uni.request.bind(uni) : null,
|
||
showToast: typeof uni !== 'undefined' && uni.showToast ? uni.showToast.bind(uni) : () => {},
|
||
navigateTo: typeof uni !== 'undefined' && uni.navigateTo ? uni.navigateTo.bind(uni) : () => {},
|
||
showLoading: typeof uni !== 'undefined' && uni.showLoading ? uni.showLoading.bind(uni) : () => {},
|
||
hideLoading: typeof uni !== 'undefined' && uni.hideLoading ? uni.hideLoading.bind(uni) : () => {},
|
||
}
|
||
}
|
||
|
||
/** 拼接完整地址 */
|
||
export function buildUrl(url) {
|
||
if (/^https?:\/\//i.test(url)) return url
|
||
const path = url.startsWith('/') ? url : `/${url}`
|
||
return `${config.baseURL}/${config.apiVersion}${path}`
|
||
}
|
||
|
||
/** 组装请求头 */
|
||
export function buildHeaders(extra = {}) {
|
||
const token = storage.get(config.storageKeys.token, '')
|
||
const headers = {
|
||
'Content-Type': 'application/json;charset=UTF-8',
|
||
'X-Client': 'fenleixinxi',
|
||
...extra,
|
||
}
|
||
if (token) headers.Authorization = `Bearer ${token}`
|
||
return headers
|
||
}
|
||
|
||
/**
|
||
* 底层请求(返回 Promise<data>)
|
||
* @param {Object} options { url, method, data, header, silent, mock }
|
||
*/
|
||
export function request(options = {}) {
|
||
const {
|
||
url,
|
||
method = 'GET',
|
||
data = {},
|
||
header = {},
|
||
silent = false, // 失败是否静默(不弹 toast)
|
||
mock = config.forceMock, // 强制走 mock
|
||
} = options
|
||
|
||
const runtime = getRuntime()
|
||
const finalUrl = buildUrl(url)
|
||
const headers = buildHeaders(header)
|
||
|
||
const doMock = () =>
|
||
matchMock({ url, method: method.toUpperCase(), data }).then((res) => res)
|
||
|
||
// 强制 mock 或运行环境无 uni.request
|
||
if (mock || !runtime.request) {
|
||
return doMock()
|
||
}
|
||
|
||
return new Promise((resolve, reject) => {
|
||
runtime.request({
|
||
url: finalUrl,
|
||
method: method.toUpperCase(),
|
||
data,
|
||
header: headers,
|
||
timeout: config.timeout,
|
||
success(res) {
|
||
// HTTP 层
|
||
if (res.statusCode === 401) {
|
||
onUnauthorized(runtime)
|
||
return reject(toErr('登录已过期,请重新登录', 401))
|
||
}
|
||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||
return handleFail(
|
||
{ message: `网络异常(${res.statusCode})`, statusCode: res.statusCode },
|
||
{ url, method, data, silent, runtime, resolve, reject }
|
||
)
|
||
}
|
||
const body = res.data || {}
|
||
// 业务层:约定 code===0 或 code===200 为成功
|
||
if (body.code === 0 || body.code === 200) {
|
||
resolve(body.data !== undefined ? body.data : body)
|
||
} else {
|
||
const err = toErr(body.message || body.msg || '请求失败', body.code)
|
||
if (!silent) runtime.showToast({ title: err.message, icon: 'none' })
|
||
reject(err)
|
||
}
|
||
},
|
||
fail(err) {
|
||
handleFail(err, { url, method, data, silent, runtime, resolve, reject })
|
||
},
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 失败处理:
|
||
* 1) 若开启 mock 降级,则使用本地 mock 路由兜底(保证离线可演示)
|
||
* 2) 否则抛出异常
|
||
*/
|
||
function handleFail(err, ctx) {
|
||
const { url, method, data, silent, runtime, resolve, reject } = ctx
|
||
if (config.enableMockFallback) {
|
||
// 尝试用本地 mock 路由兜底(离线可演示;无匹配路由时 mock 会 reject)
|
||
matchMock({ url, method: String(method).toUpperCase(), data })
|
||
.then(resolve)
|
||
.catch(() => {
|
||
const e = toErr(err && (err.errMsg || err.message) ? err.errMsg || err.message : '网络请求失败')
|
||
if (!silent) runtime.showToast({ title: e.message, icon: 'none' })
|
||
reject(e)
|
||
})
|
||
return
|
||
}
|
||
const e = toErr(err && (err.errMsg || err.message) ? err.errMsg || err.message : '网络请求失败')
|
||
if (!silent) runtime.showToast({ title: e.message, icon: 'none' })
|
||
reject(e)
|
||
}
|
||
|
||
/** 401 处理 */
|
||
function onUnauthorized(runtime) {
|
||
storage.remove(config.storageKeys.token)
|
||
storage.remove(config.storageKeys.userInfo)
|
||
runtime.showToast({ title: '登录已过期', icon: 'none' })
|
||
runtime.navigateTo({ url: '/pages/auth/login' })
|
||
}
|
||
|
||
function toErr(message, code = -1) {
|
||
const e = new Error(message)
|
||
e.code = code
|
||
return e
|
||
}
|
||
|
||
/** 语法糖 */
|
||
export const get = (url, data, opts = {}) => request({ url, method: 'GET', data, ...opts })
|
||
export const post = (url, data, opts = {}) => request({ url, method: 'POST', data, ...opts })
|
||
export const put = (url, data, opts = {}) => request({ url, method: 'PUT', data, ...opts })
|
||
export const del = (url, data, opts = {}) => request({ url, method: 'DELETE', data, ...opts })
|
||
|
||
export default request
|