auth-service.ts.ftl 3.9 KB
Newer Older
1 2 3
<#ibiztemplate>
TARGET=PSSYSAPP
</#ibiztemplate>
4
import store from '@/store';
5
import { Environment } from '@/environments/environment';
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/**
 * 实体权限服务
 *
 * @export
 * @class AuthService
 */
export default class AuthService {

    /**
     * Vue 状态管理器
     *
     * @public
     * @type {(any | null)}
     * @memberof AuthService
     */
    public $store: any;

    /**
     * 系统操作标识映射统一资源Map
     *
     * @public
     * @type {Map<string,any>}
     * @memberof AuthService
     */
    public sysOPPrivsMap:Map<string,any> = new  Map();

    /**
     * 默认操作标识
     *
     * @public
     * @type {(any)}
     * @memberof AuthService
     */
    public defaultOPPrivs: any = {<#if sys.getAllPSDEOPPrivs()??><#list sys.getAllPSDEOPPrivs() as sysOPPrivs>${sysOPPrivs.getName()}: 1<#if sysOPPrivs_has_next>,</#if></#list></#if>}; 

    /**
     * Creates an instance of AuthService.
     * 
     * @param {*} [opts={}]
     * @memberof AuthService
     */
    constructor(opts: any = {}) {
49
        this.$store = store;
50 51 52 53 54 55 56 57 58 59 60 61 62
        this.registerSysOPPrivs();
    }

    /**
     * 获取状态管理器
     *
     * @returns {(any | null)}
     * @memberof AuthService
     */
    public getStore(): any {
        return this.$store;
    }

63 64 65 66 67 68 69 70 71 72 73
    /**
     * 获取计算统一资源之后的系统操作标识
     *
     * @returns {}
     * @memberof AuthService
     */
    public getSysOPPrivs(){
        let copySysOPPrivs:any = JSON.parse(JSON.stringify(this.defaultOPPrivs));
        if(Object.keys(copySysOPPrivs).length === 0) return {};
        Object.keys(copySysOPPrivs).forEach((name:any) =>{
            if(this.sysOPPrivsMap.get(name)){
74
                copySysOPPrivs[name] = this.getResourcePermission(this.sysOPPrivsMap.get(name))?1:0;
75 76 77 78 79
            }
        })
        return copySysOPPrivs;
    }

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    /**
     * 获取实体权限服务
     *
     * @param {string} name 实体名称
     * @returns {Promise<any>}
     * @memberof AuthService
     */
    public getService(name: string): Promise<any> {
        return (window as any)['authServiceRegister'].getService(name);
    }

    /**
     * 注册系统操作标识统一资源
     *
     * @param {string} name 实体名称
     * @returns {Promise<any>}
     * @memberof AuthService
     */ 
    public registerSysOPPrivs(){
        <#if sys.getAllPSDEOPPrivs()??>
        <#list sys.getAllPSDEOPPrivs() as sysOPPrivs>
        <#if sysOPPrivs.getMapPSSysUniRes()??>
        this.sysOPPrivsMap.set('${sysOPPrivs.getName()}','${sysOPPrivs.getMapPSSysUniRes().getResCode()}');
        </#if>
        </#list>
        </#if>
    }

    /**
     * 根据当前数据获取实体操作标识
     *
     * @param {string} name 实体名称
     * @returns {any}
     * @memberof AuthService
     */
    public getOPPrivs(data: any): any {
        return null;
    }

    /**
     * 根据菜单项获取菜单权限
     *
     * @param {*} item 菜单标识
     * @returns {boolean}
     * @memberof AuthService
     */
    public getMenusPermission(item: any): boolean {
tony001's avatar
tony001 committed
127 128 129
        if(!this.$store.getters['authresource/getEnablePermissionValid']) {
            return true;
        }
130 131 132 133 134 135 136
        if(Object.is(Environment.menuPermissionMode,"RT")){
            return this.$store.getters['authresource/getAuthMenuWithRT'](item);
        }else if(Object.is(Environment.menuPermissionMode,"RESOURCE")){
            return this.$store.getters['authresource/getAuthMenuWithResource'](item);
        }else{
            return this.$store.getters['authresource/getAuthMenu'](item);
        }
137 138 139 140 141 142 143 144 145 146
    }

    /**
     * 根据统一资源标识获取统一资源权限
     *
     * @param {*} tag 统一资源标识
     * @returns {boolean}
     * @memberof AuthService
     */
    public getResourcePermission(tag: any): boolean {
tony001's avatar
tony001 committed
147 148 149
        if(!this.$store.getters['authresource/getEnablePermissionValid']) {
            return true;
        }
150 151 152 153
        return this.$store.getters['authresource/getResourceData'](tag);
    }

}