mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2026-01-04 01:18:12 +00:00
整理
This commit is contained in:
parent
dc8d4ecece
commit
a737d24d0e
137
src/comm/utils.ts
Normal file
137
src/comm/utils.ts
Normal file
@ -0,0 +1,137 @@
|
||||
import { Inject, Provide } from '@midwayjs/decorator';
|
||||
import { Context } from '@midwayjs/koa';
|
||||
import * as _ from 'lodash';
|
||||
import * as moment from 'moment';
|
||||
|
||||
/**
|
||||
* 帮助类
|
||||
*/
|
||||
@Provide()
|
||||
export class Utils {
|
||||
@Inject()
|
||||
baseDir;
|
||||
|
||||
/**
|
||||
* 获得请求IP
|
||||
*/
|
||||
async getReqIP(ctx: Context) {
|
||||
const req = ctx.req;
|
||||
return (
|
||||
req.headers['x-forwarded-for'] ||
|
||||
req.socket.remoteAddress.replace('::ffff:', '')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除对象的空值属性
|
||||
* @param obj
|
||||
*/
|
||||
async removeEmptyP(obj) {
|
||||
Object.keys(obj).forEach(key => {
|
||||
if (obj[key] === null || obj[key] === '' || obj[key] === 'undefined') {
|
||||
delete obj[key];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 线程阻塞毫秒数
|
||||
* @param ms
|
||||
*/
|
||||
sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得最近几天的日期集合
|
||||
* @param recently
|
||||
*/
|
||||
getRecentlyDates(recently, format = 'YYYY-MM-DD') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
for (let i = 0; i < recently; i++) {
|
||||
dates.push(moment().subtract(i, 'days').format(format));
|
||||
}
|
||||
return dates.reverse();
|
||||
}
|
||||
/**
|
||||
* 获得最近几个月的月数
|
||||
* @param recently
|
||||
*/
|
||||
getRecentlyMonths(recently, format = 'YYYY-MM') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const date = moment(Date.now()).format('YYYY-MM');
|
||||
for (let i = 0; i < recently; i++) {
|
||||
dates.push(moment(date).subtract(i, 'months').format(format));
|
||||
}
|
||||
return dates.reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的日期集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenDays(start, end, format = 'YYYY-MM-DD') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const days = moment(endTime).diff(moment(startTime), 'days');
|
||||
for (let i = 0; i <= days; i++) {
|
||||
dates.push(moment(startTime).add(i, 'days').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的月份集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenMonths(start, end, format = 'YYYY-MM') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const months = moment(endTime).diff(moment(startTime), 'months');
|
||||
for (let i = 0; i <= months; i++) {
|
||||
dates.push(moment(startTime).add(i, 'months').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的小时集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenHours(start, end, format = 'YYYY-MM-DD HH') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const hours = moment(endTime).diff(moment(startTime), 'hours');
|
||||
for (let i = 0; i <= hours; i++) {
|
||||
dates.push(moment(startTime).add(i, 'hours').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段转驼峰法
|
||||
* @param obj
|
||||
* @returns
|
||||
*/
|
||||
toCamelCase(obj) {
|
||||
const camelCaseObject = {};
|
||||
for (const i in obj) {
|
||||
const camelCase = i.replace(/([-_][a-z])/gi, $1 => {
|
||||
return $1.toUpperCase().replace('-', '').replace('_', '');
|
||||
});
|
||||
camelCaseObject[camelCase] = obj[i];
|
||||
}
|
||||
return camelCaseObject;
|
||||
}
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
import { Inject, Controller, Get, Query } from '@midwayjs/core';
|
||||
import { Context } from '@midwayjs/koa';
|
||||
import { UserService } from '../service/user.service';
|
||||
|
||||
@Controller('/api')
|
||||
export class APIController {
|
||||
@Inject()
|
||||
ctx: Context;
|
||||
|
||||
@Inject()
|
||||
userService: UserService;
|
||||
|
||||
@Get('/get_user')
|
||||
async getUser(@Query('uid') uid) {
|
||||
const user = await this.userService.getUser({ uid });
|
||||
return { success: true, message: 'OK', data: user };
|
||||
}
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
import { Controller, Get } from '@midwayjs/core';
|
||||
|
||||
@Controller('/')
|
||||
export class HomeController {
|
||||
@Get('/')
|
||||
async home(): Promise<string> {
|
||||
return 'Hello Midwayjs!';
|
||||
}
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
import { Catch } from '@midwayjs/core';
|
||||
import { Context } from '@midwayjs/koa';
|
||||
|
||||
@Catch()
|
||||
export class DefaultErrorFilter {
|
||||
async catch(err: Error, ctx: Context) {
|
||||
// 所有的未分类错误会到这里
|
||||
return {
|
||||
success: false,
|
||||
message: err.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
import { Catch, httpError, MidwayHttpError } from '@midwayjs/core';
|
||||
import { Context } from '@midwayjs/koa';
|
||||
|
||||
@Catch(httpError.NotFoundError)
|
||||
export class NotFoundFilter {
|
||||
async catch(err: MidwayHttpError, ctx: Context) {
|
||||
// 404 错误会到这里
|
||||
ctx.redirect('/404.html');
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,6 @@
|
||||
/** This file generated by @midwayjs/bundle-helper */
|
||||
export { MainConfiguration as Configuration } from './configuration';
|
||||
|
||||
export { MainConfiguration as Configuration } from './configuration';
|
||||
export * from './config/config.default';
|
||||
export * from './config/config.local';
|
||||
export * from './middleware/report.middleware';
|
||||
export * from './interface';
|
||||
export * from './service/user.service';
|
||||
export * from './controller/api.controller';
|
||||
export * from './controller/home.controller';
|
||||
export * from './filter/default.filter';
|
||||
export * from './filter/notfound.filter';
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
import { Middleware, IMiddleware } from '@midwayjs/core';
|
||||
import { NextFunction, Context } from '@midwayjs/koa';
|
||||
|
||||
@Middleware()
|
||||
export class ReportMiddleware implements IMiddleware<Context, NextFunction> {
|
||||
resolve() {
|
||||
return async (ctx: Context, next: NextFunction) => {
|
||||
// 控制器前执行的逻辑
|
||||
const startTime = Date.now();
|
||||
// 执行下一个 Web 中间件,最后执行到控制器
|
||||
// 这里可以拿到下一个中间件或者控制器的返回值
|
||||
const result = await next();
|
||||
// 控制器之后执行的逻辑
|
||||
ctx.logger.info(
|
||||
`Report in "src/middleware/report.middleware.ts", rt = ${
|
||||
Date.now() - startTime
|
||||
}ms`
|
||||
);
|
||||
// 返回给上一个中间件的结果
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
static getName(): string {
|
||||
return 'report';
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
import { Provide } from '@midwayjs/core';
|
||||
import { IUserOptions } from '../interface';
|
||||
|
||||
@Provide()
|
||||
export class UserService {
|
||||
async getUser(options: IUserOptions) {
|
||||
return {
|
||||
uid: options.uid,
|
||||
username: 'mockedName',
|
||||
phone: '12345678901',
|
||||
email: 'xxx.xxx@xxx.com',
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user