Blame view

components/common/tui-request/index.js 3.36 KB
46b6767c   刘淇   init 提交到库
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
  /**
   * 网络请求
   * @author echo.
   * @version 1.6.5
   * https://www.thorui.cn/doc
   **/
  
  import base from './tui-base.js'
  import createTaskKeyStore from './tui-taskKeyStore.js'
  
  const store = createTaskKeyStore()
  
  class THORUI_INNER {
  	constructor(initConfig = {}) {
  		this.initConfig = initConfig
  		this.request = []
  		this.response = []
  		this.dispatchRequest = this.dispatchRequest.bind(this)
  		this.loading = false
  	}
  	dispatchRequest(config = {}) {
  		let params = base.mergeConfig(this.initConfig, config)
  		if (params.requestTaskKey && store.requestTaskStorage(params.requestTaskKey)) {
  			return new Promise((resolve, reject) => {
  				reject({
  					statusCode: -9999,
  					errMsg: 'request:cancelled'
  				})
  			});
  		}
  		let options = base.getOptions(params)
  		let promise = Promise.resolve(options);
  		promise = promise.then(config => {
  			if (params.showLoading && !this.loading) {
  				this.loading = true
  				base.showLoading()
  			}
  			return new Promise((resolve, reject) => {
  				let requestTask = uni.request({
  					...options,
  					success: (res) => {
  						if (params.showLoading && this.loading) {
  							this.loading = false
  							uni.hideLoading()
  						}
  						resolve(params.concise ? res.data : res)
  					},
  					fail: (err) => {
  						if (params.showLoading && this.loading) {
  							this.loading = false
  							uni.hideLoading()
  						}
  						if (params.errorMsg) {
  							base.toast(params.errorMsg)
  						}
  						reject(err)
  					},
  					complete: () => {
  						store.removeRequestTaskKey(params.requestTaskKey)
  					}
  				})
  
  				if (params.timeout && typeof params.timeout === 'number' && params.timeout > 3000) {
  					setTimeout(() => {
  						try {
  							store.removeRequestTaskKey(params.requestTaskKey)
  							requestTask.abort();
  						} catch (e) {}
  						resolve({
  							statusCode: -9999,
  							errMsg: 'request:cancelled'
  						});
  					}, params.timeout)
  				}
  			});
  		})
  		return promise
  	}
  }
  
  
  const inner = new THORUI_INNER(base.config())
  
  const http = {
  	interceptors: {
  		request: {
  			use: (fulfilled, rejected) => {
  				inner.request.push({
  					fulfilled,
  					rejected
  				})
  			},
  			eject: (name) => {
  				if (inner.request[name]) {
  					inner.request[name] = null;
  				}
  			}
  		},
  		response: {
  			use: (fulfilled, rejected) => {
  				inner.response.push({
  					fulfilled,
  					rejected
  				})
  			},
  			eject: (name) => {
  				if (inner.response[name]) {
  					inner.response[name] = null;
  				}
  			}
  		}
  	},
  	create(config) {
  		inner.initConfig = base.mergeConfig(base.config(), config, true);
  	},
  	get(url, config = {}) {
  		config.method = 'GET'
  		config.url = url || config.url || ''
  		return http.request(config)
  	},
  	post(url, config = {}) {
  		config.method = 'POST'
  		config.url = url || config.url || ''
  		return http.request(config)
  	},
f2ecd120   刘淇   树 修改
126
127
128
129
130
  	put(url, config = {}) {
  		config.method = 'put'
  		config.url = url || config.url || ''
  		return http.request(config)
  	},
46b6767c   刘淇   init 提交到库
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  	all(requests) {
  		return Promise.all(requests)
  	},
  	request(config = {}) {
  		let chain = [inner.dispatchRequest, undefined];
  		let promise = Promise.resolve(config);
  
  		inner.request.forEach(interceptor => {
  			chain.unshift(interceptor.fulfilled, interceptor.rejected);
  		});
  
  		inner.response.forEach(interceptor => {
  			chain.push(interceptor.fulfilled, interceptor.rejected);
  		});
  
  		while (chain.length) {
  			promise = promise.then(chain.shift(), chain.shift());
  		}
  
  		return promise;
  	}
  }
  export default http