first commit
This commit is contained in:
+147
@@ -0,0 +1,147 @@
|
||||
// utils/api.js
|
||||
const { get, post, put, delete: del } = require("./request");
|
||||
|
||||
// 用户相关API
|
||||
const userApi = {
|
||||
// 普通用户登录
|
||||
login: (username, password) => post("/api/user/login", { username, password }),
|
||||
|
||||
// 微信小程序一键登录
|
||||
miniLogin: (code, nickname = null) => post("/api/user/mini-login", { code, nickname }),
|
||||
|
||||
// 获取当前用户信息
|
||||
getCurrentUser: () => get("/api/user/profile", true),
|
||||
|
||||
// 更新用户信息
|
||||
updateProfile: (data) => post("/api/user/update-profile", data, true),
|
||||
|
||||
// 修改密码
|
||||
changePassword: (oldPassword, newPassword, confirmPassword) =>
|
||||
post("/api/user/change-password", {
|
||||
old_password: oldPassword,
|
||||
new_password: newPassword,
|
||||
confirm_password: confirmPassword
|
||||
}, true),
|
||||
|
||||
// 刷新token
|
||||
refreshToken: () => post("/api/user/refresh-token", {}, true),
|
||||
};
|
||||
|
||||
// 医生相关API
|
||||
const doctorApi = {
|
||||
// 获取所有医生列表
|
||||
getDoctors: () => get("/api/doctors", true),
|
||||
|
||||
// 获取单个医生详情
|
||||
getDoctorById: (id) => get(`/api/doctors/${id}`, true),
|
||||
};
|
||||
|
||||
// 手术相关API
|
||||
const surgeryApi = {
|
||||
// 获取所有手术列表,支持分页参数
|
||||
getSurgeries: (params = {}) => get("/api/surgeries", params, true),
|
||||
|
||||
// 获取单个手术详情
|
||||
getSurgeryById: (id) => get(`/api/surgeries/${id}`, true),
|
||||
|
||||
// 创建新手术记录
|
||||
createSurgery: (data) => post("/api/surgeries", data, true),
|
||||
|
||||
// 修改手术记录
|
||||
updateSurgery: (id, data) => put(`/api/surgeries/${id}`, data, true),
|
||||
|
||||
// 删除手术记录
|
||||
deleteSurgery: (id) => del(`/api/surgeries/${id}`, true),
|
||||
};
|
||||
|
||||
// 设备相关API
|
||||
const deviceApi = {
|
||||
// 获取所有设备列表
|
||||
getDevices: (params = {}) => get("/api/devices", params, true),
|
||||
|
||||
// 获取单个设备详情
|
||||
getDeviceById: (id) => get(`/api/devices/${id}`, true),
|
||||
|
||||
// 创建新设备
|
||||
createDevice: (data) => post("/api/devices", data, true),
|
||||
|
||||
// 更新设备
|
||||
updateDevice: (id, data) => put(`/api/devices/${id}`, data, true),
|
||||
|
||||
// 删除设备
|
||||
deleteDevice: (id) => del(`/api/devices/${id}`, true),
|
||||
|
||||
// 获取所有子设备列表
|
||||
getAllSubDevices: () => get("/api/subdevices", true),
|
||||
|
||||
// 获取单个子设备详情
|
||||
getSubDeviceById: (id) => get(`/api/subdevices/${id}`, true),
|
||||
|
||||
// 获取指定设备下的所有子设备
|
||||
getSubDevicesByDeviceId: (deviceId, params) => get(`/api/subdevices/device/${deviceId}`, true, params),
|
||||
|
||||
// 获取所有子设备列表
|
||||
getSubDevices: () => get("/api/subdevices", true),
|
||||
|
||||
// 创建子设备
|
||||
createSubDevice: (data) => post("/api/subdevices", data, true),
|
||||
|
||||
// 更新子设备
|
||||
updateSubDevice: (id, data) => put(`/api/subdevices/${id}`, data, true),
|
||||
|
||||
// 删除子设备
|
||||
deleteSubDevice: (id) => del(`/api/subdevices/${id}`, true),
|
||||
|
||||
// 获取设备状态
|
||||
getDeviceStatus: () => get("/api/device_status", false),
|
||||
};
|
||||
|
||||
// 设备保养相关API
|
||||
const deviceMaintenanceApi = {
|
||||
// 获取保养记录列表
|
||||
getMaintenanceList: (params = {}) => get("/api/device-maintenance", params, true),
|
||||
|
||||
// 获取单个保养记录详情
|
||||
getMaintenance: (id) => get(`/api/device-maintenance/${id}`, true),
|
||||
|
||||
// 获取指定子设备的保养记录
|
||||
getMaintenanceBySubDevice: (params = {}) => get("/api/device-maintenance/sub-device/:sub_device_id", params, true),
|
||||
|
||||
// 获取保养统计信息
|
||||
getMaintenanceStats: (params = {}) => get("/api/device-maintenance/stats", params, true),
|
||||
|
||||
// 创建新保养记录
|
||||
createMaintenance: (data) => post("/api/device-maintenance", data, true),
|
||||
|
||||
// 修改保养记录
|
||||
updateMaintenance: (id, data) => put(`/api/device-maintenance/${id}`, data, true),
|
||||
|
||||
// 删除保养记录
|
||||
deleteMaintenance: (id) => del(`/api/device-maintenance/${id}`, true),
|
||||
|
||||
// 完成保养记录
|
||||
completeMaintenance: (id) => post(`/api/device-maintenance/${id}/complete`, {}, true),
|
||||
|
||||
// 取消保养记录
|
||||
cancelMaintenance: (id) => post(`/api/device-maintenance/${id}/cancel`, {}, true),
|
||||
|
||||
// 兼容旧接口
|
||||
getMaintenanceRecords: () => get("/api/device-maintenance", true),
|
||||
getMaintenanceById: (id) => get(`/api/device-maintenance/${id}`, true),
|
||||
getMaintenanceBySubDeviceId: (subDeviceId) => get(`/api/device-maintenance/sub-device/${subDeviceId}`, true),
|
||||
};
|
||||
|
||||
// 字典相关API
|
||||
const dictionaryApi = {
|
||||
// 根据类型获取字典列表
|
||||
getDictionaryByType: (type) => get(`/api/dictionaries/type/${type}`, true),
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
userApi,
|
||||
doctorApi,
|
||||
surgeryApi,
|
||||
deviceApi,
|
||||
deviceMaintenanceApi,
|
||||
dictionaryApi,
|
||||
};
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
// utils/auth.js
|
||||
const { userApi } = require("./api");
|
||||
|
||||
/**
|
||||
* 认证相关工具函数
|
||||
*/
|
||||
class AuthUtil {
|
||||
|
||||
/**
|
||||
* 检查token是否存在
|
||||
*/
|
||||
static hasToken() {
|
||||
const token = wx.getStorageSync("token");
|
||||
return !!token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取存储的token
|
||||
*/
|
||||
static getToken() {
|
||||
const token = wx.getStorageSync("token");
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置token
|
||||
*/
|
||||
static setToken(token) {
|
||||
wx.setStorageSync("token", token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除认证信息
|
||||
*/
|
||||
static clearAuth() {
|
||||
// 停止token自动刷新
|
||||
this.stopTokenRefresh();
|
||||
|
||||
wx.removeStorageSync("token");
|
||||
wx.removeStorageSync("userInfo");
|
||||
|
||||
// 清除全局状态
|
||||
const app = getApp();
|
||||
if (app && app.globalData) {
|
||||
app.globalData.userInfo = null;
|
||||
app.globalData.isLoggedIn = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存用户信息
|
||||
*/
|
||||
static setUserInfo(userInfo) {
|
||||
wx.setStorageSync("userInfo", userInfo);
|
||||
|
||||
// 更新全局状态
|
||||
const app = getApp();
|
||||
if (app && app.globalData) {
|
||||
app.globalData.userInfo = userInfo;
|
||||
app.globalData.isLoggedIn = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
static getUserInfo() {
|
||||
const userInfo = wx.getStorageSync("userInfo");
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新token
|
||||
*/
|
||||
static async refreshToken() {
|
||||
try {
|
||||
const res = await userApi.refreshToken();
|
||||
|
||||
if ((res.code === 1 || res.code === 200) && res.data && res.data.token) {
|
||||
// 保存新token
|
||||
this.setToken(res.data.token);
|
||||
|
||||
// 如果返回了用户信息,更新用户信息
|
||||
if (res.data.user) {
|
||||
this.setUserInfo(res.data.user);
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证当前用户状态
|
||||
*/
|
||||
static async validateUser() {
|
||||
try {
|
||||
const res = await userApi.getCurrentUser();
|
||||
|
||||
if ((res.code === 1 || res.code === 200) && res.data && res.data.user) {
|
||||
// 更新用户信息
|
||||
this.setUserInfo(res.data.user);
|
||||
return true;
|
||||
} else {
|
||||
// 用户验证失败,尝试刷新token
|
||||
const refreshed = await this.refreshToken();
|
||||
if (refreshed) {
|
||||
// 刷新成功,重新验证
|
||||
return await this.validateUser();
|
||||
} else {
|
||||
// 刷新失败,清除认证信息
|
||||
this.clearAuth();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
// 如果是401错误,尝试刷新token
|
||||
if (err.message && err.message.includes("401")) {
|
||||
const refreshed = await this.refreshToken();
|
||||
if (refreshed) {
|
||||
try {
|
||||
return await this.validateUser();
|
||||
} catch (e) {
|
||||
this.clearAuth();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
this.clearAuth();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
this.clearAuth();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动token自动刷新机制
|
||||
*/
|
||||
static startTokenRefresh() {
|
||||
// 先停止之前的定时器
|
||||
this.stopTokenRefresh();
|
||||
|
||||
// 设置2天后自动刷新token(token有效期3天)
|
||||
const refreshInterval = 2 * 24 * 60 * 60 * 1000; // 2天
|
||||
|
||||
const refreshTimer = setTimeout(async () => {
|
||||
if (this.hasToken()) {
|
||||
const success = await this.refreshToken();
|
||||
|
||||
if (success) {
|
||||
// 继续设置下一次刷新
|
||||
this.startTokenRefresh();
|
||||
} else {
|
||||
// 清除存储的定时器标记
|
||||
wx.removeStorageSync("tokenRefreshTimer");
|
||||
}
|
||||
}
|
||||
}, refreshInterval);
|
||||
|
||||
// 标记定时器已启动(不存储实际ID,因为小程序环境限制)
|
||||
wx.setStorageSync("tokenRefreshTimer", Date.now());
|
||||
|
||||
// 将定时器ID存储在类的静态属性中
|
||||
this._refreshTimer = refreshTimer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止token自动刷新
|
||||
*/
|
||||
static stopTokenRefresh() {
|
||||
// 清除实际的定时器
|
||||
if (this._refreshTimer) {
|
||||
clearTimeout(this._refreshTimer);
|
||||
this._refreshTimer = null;
|
||||
}
|
||||
|
||||
// 清除存储的标记
|
||||
wx.removeStorageSync("tokenRefreshTimer");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AuthUtil;
|
||||
@@ -0,0 +1,153 @@
|
||||
// utils/request.js
|
||||
// 配置API基础URL
|
||||
// const BASE_URL = "https://api.gzshuxing.cn"; // 请根据实际API地址修改
|
||||
// const BASE_URL = "http://100.82.191.127:3000"; // 旧的API地址
|
||||
// const BASE_URL = "http://100.82.191.127:8000"; // ThinkPHP 8 API地址
|
||||
// const BASE_URL = "http://localhost:8000"
|
||||
const BASE_URL = "http://eacgh.cn:8000"
|
||||
/**
|
||||
* 封装的网络请求工具
|
||||
* @param {String} url 请求路径
|
||||
* @param {String} method 请求方法
|
||||
* @param {Object} data 请求数据
|
||||
* @param {Boolean} needAuth 是否需要携带token
|
||||
* @returns {Promise} 返回Promise对象
|
||||
*/
|
||||
const request = (url, method = "GET", data = {}, needAuth = false) => {
|
||||
// 完整请求地址
|
||||
const requestUrl = BASE_URL + url;
|
||||
|
||||
// 请求头
|
||||
const header = {
|
||||
"content-type": "application/json",
|
||||
};
|
||||
|
||||
// 如果需要token,从本地存储获取token并添加到请求头
|
||||
if (needAuth) {
|
||||
const token = wx.getStorageSync("token");
|
||||
if (token) {
|
||||
header["Authorization"] = `Bearer ${token}`;
|
||||
} else {
|
||||
// 如果是刷新token接口,允许在没有token的情况下调用
|
||||
if (url === "/api/user/refresh-token") {
|
||||
// 允许刷新token接口在没有token的情况下调用
|
||||
} else {
|
||||
// 如果需要认证但没有token,可能需要跳转到登录页
|
||||
return Promise.reject(new Error("未登录或登录已过期"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 返回Promise
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: requestUrl,
|
||||
method,
|
||||
data,
|
||||
header,
|
||||
success: (res) => {
|
||||
// 请求成功
|
||||
const { statusCode, data } = res;
|
||||
|
||||
// 如果状态码为401,需要区分是登录接口还是其他接口
|
||||
if (statusCode === 401) {
|
||||
// 如果是登录接口,直接返回数据,让业务层处理
|
||||
if (url === "/api/user/login" || url === "/api/user/mini-login") {
|
||||
resolve(data);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果是刷新token接口,也直接返回,让业务层处理
|
||||
if (url === "/api/user/refresh-token") {
|
||||
resolve(data);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果是获取用户信息接口,返回错误让登录页面处理
|
||||
if (url === "/api/user/profile") {
|
||||
reject(new Error("未登录或登录已过期"));
|
||||
return;
|
||||
}
|
||||
|
||||
// 其他接口的401状态码,视为token失效
|
||||
// 清除存储的token
|
||||
wx.removeStorageSync("token");
|
||||
wx.removeStorageSync("userInfo");
|
||||
|
||||
// 跳转到登录页
|
||||
wx.navigateTo({
|
||||
url: "/pages/login/index",
|
||||
});
|
||||
|
||||
reject(new Error("未登录或登录已过期"));
|
||||
return;
|
||||
}
|
||||
|
||||
// 其他错误状态码
|
||||
if (statusCode !== 200) {
|
||||
// 如果是登录接口,仍然让业务层处理响应数据
|
||||
if (url === "/api/user/login" || url === "/api/user/mini-login") {
|
||||
resolve(data);
|
||||
return;
|
||||
}
|
||||
reject(new Error(data.message || `请求失败,状态码:${statusCode}`));
|
||||
return;
|
||||
}
|
||||
|
||||
// 正常HTTP状态码200,但需要检查业务状态码
|
||||
if (statusCode === 200) {
|
||||
// 检查业务状态码
|
||||
if (data && data.code !== 200 && data.code !== 1) {
|
||||
// 业务错误,创建包含完整错误信息的错误对象
|
||||
const businessError = new Error(data.msg || '业务错误');
|
||||
businessError.data = data;
|
||||
businessError.code = data.code;
|
||||
reject(businessError);
|
||||
return;
|
||||
}
|
||||
|
||||
// 业务成功
|
||||
resolve(data);
|
||||
return;
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
// 请求失败
|
||||
reject(err);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 导出各种请求方法
|
||||
module.exports = {
|
||||
// GET请求 - 支持向后兼容的参数格式
|
||||
get: (url, arg2 = {}, arg3 = false) => {
|
||||
// 处理向后兼容:如果第二个参数是布尔值,则说明是旧格式 (url, needAuth)
|
||||
if (typeof arg2 === 'boolean') {
|
||||
// 旧格式: get(url, needAuth)
|
||||
const needAuth = arg2;
|
||||
return request(url, "GET", {}, needAuth);
|
||||
} else {
|
||||
// 新格式: get(url, params, needAuth)
|
||||
const params = arg2;
|
||||
const needAuth = arg3 !== false ? arg3 : false;
|
||||
|
||||
// 将参数转换为查询字符串
|
||||
const queryString = Object.keys(params).length > 0
|
||||
? '?' + Object.keys(params).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`).join('&')
|
||||
: '';
|
||||
const fullUrl = url + queryString;
|
||||
return request(fullUrl, "GET", {}, needAuth);
|
||||
}
|
||||
},
|
||||
|
||||
// POST请求
|
||||
post: (url, data, needAuth = false) => request(url, "POST", data, needAuth),
|
||||
|
||||
// PUT请求
|
||||
put: (url, data, needAuth = false) => request(url, "PUT", data, needAuth),
|
||||
|
||||
// DELETE请求
|
||||
delete: (url, needAuth = false) => request(url, "DELETE", {}, needAuth),
|
||||
};
|
||||
Reference in New Issue
Block a user