UNPKG

3.57 kBPlain TextView Raw
1import { Response } from '@angular/http';
2import { ViewChild } from '@angular/core';
3
4import { Utils } from './utils';
5import { MessageBoxComponent } from './messagebox.component';
6
7declare var calLang: any, fileDownloadRoot: string, isRunning: number;
8export abstract class BaseComponent {
9 @ViewChild(MessageBoxComponent) messageBox: MessageBoxComponent;
10 calLang = calLang;
11 static MSG_SAVED_KEY = "msg.saved.success";
12 static MSG_DELETED_KEY = "msg.deleted.success";
13 /**
14 * 解析国际化消息
15 * @param key 键
16 * @param params 参数
17 */
18 resolveMessage(key: string, ...params: string[]): string {
19 return Utils.resolveLocalizedMessage(key, params);
20 }
21 /**
22 * 生成下载链接。
23 * @param uri 附件的相对路径
24 */
25 resolveDownloadUrl(uri: string): string {
26 return fileDownloadRoot + uri;
27 }
28 /**
29 * 格式化日期
30 * @param date 日期
31 * @param pattern 格式,如 'yyyy-MM-dd hh:mm:ss.S', 'yyyy年qq季度'
32 *
33 */
34 formatDate(date: Date, pattern: string): string {
35 if (date) {
36 return Utils.formatDate(date, pattern);
37 }
38 return "";
39 }
40 /**
41 * 处理错误
42 * @param error 错误对象
43 */
44 protected handleError(error: any, mBox: MessageBoxComponent = this.messageBox): void {
45 if (this && this.messageBox) {
46 this.messageBox.showMessage("error", error);
47 } else if (mBox) {
48 mBox.showMessage("error", error);
49 } else {
50 alert(error);
51 }
52 }
53 /**
54 * 显示错误消息
55 * @param message 消息
56 */
57 protected showError(message: string) {
58 this.showMessage(message, "error");
59 }
60 /**
61 * i18n的显示警告消息
62 * @param messageKey 要显示的消息key
63 */
64 protected showWarnKey(messageKey: string) {
65 this.showMessage(this.resolveMessage(messageKey), "warn");
66 }
67 /**
68 * i18n的显示成功消息
69 * @param messageKey 要显示的消息key
70 */
71 protected showSuccessKey(messageKey: string) {
72 this.showMessage(this.resolveMessage(messageKey), "success");
73 }
74 /**
75 * i18n的显示通知消息
76 * @param messageKey 要显示的消息key
77 */
78 protected showInfoKey(messageKey: string) {
79 this.showMessage(this.resolveMessage(messageKey), "info");
80 }
81 /**
82 * i18n的显示错误消息
83 * @param messageKey 要显示的消息key
84 */
85 protected showErrorKey(messageKey: string) {
86 this.showMessage(this.resolveMessage(messageKey), "error");
87 }
88 /**
89 * 显示消息
90 * @param message 要显示的消息
91 * @param style 消息风格,"warn","success","info","error"
92 */
93
94 protected showMessage(message: string, style: string) {
95 if (this && this.messageBox) {
96 this.messageBox.showMessage(style, message);
97 } else {
98 alert(message);
99 }
100 }
101 /**
102 * 获取请求串中的参数值
103 * @param name 参数名称
104 *
105 */
106 protected getRequestParamValue(name: string): string {
107 let result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
108 if (result == null || result.length < 1) {
109 return null;
110 }
111 return result[1];
112 }
113 /**
114 * 获取请求串中的参数值数组,如a=x&a=y, 则参数a的值为['x','y']
115 * @param name 参数名称
116 */
117 protected getRequestParamValues(name: string): string[] {
118 let result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
119 if (result == null || result.length < 1) {
120 return null;
121 }
122 return result;
123 }
124 /**
125 * 判断是否有异步任务正在执行,以便界面显示相关信息,让用户等待
126 */
127 isRunning(): boolean {
128 return isRunning > 0;
129 }
130}
\No newline at end of file