codelist-service.ts.ftl 5.9 KB
Newer Older
1 2 3 4 5 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 49 50 51 52
<#ibiztemplate>
TARGET=PSSYSAPP
</#ibiztemplate>
<#if app.getAllPSAppCodeLists()??>
    <#list app.getAllPSAppCodeLists() as codelist>
        <#if codelist.getCodeListType() == "DYNAMIC">
import ${srfclassname('${codelist.codeName}')} from '@/codelist/${srffilepath2(codelist.codeName)}';   
        </#if>
    </#list>
</#if>
import { Store } from 'vuex';

/**
 * 动态代码表服务类
 *
 * @export
 * @class CodeListService
 */
export default class CodeListService {

    /**
     * Vue 状态管理器
     *
     * @private
     * @type {(any | null)}
     * @memberof CodeListService
     */
    private $store: Store<any> | null = null;

    constructor(opts: any = {}) {
        this.$store = opts.$store;
    }

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


    /**
     * 动态代码表缓存(加载中)
     *
     * @type {Map<string,any>}
     * @memberof CodeListService
     */
    public static codelistCache:Map<string,any> = new Map();

tony001's avatar
tony001 committed
53 54 55 56 57 58 59 60
    /**
     * 动态代码表缓存(已完成)
     *
     * @type {Map<string,any>}
     * @memberof CodeListService
     */
    public static codelistCached:Map<string,any> = new Map();

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<#if app.getAllPSAppCodeLists()??>
    <#list app.getAllPSAppCodeLists() as codelist>
        <#if codelist.getCodeListType() == "DYNAMIC">

    /**
     * 代码表--${codelist.getName()}
     *
     * @type {${srfclassname('${codelist.codeName}')}}
     * @memberof CodeListService
     */
    public ${codelist.codeName}: ${srfclassname('${codelist.codeName}')} = new ${srfclassname('${codelist.codeName}')}();
        </#if>
    </#list>
</#if>

    /**
     * 获取动态代码表
     *
     * @param {string} tag 代码表标识
     * @param {string} context
     * @returns {Promise<any[]>}
     * @memberof CodeListService
     */
tony001's avatar
tony001 committed
84
    <#noparse>public getItems(tag: string,context:any = {}, data?: any, isloading?: boolean,): Promise<any[]> {
85
        let _this: any = this;
tony001's avatar
tony001 committed
86 87 88
        if(context && context.srfsessionid){
            delete context.srfsessionid;
        }
89 90 91 92 93
        let isEnableCache:boolean = _this[tag].isEnableCache;
        let cacheTimeout:any = _this[tag].cacheTimeout;
        return new Promise((resolve:any,reject:any) =>{
                // 启用缓存
                if(isEnableCache){
tony001's avatar
tony001 committed
94 95 96
                    // 加载完成,从本地缓存获取
                    if(CodeListService.codelistCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){
                        let items:any = CodeListService.codelistCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`);
97 98 99 100
                        if(items.length >0){
                            if(cacheTimeout !== -1){
                                if(new Date().getTime() > _this[tag].expirationTime){
                                    _this[tag].getItems(context,data,isloading).then((result:any) =>{
tony001's avatar
tony001 committed
101
                                        CodeListService.codelistCached.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,{items:result});
102 103 104 105 106 107 108 109 110 111 112 113 114 115
                                        _this[tag].expirationTime = new Date().getTime() + cacheTimeout;
                                        resolve(result);
                                    }).catch((error:any) =>{
                                        Promise.reject([]);
                                    })
                                }else{
                                    return resolve(items); 
                                }
                            }else{
                                return resolve(items);
                            }
                        }
                    }
                    if (_this[tag]) {
tony001's avatar
tony001 committed
116
                        const callback:Function = (context:any ={},data:any ={},tag:string,promise:Promise<any>) =>{
117
                            promise.then((result:any) =>{
tony001's avatar
tony001 committed
118
                                console.log()
119
                                if(result.length > 0){
tony001's avatar
tony001 committed
120
                                    CodeListService.codelistCached.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,{items:result});
121 122 123 124 125 126 127 128 129
                                    return resolve(result);
                                }else{
                                    return resolve([]);
                                }
                            }).catch((result:any) =>{
                                return reject(result);
                            })
                        }
                        // 加载中,UI又需要数据,解决连续加载同一代码表问题
tony001's avatar
tony001 committed
130 131
                        if(CodeListService.codelistCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){
                            callback(context,data,tag,CodeListService.codelistCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`));
132 133
                        }else{
                            let result:Promise<any> = _this[tag].getItems(context,data,isloading);
tony001's avatar
tony001 committed
134
                            CodeListService.codelistCache.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,result);
135 136 137
                            if(cacheTimeout !== -1){
                                _this[tag].expirationTime = new Date().getTime() + cacheTimeout;
                            }
tony001's avatar
tony001 committed
138
                            callback(context,data,tag,result);
139 140 141 142 143 144 145 146 147 148 149 150 151 152
                        }
                    }
                }else{
                    if (_this[tag]) {
                        _this[tag].getItems(context,data,isloading).then((result:any) =>{
                            resolve(result);
                        }).catch((error:any) =>{
                            Promise.reject([]);
                        })
                    }else{
                        return Promise.reject([]);
                    } 
                }
        })
tony001's avatar
tony001 committed
153
    }</#noparse>
154
}