import form from "../api/apiOutAdmin";
let apiForm = form.apis;
let axios = null;

export default {
    name: "USER_INFO_GLOBAL",

    ARR_USER_INFO: [], // 用户数据
    ID_AND_USERNAME:{},//用户id-用户名
    install(Vue,opts){
        axios = opts.axios;
        this.getAllUser();
    },

    /*
    ** 存入
    */
    setUserInfo(val) {
        this.ID_AND_USERNAME = {};
        val.forEach(item => {
            this.ID_AND_USERNAME[item.id] = item;
        })
    },
    /*
    ** 获取
    */
    getUserInfo(id) {
        return this.ID_AND_USERNAME[id];
    },

    /*
    ** 转换
    */
    getUserById(id){
        let vm = this;
        return new Promise(function (resolve) {
            let dataName = vm.getUserInfo(id);
            if(!dataName){//缓存不存在 查后台
                axios.post(apiForm.daitianjia).then(res=> {
                    if(res.data){
                        let item=res.data;
                        //放入缓存中
                        if(item && item.id){
                            this.ID_AND_USERNAME[item.id] = item;
                        }
                        resolve(item.dataName);
                    }else{
                        dataName=id;
                        resolve(dataName);
                    }
                });
            }else{
                resolve(dataName);
            }

        })

    },

    /*
    ** 初始获取
    */
    getAllUser(){
        let vm = this;
        axios.post(apiForm.daitianjia).then(res=> {
            if(res && res.data){
                vm.ARR_USER_INFO = res.data || [];
                vm.setUserInfo(res.data);
            }
        });
    }

}
