Blame view

service/request.js 1.95 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
  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,
508a9ba2   “wangming”   1
24
  			sslVerify: false,
290144e9   易尊强   第一次
25
26
27
  			header: {
  				'Authorization': service.getToken(),
  				// "Content-Type":"application/json"
508a9ba2   “wangming”   1
28
  				"Content-Type": "application/json;charset=UTF-8/x-www-form-urlencoded"
290144e9   易尊强   第一次
29
30
31
32
33
34
35
36
  			},
  			success: (res) => {
  				// debugger
  				if (res.data.code === 200) {
  					if (res.data.code < 0 && res.data.code) {
  						uni.showToast({
  							icon: 'none',
  							title: res.data.message,
508a9ba2   “wangming”   1
37
  							duration: 2000
290144e9   易尊强   第一次
38
39
40
41
  						});
  					} else {
  						resolve(res.data);
  					}
508a9ba2   “wangming”   1
42
43
  				} else if (res.code === 403 || res.data.code === -999 || res.data.code ===
  					500) {
bd028579   易尊强   2/28
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  					// 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'
  					// 	})
  					// })
508a9ba2   “wangming”   1
64
  
290144e9   易尊强   第一次
65
  				} else {
6c679290   易尊强   3/6a上午
66
67
68
69
70
  					uni.showToast({
  						icon: 'none',
  						title: res.data.message,
  						duration: 2000
  					});
290144e9   易尊强   第一次
71
  					resolve(res);
508a9ba2   “wangming”   1
72
  					setTimeout(() => {
6c679290   易尊强   3/6a上午
73
74
75
  						uni.reLaunch({
  							url: '/pages/login/index'
  						})
508a9ba2   “wangming”   1
76
  					}, 1000)
290144e9   易尊强   第一次
77
78
79
80
81
82
83
84
85
86
87
88
89
  				}
  
  			},
  		});
  	});
  }
  
  export default {
  	get,
  	post,
  	del,
  	put,
  	request,
508a9ba2   “wangming”   1
90
  }