view-message-service.ts 8.6 KB
Newer Older
1 2 3
import { ViewMessage } from '@/interface/message';
import EntityService from '@/service/entity-service';

4 5 6 7 8 9 10 11
/**
 * 视图消息
 *
 * @export
 * @class ViewMessage
 */
export default class ViewMessageService {

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 53 54 55 56 57 58 59 60
    /**
     * 单例变量声明
     *
     * @private
     * @static
     * @type {ViewMessageService}
     * @memberof ViewMessageService
     */
    private static ViewMessage: ViewMessageService;

    /**
     * 实体数据服务对象
     *
     * @protected
     * @type {EntityService}
     * @memberof ViewMessageService
     */
    protected entityService:EntityService = new EntityService();

    /**
     * 视图消息标识
     *
     * @type {string}
     * @memberof ViewMessageService
     */
    public id:string ="";

    /**
     * 视图消息名称
     *
     * @type {string}
     * @memberof ViewMessageService
     */
    public name:string ="";

    /**
     * 视图消息代码名称
     *
     * @type {string}
     * @memberof ViewMessageService
     */
    public codename:string ="";

    /**
     * 视图消息标题
     *
     * @type {string}
     * @memberof ViewMessageService
     */
tony001's avatar
tony001 committed
61
    public title:string ="";
62 63 64 65 66 67 68 69 70 71

    /**
     * 视图消息内容
     *
     * @type {string}
     * @memberof ViewMessageService
     */
    public content:string ="";

    /**
tony001's avatar
tony001 committed
72
     * 视图消息关闭模式(0:无关闭,1:默认关闭,2:本次关闭)
73
     *
tony001's avatar
tony001 committed
74
     * @type {number}
75 76
     * @memberof ViewMessageService
     */
tony001's avatar
tony001 committed
77
    public closeMode:number = 0;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    /**
     * 视图消息位置
     *
     * @type {string}
     * @memberof ViewMessageService
     */
    public position:string ="";

    /**
     * 视图消息类型
     *
     * @type {string}
     * @memberof ViewMessageService
     */
93
    public type:string = "info";
94

95 96 97 98 99 100 101 102
    /**
     * 视图消息是否支持删除
     *
     * @type {boolean}
     * @memberof ViewMessageService
     */
    public isEnableRemove:boolean = true;

tony001's avatar
tony001 committed
103 104 105 106 107 108 109 110
    /**
     * 视图消息排序值
     *
     * @type {boolean}
     * @memberof ViewMessageService
     */
    public order:number = 1;

111 112 113 114 115 116 117 118
    /**
     * 动态模式
     *
     * @type {string}
     * @memberof ViewMessageService
     */
    public dynamicMode:string = "STATIC";

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    /**
     * 消息类型(可选值:TEXT/HTML)
     *
     * @type {string}
     * @memberof ViewMessageService
     */
    public messageType:string = "TEXT";

    /**
     * 是否含有消息模板
     *
     * @type {boolean}
     * @memberof ViewMessageService
     */
    public hasMessageTemp:boolean = false;

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    /**
     * 视图消息缓存(加载中)
     *
     * @type {Map<string,any>}
     * @memberof ViewMessageService
     */
    public static messageCache:Map<string,any> = new Map();

    /**
     * 视图消息缓存(已完成)
     *
     * @type {Map<string,any>}
     * @memberof ViewMessageService
     */
    public static messageCached:Map<string,any> = new Map();

151 152 153 154 155
    /**
     * 初始化实例
     * 
     * @memberof ViewMessageService
     */
156
    constructor(opts: any = {}) {
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        this.initBasicParam();
    }

    /**
     * 获取 ViewMessageService 单例对象
     *
     * @static
     * @returns {ViewMessageService}
     * @memberof ViewMessageService
     */
    public static getInstance(): ViewMessageService {
        if (!ViewMessageService.ViewMessage) {
            ViewMessageService.ViewMessage = new ViewMessageService();
        }
        return this.ViewMessage;
    }

