123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <template>
- <div class="login login-box">
- <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="请输入账号"
- clearable
- >
- <i class="icon icon-user" 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-lock" 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="isFetchingCode ? 'default' : 'primary'"
- @click="fetchSmsCode"
- :disabled="isFetchingCode"
- >{{ codeContent }}</el-button
- >
- </div>
- <div class="vlcode-left">
- <el-input
- v-model.trim="loginModel.code"
- placeholder="请输入手机验证码"
- clearable
- ></el-input>
- </div>
- </div>
- </el-form-item>
- <el-form-item prop="schoolCode"></el-form-item>
- <el-form-item>
- <el-button
- style="width:100%;"
- type="primary"
- :disabled="isSubmit"
- @click="submit('loginForm')"
- >登录</el-button
- >
- </el-form-item>
- </el-form>
- </div>
- </div>
- <div class="login-shadow-top"></div>
- <div class="login-shadow-bottom"></div>
- <!-- 修改密码 -->
- <reset-pwd :user-password="loginModel.password" ref="ResetPwd"></reset-pwd>
- </div>
- </template>
- <script>
- import { password, smscode } from "@/plugins/formRules";
- import {
- login,
- getSmsCode,
- getSchoolInfo,
- getSysConfig,
- getSysTime
- } from "../api";
- import { Base64 } from "@/plugins/crypto";
- import ResetPwd from "@/modules/base/components/ResetPwd";
- import { ORG_CODE } from "@/constants/app";
- import { initSyncTime } from "@/plugins/syncServerTime";
- const wstorage = {
- set(key, value, expire = null) {
- window.localStorage.setItem(key, JSON.stringify({ value, expire }));
- },
- get(key, defaultVal = null) {
- const lsvalue = JSON.parse(window.localStorage.getItem(key));
- return lsvalue && (!lsvalue.expire || lsvalue.expire > new Date().getTime())
- ? lsvalue.value
- : defaultVal;
- },
- remove(key) {
- window.localStorage.removeItem(key);
- }
- };
- const codeWaitingTime = 60;
- const nameWaitTime = "login";
- export default {
- name: "login",
- components: { ResetPwd },
- data() {
- return {
- nameWaitTime,
- smsCodeRequired: false,
- loginModel: {
- schoolCode: ORG_CODE,
- loginName: "",
- code: "",
- password: ""
- },
- loginRules: {
- code: smscode,
- password,
- loginName: [
- {
- required: true,
- message: "请输入用户名",
- trigger: "change"
- }
- ],
- schoolCode: [
- {
- required: true,
- message: "学校编码缺失",
- trigger: "change"
- }
- ]
- },
- roles: [],
- isSubmit: false,
- schoolLogo: "",
- // code
- isFetchingCode: false,
- codeContent: "获取验证码",
- codeWaitingTime,
- time: codeWaitingTime
- };
- },
- mounted() {
- this.$ls.clear();
- this.setWaitingTime();
- this.getSchool();
- this.getSmsCodeRequired();
- this.syncServerTime();
- },
- methods: {
- async syncServerTime() {
- const time = await getSysTime();
- initSyncTime(time);
- },
- async getSmsCodeRequired() {
- const data = await getSysConfig("sys.code.enable");
- this.smsCodeRequired = data.configValue === "true";
- },
- async getSchool() {
- const data = await getSchoolInfo(ORG_CODE);
- 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);
- // 强制修改密码
- // if (!data.user.pwdUpdateTime) {
- // this.$refs.ResetPwd.open();
- // return;
- // }
- this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
- this.$store.commit("setUser", data);
- if (data.roleList.includes("ADMIN")) {
- this.$router.push({
- name: "SelectSchool"
- });
- } else {
- this.$router.push({
- name: "Home"
- });
- }
- },
- // code valid
- setWaitingTime() {
- let codetime = wstorage.get(this.nameWaitTime);
- if (codetime) {
- let num = Math.floor((codetime.expire - new Date().getTime()) / 1000);
- if (num > 0) {
- this.time = num;
- this.isFetchingCode = true;
- this.changeContent();
- }
- }
- },
- 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;
- this.$message.success(
- `已向手机号【${data}】成功发送短信,请在2分钟内进行验证!`
- );
- this.changeContent();
- },
- changeContent() {
- if (!this.isFetchingCode) return;
- this.codeContent = "倒计时" + this.time + "s";
- const circleTime = time => {
- let t = setInterval(() => {
- if (time > 1) {
- time--;
- let expire = new Date().getTime() + time * 1000;
- wstorage.set(
- this.nameWaitTime,
- {
- time,
- expire
- },
- expire
- );
- this.codeContent = "倒计时" + time + "s";
- } else {
- this.time = this.codeWaitingTime;
- wstorage.remove(this.nameWaitTime);
- this.codeContent = "获取验证码";
- this.isFetchingCode = false;
- clearInterval(t);
- }
- }, 1e3);
- };
- circleTime(this.time);
- }
- }
- };
- </script>
|