123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import {formatCurrency} from '@angular/common';
- import {environment} from "../../../environments/environment";
- /***************
- * 公用函数处理
- */
- export class FuncService {
- /********************
- * 格式化中文货币
- */
- public static formatCurrency(v) {
- return formatCurrency(v, 'zh', '¥');
- }
- /********************
- * 将转字符串转化为日期 yyyy-MM-dd -> Date
- */
- public static shortStringToDate(date?) {
- if (date === undefined)
- date = new Date();
- if (date instanceof Date) {
- return date;
- }
- return new Date(Date.parse(FuncService.dateToShortString(date).replace(/-/g, '/')));
- }
- public static longStringToDate(date) {
- if (date instanceof Date) {
- return date;
- }
- if (date === undefined || date.length !== 19) {
- return undefined;
- }
- return new Date(Date.parse(FuncService.dateToLongString(date).replace(/-/g, '/')));
- }
- /***********************
- * 将日期变量进行格式化
- * @param date 日期变量
- * @param fmt 字符串格式
- */
- public static dateToString(date, fmt) {
- const o = {
- 'M+': date.getMonth() + 1,
- 'd+': date.getDate(),
- 'h+': date.getHours(),
- 'm+': date.getMinutes(),
- 's+': date.getSeconds(),
- 'q+': Math.floor((date.getMonth() + 3) / 3),
- 'S': date.getMilliseconds()
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + ``).substr(4 - RegExp.$1.length));
- }
- for (let k in o) {
- if (new RegExp(`(` + k + `)`).test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((`00` + o[k]).substr((`` + o[k]).length)));
- }
- }
- return fmt;
- }
- /*********************
- * 日期转换成长格式字符串 Date -> yyyy-MM-dd HH:mm:ss
- */
- public static dateToLongString(date?) {
- if (date === undefined)
- date = new Date();
- if (date instanceof Date) {
- return FuncService.dateToString(date, 'yyyy-MM-dd hh:mm:ss');
- }
- return '';
- }
- /*********************
- * 日期转换成段字符串 Date -> yyyy-MM-dd
- */
- public static dateToShortString(date?) {
- if (date === undefined) {
- date = new Date();
- }
- if (date instanceof Date) {
- date = FuncService.dateToLongString(date);
- }
- if (date.length > 10)
- return date.substr(0, 10);
- return date;
- }
- /***************************
- * 日期转换成 具体时间格式 Date -> HH:mm:ss
- */
- public static dateToTimeString(date?) {
- if (date === undefined) {
- date = new Date();
- }
- if (date instanceof Date) {
- date = FuncService.dateToLongString(date);
- }
- if (date.length > 10) {
- return date.substr(11);
- }
- return date;
- }
- /***************************
- * 日期转换成 小时分格式 Date -> HH:mm
- */
- public static dateToMinuteString(date?) {
- if (date === undefined) {
- date = new Date();
- }
- if (date instanceof Date) {
- date = FuncService.dateToLongString(date);
- }
- if (date.length > 10 && date.length >= 15) {
- return date.substr(11, 5);
- }
- return date;
- }
- /*************************
- * 两个日期 Date,Date -> HH:mm - HH:mm
- */
- public static dateToBetweenMinutes(start, end) {
- return FuncService.dateToMinuteString(start) + ' - ' + FuncService.dateToMinuteString(end);
- }
- /*********************
- * 数组排序
- * @param array 需要排序的数组
- * @param attr_name 用来排序的字段
- */
- public static sortArray(array, attr_name, order_attr?: any) {
- for (let i = 0; i < array.length - 1; i++) {
- for (let j = 0; j < array.length - 1 - i; j++) {
- if (array[j][attr_name] > array[j + 1][attr_name]) {
- const temp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temp;
- }
- }
- if (order_attr !== undefined) {
- array[order_attr] = i + 1;
- }
- }
- }
- /***********************
- * 文件下载 iframe方式,不需要考虑弹窗,但长时间等待可能会导致用户不知道下载线程正在处理
- */
- public static fileDownload(url: string, param?: any) {
- let html = '';
- for (const name in param) {
- if (typeof (param[name]) !== 'function' && name !== '$$hashKey') {
- const value = param[name];
- if (value !== null && value !== undefined) {
- html = html + (html === '' ? '?' : '&') + `${name}=${value}`;
- }
- }
- }
- const elemIF = document.createElement('iframe');
- elemIF.src = environment.uploadRoot + url + html;
- elemIF.style.display = 'none';
- document.body.appendChild(elemIF);
- setTimeout(() => {
- document.body.removeChild(elemIF);
- }, 1000);
- }
- /*********************
- * 弹窗方式,用户可知道线程正在运行,但会受到浏览器拦截,需要手动设置
- */
- public static ajaxDownload(url: string, param?: any) {
- let html = '<form action="' + environment.uploadRoot + url + '" method="post" target="_blank">';
- for (const name in param) {
- if (typeof (param[name]) !== 'function' && name !== '$$hashKey') {
- const value = param[name];
- if (value !== null && value !== undefined) {
- html = html + '<input type="hidden" name="' + name + '" value="' + value + '"/>';
- }
- }
- }
- html = html + '</form>';
- $(html).appendTo('body').submit().remove();
- }
- }
|