fn.js
3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* @Date: 2022-05-18 22:56:08
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-12-27 23:06:36
* @FilePath: /mlaj/src/api/fn.js
* @Description: 文件描述
*/
import axios from '@/utils/axios';
import qs from 'qs'
import { showFailToast } from 'vant';
import 'vant/es/toast/style'
/**
* 网络请求功能函数
* @param {*} api 请求axios接口
* @returns 请求成功后,获取数据
*/
export const fn = (api) => {
return api
.then(res => {
if (res.data.code === 1) {
return res.data || true;
} else {
// tslint:disable-next-line: no-console
// if (!res.data.show) return false;
// 如果是401错误,不显示错误信息,因为会自动跳转到登录页
if (res.data.code !== 401) {
showFailToast(res.data.msg);
}
return false;
}
})
.catch(err => {
// tslint:disable-next-line: no-console
console.error(err);
return false;
})
.finally(() => { // 最终执行
})
}
/**
* 网络请求功能函数(带错误处理)
* @param {*} api 请求axios接口
* @param {*} options 选项对象,包含 toast、silent_codes、default_msg、network_msg 字段
* @returns 请求成功后,获取数据
*/
export const request = async (api, options = {}) => {
const {
toast = true,
silent_codes = [401],
default_msg = '请求失败',
network_msg = '网络异常',
} = options || {}
try {
const res = await api
const payload = res && res.data ? res.data : {}
const code = payload && typeof payload.code === 'number' ? payload.code : 0
if (code === 1) {
return payload || { code: 1, data: null, msg: '' }
}
const msg = payload && payload.msg ? payload.msg : default_msg
const should_toast = toast && !silent_codes.includes(code)
if (should_toast) {
showFailToast(msg)
}
return {
code: 0,
data: payload && Object.prototype.hasOwnProperty.call(payload, 'data') ? payload.data : null,
msg,
raw_code: code,
raw: payload
}
} catch (err) {
if (toast) {
showFailToast(network_msg)
}
return {
code: 0,
data: null,
msg: network_msg,
raw_code: 0,
raw: null,
error: err
}
}
}
/**
* 七牛返回格式
* @param {*} api
* @returns
*/
export const uploadFn = (api) => {
return api
.then(res => {
if (res.status === 200) {
return res.data || true;
} else {
// tslint:disable-next-line: no-console
console.warn(res);
if (!res.data.show) return false;
showFailToast({
icon: 'close',
message: res.data.msg,
});
return false;
}
})
.catch(err => {
// tslint:disable-next-line: no-console
console.error(err);
return false;
})
}
/**
* 统一 GET/POST 不同传参形式
*/
export const fetch = {
get: function (api, params) {
return axios.get(api, { params })
},
post: function (api, params) {
return axios.post(api, params)
},
stringifyPost: function (api, params) {
return axios.post(api, qs.stringify(params))
},
basePost: function (url, data, config) {
return axios.post(url, data, config)
}
}