初始化项目, 添加TDesign等包
This commit is contained in:
273
miniprogram/utils/https.js
Normal file
273
miniprogram/utils/https.js
Normal file
@@ -0,0 +1,273 @@
|
||||
/**
|
||||
* 全局http工具集
|
||||
* YangXB 2021.11.24
|
||||
* */
|
||||
import { base, http } from "./config";
|
||||
import { getStorage, isArray, setStorage, ToastErr } from "./util";
|
||||
/**
|
||||
* 请求
|
||||
* @param {*} url
|
||||
* @param {*} options
|
||||
* @param {*} config
|
||||
*/
|
||||
const request = (url, options, config = { showLoading: true, showError: true }) => {
|
||||
// 获取缓存cookie
|
||||
const header = { ...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) {
|
||||
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
|
||||
)
|
||||
) {
|
||||
wx.switchTab({ url: "/pages/index/index" });
|
||||
}
|
||||
}
|
||||
}
|
||||
resolve(request.data);
|
||||
},
|
||||
fail(error) {
|
||||
if (config.showLoading != false) {
|
||||
wx.hideLoading();
|
||||
}
|
||||
reject({ err_code: 44444, err_msg: error.data });
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 封装get方法
|
||||
export const get = (url, data = {}, config) => {
|
||||
return request(url, { method: "GET", data }, config);
|
||||
};
|
||||
|
||||
// 封装post方法
|
||||
export const post = (url, data = {}, config) => {
|
||||
return request(url, { method: "POST", data }, config);
|
||||
};
|
||||
|
||||
export const wxLogin = (config) => {
|
||||
wx.login({
|
||||
success: (res) => {
|
||||
post("Applet/code2Sess", { code: res.code, name: "ch" }, config)
|
||||
.then((res) => {
|
||||
// 记录sessionKey
|
||||
setStorage("session", {
|
||||
openid: res.openid,
|
||||
unionid: res.unionid,
|
||||
time: Date.now() + 1000 * 3600 * 24, // 缓存一天过期
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
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) => {
|
||||
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) => {
|
||||
login("", "", 4)
|
||||
.then((res) => {
|
||||
if (isArray(res.data)) {
|
||||
post("Applet/loginOut").then((res) => {
|
||||
checkSesskey({ showLoading: false, showError: false })
|
||||
.then((res) => {})
|
||||
.catch((err) => {
|
||||
console.log("checkSesskey", err);
|
||||
});
|
||||
});
|
||||
reject(res);
|
||||
} else {
|
||||
resolve(res);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} config { showLoading: true, showError: true }
|
||||
*/
|
||||
export const checkSesskey = (config) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
post("Applet/checkSesskey", {}, config)
|
||||
.then((res) => {
|
||||
resolve(res);
|
||||
})
|
||||
.catch((err) => {
|
||||
wxLogin(config);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 后端登录
|
||||
export const login = (encryptedData, iv, type, company_id) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const data = {
|
||||
type: 2,
|
||||
encryptedData,
|
||||
iv,
|
||||
};
|
||||
|
||||
if (company_id) {
|
||||
data.companyID = company_id;
|
||||
}
|
||||
|
||||
post("Applet/login", type == 4 ? { type } : data)
|
||||
.then((res) => {
|
||||
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) => {
|
||||
// 签名失败,重新登录
|
||||
// if (err.err_code == 41444) {
|
||||
// wxLogin();
|
||||
// showModal("登录结果", "服务器开小差了,请重试");
|
||||
// } else {
|
||||
// showModal("登录结果", err.err_msg);
|
||||
// }
|
||||
// wx.removeStorageSync("loginExp");
|
||||
if (type == 4) {
|
||||
checkSesskey()
|
||||
.then(() => {
|
||||
resolve(res);
|
||||
})
|
||||
.catch(() => {
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const makeURL = (url, 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) => {
|
||||
if (typeof url == "string") {
|
||||
if (url.startsWith("/")) {
|
||||
url = url.substring(1);
|
||||
}
|
||||
}
|
||||
return base.apiHost + url;
|
||||
};
|
||||
|
||||
export const urlAddWebViewBaseUrl = (url) => {
|
||||
if (typeof url == "string") {
|
||||
if (url.startsWith("/")) {
|
||||
url = url.substring(1);
|
||||
}
|
||||
}
|
||||
return base.webViewBaseUrl + url;
|
||||
};
|
||||
|
||||
/** formData请求 */
|
||||
export const formDataRequest = (url, formData, config) => {
|
||||
let data = formData.getData();
|
||||
return request(
|
||||
url,
|
||||
{
|
||||
method: "POST",
|
||||
data: data.buffer,
|
||||
"content-type": data.contentType,
|
||||
},
|
||||
config
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user