2025-11-28 16:49:36 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 全局http工具集
|
|
|
|
|
|
* YangXB 2021.11.24
|
|
|
|
|
|
* */
|
2026-01-21 17:05:30 +08:00
|
|
|
|
import { base, http } from './config';
|
2026-01-28 17:18:43 +08:00
|
|
|
|
import { getStorage, goIndexPage, isArray, setStorage, toArray, toastError } from './util';
|
2025-11-28 16:49:36 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 请求
|
|
|
|
|
|
*/
|
2026-01-21 17:05:30 +08:00
|
|
|
|
const request = (url: string, options: any, config = { showLoading: true, showError: true }) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
// 获取缓存cookie
|
2026-01-12 10:59:13 +08:00
|
|
|
|
const header: any = { ...http.header };
|
2025-11-28 16:49:36 +08:00
|
|
|
|
const cookie = getStorage(base.cookieKey);
|
|
|
|
|
|
|
2026-01-21 17:05:30 +08:00
|
|
|
|
if (cookie && !header['Cookie']) {
|
|
|
|
|
|
header['Cookie'] = cookie;
|
2025-11-28 16:49:36 +08:00
|
|
|
|
}
|
2026-01-21 17:05:30 +08:00
|
|
|
|
if (options['content-type']) {
|
|
|
|
|
|
header['content-type'] = options['content-type'];
|
2025-11-28 16:49:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
if (config.showLoading != false) {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
wx.showLoading({ title: '加载中' });
|
2025-11-28 16:49:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 17:05:30 +08:00
|
|
|
|
url = `${url}`.startsWith('http') ? url : urlAddBaseUrl(url);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
wx.request({
|
|
|
|
|
|
url: url,
|
|
|
|
|
|
method: options.method,
|
|
|
|
|
|
data: options.data,
|
|
|
|
|
|
header,
|
2026-01-12 10:59:13 +08:00
|
|
|
|
success(request: any) {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
if (config.showLoading != false) {
|
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 写入缓存
|
|
|
|
|
|
if (!cookie) {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
setStorage(base.cookieKey, request.header['Set-Cookie']);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 10:59:13 +08:00
|
|
|
|
if (request.data?.err_code === 0) {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
//
|
2026-01-21 17:05:30 +08:00
|
|
|
|
resolve(request.data);
|
|
|
|
|
|
return;
|
2025-11-28 16:49:36 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
if (config.showError != false) {
|
|
|
|
|
|
wx.showToast({
|
|
|
|
|
|
title: request.data.err_msg,
|
2026-01-21 17:05:30 +08:00
|
|
|
|
icon: 'none',
|
2025-11-28 16:49:36 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (request.data.err_code == 110000) {
|
|
|
|
|
|
const pages = getCurrentPages();
|
|
|
|
|
|
if (
|
2026-01-21 17:05:30 +08:00
|
|
|
|
!['pages/index/index', 'pages/processEntry/processEntry', 'pages/my/my'].includes(
|
|
|
|
|
|
pages[pages.length - 1].route,
|
|
|
|
|
|
)
|
2025-11-28 16:49:36 +08:00
|
|
|
|
) {
|
2026-01-12 10:59:13 +08:00
|
|
|
|
goIndexPage();
|
2025-11-28 16:49:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-21 17:05:30 +08:00
|
|
|
|
reject();
|
2025-11-28 16:49:36 +08:00
|
|
|
|
},
|
2026-01-12 10:59:13 +08:00
|
|
|
|
fail(error: any) {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
if (config.showLoading != false) {
|
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
|
}
|
|
|
|
|
|
reject({ err_code: 44444, err_msg: error.data });
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 封装get方法
|
2026-02-06 14:49:13 +08:00
|
|
|
|
export const get = (url: string, data = {}, config?: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
return request(url, { method: 'GET', data }, config);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 封装post方法
|
2026-02-06 14:49:13 +08:00
|
|
|
|
export const post = (url: string, data = {}, config?: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
return request(url, { method: 'POST', data }, config);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-12 10:59:13 +08:00
|
|
|
|
export const wxLogin = (config?: any) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
wx.login({
|
|
|
|
|
|
success: (res) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
post('Applet/code2Sess', { code: res.code, name: 'ch' }, config)
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.then((res: any) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
// 记录sessionKey
|
2026-01-21 17:05:30 +08:00
|
|
|
|
setStorage('session', {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
openid: res.openid,
|
|
|
|
|
|
unionid: res.unionid,
|
|
|
|
|
|
time: Date.now() + 1000 * 3600 * 24, // 缓存一天过期
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.catch((err: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
toastError('服务失败:' + err.err_code);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 检验微信前端登录状态
|
|
|
|
|
|
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: () => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
console.log('checkSession失效');
|
2025-11-28 16:49:36 +08:00
|
|
|
|
// 已过期,重新登录获取session_key
|
|
|
|
|
|
wxLogin();
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-28 17:18:43 +08:00
|
|
|
|
const getUsersConfigList = () => {
|
|
|
|
|
|
post('ErpConfig/ErpConfigList').then((res: any) => {
|
|
|
|
|
|
toArray(res?.data?.list).forEach((el) => {
|
|
|
|
|
|
wx.setStorageSync(el.config_type_en, el.config_value);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-11-28 16:49:36 +08:00
|
|
|
|
export const loginStatus = () => {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
post('Applet/loginStatus', {}, { showLoading: false })
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.then((res: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
setStorage('user_info', res.user_info);
|
2026-01-28 17:18:43 +08:00
|
|
|
|
setStorage('user_id', res.user_info?.user_id);
|
2026-01-21 17:05:30 +08:00
|
|
|
|
setStorage('company_info', res.company_info);
|
|
|
|
|
|
setStorage('auth_info', res.auth_info);
|
|
|
|
|
|
setStorage('session_id', res.session_id);
|
2026-01-28 17:18:43 +08:00
|
|
|
|
getUsersConfigList();
|
2025-11-28 16:49:36 +08:00
|
|
|
|
resolve(res);
|
|
|
|
|
|
})
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.catch((err: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
login('', '', 4)
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.then((res: any) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
if (isArray(res.data)) {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
post('Applet/loginOut').then(() => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
checkSesskey({ showLoading: false, showError: false })
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.then(() => {})
|
2025-11-28 16:49:36 +08:00
|
|
|
|
.catch((err) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
console.log('checkSesskey', err);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
reject(res);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resolve(res);
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => {
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-06 15:30:07 +08:00
|
|
|
|
export const loginStatusPage = (that: WechatMiniprogram.Page.Instance<any, any>) => {
|
|
|
|
|
|
that.setData({ loading: true });
|
|
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
|
|
loginStatus()
|
|
|
|
|
|
.then(() => {
|
|
|
|
|
|
that.setData({ isLogin: true, loading: false });
|
|
|
|
|
|
that?.init?.();
|
|
|
|
|
|
resolve();
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
|
that.setData({ isLogin: false, loading: false });
|
|
|
|
|
|
reject();
|
|
|
|
|
|
console.log('调用登录状态请求失败', err);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-11-28 16:49:36 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {*} config { showLoading: true, showError: true }
|
|
|
|
|
|
*/
|
2026-01-12 10:59:13 +08:00
|
|
|
|
export const checkSesskey = (config?: any) => {
|
|
|
|
|
|
return new Promise<any>((resolve, reject) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
post('Applet/checkSesskey', {}, config)
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.then((res: any) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
resolve(res);
|
|
|
|
|
|
})
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.catch((err: any) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
wxLogin(config);
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 后端登录
|
2026-01-21 17:05:30 +08:00
|
|
|
|
export const login = (encryptedData: any, iv: any, type?: any, company_id?: any) => {
|
2026-01-12 10:59:13 +08:00
|
|
|
|
return new Promise<any>((resolve, reject) => {
|
2026-01-14 16:54:47 +08:00
|
|
|
|
const data: any = { type: 2, encryptedData, iv };
|
2025-11-28 16:49:36 +08:00
|
|
|
|
|
|
|
|
|
|
if (company_id) {
|
|
|
|
|
|
data.companyID = company_id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 17:05:30 +08:00
|
|
|
|
post('Applet/login', type == 4 ? { type } : data)
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.then((res: any) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
if (isArray(res.data)) {
|
|
|
|
|
|
resolve(res);
|
|
|
|
|
|
} else {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
setStorage('user_info', res.user_info);
|
2026-01-28 17:18:43 +08:00
|
|
|
|
setStorage('user_id', res.user_info?.user_id);
|
2026-01-21 17:05:30 +08:00
|
|
|
|
setStorage('company_info', res.companys_info);
|
|
|
|
|
|
setStorage('auth_info', res.auth_info);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
loginStatus();
|
|
|
|
|
|
resolve(res);
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.catch((err: any) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
// 签名失败,重新登录
|
|
|
|
|
|
// if (err.err_code == 41444) {
|
|
|
|
|
|
// wxLogin();
|
|
|
|
|
|
// showModal("登录结果", "服务器开小差了,请重试");
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// showModal("登录结果", err.err_msg);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// wx.removeStorageSync("loginExp");
|
|
|
|
|
|
if (type == 4) {
|
|
|
|
|
|
checkSesskey()
|
2026-01-12 10:59:13 +08:00
|
|
|
|
.then((res) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
resolve(res);
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => {
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-12 10:59:13 +08:00
|
|
|
|
export const makeURL = (url: string, redirect = false, openID = false) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
return (
|
|
|
|
|
|
base.apiHost +
|
2026-01-21 17:05:30 +08:00
|
|
|
|
(redirect ? 'applet-wv?url=' : '') +
|
|
|
|
|
|
encodeURIComponent(url + (openID ? '?openID=' + wx.getStorageSync('session')['openid'] : '')) +
|
|
|
|
|
|
(redirect ? '&' : '?') +
|
|
|
|
|
|
'cookie=' +
|
2025-11-28 16:49:36 +08:00
|
|
|
|
encodeURI(wx.getStorageSync(base.cookieKey))
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-12 10:59:13 +08:00
|
|
|
|
export const urlAddBaseUrl = (url: string) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
if (typeof url == 'string') {
|
|
|
|
|
|
if (url.startsWith('/')) {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
url = url.substring(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return base.apiHost + url;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-12 10:59:13 +08:00
|
|
|
|
export const urlAddWebViewBaseUrl = (url: string) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
|
if (typeof url == 'string') {
|
|
|
|
|
|
if (url.startsWith('/')) {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
url = url.substring(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return base.webViewBaseUrl + url;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** formData请求 */
|
2026-01-12 10:59:13 +08:00
|
|
|
|
export const formDataRequest = (url: string, formData: any, config?: any) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
|
let data = formData.getData();
|
|
|
|
|
|
return request(
|
|
|
|
|
|
url,
|
|
|
|
|
|
{
|
2026-01-21 17:05:30 +08:00
|
|
|
|
method: 'POST',
|
2025-11-28 16:49:36 +08:00
|
|
|
|
data: data.buffer,
|
2026-01-21 17:05:30 +08:00
|
|
|
|
'content-type': data.contentType,
|
2025-11-28 16:49:36 +08:00
|
|
|
|
},
|
2026-01-21 17:05:30 +08:00
|
|
|
|
config,
|
2025-11-28 16:49:36 +08:00
|
|
|
|
);
|
|
|
|
|
|
};
|