import {makeAutoObservable, runInAction} from "mobx"; import * as dd from 'dingtalk-jsapi'; import * as config from "../config"; //权限管理 class AuthStore { constructor(rootStore) { this.rootStore = rootStore; makeAutoObservable(this); this.get_auth(); //放到钉钉环境才能开启 } auth = ['admin']; //开发时候用,正式环境留空 user = {name:'loading',userid:'...'};//开发时候用,正式环境留空 has_permission(requireds) { if (Object.keys(requireds).length == 0) { return true; } let has_permission = requireds.filter(item => this.auth.includes(item)); if (Object.keys(has_permission).length !== 0) { return true; } return false; } //请求权限 get_auth() { let _this = this; const CORPID = 'ding48bce8fd3957c96b';//企业的id dd.runtime.permission.requestAuthCode({ corpId: CORPID, onSuccess: function (res) { console.log(res); let code = res.code; let url = '/dingtalk/dingtalkwork/Getusers_auth?code=' + code; //请求获取HT接口获取用户权限和用户信息 fetch(config.HT_HOST + url) .then((response) => response.json()) .then((json) => { runInAction(() => { _this.user = json.result; _this.auth = json.result.authlist; }) }) .catch((error) => { console.log('fetch data failed', error); }); }, onFail: function (err) { console.log(err) } }); } } export default AuthStore;