Explorar o código

登出及解除绑定

nanMing hai 1 ano
pai
achega
232ef7fa96
Modificáronse 6 ficheiros con 184 adicións e 130 borrados
  1. 36 1
      App.vue
  2. 0 9
      api/dict.js
  3. 27 0
      api/login.js
  4. 91 99
      common/request.js
  5. 9 17
      pages/login/login.vue
  6. 21 4
      pages/my/index.vue

+ 36 - 1
App.vue

@@ -1,7 +1,42 @@
 <script>
+  import { getToken } from '@/common/auth.js'
+  import { login } from '@/api/login.js'
+
   export default {
     onLaunch: () => {
-      console.log('App Launch')
+      // if (!getToken()) {
+      //   uni.login({ // 获取用户登录凭证换取openid
+      //     provider: 'weixin',
+      //     success: (loginInfo) => {
+      //       login({ code: loginInfo.code }).then((response) => {
+      //         if (response.statusCode === 200) {
+      //           const { code, data, msg } = response.data
+      //           if (data && data.openId) {
+      //             setToken(data.token.access_token)
+      //             uni.setStorageSync('userInfo', data.sysUser)
+      //             config.header['Authorization'] = 'Bearer ' + getToken()
+      //           } else {
+      //             uni.showModal({
+      //               title: '系统提示',
+      //               content: '该微信还未绑定账号,请去绑定',
+      //               showCancel: false,
+      //               success: function(res) {
+      //                 if (res.confirm) {
+      //                   uni.reLaunch({
+      //                     url: '/pages/login/login'
+      //                   })
+      //                 }
+      //               }
+      //             });
+      //           }
+      //         } else {
+      //           toast('系统未知错误,请反馈给管理员')
+      //         }
+      //       })
+      //     }
+      //   })
+      // }
+
     },
     onShow: () => {
       console.log('App Show')

+ 0 - 9
api/dict.js

@@ -8,7 +8,6 @@ export const getDicts = (dictType) => {
   })
 }
 
-
 // 业务字典查询
 export const ObjdictList = (query) => {
   return request({
@@ -16,12 +15,4 @@ export const ObjdictList = (query) => {
     method: 'get',
     data: query
   })
-}
-
-// 解除绑定方法
-export function logout() {
-  return request({
-    url: '/auth/logout',
-    method: 'delete'
-  })
 }

+ 27 - 0
api/login.js

@@ -0,0 +1,27 @@
+import request from '@/common/request.js'
+
+
+// 登出
+export function login(data) {
+  return request({
+    url: '/auth/wxlogin',
+    method: 'post',
+    data
+  })
+}
+
+// 登出
+export function logout() {
+  return request({
+    url: '/auth/logout',
+    method: 'delete'
+  })
+}
+
+// 解除绑定
+export function unbind() {
+  return request({
+    url: '/auth/wxUnbind',
+    method: 'post'
+  })
+}

+ 91 - 99
common/request.js

@@ -1,113 +1,105 @@
-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 => {
-	let isRequest = false
-	config.header = config.header || {}
-	if (getToken()) {
-		config.header['Authorization'] = 'Bearer ' + getToken()
-		isRequest = true
-	} else {
-		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)
-              uni.setStorageSync('userInfo',data.sysUser)
-							config.header['Authorization'] = 'Bearer ' + getToken()
+  config.header = config.header || {}
+  let isRequest = false
+  if (getToken()) {
+    config.header['Authorization'] = 'Bearer ' + getToken()
+    isRequest = true
+  } else {
+    uni.login({ // 获取用户登录凭证换取openid
+      provider: 'weixin',
+      success: (loginInfo) => {
+        login({ code: loginInfo.code }).then((response) => {
+          if (response.statusCode === 200) {
+            const { code, data, msg } = response.data
+            if (data && data.openId) {
+              setToken(data.token.access_token)
+              uni.setStorageSync('userInfo', data.sysUser)
+              config.header['Authorization'] = 'Bearer ' + getToken()
+              isRequest = true
+            } 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('系统未知错误,请反馈给管理员')
-					}
-				})
-			}
-		})
-	}
+  if (isRequest) {
+    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 === 401) {
+            uni.showModal({
+              title: '系统提示',
+              content: '登录状态已过期,请重新登录',
+              showCancel: false,
+              success: function(res) {
+                if (res.confirm) {
+                  uni.reLaunch({
+                    url: '/pages/login/login'
+                  })
+                }
+              }
+            });
+          } else if (code !== 200) {
+            toast(msg)
+            reject(code)
+          }
 
-	if (isRequest) {
-		// console.log("请求",BASE_URL + config.url, 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
+          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) + '异常'
+        }
 
-					if (code !== 200) {
-						toast(msg)
-						reject(code)
-					}
+        toast(message)
+        reject(error)
+      })
+    })
 
-					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) + '异常'
-				}
+  }
 
-				toast(message)
-				reject(error)
-			})
-		})
-	}
 
 }
 

+ 9 - 17
pages/login/login.vue

@@ -15,14 +15,15 @@
 
 <script>
   import { setToken } from '@/common/auth.js'
+  import { login } from '@/api/login.js'
   import config from '@/config.js'
 
   export default {
     data() {
       return {
         form: {
-          username: '', //15512341234
-          password: '', //sckj@2022
+          username: '15512341234', //15512341234
+          password: 'sckj@2022', //sckj@2022
         },
         rules: {
           username: {
@@ -50,21 +51,12 @@
                 ...this.form,
                 code: loginRes.code,
               }
-              uni.request({
-                method: 'POST',
-                dataType: 'json',
-                timeout: 10000,
-                url: config.service + '/auth/wxlogin',
-                data: params,
-              }).then((res) => {
-                if (res.statusCode === 200) {
-                  const { data } = res.data
-                  setToken(data.token.access_token)
-                  uni.setStorageSync('userInfo', data.sysUser)
-                  uni.reLaunch({ url: '/pages/index/index' })
-                } else {
-                  this.toast('系统未知错误,请反馈给管理员')
-                }
+              login(params).then((res) => {
+                const { code, data } = res
+                if (code !== 200) return
+                setToken(data.token.access_token)
+                uni.setStorageSync('userInfo', data.sysUser)
+                uni.reLaunch({ url: '/pages/index/index' })
               })
             }
           })

+ 21 - 4
pages/my/index.vue

@@ -11,14 +11,14 @@
     <cc-wxMenuBtn leftTitle="我的兑换" icon="../../static/image/add.png" @click="goFunctionClick(1)"></cc-wxMenuBtn>
     <cc-wxMenuBtn leftTitle="我的资料" icon="../../static/image/add.png" @click="goFunctionClick(3)"></cc-wxMenuBtn> -->
     <view class="bottomButton">
-      <button type="primary" size="mini" @click="loginOut">解除绑定</button>
+      <button type="primary" size="mini" @click="loginOut">退出登录</button>
     </view>
   </view>
 
 </template>
 
 <script>
-  import { logOut } from '@/api/dict.js'
+  import { logout, unbind } from '@/api/login.js'
 
   export default {
     data() {
@@ -41,9 +41,26 @@
         })
       },
       loginOut() {
-        logOut().then(res => {
-          uni.clearStorageSync()
+        uni.showModal({
+          title: '系统提示',
+          content: '确定要退出登录吗?',
+          success(res) {
+            if (res.confirm) {
+              unbind().then(res => {
+                logout().then(resp => {
+                  uni.clearStorageSync()
+                  uni.reLaunch({
+                    url: '/pages/login/login'
+                  })
+                })
+              })
+            }
+          },
+          fail() {
+
+          }
         })
+
       },
 
     }