Blame view

service/request.js 1.9 KB
290144e9   易尊强   第一次
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
  import Vue from 'vue'
  import service from './service.js'
  import BASE_URL from '../common/config.js'
  
  const get = function(url, data, noApiBase) {
  	return request(url, 'GET', data, noApiBase);
  }
  const post = function(url, data, noApiBase) {
  	return request(url, 'POST', data, noApiBase);
  }
  const del = function(url, data, noApiBase) {
  	return request(url, 'DELETE', data, noApiBase);
  }
  const put = function(url, data, noApiBase) {
  	return request(url, 'PUT', data, noApiBase);
  }
  const request = function(url, method, data, noApiBase) {
  	url = BASE_URL + '/api' + url;
  	return new Promise((resolve, reject) => {
  		uni.request({
  			url,
  			data,
  			method,
  			header: {
  				'Authorization': service.getToken(),
  				// "Content-Type":"application/json"
  				"Content-Type":"application/json;charset=UTF-8/x-www-form-urlencoded"
  			},
  			success: (res) => {
  				// debugger
  				if (res.data.code === 200) {
  					if (res.data.code < 0 && res.data.code) {
  						uni.showToast({
  							icon: 'none',
  							title: res.data.message,
  						 duration: 2000
  						});
  					} else {
  						resolve(res.data);
  					}
  				} else if (res.code === 403 || res.data.code === -999 || res.data.code === 500) {
  					uni.removeStorage({
  						key:'token'
  					})
  					uni.removeStorage({
  						key:'AuthToken_KEY'
  					})
  					uni.removeStorage({
  						key:'user'
  					})
  					service.addToken('');
  					service.saveUser('');
  					uni.showToast({
  						icon:'fail',
  						title:"登录过期,请重新登录",
  						duration:1500
  					}).then(res=>{
  						uni.reLaunch({
  							url: '/pages/login/index'
  						})
  					})
  					
  				} else {
  					// uni.showToast({
  					// 	icon: 'none',
  					// 	title: res.data.message,
  					// 	duration: 2000
  					// });
  					resolve(res);
  					// setTimeout(()=>{
  					// 	uni.reLaunch({
  					// 		url: '/pages/login/index'
  					// 	})
  					// },1000)
  				}
  
  			},
  		});
  	});
  }
  
  export default {
  	get,
  	post,
  	del,
  	put,
  	request,
  }