import { Response } from '@angular/http'; import { ViewChild } from '@angular/core'; import { Utils } from './utils'; import { MessageBoxComponent } from './messagebox.component'; declare var calLang: any, fileDownloadRoot: string, isRunning: number; export abstract class BaseComponent { @ViewChild(MessageBoxComponent) messageBox: MessageBoxComponent; calLang = calLang; static MSG_SAVED_KEY = "msg.saved.success"; static MSG_DELETED_KEY = "msg.deleted.success"; /** * 解析国际化消息 * @param key 键 * @param params 参数 */ resolveMessage(key: string, ...params: string[]): string { return Utils.resolveLocalizedMessage(key, params); } /** * 生成下载链接。 * @param uri 附件的相对路径 */ resolveDownloadUrl(uri: string): string { return fileDownloadRoot + uri; } /** * 格式化日期 * @param date 日期 * @param pattern 格式,如 'yyyy-MM-dd hh:mm:ss.S', 'yyyy年qq季度' * */ formatDate(date: Date, pattern: string): string { if (date) { return Utils.formatDate(date, pattern); } return ""; } /** * 处理错误 * @param error 错误对象 */ protected handleError(error: any, mBox: MessageBoxComponent = this.messageBox): void { if (this && this.messageBox) { this.messageBox.showMessage("error", error); } else if (mBox) { mBox.showMessage("error", error); } else { alert(error); } } /** * 显示错误消息 * @param message 消息 */ protected showError(message: string) { this.showMessage(message, "error"); } /** * i18n的显示警告消息 * @param messageKey 要显示的消息key */ protected showWarnKey(messageKey: string) { this.showMessage(this.resolveMessage(messageKey), "warn"); } /** * i18n的显示成功消息 * @param messageKey 要显示的消息key */ protected showSuccessKey(messageKey: string) { this.showMessage(this.resolveMessage(messageKey), "success"); } /** * i18n的显示通知消息 * @param messageKey 要显示的消息key */ protected showInfoKey(messageKey: string) { this.showMessage(this.resolveMessage(messageKey), "info"); } /** * i18n的显示错误消息 * @param messageKey 要显示的消息key */ protected showErrorKey(messageKey: string) { this.showMessage(this.resolveMessage(messageKey), "error"); } /** * 显示消息 * @param message 要显示的消息 * @param style 消息风格,"warn","success","info","error" */ protected showMessage(message: string, style: string) { if (this && this.messageBox) { this.messageBox.showMessage(style, message); } else { alert(message); } } /** * 获取请求串中的参数值 * @param name 参数名称 * */ protected getRequestParamValue(name: string): string { let result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i")); if (result == null || result.length < 1) { return null; } return result[1]; } /** * 获取请求串中的参数值数组,如a=x&a=y, 则参数a的值为['x','y'] * @param name 参数名称 */ protected getRequestParamValues(name: string): string[] { let result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i")); if (result == null || result.length < 1) { return null; } return result; } /** * 判断是否有异步任务正在执行,以便界面显示相关信息,让用户等待 */ isRunning(): boolean { return isRunning > 0; } }