浏览代码

登录功能1.0

nanMing 1 年之前
父节点
当前提交
2b30afb6f5
共有 3 个文件被更改,包括 104 次插入82 次删除
  1. 2 8
      App.vue
  2. 90 69
      common/request.js
  3. 12 5
      pages/login/login.vue

+ 2 - 8
App.vue

@@ -1,14 +1,8 @@
 <script>
+  import { login } from '@/api/login.js'
+
   export default {
     onLaunch: () => {
-      const openid = uni.getStorageSync('openid') || null
-      if (!openid) {
-        console.log('openid', openid);
-        uni.reLaunch({
-          url: '/pages/login/login'
-        })
-      }
-      // uni.hideTabBar()
       console.log('App Launch')
     },
     onShow: () => {

+ 90 - 69
common/request.js

@@ -1,85 +1,106 @@
 import {
-	getToken
+  getToken,
+  setToken
 } from './auth.js'
 import {
-	toast
+  toast
 } from './common.js'
+import { login } from '@/api/login.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()
-	}
-	//  else {
-	//   config.header['Authorization'] = 'Bearer ' +
-	//     'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjo3MDA0NDMsInVzZXJfa2V5IjoiNjFlMDVkMWItODY1Zi00MGIwLThiOWItM2VjY2I0ZGEzYjAwIiwidXNlcm5hbWUiOiJ5bGpnMjIwMjgyMDAzMiJ9.K16sZmP0X54BsIt5dpPBm5Jw8VLoQR3Vt1VJ8IpdRNChDwZNthxfCBAKPkUAilFtb5NuBIppH54dktTPSV5mNA'
-	// }
+  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()
 
-	uni.showLoading({
-		title: '请求中...'
-	})
-	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)
-			const code = res.statusCode || 200
-			const msg = res.data.msg || '未知错误,请反馈给管理员'
+              reqFunc(config)
+            } else {
+              uni.showModal({
+                title: '系统提示',
+                content: '该微信还未绑定账号,请去绑定',
+                showCancel: false,
+                success: function(res) {
+                  if (res.confirm) {
+                    uni.reLaunch({
+                      url: '/pages/login/login'
+                    })
+                  }
+                }
+              });
+            }
+          } else {
+            toast('系统未知错误,请反馈给管理员')
+          }
+        })
+      }
+    })
+  }
 
-			// if (res.data.code === 500) {
-			//   uni.showToast({
-			//     icon: 'error',
-			//     title: '接口未知异常',
-			//   })
-			//   reject(msg)
-			// }
-			if (code === 401) {
-				uni.showModal({
-					title: '错误提示',
-					content: msg,
-					showCancel: false,
-					success: function(res) {
-						if (res.confirm) {
-							uni.reLaunch({
-								url: '/pages/login/login'
-							})
-						}
-					}
-				});
-				reject(msg)
-			} else if (code !== 200) {
-				toast(msg)
-				reject(code)
-			}
+}
+
+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 (code !== 200) {
+          toast(msg)
+          reject(code)
+        }
 
-			uni.hideLoading()
-			resolve(res.data)
-		}).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) + '异常'
-			}
+        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) + '异常'
+      }
 
-			uni.hideLoading()
-			toast(message)
-			reject(error)
-		})
-	})
+      toast(message)
+      reject(error)
+    })
+  })
 }
 
 export default request

+ 12 - 5
pages/login/login.vue

@@ -18,6 +18,7 @@
 <script>
   import { setToken } from '@/common/auth.js'
   import { login } from '@/api/login.js'
+  import config from '@/config.js'
 
   export default {
     data() {
@@ -59,14 +60,20 @@
                 ...this.form,
                 code: loginRes.code,
               }
-              login(params).then((res) => {
-                if (res.code === 200) {
-                  const { data } = res
-                  uni.setStorageSync('openid', data.openId)
+              uni.request({
+                method: 'POST',
+                dataType: 'json',
+                timeout: 10000,
+                url: config.service + '/auth/wxlogin',
+                data: params,
+              }).then((res) => {
+                console.log('login111111111', res);
+                if (res.statusCode === 200) {
+                  const { data } = res.data
                   setToken(data.token.access_token)
                   uni.reLaunch({ url: '/pages/index/index' })
                 } else {
-                  this.toast(res.msg)
+                  this.toast('系统未知错误,请反馈给管理员')
                 }
               })
             }