修改样式兼容微信小程序
This commit is contained in:
152
utils/dialog.js
152
utils/dialog.js
@ -1,41 +1,31 @@
|
||||
// import {
|
||||
// Confirm as confirm,
|
||||
// Alert as alert,
|
||||
// Toast as toast,
|
||||
// Notify as notify,
|
||||
// Loading as loading
|
||||
// } from "vue-ydui/dist/lib.rem/dialog";
|
||||
|
||||
// import Dialog from "../../static/vant-weapp/dialog/dialog";
|
||||
// import notify from "../../static/vant-weapp/notify/notify";
|
||||
|
||||
const dialog = {
|
||||
confirm: (options) => {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: options.mes,
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
opts()
|
||||
} else if (res.cancel) {}
|
||||
}
|
||||
})
|
||||
},
|
||||
alert: null,
|
||||
// alert: Dialog.alert,
|
||||
notify: null,
|
||||
// notify,
|
||||
loading: {
|
||||
open: () => {
|
||||
// uni.showLoading({
|
||||
// title: '加载中'
|
||||
// })
|
||||
},
|
||||
close: () => {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
};
|
||||
confirm: options => {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: options.mes,
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
opts()
|
||||
} else if (res.cancel) {
|
||||
}
|
||||
},
|
||||
})
|
||||
},
|
||||
alert: null,
|
||||
// alert: Dialog.alert,
|
||||
notify: null,
|
||||
// notify,
|
||||
loading: {
|
||||
open: () => {
|
||||
// uni.showLoading({
|
||||
// title: '加载中'
|
||||
// })
|
||||
},
|
||||
close: () => {
|
||||
uni.hideLoading()
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// const icons = { error: "操作失败", success: "操作成功" };
|
||||
// Object.keys(icons).reduce((dialog, key) => {
|
||||
@ -55,49 +45,49 @@ const dialog = {
|
||||
// return dialog;
|
||||
// }, dialog);
|
||||
|
||||
dialog.message = (mes = "操作失败", obj = {}) => {
|
||||
return new Promise(function(resolve) {
|
||||
uni.showToast({
|
||||
title: mes,
|
||||
icon: "none",
|
||||
duration: 2000,
|
||||
complete: () => {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
dialog.toast = (options) => {
|
||||
uni.showToast({
|
||||
title: options.mes,
|
||||
icon: "none",
|
||||
duration: 2000,
|
||||
complete: () => {
|
||||
options.callback ? options.callback() : null
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
dialog.error = (mes) => {
|
||||
uni.showToast({
|
||||
title: mes,
|
||||
icon: "none",
|
||||
duration: 2000
|
||||
});
|
||||
};
|
||||
|
||||
dialog.validateError = (...args) => {
|
||||
validatorDefaultCatch(...args);
|
||||
};
|
||||
|
||||
export function validatorDefaultCatch(err, type = "message") {
|
||||
uni.showToast({
|
||||
title: err.errors[0].message,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
return false
|
||||
dialog.message = (mes = '操作失败', obj = {}) => {
|
||||
return new Promise(function(resolve) {
|
||||
uni.showToast({
|
||||
title: mes,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
complete: () => {
|
||||
resolve()
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default dialog;
|
||||
dialog.toast = options => {
|
||||
uni.showToast({
|
||||
title: options.mes,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
complete: () => {
|
||||
options.callback ? options.callback() : null
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
dialog.error = mes => {
|
||||
uni.showToast({
|
||||
title: mes,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
}
|
||||
|
||||
dialog.validateError = (...args) => {
|
||||
validatorDefaultCatch(...args)
|
||||
}
|
||||
|
||||
export function validatorDefaultCatch(err, type = 'message') {
|
||||
uni.showToast({
|
||||
title: err.errors[0].message,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
export default dialog
|
||||
|
110
utils/request.js
110
utils/request.js
@ -1,22 +1,21 @@
|
||||
// #ifdef H5
|
||||
// h5端
|
||||
import Fly from "flyio/dist/npm/fly";
|
||||
import Fly from 'flyio/dist/npm/fly'
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
// app端
|
||||
import Fly from "flyio/dist/npm/wx";
|
||||
import Fly from 'flyio/dist/npm/wx'
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
import Fly from "flyio/dist/npm/wx";
|
||||
import Fly from 'flyio/dist/npm/wx'
|
||||
// #endif
|
||||
|
||||
import store from "../store";
|
||||
import { handleLoginFailure } from "@/utils";
|
||||
import { VUE_APP_API_URL } from "@/config";
|
||||
import cookie from "@/utils/store/cookie";
|
||||
|
||||
import store from '../store'
|
||||
import { handleLoginFailure } from '@/utils'
|
||||
import { VUE_APP_API_URL } from '@/config'
|
||||
import cookie from '@/utils/store/cookie'
|
||||
|
||||
const fly = new Fly()
|
||||
fly.config.baseURL = VUE_APP_API_URL
|
||||
@ -25,82 +24,87 @@ fly.interceptors.response.use(
|
||||
response => {
|
||||
// console.log(response)
|
||||
// 定时刷新access-token
|
||||
return response;
|
||||
return response
|
||||
},
|
||||
error => {
|
||||
if (error.toString() == 'Error: Network Error') {
|
||||
console.log('————————')
|
||||
console.log('发送请求失败', error)
|
||||
console.log('————————')
|
||||
handleLoginFailure();
|
||||
return Promise.reject({ msg: "未登录", toLogin: true });
|
||||
handleLoginFailure()
|
||||
return Promise.reject({ msg: '未登录', toLogin: true })
|
||||
}
|
||||
if (error.status == 401) {
|
||||
console.log('————————')
|
||||
console.log('登录失效 401', error)
|
||||
console.log('————————')
|
||||
handleLoginFailure();
|
||||
return Promise.reject({ msg: "未登录", toLogin: true });
|
||||
handleLoginFailure()
|
||||
return Promise.reject({ msg: '未登录', toLogin: true })
|
||||
}
|
||||
if (error.response.data.status == 5109) {
|
||||
uni.showToast({
|
||||
title: error.response.data.msg,
|
||||
icon: "none",
|
||||
duration: 2000
|
||||
});
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
})
|
||||
}
|
||||
return Promise.reject(error);
|
||||
return Promise.reject(error)
|
||||
}
|
||||
);
|
||||
)
|
||||
|
||||
const defaultOpt = { login: true };
|
||||
const defaultOpt = { login: true }
|
||||
|
||||
function baseRequest(options) {
|
||||
|
||||
// 从缓存中获取 token 防止 token 失效后还会继续请求的情况
|
||||
const token = cookie.get('login_status');
|
||||
const token = cookie.get('login_status')
|
||||
|
||||
// 合并传参过来的 headers
|
||||
// 如果接口需要登录,携带 token 去请求
|
||||
|
||||
options.headers = {
|
||||
...options.headers
|
||||
...options.headers,
|
||||
}
|
||||
|
||||
if (options.login === true) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
Authorization: "Bearer " + token
|
||||
Authorization: 'Bearer ' + token,
|
||||
}
|
||||
}
|
||||
|
||||
// 如果需要登录才可访问的接口没有拿到 token 视为登录失效
|
||||
if (options.login === true && !token) {
|
||||
// 跳转到登录或授权页面
|
||||
handleLoginFailure();
|
||||
handleLoginFailure()
|
||||
// 提示错误信息
|
||||
return Promise.reject({ msg: "未登录", toLogin: true });
|
||||
return Promise.reject({ msg: '未登录', toLogin: true })
|
||||
}
|
||||
// 结构请求需要的参数
|
||||
const { url, params, data, login, ...option } = options
|
||||
|
||||
// 发起请求
|
||||
return fly.request(url, params || data, {
|
||||
...option
|
||||
}).then(res => {
|
||||
const data = res.data || {};
|
||||
if (res.status !== 200) {
|
||||
return Promise.reject({ msg: "请求失败", res, data });
|
||||
}
|
||||
if ([401, 403].indexOf(data.status) !== -1) {
|
||||
handleLoginFailure();
|
||||
return Promise.reject({ msg: res.data.msg, res, data, toLogin: true });
|
||||
} else if (data.status === 200) {
|
||||
return Promise.resolve(data, res);
|
||||
} else {
|
||||
return Promise.reject({ msg: res.data.msg, res, data });
|
||||
}
|
||||
});
|
||||
return fly
|
||||
.request(url, params || data, {
|
||||
...option,
|
||||
})
|
||||
.then(res => {
|
||||
const data = res.data || {}
|
||||
|
||||
if (res.status !== 200) {
|
||||
return Promise.reject({ msg: '请求失败', res, data })
|
||||
}
|
||||
console.log(data)
|
||||
if ([401, 403].indexOf(data.status) !== -1) {
|
||||
handleLoginFailure()
|
||||
return Promise.reject({ msg: res.data.msg, res, data, toLogin: true })
|
||||
} else if (data.status === 200) {
|
||||
return Promise.resolve(data, res)
|
||||
} else if (data.status == 5101) {
|
||||
return Promise.resolve(data, res)
|
||||
} else {
|
||||
return Promise.reject({ msg: res.data.msg, res, data })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,7 +112,7 @@ function baseRequest(options) {
|
||||
* 参考文档 https://www.kancloud.cn/yunye/axios/234845
|
||||
*
|
||||
*/
|
||||
const request = ["post", "put", "patch"].reduce((request, method) => {
|
||||
const request = ['post', 'put', 'patch'].reduce((request, method) => {
|
||||
/**
|
||||
*
|
||||
* @param url string 接口地址
|
||||
@ -117,14 +121,12 @@ const request = ["post", "put", "patch"].reduce((request, method) => {
|
||||
* @returns {AxiosPromise}
|
||||
*/
|
||||
request[method] = (url, data = {}, options = {}) => {
|
||||
return baseRequest(
|
||||
Object.assign({ url, data, method }, defaultOpt, options)
|
||||
);
|
||||
};
|
||||
return request;
|
||||
}, {});
|
||||
return baseRequest(Object.assign({ url, data, method }, defaultOpt, options))
|
||||
}
|
||||
return request
|
||||
}, {})
|
||||
|
||||
["get", "delete", "head"].forEach(method => {
|
||||
;['get', 'delete', 'head'].forEach(method => {
|
||||
/**
|
||||
*
|
||||
* @param url string 接口地址
|
||||
@ -133,10 +135,8 @@ const request = ["post", "put", "patch"].reduce((request, method) => {
|
||||
* @returns {AxiosPromise}
|
||||
*/
|
||||
request[method] = (url, params = {}, options = {}) => {
|
||||
return baseRequest(
|
||||
Object.assign({ url, params, method }, defaultOpt, options)
|
||||
);
|
||||
};
|
||||
});
|
||||
return baseRequest(Object.assign({ url, params, method }, defaultOpt, options))
|
||||
}
|
||||
})
|
||||
|
||||
export default request;
|
||||
export default request
|
||||
|
Reference in New Issue
Block a user