文件
yjyf/yjyf-master-app/utils/request.js
MiAn 14533eadb7 feat: 用户端与师傅端双小程序架构解耦,新增师傅独立认证及超时自动关单机制
1. 账号体系与数据库解耦:
   - 师傅表 fa_master 解除对 fa_user.id 的外键依赖,升级为独立认证账号主表;
   - 新增师傅端独立支撑表:fa_master_security、fa_master_token、fa_master_wechat、fa_master_agreement_read;
   - 新增工单超时未接单自动关闭与原路退款配置(order_unaccepted_close_days)。

2. 服务端(Server):
   - 新增师傅端独立 API 控制器与认证库(MasterAuth, MasterBase, MasterMessage, MasterSecurity, MasterToken);
   - WxLogin 支持双小程序端(user/master)独立换取 session_key、openid 与 access_token;
   - 新增控制台调度命令 php think order:autoclose,支持订单超时自动关闭与售后退款闭环;
   - 修复 PHP 8.5 兼容性问题(Request.php 的 (bool) 强制转换);
   - 系统业务配置全面收敛至数据库 fa_config 及 site.php。

3. 小程序端:
   - yjyf-app(用户端):彻底剥离师傅端路由、组件及静态资源,增加隐私授权弹窗与评价模块;
   - yjyf-master-app(师傅端):全新独立微信小程序工程(wx140d90ceda786107),实现工作台、接单大厅、资质认证、工单协同与钱包提现完整闭环。
2026-11-23 15:10:30 +08:00

206 行
5.3 KiB
JavaScript

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

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

/**
* 师傅端统一请求层(完全独立于用户端存储 Key)
*/
import config from '@/config/config.js';
export const BASE_URL = (config.baseUrl || config.dev).replace(/\/+$/, '') + '/api';
const TOKEN_KEY = 'yjyf_master_token';
const MASTER_KEY = 'yjyf_master_info';
const pendingPostMap = new Map();
let lastDupToastAt = 0;
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(MASTER_KEY) || null;
} catch (e) {
return null;
}
}
export function setUser(master) {
uni.setStorageSync(MASTER_KEY, master || null);
}
export function clearAuth() {
uni.removeStorageSync(TOKEN_KEY);
uni.removeStorageSync(MASTER_KEY);
}
export function isLogin() {
return !!getToken();
}
let loginRedirectLock = false;
export function toLogin(redirect) {
clearAuth();
if (loginRedirectLock) return;
loginRedirectLock = true;
setTimeout(() => {
loginRedirectLock = false;
}, 800);
let targetRedirect = redirect || '';
if (!targetRedirect) {
try {
const pages = getCurrentPages() || [];
const cur = pages[pages.length - 1];
if (cur && cur.route && cur.route !== 'pages/login/login') {
const opts = cur.options || {};
const qs = Object.keys(opts)
.filter((k) => opts[k] !== undefined && opts[k] !== '')
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(opts[k])}`)
.join('&');
targetRedirect = '/' + cur.route + (qs ? `?${qs}` : '');
}
} catch (e) {}
}
const url = targetRedirect
? `/pages/login/login?redirect=${encodeURIComponent(targetRedirect)}`
: '/pages/login/login';
uni.navigateTo({
url,
fail: () => {
uni.reLaunch({ url });
}
});
}
export function request(path, options) {
const opts = options || {};
const method = (opts.method || 'GET').toUpperCase();
const data = opts.data || {};
const silent = !!opts.silent;
const needPostLock = method === 'POST' && !opts.allowConcurrent;
const requestKey = needPostLock ? (path + '_' + JSON.stringify(data)) : '';
if (needPostLock) {
const lockState = pendingPostMap.get(requestKey);
const now = Date.now();
if (lockState && (lockState.inflight || now < lockState.expireAt)) {
if (!silent && now - lastDupToastAt > 800) {
lastDupToastAt = now;
uni.showToast({ title: '正在处理中,请勿重复操作', icon: 'none' });
}
return Promise.reject(Object.assign(new Error('正在处理中,请勿重复操作'), { isDuplicate: true }));
}
pendingPostMap.set(requestKey, { inflight: true, expireAt: now + 16000 });
}
const releasePostLock = (cooldownMs) => {
if (!needPostLock) return;
const until = Date.now() + cooldownMs;
pendingPostMap.set(requestKey, { inflight: false, expireAt: until });
setTimeout(() => {
const cur = pendingPostMap.get(requestKey);
if (cur && !cur.inflight && Date.now() >= cur.expireAt) {
pendingPostMap.delete(requestKey);
}
}, cooldownMs + 50);
};
return new Promise((resolve, reject) => {
const header = {
'content-type': 'application/x-www-form-urlencoded',
'X-Client-App': 'master'
};
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) {
releasePostLock(500);
resolve(body.data === undefined || body.data === null ? {} : body.data);
return;
}
releasePostLock(300);
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: () => {
releasePostLock(300);
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 || {}));
}
export function uploadFile(filePath, category = 'other') {
return new Promise((resolve, reject) => {
const header = { 'X-Client-App': 'master' };
const token = getToken();
if (token) {
header.token = token;
}
uni.uploadFile({
url: BASE_URL + '/common/upload',
filePath: filePath,
name: 'file',
formData: {
category: category
},
header: header,
success: (res) => {
try {
const body = JSON.parse(res.data);
if (body.code === 1) {
resolve(body.data);
return;
}
uni.showToast({ title: body.msg || '上传失败', icon: 'none' });
reject(new Error(body.msg));
} catch (e) {
uni.showToast({ title: '解析上传响应失败', icon: 'none' });
reject(e);
}
},
fail: (err) => {
uni.showToast({ title: '网络异常,上传失败', icon: 'none' });
reject(err);
}
});
});
}