    /**
     * 初始化基础参数
     * 
     * @memberof ViewMessageService
     */
    public initBasicParam(){}

181 182 183 184 185 186 187 188 189 190 191 192
    /**
     * 获取视图消息服务
     *
     * @protected
     * @param {string} name 视图消息codename
     * @returns {Promise<any>}
     * @memberof ViewMessageService
     */
    public getService(name: string): Promise<any> {
        return (window as any)['messageServiceRegister'].getService(name);
    }

193 194 195 196 197 198 199 200
    /**
     * 通过tag获取视图消息
     * 
     * @param {tag:string} 视图消息标识
     * @param {context:any} 导航上下文
     * @param {viewparam:any} 导航参数
     * @memberof ViewMessageService
     */
201
    public async getViewMessageByTag(tag:string,context:any = {},viewparam:any = {}){
202 203
        let messageService:any = await this.getService(tag);
        if(messageService.dynamicMode && Object.is(messageService.dynamicMode,"STATIC")){
204
            return messageService.getStaticViewMessage();
205
        }else{
206
            return messageService.getDynamicViewMessage(tag,messageService,context,viewparam);
207
        }
208 209
    }

210 211 212 213 214 215
    /**
     * 转化消息模板标题和内容
     * 
     * @memberof ViewMessageService
     */
    public translateMessageTemp(target:any,item?:any){
216
        
217 218
    }

219 220 221 222 223 224 225 226 227 228
    /**
     * 获取动态模式(静态)类型视图消息
     * 
     * @memberof ViewMessageService
     */
    public getStaticViewMessage():Array<ViewMessage>{
        let returnViewMessage:ViewMessage ={
            id:this.id,
            name:this.name,
            codename:this.codename,
tony001's avatar
tony001 committed
229
            title:this.title,
230 231 232 233
            content:this.content,
            closeMode:this.closeMode,
            position:this.position,
            type:this.type,
234
            isEnableRemove:this.isEnableRemove,
tony001's avatar
tony001 committed
235
            order:this.order,
236 237
            dynamicMode:this.dynamicMode,
            messageType:this.messageType
238
        };
239
        this.translateMessageTemp(returnViewMessage);
240 241 242 243 244
        return [returnViewMessage];
    }

    /**
     * 获取动态模式(实体数据集合)类型视图消息
245 246 247 248 249
     *
     * @param {any} tag 视图消息标识
     * @param {any} messageService 消息服务
     * @param {string} context
     * @returns {Promise<any[]>}
250 251
     * @memberof ViewMessageService
     */
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    public getDynamicViewMessage(tag:string,messageService: any,context:any = {}, data: any = {}, isloading?: boolean): Promise<any[]> {
        if(context && context.srfsessionid){
            delete context.srfsessionid;
        }
        return new Promise((resolve:any,reject:any) =>{
            let isEnableCache:boolean = messageService.isEnableCache;
            let cacheTimeout:any = messageService.cacheTimeout;
            // 启用缓存
            if(isEnableCache){
                const callback:Function = (context:any ={},data:any ={},tag:string,promise:Promise<any>) =>{
                    promise.then((result:any) =>{
                        if(result.length > 0){
                            ViewMessageService.messageCached.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,{items:result});
                            ViewMessageService.messageCache.delete(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`);
                            return resolve(result);
                        }else{
                            return resolve([]);
                        }
                    }).catch((result:any) =>{
                        return reject(result);
                    })
                }
                // 加载完成,从本地缓存获取
                if(ViewMessageService.messageCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){
                    let items:any = ViewMessageService.messageCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`).items;
                    if(items.length >0){
                        if(new Date().getTime() <= messageService.getExpirationTime()){
                            return resolve(items); 
                        }
                    }
                }
                if (messageService) {
                    // 加载中,UI又需要数据,解决连续加载同一代码表问题
                    if(ViewMessageService.messageCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){
                        callback(context,data,tag,ViewMessageService.messageCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`));
                    }else{
                        let result:Promise<any> = messageService.getItems(context,data,isloading);
                        ViewMessageService.messageCache.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,result);
                        messageService.setExpirationTime(new Date().getTime() + cacheTimeout);
                        callback(context,data,tag,result);
                    }
                }
            }else{
                if (messageService) {
                    messageService.getItems(context,data,isloading).then((result:any) =>{
                        resolve(result);
                    }).catch((error:any) =>{
                        Promise.reject([]);
                    })
                }else{
                    return Promise.reject([]);
                } 
            }
        })
    } 
307 308

}