فهرست منبع

多学校切换

zhangjie 2 سال پیش
والد
کامیت
db409ae0d9

+ 119 - 0
src/modules/login/components/SwitchSchoolDialog.vue

@@ -0,0 +1,119 @@
+<template>
+  <el-dialog
+    class="switch-school-dialog"
+    :visible.sync="modalIsShow"
+    title="切换学校"
+    top="10vh"
+    width="440px"
+    :close-on-click-modal="false"
+    :close-on-press-escape="false"
+    append-to-body
+    @open="visibleChange"
+  >
+    <el-form
+      ref="modalFormComp"
+      :model="modalForm"
+      :rules="rules"
+      label-width="70px"
+    >
+      <el-form-item prop="schoolId" label="学校:">
+        <el-select
+          v-model="modalForm.schoolId"
+          class="width-full"
+          placeholder="请选择学校"
+          filterable
+        >
+          <el-option
+            v-for="item in schoolInfo"
+            :key="item.id"
+            :value="item.id"
+            :label="item.name"
+          >
+          </el-option>
+        </el-select>
+      </el-form-item>
+    </el-form>
+    <div slot="footer">
+      <el-button type="primary" :disabled="isSubmit" @click="submit"
+        >确认</el-button
+      >
+      <el-button @click="cancel">取消</el-button>
+    </div>
+  </el-dialog>
+</template>
+
+<script>
+import { login } from "../api";
+
+export default {
+  name: "switch-school-dialog",
+  data() {
+    return {
+      modalIsShow: false,
+      isSubmit: false,
+      schoolInfo: [],
+      loginName: "",
+      modalForm: { schoolId: "" },
+      rules: {
+        schoolId: [
+          {
+            required: true,
+            message: "请选择学校",
+            trigger: "change",
+          },
+        ],
+      },
+    };
+  },
+  methods: {
+    visibleChange() {
+      const user = this.$ls.get("user", {
+        schoolInfo: [],
+        loginName: "",
+      });
+      this.schoolInfo = user.schoolInfo;
+      this.loginName = user.loginName;
+      this.modalForm.schoolId = this.$ls.get("schoolId");
+    },
+    cancel() {
+      this.modalIsShow = false;
+    },
+    open() {
+      this.modalIsShow = true;
+    },
+    async submit() {
+      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
+      if (!valid) return;
+
+      if (this.isSubmit) return;
+      this.isSubmit = true;
+      const curSchool = this.schoolInfo.find(
+        (item) => item.id === this.modalForm.schoolId
+      );
+
+      const datas = {
+        schoolCode: curSchool.code,
+        type: "ACCOUNT",
+        loginName: this.loginName,
+        password: curSchool.password,
+      };
+      const data = await login(datas).catch(() => {});
+      this.isSubmit = false;
+      if (!data) return;
+
+      if (data.orgInfo)
+        this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
+      this.$ls.set("schoolId", curSchool.id, this.GLOBAL.authTimeout);
+      this.$ls.set("schoolName", curSchool.name);
+      this.$ls.set("user", data, this.GLOBAL.authTimeout);
+      this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
+      this.cancel();
+
+      window.location.href = this.getRouterPath({
+        name: "Home",
+      });
+      window.location.reload();
+    },
+  },
+};
+</script>

+ 18 - 4
src/modules/login/views/Login.vue

