浏览代码

自动登录

xiaohaizhao 1 年之前
父节点
当前提交
12a4309f2b
共有 5 个文件被更改,包括 156 次插入116 次删除
  1. 3 3
      App.vue
  2. 7 0
      main.js
  3. 7 8
      pages/index/index.vue
  4. 48 18
      utils/Http.js
  5. 91 87
      utils/login.js

+ 3 - 3
App.vue

@@ -1,10 +1,7 @@
 <script>
-import { Login } from "./utils/login"
 export default {
 	onLaunch: function () {
 		require("./utils/tool").mount();
-		new Login().wechatLogin()
-
 		/* uni.addInterceptor('navigateTo', {//监听跳转
 			success(e) {
 				console.log("监听页面跳转", e)
@@ -19,6 +16,9 @@ export default {
 
 	},
 	onHide: function () {
+	},
+	globalData: {
+	
 	}
 }
 </script>

+ 7 - 0
main.js

@@ -38,10 +38,17 @@ http.socket = isDev ? "ws://61.164.207.46:8200" : "wss://www.ibpchina.com.cn";
 // #endif
 console.log("接口地址", http)
 
+import {
+  Login
+} from "./utils/login"
+Vue.prototype.$Login = new Login();
+Vue.prototype.$Login.wechatLogin()
+
 import {
   ApiModel
 } from './utils/api'
 Vue.prototype.$Http = new ApiModel(http);
+Vue.prototype.$Http.wechatLogin = Vue.prototype.$Login.wechatLogin;
 App.mpType = 'app'
 const app = new Vue({
   ...App

+ 7 - 8
pages/index/index.vue

@@ -31,17 +31,16 @@ export default {
 	onShow() {
 		const systemInitIsComplete = this.$Http.systemInitIsComplete;
 		if (!systemInitIsComplete || typeof systemInitIsComplete == 'object') {
-			this.$Http.HomePageStartRendering = render.bind(this)
+			this.$Http.HomePageStartRendering = render(this)
 		} else {
-			render()
+			render(this)
 		}
-		function render() {
-			console.log(this.$Http.systemInitIsComplete)
-			this.$Http.changePage = function (itemId, page, params) {
-				this.$refs.pages.onClick(itemId, page, false, params)
-			}.bind(this)
+		function render(that) {
+			that.$Http.changePage = function (itemId, page, params) {
+				that.$refs.pages.onClick(itemId, page, false, params)
+			}.bind(that)
 			// #ifdef MP-WEIXIN
-			this.updatePageData(this.swiperItemID, this.page)
+			that.updatePageData(that.swiperItemID, that.page)
 			// #endif
 		}
 	},

+ 48 - 18
utils/Http.js

@@ -3,6 +3,9 @@ class HTTP {
         baseUrl
     }) {
         this.baseUrl = baseUrl;
+        this.hangUp = [];
+        this.intercept = false;
+        this.logInAgain = null;
     }
     request({
         url,
@@ -14,7 +17,19 @@ class HTTP {
         showLoading = ''
     }) {
         return new Promise((resolve, reject) => {
-            this._request(url, resolve, reject, data, method, header, showLoading);
+            if (this.intercept && url != '/index/loginbywechat') {
+                this.hangUp.push({
+                    url,
+                    resolve,
+                    reject,
+                    data,
+                    method,
+                    header,
+                    showLoading
+                })
+            } else {
+                this._request(url, resolve, reject, data, method, header, showLoading);
+            }
         })
     }
     _request(url, resolve, reject, data, method, header, showLoading) {
@@ -28,26 +43,41 @@ class HTTP {
             method: method,
             header: header,
             timeout: 60000,
-            success: res => resolve(res.data),
+            success: res => {
+                if (res.data.msg == '登陆状态已过期,请重新登陆!') {
+                    this.intercept = true;
+                    this.hangUp.push({
+                        url,
+                        resolve,
+                        reject,
+                        data,
+                        method,
+                        header,
+                        showLoading
+                    })
+                    if (this.logInAgain == null) this.wechatLogin().then(token => {
+                        Promise.all(this.hangUp.map(v => {
+                            v.data.accesstoken = token;
+                            v.data.date = Date.now()
+                            return this._request(v.url, v.resolve, v.reject, v.data, v.method, v.header, v.showLoading);
+                        })).then(res => {
+                            console.log("重新获取数据")
+                            this.hangUp = []
+                            this.intercept = false;
+                            this.logInAgain = null;
+                        })
+                    })
+                } else {
+                    resolve(res.data)
+                }
+            },
             fial: err => reject(err),
             complete: (res) => {
                 if (showLoading) uni.hideLoading()
-                if (res.errMsg != 'request:ok') {
-                    uni.showToast({
-                        title: '网络异常,请稍后再试',
-                        icon: "none"
-                    })
-                } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
-                    uni.redirectTo({
-                        url: '/pages/login/login',
-                        success() {
-                            uni.showToast({
-                                title: res.msg,
-                                icon: "none"
-                            })
-                        }
-                    });
-                }
+                if (res.errMsg != 'request:ok') uni.showToast({
+                    title: '网络异常,请稍后再试',
+                    icon: "none"
+                })
             }
         })
     }

+ 91 - 87
utils/login.js

@@ -1,7 +1,93 @@
-import Vue from 'vue'
+import Vue from 'vue';
+
+/* 获取用户权限 */
+function query_userauth() {
+    return new Promise((resolve, reject) => {
+        Vue.prototype.$Http.basic({
+            "classname": "sysmanage.develop.userauth.userauth",
+            "method": "query_userauth",
+            content: {
+                nocache: true
+            }
+        }).then(res => {
+            console.log("查询用户权限", res)
+            if (Vue.prototype.cutoff(res.msg)) return resolve(false);
+            resolve(true)
+            parsingAuth(res.data)
+        });
+    })
+
+}
+/* 查询站点数据 */
+function querySite_Parameter() {
+    return new Promise((resolve, reject) => {
+        Vue.prototype.$Http.basic({
+            "classname": "webmanage.site.site",
+            "method": "querySite_Parameter", //查询站点数据
+            "content": {}
+        }).then(res => {
+            console.log("查询站点配置", res)
+            if (Vue.prototype.cutoff(res.msg)) return resolve(false);
+            uni.removeStorage({
+                key: 'siteP',
+            })
+            uni.setStorageSync('siteP', res.data);
+            resolve(true)
+        });
+    })
+}
+/* 查询站点数据 */
+function querySite() {
+    return new Promise((resolve, reject) => {
+        Vue.prototype.$Http.basic({
+            "classname": "webmanage.site.site",
+            "method": "querySite",
+            "content": {},
+        }).then(res => {
+            console.log("查询站点配置", res)
+            if (Vue.prototype.cutoff(res.msg)) return resolve(false);
+            res.data.logo = res.data.attinfos.length ? Vue.prototype.getSpecifiedImage(res.data.attinfos.find(s => s.usetype == "actionlogo") || res.data.attinfos[0]) : ''
+            uni.removeStorage({
+                key: 'site',
+            })
+            uni.setStorageSync('site', res.data);
+            resolve(true)
+        });
+    })
+}
+/* 格式化权限 */
+function parsingAuth(list) {
+    let authList = {}
+    list.forEach(system => {
+        // let systemObj = {}
+        system.modules.forEach(app => {
+            let appObj = {}
+            app.apps.forEach(m => {
+                appObj[m.meta.title] = {
+                    path: m.path,
+                    pathDetail: m.path_index,
+                    pathDetail: m.path_index,
+                    name: m.name,
+                    remark: m.meta.title,
+                    cover: m.cover,
+                    option: m.meta.auth.map(v => v.option),
+                    optionname: m.meta.auth.map(v => v.optionname)
+                }
+            })
+            // systemObj[app.systemmodulename] = appObj;
+            authList[app.systemmodulename] = appObj;
+        })
+        // authList[system.systemname] = systemObj;
+    });
+    uni.removeStorage({
+        key: 'authList'
+    })
+    uni.setStorageSync('authList', authList)
+}
+
+
 class Login {
     wechatLogin() {
-        let that = this;
         return new Promise((resolve, reject) => {
             uni.login({
                 success(res) {
@@ -10,12 +96,13 @@ class Login {
                         "systemclient": "marketingtool"
                     }).then(res => {
                         console.log("微信登录", res)
-                        if (Vue.prototype.cutoff(res.msg)) return resolve({});
+                        if (Vue.prototype.cutoff(res.msg)) return resolve(false);
+                        resolve(res.account_list[0].token)
                         uni.removeStorage({
                             key: 'userMsg',
                         })
                         uni.setStorageSync('userMsg', res.account_list[0])
-                        Vue.prototype.$Http.systemInitIsComplete = Promise.all([that.query_userauth(), that.querySite_Parameter(), that.querySite()]).then(res => {
+                        Vue.prototype.$Http.systemInitIsComplete = Promise.all([query_userauth(), querySite_Parameter(), querySite()]).then(res => {
                             Vue.prototype.$Http.systemInitIsComplete = true
                             Vue.prototype.$Http.HomePageStartRendering && Vue.prototype.$Http.HomePageStartRendering()
                         })
@@ -24,90 +111,7 @@ class Login {
             })
         })
     }
-    /* 获取用户权限 */
-    query_userauth() {
-        return new Promise((resolve, reject) => {
-            Vue.prototype.$Http.basic({
-                "classname": "sysmanage.develop.userauth.userauth",
-                "method": "query_userauth",
-                content: {
-                    nocache: true
-                }
-            }).then(res => {
-                console.log("查询用户权限", res)
-                if (Vue.prototype.cutoff(res.msg)) return resolve(false);
-                resolve(true)
-                this.parsingAuth(res.data)
-            });
-        })
 
-    }
-    /* 查询站点数据 */
-    querySite_Parameter() {
-        return new Promise((resolve, reject) => {
-            Vue.prototype.$Http.basic({
-                "classname": "webmanage.site.site",
-                "method": "querySite_Parameter", //查询站点数据
-                "content": {}
-            }).then(res => {
-                console.log("查询站点配置", res)
-                if (Vue.prototype.cutoff(res.msg)) return resolve(false);
-                uni.removeStorage({
-                    key: 'siteP',
-                })
-                uni.setStorageSync('siteP', res.data);
-                resolve(true)
-            });
-        })
-    }
-    /* 查询站点数据 */
-    querySite() {
-        return new Promise((resolve, reject) => {
-            Vue.prototype.$Http.basic({
-                "classname": "webmanage.site.site",
-                "method": "querySite",
-                "content": {},
-            }).then(res => {
-                console.log("查询站点配置", res)
-                if (Vue.prototype.cutoff(res.msg)) return resolve(false);
-                res.data.logo = res.data.attinfos.length ? Vue.prototype.getSpecifiedImage(res.data.attinfos.find(s => s.usetype == "actionlogo") || res.data.attinfos[0]) : ''
-                uni.removeStorage({
-                    key: 'site',
-                })
-                uni.setStorageSync('site', res.data);
-                resolve(true)
-            });
-        })
-    }
-    /* 格式化权限 */
-    parsingAuth(list) {
-        let authList = {}
-        list.forEach(system => {
-            // let systemObj = {}
-            system.modules.forEach(app => {
-                let appObj = {}
-                app.apps.forEach(m => {
-                    appObj[m.meta.title] = {
-                        path: m.path,
-                        pathDetail: m.path_index,
-                        pathDetail: m.path_index,
-                        name: m.name,
-                        remark: m.meta.title,
-                        cover: m.cover,
-                        option: m.meta.auth.map(v => v.option),
-                        optionname: m.meta.auth.map(v => v.optionname)
-                    }
-                })
-                // systemObj[app.systemmodulename] = appObj;
-                authList[app.systemmodulename] = appObj;
-            })
-            // authList[system.systemname] = systemObj;
-        });
-        uni.removeStorage({
-            key: 'authList'
-        })
-        uni.setStorageSync('authList', authList)
-    }
 }
 export {
     Login