feat: 迁移项目到 yjyf-app 目录并更新首页路由配置
这个提交包含在:
84
yjyf-app/utils/api.js
普通文件
84
yjyf-app/utils/api.js
普通文件
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* 接口清单(与后端 application/api/controller 一一对应)
|
||||
*/
|
||||
import { get, post } from './request';
|
||||
|
||||
export default {
|
||||
/* ---------- 鉴权 ---------- */
|
||||
login: (data) => post('/auth/login', data, { silent: true }),
|
||||
wxLogin: (data) => post('/auth/wxlogin', data, { silent: true }),
|
||||
switchRole: (data) => post('/auth/switchRole', data),
|
||||
profile: () => get('/auth/profile'),
|
||||
|
||||
/* ---------- 首页 / 服务 ---------- */
|
||||
home: () => get('/home/index', {}, { silent: true }),
|
||||
cats: () => get('/home/cats'),
|
||||
services: (data) => get('/home/services', data),
|
||||
serviceDetail: (id) => get('/home/detail', { id: id }),
|
||||
calc: (data) => get('/home/calc', data),
|
||||
|
||||
/* ---------- 地址 ---------- */
|
||||
addressList: () => get('/address/index'),
|
||||
addressDetail: (id) => get('/address/detail', { id: id }),
|
||||
addressSave: (data) => post('/address/save', data),
|
||||
addressDelete: (id) => post('/address/delete', { id: id }),
|
||||
addressDefault: (id) => post('/address/setDefault', { id: id }),
|
||||
|
||||
/* ---------- 订单(用户端) ---------- */
|
||||
orderCreate: (data) => post('/order/create', data),
|
||||
orderPay: (data) => post('/order/pay', data),
|
||||
orderCancel: (data) => post('/order/cancel', data),
|
||||
orderVerify: (data) => post('/order/verify', data),
|
||||
orderList: (data) => get('/order/index', data),
|
||||
orderDetail: (id) => get('/order/detail', { id: id }),
|
||||
orderQuotes: (orderId) => get('/order/quotes', { order_id: orderId }),
|
||||
orderChooseQuote: (data) => post('/order/chooseQuote', data),
|
||||
|
||||
/* ---------- 师傅端 ---------- */
|
||||
masterApply: (data) => post('/master/apply', data),
|
||||
masterInfo: () => get('/master/info'),
|
||||
masterHall: (data) => get('/master/hall', data),
|
||||
masterGrab: (data) => post('/master/grab', data),
|
||||
masterOrders: (data) => get('/master/orders', data),
|
||||
masterContact: (data) => post('/master/contact', data),
|
||||
masterSchedule: (data) => post('/master/schedule', data),
|
||||
masterArrive: (data) => post('/master/arrive', data),
|
||||
masterStart: (data) => post('/master/startJob', data),
|
||||
masterFinish: (data) => post('/master/finish', data),
|
||||
masterIncome: () => get('/master/income'),
|
||||
masterToggle: (data) => post('/master/toggleWork', data),
|
||||
masterCredit: () => get('/master/credit'),
|
||||
masterAppeals: () => get('/master/appeals'),
|
||||
masterAppeal: (data) => post('/master/appeal', data),
|
||||
masterQuotes: () => get('/master/quotes'),
|
||||
masterQuote: (data) => post('/master/quote', data),
|
||||
masterWithdraw: (data) => post('/master/withdraw', data),
|
||||
|
||||
/* ---------- 评价 ---------- */
|
||||
reviewCreate: (data) => post('/review/create', data),
|
||||
reviewList: (masterId) => get('/review/masterList', { master_id: masterId }),
|
||||
|
||||
/* ---------- 优惠券 ---------- */
|
||||
couponCenter: () => get('/coupon/center'),
|
||||
couponMy: (data) => get('/coupon/my', data),
|
||||
couponReceive: (data) => post('/coupon/receive', data),
|
||||
|
||||
/* ---------- 消息中心 ---------- */
|
||||
messageList: (data) => get('/message/index', data),
|
||||
messageRead: (data) => post('/message/read', data),
|
||||
messageReadAll: (data) => post('/message/readAll', data),
|
||||
messageUnread: (data) => get('/message/unread', data),
|
||||
|
||||
/* ---------- 商家端 ---------- */
|
||||
merchantApply: (data) => post('/merchant/apply', data),
|
||||
merchantInfo: () => get('/merchant/info'),
|
||||
merchantUpdate: (data) => post('/merchant/update', data),
|
||||
merchantBills: () => get('/merchant/bills'),
|
||||
merchantOrders: (data) => get('/merchant/orders', data),
|
||||
merchantInvoices: () => get('/merchant/invoices'),
|
||||
merchantInvoice: (data) => post('/merchant/invoice', data),
|
||||
merchantMembers: () => get('/merchant/members'),
|
||||
merchantBindMember: (data) => post('/merchant/bindMember', data),
|
||||
merchantUnbindMember: (data) => post('/merchant/unbindMember', data),
|
||||
merchantBindBatch: (data) => post('/merchant/bindBatch', data)
|
||||
};
|
||||
107
yjyf-app/utils/request.js
普通文件
107
yjyf-app/utils/request.js
普通文件
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* 统一请求层
|
||||
* - 自动携带 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 || {}));
|
||||
}
|
||||
21
yjyf-app/utils/role.js
普通文件
21
yjyf-app/utils/role.js
普通文件
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 角色上下文(前端本地保存当前身份,后端以 switchRole 校验权限)
|
||||
*/
|
||||
const ROLE_KEY = 'yjyf_role';
|
||||
|
||||
export function getRole() {
|
||||
try {
|
||||
return uni.getStorageSync(ROLE_KEY) || 'user';
|
||||
} catch (e) {
|
||||
return 'user';
|
||||
}
|
||||
}
|
||||
|
||||
export function setRole(role) {
|
||||
const valid = ['user', 'master', 'merchant'];
|
||||
uni.setStorageSync(ROLE_KEY, valid.indexOf(role) > -1 ? role : 'user');
|
||||
}
|
||||
|
||||
export function clearRole() {
|
||||
uni.removeStorageSync(ROLE_KEY);
|
||||
}
|
||||
81
yjyf-app/utils/wxlogin.js
普通文件
81
yjyf-app/utils/wxlogin.js
普通文件
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* 微信登录统一入口
|
||||
* 解决两个线上问题:
|
||||
* 1) code 一次性(5 分钟、单次有效)——静默登录与手动登录若同时发起会互相抢 code
|
||||
* → 用「单飞(in-flight)」保证同一时刻只有一个换取流程
|
||||
* 2) 40029(invalid code)——可能来自 code 已被消费/过期
|
||||
* → 自动用全新 code 重试一次
|
||||
*/
|
||||
import api from './api.js';
|
||||
import { getToken, setToken, setUser } from './request.js';
|
||||
|
||||
let inflight = null;
|
||||
|
||||
/** 取微信一次性 code */
|
||||
export function getWxCode() {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success: (res) => (res && res.code ? resolve(res.code) : reject(new Error('微信未返回 code'))),
|
||||
fail: () => reject(new Error('未检测到微信环境,请在小程序内打开'))
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function isCodeInvalid(err) {
|
||||
const code = err && err.data ? Number(err.data.wx_errcode) : 0;
|
||||
if (code === 40029 || code === 40163) return true;
|
||||
return /invalid code|code been used|40029/i.test((err && err.message) || '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 用 code 换 token;遇到 code 失效自动换新 code 重试一次
|
||||
*/
|
||||
async function exchange(payload, retry) {
|
||||
const code = await getWxCode();
|
||||
try {
|
||||
const data = await api.wxLogin(Object.assign({}, payload, { code: code }));
|
||||
setToken(data.token);
|
||||
setUser(data.userinfo);
|
||||
return data;
|
||||
} catch (err) {
|
||||
if (retry && isCodeInvalid(err)) {
|
||||
return exchange(payload, false);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 静默登录(进主页时调用):已登录直接返回,失败不抛错
|
||||
*/
|
||||
export function ensureSilentLogin() {
|
||||
if (getToken()) return Promise.resolve(null);
|
||||
if (inflight) return inflight;
|
||||
|
||||
inflight = exchange({}, true)
|
||||
.catch(() => null)
|
||||
.then((data) => {
|
||||
inflight = null;
|
||||
return data;
|
||||
});
|
||||
return inflight;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主动授权登录(点击「微信一键登录」)
|
||||
*/
|
||||
export function wxAuthorizeLogin(payload) {
|
||||
if (inflight) {
|
||||
// 复用在途结果,避免抢同一个 code
|
||||
return inflight.then((data) => {
|
||||
if (data) return data;
|
||||
return exchange(payload || {}, true);
|
||||
});
|
||||
}
|
||||
inflight = exchange(payload || {}, true).then(
|
||||
(data) => { inflight = null; return data; },
|
||||
(err) => { inflight = null; throw err; }
|
||||
);
|
||||
return inflight;
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户