You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							116 lines
						
					
					
						
							3.0 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							116 lines
						
					
					
						
							3.0 KiB
						
					
					
				| import { config } from '../config.js' | |
| import { store } from './store.js' | |
| 
 | |
| const Method = { | |
|   GET: 'GET', | |
|   POST: 'POST' | |
| } | |
| // 定义一些通用常量 | |
| const HTTPConst = { | |
|   TokenURL: '/api/miniuser/getToken' | |
| } | |
| class HTTP { | |
|   constructor() { | |
|     this.baseUrl = config.api_url | |
|   } | |
|   request = (params) => { | |
|     // 从缓存中读取 token | |
|     // let token = wx.getStorageSync(HTTPConst.TokenStoreKey) || '' | |
|     const token = store.readToken() | |
|     let url = this.baseUrl + params.url | |
|     //console.log(params.url) | |
|     if (!params.method) { | |
|       params.method = Method.GET | |
|     } | |
|     // token 拼接到 请求体中 | |
|     let data = params.data | |
|     //console.log('请求Header和Body参数:') | |
|     let header = { | |
|       'token': token, | |
|       'content-type': 'application/json', | |
|     } | |
|     //console.log(header) | |
|     //console.log(data || {}) | |
|     wx.request({ | |
|       url: url, | |
|       data: data, | |
|       header: header, | |
|       method: params.method, | |
|       dataType: 'json', | |
|       success: (res) => { | |
|         //console.log(res.data) | |
|         let code = res.statusCode.toString() | |
|         let startCode = code.charAt(0) | |
|         if (startCode == '2') { | |
|           if (res.data.code == 200 || res.data.code == 0) { | |
|             params.success && params.success(res.data) | |
|           } else if (res.data.code == 302) { | |
|             // token 无效 | |
|             this.fetchAuthToken(res => { | |
|               // 请求Token后,重新请求URL | |
|               if (res.code == 200) { | |
|                 this.request(params) | |
|               } | |
|             }) | |
|           } else { | |
|             //console.log('数据请求失败') | |
|             wx.showToast({ | |
|               title: res.data.message || '服务器错误,请稍后重试', | |
|               icon: 'none' | |
|             }) | |
|           } | |
|         } else { | |
|           //console.log('请求错误') | |
|           wx.showToast({ | |
|             title: '服务器错误!', | |
|             icon: 'none' | |
|           }) | |
|           params.error && params.error(res) | |
|         } | |
|       }, | |
|       fail: function (res) { | |
|         wx.showToast({ | |
|           title: '网络连接错误,请求稍后重试!', | |
|           icon: 'none' | |
|         }) | |
|         params.fail && params.fail(res) | |
|       }, | |
|     }) | |
|   } | |
|   // 获取 Token | |
|   fetchAuthToken (success) { | |
|     // 获取token | |
|     this._fetchWXCode().then(res => { | |
|       //console.log('啦啦啦啦凉啊') | |
|       //console.log(res) | |
|       if (!res) return | |
|       let params = { | |
|         url: HTTPConst.TokenURL, | |
|         method: Method.POST, | |
|         data: { | |
|           code: res | |
|         }, | |
|         success: (data) => { | |
|           // 将获取到的token存放起来 | |
|           const { token, userInfo } = data.result | |
|           store.saveToken(token) | |
|           store.saveUserInfo(userInfo) | |
|           success(data) | |
|         }, | |
|       } | |
|       this.request(params) | |
|     }) | |
|   } | |
|   // 获取微信code  | |
|   _fetchWXCode () { | |
|     return new Promise(resolve => { | |
|       wx.login({ | |
|         success (res) { | |
|           //console.log('获取Code成功 ' + res.code) | |
|           resolve(res.code) | |
|         } | |
|       }) | |
|     }) | |
|   } | |
| } | |
| export { HTTP, Method, HTTPConst }
 | |
| 
 |