Pear-Admin-Layui/Pear Admin v 2.0/component/layui/lay/extends/pearAuth.js

68 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-06-10 20:20:47 +00:00
layui.define(['table', 'jquery', 'element'], function (exports) {
"use strict";
var MOD_NAME = 'pearAuth',
$ = layui.jquery,
element = layui.element;
2020-06-10 22:29:16 +00:00
var pearAuth = function () {};
2020-06-10 20:20:47 +00:00
/**
* 初始化
* */
2020-06-10 22:29:16 +00:00
pearAuth.prototype.loadPermission = function (url) {
2020-06-10 20:20:47 +00:00
2020-06-10 22:29:16 +00:00
var data = getData(url);
localStorage.setItem("auth_cache", data);
2020-06-10 20:20:47 +00:00
}
/**
2020-06-10 22:29:16 +00:00
* 清除权限
2020-06-10 20:20:47 +00:00
* */
2020-06-10 22:29:16 +00:00
pearAuth.prototype.clearPermission = function () {
2020-06-10 20:20:47 +00:00
2020-06-10 22:29:16 +00:00
localStorage.removeItem('auth_cache')
2020-06-10 20:20:47 +00:00
}
2020-06-10 22:29:16 +00:00
/** 同 步 请 求 获 取 数 据 */
function getData(url) {
2020-06-10 20:20:47 +00:00
2020-06-10 22:29:16 +00:00
$.ajaxSettings.async = false;
var data = null;
2020-06-10 20:20:47 +00:00
2020-06-10 22:29:16 +00:00
$.get(url, function(result) {
data = result;
});
2020-06-10 20:20:47 +00:00
2020-06-10 22:29:16 +00:00
$.ajaxSettings.async = true;
return data;
}
var doms = $("*[pear-has-permission]");
2020-06-10 20:20:47 +00:00
2020-06-10 22:29:16 +00:00
$.each(doms,function(i,dom){
var permission = $(dom).attr("pear-has-permission");
var b = false;
2020-06-10 20:20:47 +00:00
2020-06-10 22:29:16 +00:00
$.each(localStorage.getItem("auth_cache").split(","),function(i,auth){
if(auth == permission){
b = true;
return false;
}
})
if(!b){
$(dom).hide();
}
})
2020-06-10 20:20:47 +00:00
exports(MOD_NAME,new pearAuth());
})