<template>
  <div>
  </div>
</template>

<script>
import { getUserInfo, getRoutesConfig, getPermission, getSettings, getCompany } from '@/services/base/user'
import { setAuthorization, setCompany } from '@/utils/request'
import { loadRoutes } from '@/utils/routerUtil'
import { mapMutations } from 'vuex'
import Cookie from 'js-cookie'
import { USER_MODEL } from '@/services/base/api.js';



//封装 登录的相关操作
export default {
  name: 'loginActions',
  components: {},
  data() {
    return {
      logging: false,
      error: '',
      form: this.$form.createForm(this),
      user: {
        mobile: '18192398883',
        password: '888888'
      },
      Project: [],
      companyShow: false,
      companyData: [],
      company: Cookie.get('company'),
      loginResult: null
    }
  },
  computed: {
  },
  beforeMount() {

  },
  created() {

  },
  methods: {
    ...mapMutations('account', ['setUid', 'setUser', 'setPermissions', 'setRoutesConfig', 'setSettings', 'setCompany', 'setProject']),

    isMiddleAdmin() {
      return USER_MODEL == "AdminUser";
    },

    initRoutes() {

      let routesConfig = { };

      if(process.env.VUE_APP_BEID == 1){
        routesConfig = {
          router: "touch",
          children:[
            {router:'TouchMesReporting'}
          ] 
        }; 
         
      }
      else if(process.env.VUE_APP_BEID == 5){
        routesConfig= {
          router: "touch",
          children:[
            {router:'TouchMoldHome'},
            {router:'TouchMoldReporting'},
            {router:'TouchMoldSend'},
          ] 
        };  
      }

      this.setRoutesConfig(routesConfig)
      loadRoutes([routesConfig])
    },

    afterLoginSuccess({ closeOthers, token, access_expire, uid, msg = '登录成功', pushTo = '/dashboard/workplace' }) {

      setAuthorization({ token: token, expireAt: new Date(access_expire * 1000) }) // 设置token

      this.setUid(uid) // 设置用户id


      getUserInfo().then(result => { // 获取用户信息
        if (result.data.data != null) {
          const UserInfo = result.data.data.user_info
          this.setUser(UserInfo)
        }
      })

      getPermission().then(result => { // 获取权限
        if (result.data.data != null) {
          const Permission = result.data.data.setPermissions
          this.setPermissions(Permission)
        }
      })

      getRoutesConfig().then(result => { // 获取路由配置
        if (result.data.data != null) {



          const routesConfig = result.data.data;


          this.setRoutesConfig(routesConfig)
          loadRoutes([routesConfig])

          if (closeOthers) {
            this.$closeOthers(pushTo)
          } else {
            this.$router.push(pushTo)
          }

          this.$message.success(msg, 3)
        }
      })

      getSettings().then(result => { // 获取系统配置
        if (result.data.data != null) {
          const settings = result.data.data
          this.setSettings(settings)
        }
      })

      getCompany().then(result => { // 获取公司信息
        if (result.data.data != null) {
          const company = result.data.data
          this.setCompany(company) // 将公司信息存入vuex
          setCompany(company.company_info) // 将公司信息存入cookies
        }
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
 