添加组件及页面
This commit is contained in:
@@ -2,39 +2,29 @@
|
||||
* 全局http工具集
|
||||
* YangXB 2021.11.24
|
||||
* */
|
||||
import { base, http } from "./config";
|
||||
import {
|
||||
getStorage,
|
||||
goIndexPage,
|
||||
isArray,
|
||||
setStorage,
|
||||
toastError,
|
||||
} from "./util";
|
||||
import { base, http } from './config';
|
||||
import { getStorage, goIndexPage, isArray, setStorage, toastError } from './util';
|
||||
/**
|
||||
* 请求
|
||||
*/
|
||||
const request = (
|
||||
url: string,
|
||||
options: any,
|
||||
config = { showLoading: true, showError: true }
|
||||
) => {
|
||||
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 (cookie && !header['Cookie']) {
|
||||
header['Cookie'] = cookie;
|
||||
}
|
||||
if (options["content-type"]) {
|
||||
header["content-type"] = options["content-type"];
|
||||
if (options['content-type']) {
|
||||
header['content-type'] = options['content-type'];
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (config.showLoading != false) {
|
||||
wx.showLoading({ title: "加载中" });
|
||||
wx.showLoading({ title: '加载中' });
|
||||
}
|
||||
|
||||
url = `${url}`.startsWith("http") ? url : urlAddBaseUrl(url);
|
||||
url = `${url}`.startsWith('http') ? url : urlAddBaseUrl(url);
|
||||
wx.request({
|
||||
url: url,
|
||||
method: options.method,
|
||||
@@ -46,32 +36,32 @@ const request = (
|
||||
}
|
||||
// 写入缓存
|
||||
if (!cookie) {
|
||||
setStorage(base.cookieKey, request.header["Set-Cookie"]);
|
||||
setStorage(base.cookieKey, request.header['Set-Cookie']);
|
||||
}
|
||||
|
||||
if (request.data?.err_code === 0) {
|
||||
//
|
||||
resolve(request.data);
|
||||
return;
|
||||
} else {
|
||||
if (config.showError != false) {
|
||||
wx.showToast({
|
||||
title: request.data.err_msg,
|
||||
icon: "none",
|
||||
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)
|
||||
!['pages/index/index', 'pages/processEntry/processEntry', 'pages/my/my'].includes(
|
||||
pages[pages.length - 1].route,
|
||||
)
|
||||
) {
|
||||
goIndexPage();
|
||||
}
|
||||
}
|
||||
}
|
||||
resolve(request.data);
|
||||
reject();
|
||||
},
|
||||
fail(error: any) {
|
||||
if (config.showLoading != false) {
|
||||
@@ -85,28 +75,28 @@ const request = (
|
||||
|
||||
// 封装get方法
|
||||
export const get = (url: string, data = {}, config?: any): any => {
|
||||
return request(url, { method: "GET", data }, config);
|
||||
return request(url, { method: 'GET', data }, config);
|
||||
};
|
||||
|
||||
// 封装post方法
|
||||
export const post = (url: string, data = {}, config?: any): any => {
|
||||
return request(url, { method: "POST", data }, config);
|
||||
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)
|
||||
post('Applet/code2Sess', { code: res.code, name: 'ch' }, config)
|
||||
.then((res: any) => {
|
||||
// 记录sessionKey
|
||||
setStorage("session", {
|
||||
setStorage('session', {
|
||||
openid: res.openid,
|
||||
unionid: res.unionid,
|
||||
time: Date.now() + 1000 * 3600 * 24, // 缓存一天过期
|
||||
});
|
||||
})
|
||||
.catch((err: any) => {
|
||||
toastError("服务失败:" + err.err_code);
|
||||
toastError('服务失败:' + err.err_code);
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -132,7 +122,7 @@ export const checkSession = () => {
|
||||
// }
|
||||
},
|
||||
fail: () => {
|
||||
console.log("checkSession失效");
|
||||
console.log('checkSession失效');
|
||||
// 已过期,重新登录获取session_key
|
||||
wxLogin();
|
||||
},
|
||||
@@ -141,23 +131,23 @@ export const checkSession = () => {
|
||||
|
||||
export const loginStatus = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
post("Applet/loginStatus", {}, { showLoading: false })
|
||||
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);
|
||||
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)
|
||||
login('', '', 4)
|
||||
.then((res: any) => {
|
||||
if (isArray(res.data)) {
|
||||
post("Applet/loginOut").then(() => {
|
||||
post('Applet/loginOut').then(() => {
|
||||
checkSesskey({ showLoading: false, showError: false })
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
console.log("checkSesskey", err);
|
||||
console.log('checkSesskey', err);
|
||||
});
|
||||
});
|
||||
reject(res);
|
||||
@@ -178,7 +168,7 @@ export const loginStatus = () => {
|
||||
*/
|
||||
export const checkSesskey = (config?: any) => {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
post("Applet/checkSesskey", {}, config)
|
||||
post('Applet/checkSesskey', {}, config)
|
||||
.then((res: any) => {
|
||||
resolve(res);
|
||||
})
|
||||
@@ -190,12 +180,7 @@ export const checkSesskey = (config?: any) => {
|
||||
};
|
||||
|
||||
// 后端登录
|
||||
export const login = (
|
||||
encryptedData: any,
|
||||
iv: any,
|
||||
type: any,
|
||||
company_id?: any
|
||||
) => {
|
||||
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 };
|
||||
|
||||
@@ -203,14 +188,14 @@ export const login = (
|
||||
data.companyID = company_id;
|
||||
}
|
||||
|
||||
post("Applet/login", type == 4 ? { type } : data)
|
||||
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);
|
||||
setStorage('user_info', res.user_info);
|
||||
setStorage('company_info', res.companys_info);
|
||||
setStorage('auth_info', res.auth_info);
|
||||
loginStatus();
|
||||
resolve(res);
|
||||
}
|
||||
@@ -242,19 +227,17 @@ export const login = (
|
||||
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=" +
|
||||
(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("/")) {
|
||||
if (typeof url == 'string') {
|
||||
if (url.startsWith('/')) {
|
||||
url = url.substring(1);
|
||||
}
|
||||
}
|
||||
@@ -262,8 +245,8 @@ export const urlAddBaseUrl = (url: string) => {
|
||||
};
|
||||
|
||||
export const urlAddWebViewBaseUrl = (url: string) => {
|
||||
if (typeof url == "string") {
|
||||
if (url.startsWith("/")) {
|
||||
if (typeof url == 'string') {
|
||||
if (url.startsWith('/')) {
|
||||
url = url.substring(1);
|
||||
}
|
||||
}
|
||||
@@ -276,10 +259,10 @@ export const formDataRequest = (url: string, formData: any, config?: any) => {
|
||||
return request(
|
||||
url,
|
||||
{
|
||||
method: "POST",
|
||||
method: 'POST',
|
||||
data: data.buffer,
|
||||
"content-type": data.contentType,
|
||||
'content-type': data.contentType,
|
||||
},
|
||||
config
|
||||
config,
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user