Blame view

uniapp_jiju/service/request.js 2.86 KB
d56bd957   “wangming”   修复问题,组建AI
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
  import service from './service.js'
  import BASE_URL from '../common/config.js'
  
  const get = function(url, data) {
  	return request(url, 'GET', data);
  }
  const getp = function(url, data) {
  	return request(url, 'GET');
  }
  const post = function(url, data) {
  	return request(url, 'POST', data);
  }
  const put = function(url, data) {
  	return request(url, 'put', data);
  }
  
  // const postAL = function(url, data) {
  // 	return request(url, 'POST', data, {
  // 		'accept: text/plain'
  // 	});
  // }
  const postFormData = function(url, data) {
  	return request(url, 'POST', data, {
  		'Content-Type': 'application/x-www-form-urlencoded'
  	});
  }
  const postPatchJson = function(url, data) {
  	return request(url, 'POST', data, {
  		'Content-Type': 'application/json-patch+json'
  	});
  }
  const del = function(url, data) {
  	return request(url, 'DELETE', data);
  }
  
  const request = function(url, method, data, headers) {
  	let header = {};
  	// let urlsNoAuth = ['/authentication/form'];
  	// if (!urlsNoAuth.includes(url)) {
  		// header['Authorization']=`${service.getToken()}`
  		header['Authorization'] = service.getToken();
  		header['token'] = service.getToken();
  		header['platform'] = 'wxMiniProgram';
  		
  			
  	// }
  	url = BASE_URL + url;
  	if (headers) {
  		header = Object.assign({}, header, headers)
  	}
  	return new Promise((resolve, reject) => {
  		uni.request({
  			url,
  			data,
  			method,
  			header,
  			timeout: 50000,
  			success: (res) => {
  				console.log(url,res)
  				if (res.statusCode === 200) {
  					if (res.data.code == 600 || res.data.msg == '登录过期,请重新登录') {
  						console.log('登录过期,跳转到登录界面');
  						uni.navigateTo({
  							url: '/pages/login/login'
  						});
  						uni.removeStorageSync('UserToken_KEY');
  						return;
  					}
  					if (res.data.code < 0 && res.data.message) {
  						uni.showToast({
  							icon: 'none',
  							title: res.data.data.message,
  							duration: 2000
  						});
  						reject(new Error(res.data.data.message || '请求失败'));
  					} else if ( res.data.code === 500 ) {
  						uni.showToast({
  							icon: 'none',
  							title: res.data.msg || '操作失败,请联系管理员',
  							duration: 2000
  						});
  						reject(new Error(res.data.msg || '操作失败,请联系管理员'));
  					} else {
  						resolve(res.data);
  					}
  				} else if (res.statusCode === 403 || res.statusCode === 401) {
  					service.addToken('');
  					uni.navigateTo({
  						url: '/pages/login/login'
  					});
  					reject();
  				} else {
  					reject();
  					// uni.showToast({
  					// 	icon:'none',
  					//     title: res.data.msg || '系统繁忙!',
  					//     duration: 2000
  					// });
  				}
  
  			},
  			fail: (error) => {
  				uni.hideLoading();
  				// uni.showToast({
  				// 	icon:'none',
  				//     title: '网络错误!',
  				//     duration: 2000
  				// });
  				console.log(error);
  			}
  		});
  	});
  }
  
  export default {
  	get,
  	post,
  	del,
  	request,
  	postFormData,
  	put,
  	postPatchJson,
  	getp
  }