fn.js
2.16 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
/*
* @Date: 2022-05-18 22:56:08
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-16 21:56:59
* @FilePath: /lls_program/src/api/fn.js
* @Description: 文件描述
*/
import axios from '@/utils/request';
import Taro from '@tarojs/taro'
// import qs from 'qs'
/**
* 网络请求功能函数
* @param {*} api 请求axios接口
* @param {Object} options 配置选项
* @param {boolean} options.silent 是否静默处理错误,不显示错误提示弹框
* @returns 请求成功后,获取数据
*/
export const fn = (api, options = {}) => {
const { silent = false } = options;
return api
.then(res => {
if (res.data.code) {
return res.data || true;
} else {
// tslint:disable-next-line: no-console
console.warn(res);
// 只有在非静默模式下才显示错误提示
if (!silent) {
Taro.showToast({
title: res?.data?.msg || '请求失败',
icon: 'none',
duration: 2000
});
}
return false;
}
})
.catch(err => {
// tslint:disable-next-line: no-console
console.error(err);
return false;
})
.finally(() => { // 最终执行
})
}
/**
* 七牛返回格式
* @param {*} api
* @returns
*/
export const uploadFn = (api) => {
return api
.then(res => {
if (res.statusText === 'OK') {
return res.data || true;
} else {
// tslint:disable-next-line: no-console
console.warn(res);
Taro.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
});
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)
}
}