项目初始化

This commit is contained in:
Taric Xin
2021-11-26 16:34:35 +08:00
parent 66644bcf0a
commit 5287578452
354 changed files with 45736 additions and 0 deletions

View File

@ -0,0 +1,56 @@
export class EADateUtil {
/**
* @description 时间戳转换日期
* @param timestamp 时间戳
* @returns 时间戳对应的日期
*/
static timestampToDate(timestamp: number): Date {
let date;
// 时间戳为10位需*1000时间戳为13位的话不需乘1000
if (timestamp.toString().length === 10) {
date = new Date(timestamp * 1000);
} else {
date = new Date(timestamp);
}
const Y = date.getFullYear();
const M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
const D = date.getDate();
const h = date.getHours();
const m = date.getMinutes();
const s = date.getSeconds();
const dateStr = `${Y}-${M}-${D} ${h}:${m}:${s}`;
return new Date(dateStr);
}
/**
* @description 日期相加
* @param start 开始日期
* @param intervalMS 间隔秒数(毫秒)
* @returns 相加后的日期
*/
static dateAdd(start: Date, intervalMS: number): Date {
return this.timestampToDate(start.getTime() + intervalMS);
}
/**
* @description 日期相减
* @param start 开始日期
* @param intervalMS 时间间隔(毫秒)
* @returns 相减后的日期
*/
static dateMinus(start: Date, intervalMS: number): Date {
return this.timestampToDate(start.getTime() - intervalMS);
}
/**
* @description 计算两个日期的时间差
* @param start 开始日期
* @param end 结束日期
* @returns 两时间相差的毫秒数
*/
static dateDiff(start: Date, end: Date): number {
return start.getTime() - end.getTime();
}
}

View File

@ -0,0 +1,76 @@
import { Injectable } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import * as FileSaver from 'file-saver';
import { NzModalService } from 'ng-zorro-antd/modal';
@Injectable({
providedIn: 'root',
})
export class EAFileUtil {
constructor(private modal: NzModalService, private http: _HttpClient) {}
/**
* 保存文件
* @param content 需要保存的内容
* @param filename 文件名
* @param confirm 显示确认框
*/
save(content: any, filename: string, confirm: boolean = true): void {
const blob = new Blob([JSON.stringify(content)], { type: 'text/plain;charset=utf-8' });
if (confirm === true) {
this.modal.confirm({
nzTitle: '<i>文件下载确认</i>',
nzContent: `<b>是否下载文件:<u><i>${filename}</i></u> </b>`,
nzOnOk: () => {
FileSaver.saveAs(blob, filename);
},
});
} else {
FileSaver.saveAs(blob, filename);
}
}
/**
* @param url 接口地址
* @param body post请求参数
* @param params get请求参数
* @method 请求方法
*/
download(url: string, body: any, params: any, method: 'POST' | 'GET' = 'POST'): void {
if (method === 'POST') {
this.http
.post(url, body, params, {
responseType: 'blob',
observe: 'response',
})
.subscribe((res) => {
this.saveBlob(res);
});
} else if (method === 'GET') {
this.http
.get(url, params, {
responseType: 'blob',
observe: 'response',
})
.subscribe((res) => {
this.saveBlob(res);
});
}
}
/**
* 创建blob对象并利用浏览器打开url进行下载
* @param data 文件流数据
*/
private saveBlob(res: any): void {
const disp = res.headers.get('Content-Disposition');
const blob = new Blob([res.body], { type: 'text/plain;charset=utf-8' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
const fileName = disp.split(';')[1].split('=')[1];
a.href = url;
a.download = decodeURI(fileName);
a.click();
window.URL.revokeObjectURL(url);
}
}

View File

@ -0,0 +1,4 @@
export * from './date.util';
export * from './file.util';
export * from './processSingleSort.func';
export * from './yuan';

View File

@ -0,0 +1,38 @@
/*
* @Author: Maple
* @Date: 2021-02-27 17:40:54
* @LastEditors: Do not edit
* @LastEditTime: 2021-03-23 20:58:18
* @Description: 处理ST表格的单列排序
*/
import { STRequestOptions } from '@delon/abc/st';
/**
* 处理ST表格单条件排序
* @param reqOtions ST的Req
* @param sortField 排序字段默认为sortType
* @returns ST的Req
*/
export function processSingleSort(reqOtions: STRequestOptions, sortField: string = 'sort'): STRequestOptions {
if (!reqOtions.body) {
return reqOtions;
}
// 获取排序字段
const body: any = {};
for (const _key of Object.keys(reqOtions.body)) {
if (typeof reqOtions.body[_key] === 'string') {
const value: string = reqOtions.body[_key]?.trim();
if (value && (value === 'ascend' || value === 'descend')) {
body[sortField] = `${_key}.${value}`;
} else {
body[_key] = reqOtions.body[_key];
}
} else {
body[_key] = reqOtions.body[_key];
}
}
if (JSON.stringify(body) !== '{}') {
reqOtions.body = body;
}
return reqOtions;
}

View File

@ -0,0 +1,11 @@
/**
* 转化成RMB元字符串
*
* @param digits 当数字类型时允许指定小数点后数字的个数默认2位小数
*/
export function yuan(value: number | string, digits: number = 2): string {
if (typeof value === 'number') {
value = value.toFixed(digits);
}
return `&yen ${value}`;
}