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

View File

@@ -3,16 +3,20 @@
* YangXB 2021.11.24
* */
import { base, http } from "./config";
import { getStorage, isArray, setStorage, ToastErr } from "./util";
import { getStorage, goIndexPage, isArray, setStorage, ToastErr } from "./util";
/**
*
* @param {*} url
* @param {*} options
* @param {*} config
*/
const request = (url, options, config = { showLoading: true, showError: true }) => {
const request = (
url: string,
options: any,
config = { showLoading: true, showError: true }
) => {
// 获取缓存cookie
const header = { ...http.header };
const header: any = { ...http.header };
const cookie = getStorage(base.cookieKey);
if (cookie && !header["Cookie"]) {
@@ -33,7 +37,7 @@ const request = (url, options, config = { showLoading: true, showError: true })
method: options.method,
data: options.data,
header,
success(request) {
success(request: any) {
if (config.showLoading != false) {
wx.hideLoading();
}
@@ -42,7 +46,7 @@ const request = (url, options, config = { showLoading: true, showError: true })
setStorage(base.cookieKey, request.header["Set-Cookie"]);
}
if (request.data.err_code === 0) {
if (request.data?.err_code === 0) {
//
} else {
if (config.showError != false) {
@@ -54,17 +58,19 @@ const request = (url, options, config = { showLoading: true, showError: true })
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
)
![
"pages/index/index",
"pages/processEntry/processEntry",
"pages/my/my",
].includes(pages[pages.length - 1].route)
) {
wx.switchTab({ url: "/pages/index/index" });
goIndexPage();
}
}
}
resolve(request.data);
},
fail(error) {
fail(error: any) {
if (config.showLoading != false) {
wx.hideLoading();
}
@@ -75,20 +81,20 @@ const request = (url, options, config = { showLoading: true, showError: true })
};
// 封装get方法
export const get = (url, data = {}, config) => {
export const get = (url: string, data = {}, config?: any): any => {
return request(url, { method: "GET", data }, config);
};
// 封装post方法
export const post = (url, data = {}, config) => {
export const post = (url: string, data = {}, config?: any): any => {
return request(url, { method: "POST", data }, config);
};
export const wxLogin = (config) => {
export const wxLogin = (config?: any) => {
wx.login({
success: (res) => {
post("Applet/code2Sess", { code: res.code, name: "ch" }, config)
.then((res) => {
.then((res: any) => {
// 记录sessionKey
setStorage("session", {
openid: res.openid,
@@ -96,7 +102,7 @@ export const wxLogin = (config) => {
time: Date.now() + 1000 * 3600 * 24, // 缓存一天过期
});
})
.catch((err) => {
.catch((err: any) => {
ToastErr("服务失败:" + err.err_code);
});
},
@@ -133,20 +139,20 @@ export const checkSession = () => {
export const loginStatus = () => {
return new Promise((resolve, reject) => {
post("Applet/loginStatus", {}, { showLoading: false })
.then((res) => {
.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) => {
.catch((err: any) => {
login("", "", 4)
.then((res) => {
.then((res: any) => {
if (isArray(res.data)) {
post("Applet/loginOut").then((res) => {
post("Applet/loginOut").then(() => {
checkSesskey({ showLoading: false, showError: false })
.then((res) => {})
.then(() => {})
.catch((err) => {
console.log("checkSesskey", err);
});
@@ -167,13 +173,13 @@ export const loginStatus = () => {
*
* @param {*} config { showLoading: true, showError: true }
*/
export const checkSesskey = (config) => {
return new Promise((resolve, reject) => {
export const checkSesskey = (config?: any) => {
return new Promise<any>((resolve, reject) => {
post("Applet/checkSesskey", {}, config)
.then((res) => {
.then((res: any) => {
resolve(res);
})
.catch((err) => {
.catch((err: any) => {
wxLogin(config);
reject(err);
});
@@ -181,9 +187,14 @@ export const checkSesskey = (config) => {
};
// 后端登录
export const login = (encryptedData, iv, type, company_id) => {
return new Promise((resolve, reject) => {
const data = {
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,
@@ -194,7 +205,7 @@ export const login = (encryptedData, iv, type, company_id) => {
}
post("Applet/login", type == 4 ? { type } : data)
.then((res) => {
.then((res: any) => {
if (isArray(res.data)) {
resolve(res);
} else {
@@ -205,7 +216,7 @@ export const login = (encryptedData, iv, type, company_id) => {
resolve(res);
}
})
.catch((err) => {
.catch((err: any) => {
// 签名失败,重新登录
// if (err.err_code == 41444) {
// wxLogin();
@@ -216,7 +227,7 @@ export const login = (encryptedData, iv, type, company_id) => {
// wx.removeStorageSync("loginExp");
if (type == 4) {
checkSesskey()
.then(() => {
.then((res) => {
resolve(res);
})
.catch(() => {
@@ -229,18 +240,20 @@ export const login = (encryptedData, iv, type, company_id) => {
});
};
export const makeURL = (url, redirect = false, openID = false) => {
export const makeURL = (url: string, redirect = false, openID = false) => {
return (
base.apiHost +
(redirect ? "applet-wv?url=" : "") +
encodeURIComponent(url + (openID ? "?openID=" + wx.getStorageSync("session")["openid"] : "")) +
encodeURIComponent(
url + (openID ? "?openID=" + wx.getStorageSync("session")["openid"] : "")
) +
(redirect ? "&" : "?") +
"cookie=" +
encodeURI(wx.getStorageSync(base.cookieKey))
);
};
export const urlAddBaseUrl = (url) => {
export const urlAddBaseUrl = (url: string) => {
if (typeof url == "string") {
if (url.startsWith("/")) {
url = url.substring(1);
@@ -249,7 +262,7 @@ export const urlAddBaseUrl = (url) => {
return base.apiHost + url;
};
export const urlAddWebViewBaseUrl = (url) => {
export const urlAddWebViewBaseUrl = (url: string) => {
if (typeof url == "string") {
if (url.startsWith("/")) {
url = url.substring(1);
@@ -259,7 +272,7 @@ export const urlAddWebViewBaseUrl = (url) => {
};
/** formData请求 */
export const formDataRequest = (url, formData, config) => {
export const formDataRequest = (url: string, formData: any, config?: any) => {
let data = formData.getData();
return request(
url,

View File

@@ -1,54 +0,0 @@
/** 首页菜单 */
const iconColor = "#0052D9";
export const menuConfig = [
{
title: "订单管理",
icon: "form",
iconColor: iconColor,
children: [
{ title: "订单列表", url: "/pages/orders/ordersList/ordersList", auth: "SF_VIEW_ORDERS" },
{ title: "流程管理", url: "/pages/orders/processManage/processManage", auth: "SF_VIEW_ORDER_PROCESS_MANAGE" },
{ title: "录入流程", url: "/pages/processEntry/processEntry", auth: "SF_ENTER_PROCESS" },
{ title: "工量查询", url: "/pages/orders/workload/workload", auth: "SF_VIEW_WORK_LIST" },
{ title: "订单备忘", url: "/pages/orders/orderMemo/orderMemo", auth: "SF_VIEW_ORDER_MEMO" },
{ title: "订单排序", url: "/pages/orders/ordersSort/ordersSort", auth: "SF_SET_ORDER_SORT" },
{ title: "板号查询", url: "/pages/orders/sale_no_query/sale_no_query", auth: "SF_VIEW_ORDER_SALE_NO_PROCESS" },
],
},
{
title: "客户管理",
icon: "user-vip",
iconColor: iconColor,
children: [
{ title: "供应商管理", url: "/pages/crm_manage/suppliers/suppliers", auth: "SF_VIEW_CRM_SUPPLIER" },
{ title: "经销商管理", url: "/pages/crm_manage/sales/sales", auth: "SF_VIEW_CRM_SALE" },
{ title: "经销商等级", url: "/pages/crm_manage/crm_level/crm_level", auth: "SF_VIEW_CRM_LEVEL" },
],
},
];
const iconPath = "/images/nav_icons/";
/** 导航栏 */
export const navTabBar = [
{
iconPath: `${iconPath}home.svg`,
selectedIconPath: `${iconPath}home2.svg`,
pagePath: "/pages/index/index",
text: "首页",
key: "1",
},
{
iconPath: `${iconPath}code.svg`,
selectedIconPath: `${iconPath}code2.svg`,
pagePath: "/pages/processEntry/processEntry",
text: "扫码",
key: "2",
},
{
iconPath: `${iconPath}my.svg`,
selectedIconPath: `${iconPath}my2.svg`,
pagePath: "/pages/my/my",
text: "我的",
key: "3",
},
];

View File

@@ -0,0 +1,94 @@
/** 首页菜单 */
const iconColor = "#0052D9";
export const menuConfig = [
{
title: "订单管理",
icon: "form",
iconColor: iconColor,
children: [
{
title: "订单列表",
url: "/pages/orders/ordersList/ordersList",
auth: "SF_VIEW_ORDERS",
},
{
title: "流程管理",
url: "/pages/orders/processManage/processManage",
auth: "SF_VIEW_ORDER_PROCESS_MANAGE",
},
{
title: "录入流程",
url: "/pages/processEntry/processEntry",
auth: "SF_ENTER_PROCESS",
},
{
title: "工量查询",
url: "/pages/orders/workload/workload",
auth: "SF_VIEW_WORK_LIST",
},
{
title: "订单备忘",
url: "/pages/orders/orderMemo/orderMemo",
auth: "SF_VIEW_ORDER_MEMO",
},
{
title: "订单排序",
url: "/pages/orders/ordersSort/ordersSort",
auth: "SF_SET_ORDER_SORT",
},
{
title: "板号查询",
url: "/pages/orders/sale_no_query/sale_no_query",
auth: "SF_VIEW_ORDER_SALE_NO_PROCESS",
},
],
},
{
title: "客户管理",
icon: "user-vip",
iconColor: iconColor,
children: [
{
title: "供应商管理",
url: "/pages/crm_manage/suppliers/suppliers",
auth: "SF_VIEW_CRM_SUPPLIER",
},
{
title: "经销商管理",
url: "/pages/crm_manage/sales/sales",
auth: "SF_VIEW_CRM_SALE",
},
{
title: "经销商等级",
url: "/pages/crm_manage/crm_level/crm_level",
auth: "SF_VIEW_CRM_LEVEL",
},
],
},
];
const iconPath = "/images/nav_icons/";
/** 导航栏 */
export const navTabBar = [
{
iconPath: `${iconPath}home.svg`,
selectedIconPath: `${iconPath}home2.svg`,
pagePath: "/pages/index/index",
text: "首页",
key: "1",
},
{
iconPath: `${iconPath}code.svg`,
selectedIconPath: `${iconPath}code2.svg`,
pagePath: "/pages/processEntry/processEntry",
text: "扫码",
key: "2",
},
{
iconPath: `${iconPath}my.svg`,
selectedIconPath: `${iconPath}my2.svg`,
pagePath: "/pages/my/my",
text: "我的",
key: "3",
},
];

View File

@@ -1,6 +1,6 @@
import { post, urlAddBaseUrl } from "./https";
const playVoice = (src, text) => {
const playVoice = (src: string, text: string) => {
const innerAudioContext = wx.createInnerAudioContext({
useWebAudioImplement: true,
});
@@ -17,7 +17,7 @@ const playVoice = (src, text) => {
});
};
export const voiceRequest = (text, cacheVoice) => {
export const voiceRequest = (text: string, cacheVoice?: boolean) => {
// if (cacheVoice) {
// const data = wx.getStorageSync(text);
// console.log(data);
@@ -53,7 +53,7 @@ export const voiceRequest = (text, cacheVoice) => {
vol: 9,
},
{ showLoading: false }
).then((res) => {
).then((res: any) => {
if (res.err_code == 0) {
playVoice(urlAddBaseUrl(res.data), text);
// if (cacheVoice) {