文件
yjyf/utils/request.js
linda 78722b3912 feat(ui): 登录起全链路页面接通 + 逐页面验证(47 项)+ 修复订单列表越权泄漏
前端:
- utils/request.js 统一请求层(token/错误码/401 引导登录)
- utils/api.js 接口清单(与后端控制器一一对应)
- pages/login 登录(手机号+验证码+角色选择)
- pages/service/list|detail 服务列表/详情
- pages/order/create 下单(地址/预约/急单/费用试算/自动支付)
- pages/order/list|detail|review 订单列表/详情轨迹/评价
- pages/master/apply|hall|orders 师傅入驻/订单大厅/接单履约
- YJYF/YJYF 首页接 home/index 真实类目 + 路由跳转

后端 Bug 修复(严重):
- TP5 中 count() 会重置查询条件,导致 Order::index / Master::hall / Master::orders
  在 select 时丢失 where 条件 → 泄漏全部用户订单/跨用户数据
- 改为闭包重建查询,已验证修复
2026-10-28 15:52:39 +08:00

108 行
2.6 KiB
JavaScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

/**
* 统一请求层
* - 自动携带 token(header: token)
* - 统一响应契约:code === 1 成功;其余为失败(0 通用 / 401 未登录 / 403 无权限 / 2001~2003 业务语义)
* - 失败统一 toast,未登录自动清理并引导登录
*/
const BASE_URL = 'https://yjyf.gzyakj.com.cn/api';
const TOKEN_KEY = 'yjyf_token';
const USER_KEY = 'yjyf_user';
export function getToken() {
try {
return uni.getStorageSync(TOKEN_KEY) || '';
} catch (e) {
return '';
}
}
export function setToken(token) {
uni.setStorageSync(TOKEN_KEY, token || '');
}
export function getUser() {
try {
return uni.getStorageSync(USER_KEY) || null;
} catch (e) {
return null;
}
}
export function setUser(user) {
uni.setStorageSync(USER_KEY, user || null);
}
export function clearAuth() {
uni.removeStorageSync(TOKEN_KEY);
uni.removeStorageSync(USER_KEY);
}
export function isLogin() {
return !!getToken();
}
export function toLogin() {
clearAuth();
uni.navigateTo({ url: '/pages/login/login' });
}
/**
* @param {String} path 接口路径,如 /order/index
* @param {Object} options { method, data, auth, silent }
*/
export function request(path, options) {
const opts = options || {};
const method = opts.method || 'GET';
const data = opts.data || {};
const silent = !!opts.silent;
return new Promise((resolve, reject) => {
const header = { 'content-type': 'application/x-www-form-urlencoded' };
const token = getToken();
if (token) {
header.token = token;
}
uni.request({
url: BASE_URL + path,
method: method,
data: data,
header: header,
timeout: 15000,
success: (res) => {
const body = res.data || {};
if (body.code === 1) {
resolve(body.data === undefined || body.data === null ? {} : body.data);
return;
}
const msg = body.msg || ('请求失败(' + res.statusCode + ')');
if (body.code === 401) {
clearAuth();
if (!silent) {
uni.showToast({ title: '登录已失效,请重新登录', icon: 'none' });
setTimeout(() => toLogin(), 800);
}
} else if (!silent) {
uni.showToast({ title: msg, icon: 'none' });
}
reject(Object.assign(new Error(msg), { code: body.code, data: body.data }));
},
fail: () => {
if (!silent) {
uni.showToast({ title: '网络异常,请稍后重试', icon: 'none' });
}
reject(new Error('网络异常'));
}
});
});
}
export function get(path, data, options) {
return request(path, Object.assign({ method: 'GET', data: data }, options || {}));
}
export function post(path, data, options) {
return request(path, Object.assign({ method: 'POST', data: data }, options || {}));
}