@@ -242,8 +242,15 @@ export default {
 
       if (data.orgInfo)
         this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
-      if (data.schoolInfo)
-        this.$ls.set("schoolId", data.schoolInfo.id, this.GLOBAL.authTimeout);
+      if (data.schoolInfo && data.schoolInfo.length) {
+        const curSchool = data.schoolInfo.find(
+          (item) => item.code === this.loginModel.schoolCode
+        );
+        if (curSchool) {
+          this.$ls.set("schoolId", curSchool.id, this.GLOBAL.authTimeout);
+          this.$ls.set("schoolName", curSchool.name, this.GLOBAL.authTimeout);
+        }
+      }
       this.$ls.set("user", data, this.GLOBAL.authTimeout);
 
       this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
@@ -275,8 +282,15 @@ export default {
     resetPwdSuccess(data) {
       if (data.orgInfo)
         this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
-      if (data.schoolInfo)
-        this.$ls.set("schoolId", data.schoolInfo.id, this.GLOBAL.authTimeout);
+      if (data.schoolInfo && data.schoolInfo.length) {
+        const curSchool = data.schoolInfo.find(
+          (item) => item.code === this.loginModel.schoolCode
+        );
+        if (curSchool) {
+          this.$ls.set("schoolId", curSchool.id, this.GLOBAL.authTimeout);
+          this.$ls.set("schoolName", curSchool.name, this.GLOBAL.authTimeout);
+        }
+      }
       this.$ls.set("user", data, this.GLOBAL.authTimeout);
 
       this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);

+ 0 - 266
src/modules/login/views/Login2.vue

@@ -1,266 +0,0 @@
-<template>
-  <div class="login login-box">
-    <div class="login-theme"></div>
-    <div class="login-body" @keyup.enter="submit('loginForm')">
-      <div class="login-title">
-        <img v-if="schoolLogo" :src="schoolLogo" alt="学校logo" />
-        <h1 v-else>知学知考</h1>
-      </div>
-      <div class="login-form">
-        <el-form ref="loginForm" :model="loginModel" :rules="loginRules">
-          <el-form-item prop="loginName">
-            <el-input
-              v-model.trim="loginModel.loginName"
-              placeholder="请输入账号"
-              name="username"
-              clearable
-            >
-              <i class="icon icon-phone" slot="prefix"></i>
-            </el-input>
-          </el-form-item>
-          <el-form-item prop="password">
-            <el-input
-              type="password"
-              v-model.trim="loginModel.password"
-              placeholder="请输入密码"
-              clearable
-            >
-              <i class="icon icon-password" slot="prefix"></i>
-            </el-input>
-          </el-form-item>
-          <el-form-item prop="code" v-if="smsCodeRequired">
-            <div class="vlcode">
-              <div class="vlcode-right">
-                <el-button
-                  style="width: 100%"
-                  type="text"
-                  @click="fetchSmsCode"
-                  :disabled="isFetchingCode"
-                  >+{{ codeContent }}</el-button
-                >
-              </div>
-              <div class="vlcode-left">
-                <el-input
-                  v-model.trim="loginModel.code"
-                  placeholder="请输入手机验证码"
-                  name="code"
-                  clearable
-                >
-                  <i class="icon icon-checkcode" slot="prefix"></i>
-                </el-input>
-              </div>
-            </div>
-          </el-form-item>
-          <el-form-item prop="schoolCode"></el-form-item>
-          <el-form-item>
-            <el-button
-              class="login-submit-btn"
-              size="large"
-              type="info"
-              :disabled="isSubmit"
-              round
-              @click="submit('loginForm')"
-              >登录</el-button
-            >
-          </el-form-item>
-        </el-form>
-      </div>
-    </div>
-
-    <!-- 修改密码 -->
-    <reset-pwd
-      :user-info="userInfo"
-      ref="ResetPwd"
-      @modified="resetPwdSuccess"
-    ></reset-pwd>
-  </div>
-</template>
-
-<script>
-import { password, smscode } from "@/plugins/formRules";
-import { login, getSmsCode, getSchoolInfo, getSysConfig } from "../api";
-import { Base64 } from "@/plugins/crypto";
-import ResetPwd from "@/modules/base/components/ResetPwd";
-import { getOrgCode } from "@/constants/app";
-import fetchSmsMixins from "../fetchSmsMixins";
-
-export default {
-  name: "login",
-  components: { ResetPwd },
-  mixins: [fetchSmsMixins],
-  data() {
-    return {
-      nameWaitTime: "login",
-      smsCodeRequired: false,
-      loginModel: {
-        schoolCode: "",
-        loginName: "",
-        code: "",
-        password: "",
-      },
-      loginRules: {
-        code: smscode,
-        password,
-        loginName: [
-          {
-            required: true,
-            message: "请输入用户名",
-            trigger: "change",
-          },
-        ],
-        schoolCode: [
-          {
-            required: true,
-            message: "学校编码缺失",
-            trigger: "change",
-          },
-        ],
-      },
-      userInfo: {},
-      roles: [],
-      isSubmit: false,
-      schoolLogo: "",
-    };
-  },
-  mounted() {
-    this.$ls.clear();
-    this.setWaitingTime();
-    this.getSchool();
-    this.getSmsCodeRequired();
-  },
-  methods: {
-    async getSmsCodeRequired() {
-      const data = await getSysConfig("sys.code.enable");
-      this.smsCodeRequired = data && data.configValue === "true";
-    },
-    async getSchool() {
-      this.loginModel.schoolCode = getOrgCode();
-      const data = await getSchoolInfo(this.loginModel.schoolCode);
-      this.$ls.set("schoolLogo", data.logo);
-      this.$ls.set("schoolName", data.name);
-      this.schoolLogo = data.logo;
-    },
-    async submit(name) {
-      const valid = await this.$refs[name].validate().catch(() => {});
-      if (!valid) return;
-
-      if (this.isSubmit) return;
-      this.isSubmit = true;
-      const data = await login({
-        loginName: this.loginModel.loginName,
-        password: Base64(this.loginModel.password),
-        code: this.smsCodeRequired ? this.loginModel.code : null,
-        schoolCode: this.loginModel.schoolCode,
-      }).catch(() => {});
-      this.isSubmit = false;
-      if (!data) return;
-
-      if (data.orgInfo)
-        this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
-      if (data.schoolInfo)
-        this.$ls.set("schoolId", data.schoolInfo.id, this.GLOBAL.authTimeout);
-      this.$ls.set("user", data, this.GLOBAL.authTimeout);
-
-      this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
-
-      // 强制修改密码和绑定手机号
-      if (
-        data.userLoginCheckResult &&
-        (!data.userLoginCheckResult.pwdCount ||
-          !data.userLoginCheckResult.mobileNumber)
-      ) {
-        this.userInfo = {
-          ...this.loginModel,
-          ...data.userLoginCheckResult,
-        };
-        this.$refs.ResetPwd.open();
-        return;
-      }
-
-      if (data.roleList && data.roleList.includes("ADMIN")) {
-        this.$router.push({
-          name: "SelectSchool",
-        });
-      } else {
-        this.$router.push({
-          name: "Home",
-        });
-      }
-    },
-    resetPwdSuccess(data) {
-      if (data.orgInfo)
-        this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
-      if (data.schoolInfo)
-        this.$ls.set("schoolId", data.schoolInfo.id, this.GLOBAL.authTimeout);
-      this.$ls.set("user", data, this.GLOBAL.authTimeout);
-
-      this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
-
-      if (data.roleList && data.roleList.includes("ADMIN")) {
-        this.$router.push({
-          name: "SelectSchool",
-        });
-      } else {
-        this.$router.push({
-          name: "Home",
-        });
-      }
-    },
-    // code valid
-    checkField(field) {
-      return new Promise((resolve, reject) => {
-        this.$refs.loginForm.validateField(field, (unvalid) => {
-          if (unvalid) {
-            reject();
-          } else {
-            resolve();
-          }
-        });
-      });
-    },
-    async fetchSmsCode() {
-      const validAll = [
-        this.checkField("loginName"),
-        this.checkField("password"),
-        this.checkField("schoolCode"),
-      ];
-
-      let result = true;
-      await Promise.all(validAll).catch(() => {
-        result = false;
-      });
-
-      if (!result) return;
-
-      this.isFetchingCode = true;
-      const data = await getSmsCode({
-        loginName: this.loginModel.loginName,
-        schoolCode: this.loginModel.schoolCode,
-        password: Base64(this.loginModel.password),
-      }).catch(() => {
-        this.isFetchingCode = false;
-      });
-      if (!data) return;
-
-      if (data.mobileNumber) {
-        this.$message.success(
-          `已向手机尾号【${data.mobileNumber.slice(
-            -4
-          )}】成功发送短信,请在2分钟内进行验证!`
-        );
-        this.changeContent();
-      } else {
-        this.isFetchingCode = false;
-        this.$message.error("未绑定手机号,请先绑定!");
-        this.userInfo = {
-          ...data,
-          loginName: this.loginModel.loginName,
-          schoolCode: this.loginModel.schoolCode,
-          password: this.loginModel.password,
-        };
-        this.$refs.ResetPwd.open();
-      }
-    },
-  },
-};
-</script>

+ 5 - 2
src/modules/login/views/LoginOpen.vue

@@ -39,8 +39,11 @@ export default {
       // 不同系统登录成功之后的处理逻辑可能会有差异
       if (data.orgInfo)
         this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
-      if (data.schoolInfo)
-        this.$ls.set("schoolId", data.schoolInfo.id, this.GLOBAL.authTimeout);
+      if (data.schoolInfo && data.schoolInfo.length) {
+        const curSchool = data.schoolInfo[0];
+        this.$ls.set("schoolId", curSchool.id, this.GLOBAL.authTimeout);
+        this.$ls.set("schoolName", curSchool.name, this.GLOBAL.authTimeout);
+      }
       this.$ls.set("user", data, this.GLOBAL.authTimeout);
 
       this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);

+ 18 - 2
src/views/Home.vue

@@ -115,6 +115,9 @@
       ref="ResetPwd"
       @modified="resetPwdModified"
     ></reset-pwd>
+    <!-- 切换学校 -->
+    <switch-school-dialog v-if="hasMoreSchool" ref="SwitchSchoolDialog">
+    </switch-school-dialog>
   </div>
 </template>
 
@@ -125,14 +128,20 @@ import { sysMenu, logout } from "../modules/login/api";
 import ResetPwd from "../modules/base/components/ResetPwd";
 import { SYS_ADMIN_NAME } from "@/constants/enumerate";
 import staticMenu from "../constants/staticMenu";
+import SwitchSchoolDialog from "../modules/login/components/SwitchSchoolDialog.vue";
 
 const HOME_PAGE_ROUTE = "HomePage";
 
 export default {
   name: "home",
-  components: { ResetPwd },
+  components: { ResetPwd, SwitchSchoolDialog },
   data() {
-    const user = this.$ls.get("user", { id: "", realName: "", roleList: [] });
+    const user = this.$ls.get("user", {
+      id: "",
+      realName: "",
+      roleList: [],
+      schoolInfo: [],
+    });
     const IS_SUPER_ADMIN = user.loginName === SYS_ADMIN_NAME;
 
     return {
@@ -147,6 +156,7 @@ export default {
       userRoles: user.roleList,
       schoolLogo: this.$ls.get("schoolLogo"),
       schoolName: this.$ls.get("schoolName"),
+      schoolInfo: user.schoolInfo,
       userInfo: {
         pwdCount: 0,
         mobileNumber: 1,
@@ -170,6 +180,9 @@ export default {
     IS_HOME_PAGE() {
       return this.$route.name === HOME_PAGE_ROUTE;
     },
+    hasMoreSchool() {
+      return this.schoolInfo && this.schoolInfo.length > 1;
+    },
   },
   created() {
     // this.initData1();
@@ -405,6 +418,9 @@ export default {
     },
     toSelectSchool() {
       if (this.IS_SUPER_ADMIN) this.$router.push({ name: "SelectSchool" });
+      if (this.hasMoreSchool) {
+        this.$refs.SwitchSchoolDialog.open();
+      }
     },
     toResetPwd() {
       if (this.IS_SUPER_ADMIN) return;