ddlogin.vue 6.29 KB
<template>
  <div class="login-bg"></div>
</template>

<script>
import types from "../store/types.js";
export default {
  data() {
    return {
      ddLoginUrl: ""
    };
  },
  created() {
    let host = `http://${location.host}`;
    this.ddLoginUrl = `${
      window.config.huasanDD
    }&redirect_uri=${`${window.config.host}/#/ddlogin`}`;
    this.switchLogin();
  },
  mounted() {},
  methods: {
    switchLogin() {
      setTimeout(() => {
        let { href } = window.location,
          code = "";
        console.log(href, href.includes("code"));
        if (href.includes("code")) {
          code = href.split("?")[1].split("#")[0];
        }
        console.log("code", code);
        if (code) {
          this.getLoginInfo(code);
        } else {
          window.location.href = this.ddLoginUrl;
        }
      }, 200);
    },
    getLoginInfo(code) {
      this.$api.login.ddlogin({"code":code.split("=")[1]}).then(res => {
        if (res.code == 200) {
          this.loginMount(res.data);
        } else if (res.code == 500) {
          window.location.href = this.ddLoginUrl;
        }
      });
    },
    dealRedirect (){
      let { redirect } ="";
      if(redirect) {
        redirect = redirect.split('/')[ redirect.split('/').length - 1 ];
      }
      return redirect
    },
    loginMount(res, logintype) {
      this.$store.commit(types.ATOKEN, res.atoken);
      // localStorage.setItem('rtoken',m.data.rtoken)
      sessionStorage.setItem("user_unid", res.user_unid);
      //本系统可以直接用本地缓存做
      localStorage.setItem("atoken", res.atoken);
      localStorage.setItem("uname", res.user_name);
      this.$logs.oplogs(res.user_name, "serv_login", `登录了平台`);
      localStorage.setItem("access_token", res.access_token || "");
      // 处理登录用户权限菜单显示问题;
      // if(res.firstLogin && logintype !== "dingding") {
      //   this.$router.push('resetpass')
      //   return;
      // }
      //算法配置列表
      this.algoList();
      //存储配置列表
      this.storeConfList();
      //code列表
      this.getCatesList();
      this.getCodeList();
      this.getCustomCode();
      this.getEventList();
      this.getDev();
      setTimeout(() => {
        this.getMenu(res.user_unid);
      }, 10);
    },
    getMenu(id) {
      //获取菜单
      this.$api.login
        .getMenus({
          shape: "tree"
        })
        .then(res => {
          localStorage.setItem(
            "menu",
            JSON.stringify(res.menu_tree[0].children)
          );
          this.$store
            .dispatch("GetMenuRole", res.menu_tree[0].children)
            .then(r => {
              this.$router
                .push(res.menu_tree[0].children[0].children[0].path)
                .catch(err => {
                  err;
                });
            });
        });
    },
    algoList() {
      this.$api.login
        .algocombs({
          limit: "",
          algo_set: "video"
        })
        .then(res => {
          if (!res.ecode) {
            this.$store.commit(types.ALGO, res.list_data);
          }
        })
        .catch(err => {});
    },
    storeConfList() {
      this.$api.login
        .storeconfs({
          offset: 0,
          limit: ""
        })
        .then(res => {
          if (!res.ecode) {
            this.$store.commit(types.STORECONF, res.list_data);
          }
        })
        .catch(err => {});
    },
    getCodeList() {
      this.$api.codes
        .cates()
        .then(res => {
          res.list_data.forEach(item => {
            this.$api.codes
              .codes({ active: true }, item.cate_unid)
              .then(res => {
                // 存储code列表
                window.localStorage.setItem(
                  item.name,
                  JSON.stringify(res.list_data)
                );
                // 存储单独code
                res.list_data.forEach(chilItem => {
                  window.localStorage.setItem(
                    item.name + "-" + chilItem.code,
                    chilItem.name
                  );
                });
              })
              .catch(err => {});
          });
        })
        .catch(err => {});
    },
    getCustomCode() {
      this.$api.codes
        .customCode()
        .then(res => {
          if (res.list_data.length > 0) {
            res.list_data.forEach(item => {
              window.localStorage.setItem(
                item.name + "-" + item.cate,
                item.unid
              );
            });
          } else {
            this.$message({
              type: "warning",
              message: "获取自定义编码失败!"
            });
          }
        })
        .catch(err => {});
    },
    getCatesList() {
      this.$api.codes.eventCates({}).then(res => {
        // 存储cate列表
        window.localStorage.setItem("cate列表", JSON.stringify(res.list_data));
        // 存储单独code
        res.list_data.forEach(item => {
          this.getOneEventList(item.code, item.event_cate_unid);
        });
      });
    },
    getOneEventList(code, id) {
      this.$api.codes.eventType({}, id).then(res => {
        // 存储cate列表
        window.localStorage.setItem(code, JSON.stringify(res.list_data));
      });
    },
    getEventList() {
      this.$api.search.eventTypes({ active: true }).then(res => {
        // 存储code列表
        window.localStorage.setItem("安防事件", JSON.stringify(res.list_data));
        // 存储单独code
        res.list_data.forEach(item => {
          window.localStorage.setItem("安防事件-" + item.code, item.name);
        });
      });
    },
    getDev() {
      this.$api.resource.devs().then(res => {
        localStorage.setItem("dev_unid", res[0].dev_unid);
        this.getDevsName(res[0].dev_unid);
      });
    },
    getDevsName(id) {
      this.$api.resource
        .getDevsName(
          {
            is_leaf: 0
          },
          id
        )
        .then(res => {
          if (res.list_data.length > 0) {
            sessionStorage.setItem("device_id", res.list_data[0].device_id);
          }
        });
    }
  }
};
</script>

<style scoped>
.login-bg {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url("../assets/img/login/background.png");
  background-size: 100% 100%;
}
</style>