feat: 将多个页面从JavaScript转换为TypeScript

- 将 agreement.js、no-auth-plugin.js、processEntry.js、https.js、
  menuConfig.js、voiceUtil.js 文件重命名为对应的 .ts 扩展名

- 在 agreement.ts 中引入配置并动态设置标题
- 在 no-auth-plugin.ts 中使用工具函数替代硬编码跳转
- 为 processEntry.ts 添加类型注解并重构代码结构
- 为 https.ts 添加类型定义并优化错误处理
- 创建 menuConfig.ts 并迁移导航配置
- 为 voiceUtil.ts 添加类型注解
This commit is contained in:
zhengw
2026-01-12 10:59:13 +08:00
parent d36c005100
commit f0c68126dc
7 changed files with 206 additions and 145 deletions

286
miniprogram/utils/https.ts Normal file
View File

@@ -0,0 +1,286 @@
/**
* 全局http工具集
* YangXB 2021.11.24
* */
import { base, http } from "./config";
import { getStorage, goIndexPage, isArray, setStorage, ToastErr } from "./util";
/**
* 请求
* @param {*} url
* @param {*} options
* @param {*} config
*/
const request = (
url: string,
options: any,
config = { showLoading: true, showError: true }
) => {
// 获取缓存cookie
const header: any = { ...http.header };
const cookie = getStorage(base.cookieKey);
if (cookie && !header["Cookie"]) {
header["Cookie"] = cookie;
}
if (options["content-type"]) {
header["content-type"] = options["content-type"];
}
return new Promise((resolve, reject) => {
if (config.showLoading != false) {
wx.showLoading({ title: "加载中" });
}
url = `${url}`.startsWith("http") ? url : urlAddBaseUrl(url);
wx.request({
url: url,
method: options.method,
data: options.data,
header,
success(request: any) {
if (config.showLoading != false) {
wx.hideLoading();
}
// 写入缓存
if (!cookie) {
setStorage(base.cookieKey, request.header["Set-Cookie"]);
}
if (request.data?.err_code === 0) {
//
} else {
if (config.showError != false) {
wx.showToast({
title: request.data.err_msg,
icon: "none",
});
}
if (request.data.err_code == 110000) {
const pages = getCurrentPages();
if (
![
"pages/index/index",
"pages/processEntry/processEntry",
"pages/my/my",
].includes(pages[pages.length - 1].route)
) {
goIndexPage();
}
}
}
resolve(request.data);
},
fail(error: any) {
if (config.showLoading != false) {
wx.hideLoading();
}
reject({ err_code: 44444, err_msg: error.data });
},
});
});
};
// 封装get方法
export const get = (url: string, data = {}, config?: any): any => {
return request(url, { method: "GET", data }, config);
};
// 封装post方法
export const post = (url: string, data = {}, config?: any): any => {
return request(url, { method: "POST", data }, config);
};
export const wxLogin = (config?: any) => {
wx.login({
success: (res) => {
post("Applet/code2Sess", { code: res.code, name: "ch" }, config)
.then((res: any) => {
// 记录sessionKey
setStorage("session", {
openid: res.openid,
unionid: res.unionid,
time: Date.now() + 1000 * 3600 * 24, // 缓存一天过期
});
})
.catch((err: any) => {
ToastErr("服务失败:" + err.err_code);
});
},
});
};
// 检验微信前端登录状态
export const checkSession = () => {
wx.checkSession({
// 没有过期
success: () => {
// const session = wx.getStorageSync("session");
// console.log("checkSession生效", session);
// // 没有缓存,或者缓存已过期
// if (!session) {
// console.log("session缓存已不存在");
// wxLogin();
// } else {
// const time = session.time || 0;
// if (Date.now() > time) {
// console.log("session缓存已过期");
// wxLogin();
// }
// }
},
fail: () => {
console.log("checkSession失效");
// 已过期重新登录获取session_key
wxLogin();
},
});
};
export const loginStatus = () => {
return new Promise((resolve, reject) => {
post("Applet/loginStatus", {}, { showLoading: false })
.then((res: any) => {
setStorage("user_info", res.user_info);
setStorage("company_info", res.company_info);
setStorage("auth_info", res.auth_info);
setStorage("session_id", res.session_id);
resolve(res);
})
.catch((err: any) => {
login("", "", 4)
.then((res: any) => {
if (isArray(res.data)) {
post("Applet/loginOut").then(() => {
checkSesskey({ showLoading: false, showError: false })
.then(() => {})
.catch((err) => {
console.log("checkSesskey", err);
});
});
reject(res);
} else {
resolve(res);
}
})
.catch(() => {
reject(err);
});
});
});
};
/**
*
* @param {*} config { showLoading: true, showError: true }
*/
export const checkSesskey = (config?: any) => {
return new Promise<any>((resolve, reject) => {
post("Applet/checkSesskey", {}, config)
.then((res: any) => {
resolve(res);
})
.catch((err: any) => {
wxLogin(config);
reject(err);
});
});
};
// 后端登录
export const login = (
encryptedData: any,
iv: any,
type: any,
company_id?: any
) => {
return new Promise<any>((resolve, reject) => {
const data: any = {
type: 2,
encryptedData,
iv,
};
if (company_id) {
data.companyID = company_id;
}
post("Applet/login", type == 4 ? { type } : data)
.then((res: any) => {
if (isArray(res.data)) {
resolve(res);
} else {
setStorage("user_info", res.user_info);
setStorage("company_info", res.companys_info);
setStorage("auth_info", res.auth_info);
loginStatus();
resolve(res);
}
})
.catch((err: any) => {
// 签名失败,重新登录
// if (err.err_code == 41444) {
// wxLogin();
// showModal("登录结果", "服务器开小差了,请重试");
// } else {
// showModal("登录结果", err.err_msg);
// }
// wx.removeStorageSync("loginExp");
if (type == 4) {
checkSesskey()
.then((res) => {
resolve(res);
})
.catch(() => {
reject(err);
});
} else {
reject(err);
}
});
});
};
export const makeURL = (url: string, redirect = false, openID = false) => {
return (
base.apiHost +
(redirect ? "applet-wv?url=" : "") +
encodeURIComponent(
url + (openID ? "?openID=" + wx.getStorageSync("session")["openid"] : "")
) +
(redirect ? "&" : "?") +
"cookie=" +
encodeURI(wx.getStorageSync(base.cookieKey))
);
};
export const urlAddBaseUrl = (url: string) => {
if (typeof url == "string") {
if (url.startsWith("/")) {
url = url.substring(1);
}
}
return base.apiHost + url;
};
export const urlAddWebViewBaseUrl = (url: string) => {
if (typeof url == "string") {
if (url.startsWith("/")) {
url = url.substring(1);
}
}
return base.webViewBaseUrl + url;
};
/** formData请求 */
export const formDataRequest = (url: string, formData: any, config?: any) => {
let data = formData.getData();
return request(
url,
{
method: "POST",
data: data.buffer,
"content-type": data.contentType,
},
config
);
};