12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- /**
- * @Description:request 封装
- * @Version: 1.0.0
- * @author:苏园园
- * @Date: 2024-3-29 15:42:41
- */
- import config from './config.js'
- class Request {
- http(param) {
- let url = param.url,
- header = param.header,
- method = param.method,
- data = param.data,
- dataType = 'json',
- responseType = param.responseType || 'text'
- if (header) {
- header = param.header
- } else {
- header = {
- 'Content-type': "application/x-www-form-urlencoded",
- };
- }
- const token = uni.getStorageSync('token');
- header['Authorization'] = header['Authorization'] ? header['Authorization'] : token;
- // 返回promise
- return new Promise((resolve, reject) => {
- // 请求
- uni.request({
- url: config.baseUrl + url,
- data: data,
- method: method,
- dataType: dataType,
- responseType: responseType,
- header: header,
- success: (res) => {
- if (res.statusCode && res.statusCode != 200) {
- uni.$u.toast("api错误" + res.data.error)
- return;
- }
- if (res.data.code !== 200) {
- // code判断:200成功,不等于200错误
- if (res.data.code == 501) {
- webUni.webView.reLaunch({
- url: "/pages/index/login/login",
- });
- } else if (res.data.code == 400 && res.data.msg == '登录凭证不为空') {
- uni.$u.toast('请登录小程序~');
- setTimeout(() => {
- webUni.webView.reLaunch({
- url: "/pages/index/login/login",
- });
- }, 500)
- } else {
- uni.$u.toast(res.data.msg)
- }
- }
- // 将结果抛出
- resolve(res.data)
- },
- //请求失败
- fail: (e) => {
- uni.$u.toast('网络异常, 请检查网络连接')
- resolve(e.data);
- },
- //请求完成
- complete() {
- resolve();
- return;
- },
- })
- })
- }
- }
- export default new Request()
|