Quellcode durchsuchen

登录js逻辑封装至encrypt‌.js中,修改系统初始化时默认登录逻辑

wdq vor 3 Wochen
Ursprung
Commit
0a59d03a9e
2 geänderte Dateien mit 75 neuen und 68 gelöschten Zeilen
  1. 36 47
      src/App.vue
  2. 39 21
      src/utils/encrypt‌.js

+ 36 - 47
src/App.vue

@@ -2,7 +2,7 @@
   <router-view />
 </template>
 <script>
-// import api from "@/api/common";
+import api from "@/api/common";
 import encrypt‌ from "@/utils/encrypt‌";
 export default {
   name: "App",
@@ -24,57 +24,46 @@ export default {
     // });
     let that = this;
     // 默认登录
-    encrypt‌().then((result) => {
-      if (result.code == 200) {
-        that.$store.commit("setUserInfo", result.content);
-        that.$store.commit("setToken", result.message);
-        that.$store.commit("setUserState", true);
-        // 登录成功之后要批量加载一下DMS的字典,后续全局可调用
-        that.$store.state.DMSTypes.forEach((item) => {
-          api
-            .getDmsTypes({
-              cName: item,
-              type: 0,
-            })
-            .then((result2) => {
-              if (result2.code == 200) {
-                // 遍历result2.content,将每个元素的index作为key,name作为value,存储到DmsTypesMap中
-                let dmsTypesMap = {};
-                result2.content.forEach((element) => {
-                  dmsTypesMap[element.index] = element.name;
-                });
-                that.$store.commit("setDmsTypesMap", {
-                  name: item,
-                  list: dmsTypesMap,
-                });
-              } else {
-                that.$message({
-                  type: "error",
-                  message: result2.content,
-                });
-              }
-            })
-            .catch((err) => {
-              console.log(err);
+    encrypt‌().then(() => {
+      // 登录成功之后要批量加载一下DMS的字典,后续全局可调用
+      that.$store.state.DMSTypes.forEach((item) => {
+        api
+          .getDmsTypes({
+            cName: item,
+            type: 0,
+          })
+          .then((result2) => {
+            if (result2.code == 200) {
+              // 遍历result2.content,将每个元素的index作为key,name作为value,存储到DmsTypesMap中
+              let dmsTypesMap = {};
+              result2.content.forEach((element) => {
+                dmsTypesMap[element.index] = element.name;
+              });
+              that.$store.commit("setDmsTypesMap", {
+                name: item,
+                list: dmsTypesMap,
+              });
+            } else {
               that.$message({
                 type: "error",
-                message: "服务器忙碌,请稍后重试!",
+                message: result2.content,
               });
+            }
+          })
+          .catch((err) => {
+            console.log(err);
+            that.$message({
+              type: "error",
+              message: "服务器忙碌,请稍后重试!",
             });
-        });
-      } else {
-        that.$message({
-          type: "error",
-          message: result.content,
-        });
-      }
-    })
-      .catch((err) => {
-        that.$message({
-          type: "error",
-          message: "服务器忙碌,请稍后重试!",
-        });
+          });
+      });
+    }).catch((err) => {
+      that.$message({
+        type: "error",
+        message: err,
       });
+    });
   },
   methods: {
     // scrollUpdate() {

+ 39 - 21
src/utils/encrypt‌.js

@@ -1,28 +1,46 @@
 import api from "../api/common";
+import store from '../store';
+
+function login(username, password) {
+    return new Promise((resolve, reject) => {
+        api.login({
+            userName: username,
+            password: password,
+            clientId: "1",
+        }).then((result) => {
+            if (result.code == 200) {
+                store.commit("setUserInfo", result.content);
+                store.commit("setToken", result.message);
+                store.commit("setUserState", true);
+                resolve();
+            } else {
+                reject(result.content);
+            }
+        }).catch((err) => {
+            reject("服务器忙碌,请稍后重试!");
+        });
+    });
+}
 export default function encrypt(loginObj) {
-    if (loginObj == undefined) {
-        return api
-            .login({
-                userName: systemConfig.defaultAccount.username,
-                password: AesEncryptUtil.getPassword(),
-                clientId: "1",
-            })
-    } else {
-        if (loginObj.username != undefined || loginObj.username != "" || loginObj.username != null) {
-            return new Promise((resolve, reject) => {
-                reject("用户名为空")
+    return new Promise((resolve, reject) => {
+        if (loginObj == undefined) {
+            login(systemConfig.defaultAccount.username, AesEncryptUtil.getPassword()).then(function (result) {
+                resolve("登录成功");
+            }).catch(function (err) {
+                reject(err);
             });
-        }
-        if (loginObj.password != undefined || loginObj.password != "" || loginObj.password != null) {
-            return new Promise((resolve, reject) => {
+        } else {
+            if (loginObj.username != undefined || loginObj.username != "" || loginObj.username != null) {
+                reject("用户名为空")
+            }
+            if (loginObj.password != undefined || loginObj.password != "" || loginObj.password != null) {
                 reject("密码为空")
+            }
+            return login(loginObj.username, AesEncryptUtil.getPassword(loginObj.password)).then(function (result) {
+                resolve("登录成功");
+            }).catch(function (err) {
+                reject(err);
             });
         }
-        return api
-            .login({
-                userName: loginObj.username,
-                password: AesEncryptUtil.getPassword(loginObj.password),
-                clientId: "1",
-            })
-    }
+    })
 }