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
| import {
| HTTP_REQUEST_URL,
| HEADER,
| TOKENNAME,
| HEADERPARAMS
| } from '@/config/app';
| import {
| toLogin,
| checkLogin
| } from '../libs/login';
| import store from '../store';
|
| function filterData(data) {
| if (!data) return {}
| if (Array.isArray(data)) return data
| if (typeof data == 'object') {
| let result = {}
| Object.keys(data).forEach(key => {
| if (data[key] || data[key] == 0) {
| result[key] = data[key]
| }
| })
| return result
| }
| }
| /**
| * 发送请求
| */
| function baseRequest(url, method, data, {
| noAuth = false,
| noVerify = false
| }, params, isForm) {
| let Url = HTTP_REQUEST_URL,
| header = HEADER
| if (params != undefined) {
| header = HEADERPARAMS;
| }
| console.log(123123123123123123, url)
| if (!noAuth) {
| //登录过期自动登录
| if (!store.state.app.token && !checkLogin()) {
| console.log('needAuth', url)
| uni.showToast({
| 'title': '未登录'
| });
| return Promise.reject({
| msg: '未登录'
| });
| }
| }
| if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
| if (isForm) {
| header['content-type'] = 'multipart/form-data'
| }
| return new Promise((resolve, reject) => {
| Url = HTTP_REQUEST_URL || 'http://api.front.hdq.xbdzz.cn';
| let app = getApp();
| uni.request({
| url: Url + '/api/front/' + url,
| method: method || 'GET',
| header: header,
| data: filterData(data),
| success: (res) => {
| // if(url.indexOf('pre/order')>=0){
| // res.data.code=601;
| // }
| if (url.indexOf('gio/log') >= 0) {
| resolve(res.data, res);
| } else if (noVerify) {
| resolve(res.data, res);
| } else if (res.data.code == 200) {
| resolve(res.data, res);
| } else if ([410000, 410001, 410002, 401].indexOf(res.data.code) !== -1) {
| //接口返回需要登录
| console.log('需要登录 url ' + url, res.data)
| uni.$emit('needLogin');
| uni.showToast({
| 'title': '未登录'
| });
| reject(res.data);
| } else {
| if (app) {
| uni.$emit("showTips", {
| 'title': res.data.message || '系统错误',
| 'icon': 'none'
| });
| } else {
| uni.showToast({
| 'icon': 'none',
| 'title': res.data.message || '系统错误'
| });
| }
| console.log('系统错误 url ' + url, res.data)
| reject(res.data);
| }
| },
| fail: (msg) => {
| console.log(url, msg);
| if (app) {
| uni.$emit("showTips", {
| 'title': '请求失败',
| 'icon': 'none'
| });
| } else {
| uni.showToast({
| 'icon': 'none',
| 'title': '请求失败'
| });
| }
| reject('请求失败');
| }
| })
| });
| }
|
| const request = {};
|
| ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
| request[method] = (api, data, opt, params, isForm) => baseRequest(api, method, data, opt || {}, params,
| isForm)
| });
|
|
|
| export default request;
|
|