1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- uni.addInterceptor('request', {
- invoke(args) {
- const token = uni.getStorageSync(token)
- if (token) args.headers.authorization = token;
- },
- success(args) {
- if (args.statusCode == 401) {
- // 返回登录处理
- sessionStorage.removeItem('token');
- // router.replace('/frame/login');
- }
- if (args.statusCode == 500) {
- // 错误处理
- args.data.data = { errcode: -1001, errmsg: '服务器错误' };
- }
- },
- fail(err) {
- console.log('interceptor-fail',err)
- },
- complete(res) {
- console.log('interceptor-complete',res)
- }
- })
- // 私有方法
- const request = async ({ url, method, params, data, headers }) => {
- return await uni.request({ url, method, params, data, headers });
- };
- // 定义类
- class Point {
- get(url, params, headers) {
- return request({ url, method: 'get', params, headers });
- }
- post(url, data, params, headers) {
- return request({ url, method: 'post', data, params, headers });
- }
- delete(url, params, headers) {
- return request({ url, method: 'delete', params, headers });
- }
- }
- export default new Point();
-
|