马宇豪
2024-02-06 140e83402814ffd2e92dfe313f5681feedd39f86
src/views/Login.vue
@@ -80,7 +80,7 @@
<!--        </a-tab-pane>-->
<!--      </a-tabs>-->
      <div style="margin-bottom: 20px">
        <a-checkbox :checked="isAuto" style="color:#fff;">自动登录</a-checkbox>
        <a-checkbox :checked="saveAccount" style="color:#fff;" @change="isSave">记住密码</a-checkbox>
<!--        <a style="float: right">忘记密码</a>-->
      </div>
      <a-form-item style="text-align: center">
@@ -91,11 +91,12 @@
          size="large"
          @click="handleSubmit"
          :loading="isLoading"
          v-preventReClick="1500"
        >
          登录
        </a-button>
      </a-form-item>
     <center><p>技术支持:中国科学院</p></center>
     <center><p>技术支持:技术保障部</p></center>
    </a-form-model>
  </div>
  </div>
@@ -105,12 +106,16 @@
import { login, getMenuAdmin } from "@/api/login";
import Cookies from 'js-cookie';
import {Base64} from "js-base64";
import {getAreaWithUserIfo} from "@/api/user";
export default {
  name: "login",
  data() {
    return {
      isLoading: false,
      isAuto: false,
      saveAccount: false,
      // hasErrors,
      // form: this.$form.createForm(this),
      form: {
@@ -128,8 +133,22 @@
    // this.$nextTick(() => {
    //   this.form.validateFields();
    // });
    this.hasUserCodeOrPassword()
  },
  methods: {
    hasUserCodeOrPassword(){
      if (localStorage.getItem('userName') && localStorage.getItem('userPassword')) {
        this.form.name = localStorage.getItem('userName')
        this.form.pwd = Base64.decode(localStorage.getItem('userPassword'))//解密
        this.saveAccount = true
      }
    },
    isSave(e){
      const t = this
      t.saveAccount = !t.saveAccount
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
@@ -139,6 +158,14 @@
            Cookies.set('resTk', res.data.data.tk);
            Cookies.set('resUid', res.data.data.uid);
            Cookies.set('userInfo',JSON.stringify(res.data.data),{expires: 7})
            await this.getAreaUsers(res.data.data.districtId)
            if (this.saveAccount) {
              localStorage.setItem('userName', this.form.name)
              localStorage.setItem('userPassword', Base64.encode(this.form.pwd))
            } else {
              localStorage.removeItem('userName')
              localStorage.removeItem('userPassword')
            }
            this.$router.push('/home')
          } else {
            this.$message.warning(res.data.msg);
@@ -149,6 +176,60 @@
          return false;
        }
      });
    },
    async getAreaUsers(districtId) {
      let t = this
      let res = await getAreaWithUserIfo()
      if (res.data.code == 100) {
        if (res.data.data) {
          t.userTitTree(res.data.data)
          const areaUsers = t.findNodeById(res.data.data, districtId).children
          localStorage.removeItem('areaUsers')
          localStorage.setItem('areaUsers',JSON.stringify(areaUsers))
        } else {
          console.log('暂无数据')
        }
      } else {
        this.$message.warning(res.data.msg);
      }
    },
    // 根据id查对象
    findNodeById(data, value) {
      for (const node of data) {
        if (node.id === value) {
          return node;
        }
        if (node.children) {
          const foundNode = this.findNodeById(node.children, value);
          if (foundNode) {
            return foundNode;
          }
        }
      }
      return null;
    },
    // 将树状数据name字段放入users的姓名电话
    userTitTree(treeData) {
      for (const node of treeData) {
        if (node.users) {
          node.users = node.users.filter(i => i.roleId == 3)
          node.users = node.users.map((i) => {
            return {
              ...i,
              unittype: node.type,
              districtId: node.id
            }
          })
          node.name = node.name + '(' + node.users.map(i => i.realName + ' ' + i.phone).join(',') + ')'
        }
        if (node.children) {
          this.userTitTree(node.children)
        }
      }
      return treeData
    }
  },
};