You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dashboard/src/stores/AuthStore.js

60 lines
1.7 KiB
JavaScript

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);
if (process.env.NODE_ENV === 'production') {
this.get_auth(); // 放到钉钉环境才能开启
}
}
auth = process.env.NODE_ENV === 'production' ? [] : ['admin']; // 开发时候用,正式环境留空
user = { name: 'loading', userid: '...' }; // 开发时候用,正式环境留空
has_permission(requireds) {
if (Object.keys(requireds).length === 0) {
return true;
}
const has_permission = requireds.filter((item) => this.auth.includes(item));
if (Object.keys(has_permission).length !== 0) {
return true;
}
return false;
}
// 请求权限
get_auth() {
const _this = this;
const CORPID = 'ding48bce8fd3957c96b'; // 企业的id
dd.runtime.permission.requestAuthCode({
corpId: CORPID,
onSuccess: function (res) {
console.log(res);
const code = res.code;
const 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;