Browse Source

Merge branch 'master' of http://git.cc-lotus.info/chubiao/yl-app

chubiao 1 year ago
parent
commit
db4a1851d8
1 changed files with 105 additions and 86 deletions
  1. 105 86
      common/request.js

+ 105 - 86
common/request.js

@@ -1,100 +1,119 @@
-import { getToken, setToken } from './auth.js'
-import { toast } from './common.js'
+import {
+	getToken,
+	setToken
+} from './auth.js'
+import {
+	toast
+} from './common.js'
 import config from '../config.js'
 
 const BASE_URL = config.service
 
 const request = config => {
-  config.header = config.header || {}
-  if (getToken()) {
-    config.header['Authorization'] = 'Bearer ' + getToken()
-    reqFunc(config)
-  } else {
-    uni.getUserInfo({ // 获取用户头像并存储
-      provider: 'weixin',
-      withCredentials: true,
-    }).then((userData) => {
-      uni.setStorageSync('avatar', userData.userInfo.avatarUrl)
-    })
-    uni.login({ // 获取用户登录凭证换取openid
-      provider: 'weixin',
-      success: (loginInfo) => {
-        uni.request({
-          method: 'POST',
-          dataType: 'json',
-          timeout: 10000,
-          url: BASE_URL + '/auth/wxlogin',
-          data: { code: loginInfo.code },
-        }).then((response) => {
-          if (response.statusCode === 200) {
-            const { code, data, msg } = response.data
-            if (data && data.openId) {
-              setToken(data.token.access_token)
-              config.header['Authorization'] = 'Bearer ' + getToken()
+	let isRequest = false
+	config.header = config.header || {}
+	if (getToken()) {
+		config.header['Authorization'] = 'Bearer ' + getToken()
+		isRequest = true
+	} else {
+		uni.getUserInfo({ // 获取用户头像并存储
+			provider: 'weixin',
+			withCredentials: true,
+		}).then((userData) => {
+			uni.setStorageSync('avatar', userData.userInfo.avatarUrl)
+		})
+		uni.login({ // 获取用户登录凭证换取openid
+			provider: 'weixin',
+			success: (loginInfo) => {
+				uni.request({
+					method: 'POST',
+					dataType: 'json',
+					timeout: 10000,
+					url: BASE_URL + '/auth/wxlogin',
+					data: {
+						code: loginInfo.code
+					},
+				}).then((response) => {
+					if (response.statusCode === 200) {
+						const {
+							code,
+							data,
+							msg
+						} = response.data
+						if (data && data.openId) {
+							setToken(data.token.access_token)
+							config.header['Authorization'] = 'Bearer ' + getToken()
 
-              reqFunc(config)
-            } else {
-              uni.showModal({
-                title: '系统提示',
-                content: '该微信还未绑定账号,请去绑定',
-                showCancel: false,
-                success: function(res) {
-                  if (res.confirm) {
-                    uni.reLaunch({
-                      url: '/pages/login/login'
-                    })
-                  }
-                }
-              });
-            }
-          } else {
-            toast('系统未知错误,请反馈给管理员')
-          }
-        })
-      }
-    })
-  }
+							isRequest = true
+						} else {
+							uni.showModal({
+								title: '系统提示',
+								content: '该微信还未绑定账号,请去绑定',
+								showCancel: false,
+								success: function(res) {
+									if (res.confirm) {
+										uni.reLaunch({
+											url: '/pages/login/login'
+										})
+									}
+								}
+							});
+						}
+					} else {
+						toast('系统未知错误,请反馈给管理员')
+					}
+				})
+			}
+		})
+	}
 
-}
 
-const reqFunc = config => {
-  return new Promise((resolve, reject) => {
-    uni.request({
-      header: config.header,
-      method: config.method.toUpperCase() || 'GET',
-      dataType: 'json',
-      timeout: config.timeout || 10000,
-      url: BASE_URL + config.url,
-      data: config.data,
-    }).then((res) => {
-      if (res.statusCode === 200) {
-        const { code, msg } = res.data
+	if (isRequest) {
+		console.log("请求", config)
+		return new Promise((resolve, reject) => {
+			uni.request({
+				header: config.header,
+				method: config.method.toUpperCase() || 'GET',
+				dataType: 'json',
+				timeout: config.timeout || 10000,
+				url: BASE_URL + config.url,
+				data: config.data,
+			}).then((res) => {
+				console.log("请求返回:", res)
+				if (res.statusCode === 200) {
+					const {
+						code,
+						msg
+					} = res.data
+
+					if (code !== 200) {
+						toast(msg)
+						reject(code)
+					}
 
-        if (code !== 200) {
-          toast(msg)
-          reject(code)
-        }
+					resolve(res.data)
+				} else {
+					toast('未知错误,请反馈给管理员')
+				}
+			}).catch(error => {
+				console.log("error", error)
+				let {
+					message
+				} = error
+				if (message === 'Network Error') {
+					message = '后端接口连接异常'
+				} else if (message.includes('timeout')) {
+					message = '系统接口请求超时'
+				} else if (message.includes('Request failed with status code')) {
+					message = '系统接口' + message.substr(message.length - 3) + '异常'
+				}
 
-        resolve(res.data)
-      } else {
-        toast('未知错误,请反馈给管理员')
-      }
-    }).catch(error => {
-      let {
-        message
-      } = error
-      if (message === 'Network Error') {
-        message = '后端接口连接异常'
-      } else if (message.includes('timeout')) {
-        message = '系统接口请求超时'
-      } else if (message.includes('Request failed with status code')) {
-        message = '系统接口' + message.substr(message.length - 3) + '异常'
-      }
+				toast(message)
+				reject(error)
+			})
+		})
+	}
 
-      toast(message)
-      reject(error)
-    })
-  })
 }
 
 export default request