|
@@ -1,10 +1,12 @@
|
|
-import { Inject, Provide } from '@midwayjs/core';
|
|
|
|
|
|
+import { Config, Inject, Provide } from '@midwayjs/core';
|
|
import { get, union } from 'lodash';
|
|
import { get, union } from 'lodash';
|
|
import { CollectionService } from './platform/collection.service';
|
|
import { CollectionService } from './platform/collection.service';
|
|
import { UserService } from './system/user.service';
|
|
import { UserService } from './system/user.service';
|
|
import { Context } from '@midwayjs/koa';
|
|
import { Context } from '@midwayjs/koa';
|
|
import { MatchService } from './platform/match.service';
|
|
import { MatchService } from './platform/match.service';
|
|
-
|
|
|
|
|
|
+import { AdminService } from './system/admin.service';
|
|
|
|
+import * as Excel from 'exceljs';
|
|
|
|
+import * as Path from 'path';
|
|
/**
|
|
/**
|
|
* 工具类服务,为其他地方提供一些通用的函数
|
|
* 工具类服务,为其他地方提供一些通用的函数
|
|
*/
|
|
*/
|
|
@@ -13,6 +15,8 @@ export class ServiceUtilService {
|
|
@Inject()
|
|
@Inject()
|
|
ctx: Context;
|
|
ctx: Context;
|
|
@Inject()
|
|
@Inject()
|
|
|
|
+ adminService: AdminService;
|
|
|
|
+ @Inject()
|
|
userService: UserService;
|
|
userService: UserService;
|
|
@Inject()
|
|
@Inject()
|
|
collectionService: CollectionService;
|
|
collectionService: CollectionService;
|
|
@@ -65,4 +69,62 @@ export class ServiceUtilService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Config('PathConfig.path')
|
|
|
|
+ path;
|
|
|
|
+
|
|
|
|
+ async initPassword() {
|
|
|
|
+ const { data: admins } = await this.adminService.query();
|
|
|
|
+ const { data: users } = await this.userService.query();
|
|
|
|
+ let workbook = new Excel.Workbook();
|
|
|
|
+ const head = ['登录账号', '密码'];
|
|
|
|
+ const adminSheet = workbook.addWorksheet('管理员密码');
|
|
|
|
+ const rows = [head];
|
|
|
|
+ for (const i of admins) {
|
|
|
|
+ const password = this.makePassword();
|
|
|
|
+ const arr = [i.account, password];
|
|
|
|
+ await this.adminService.update({ id: i.id }, { password });
|
|
|
|
+ rows.push(arr);
|
|
|
|
+ }
|
|
|
|
+ adminSheet.addRows(rows);
|
|
|
|
+ const userSheet = workbook.addWorksheet('用户密码');
|
|
|
|
+ const userRows = [head];
|
|
|
|
+ for (const i of users) {
|
|
|
|
+ const password = this.makePassword();
|
|
|
|
+ const arr = [i.account, password];
|
|
|
|
+ await this.userService.update({ id: i.id }, { password });
|
|
|
|
+ userRows.push(arr);
|
|
|
|
+ }
|
|
|
|
+ userSheet.addRows(userRows);
|
|
|
|
+ const fullPath = Path.resolve(this.path, 'pwd.xlsx');
|
|
|
|
+ await workbook.xlsx.writeFile(fullPath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ makePassword(len = 16) {
|
|
|
|
+ const lowerCaseArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
|
|
|
|
+ const blockLetterArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
|
|
|
|
+ const numberArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
|
|
+ const specialArr = ['.', '_', '~', '!', '@', '#', '$', '^', '&', '*'];
|
|
|
|
+ const passArr = [];
|
|
|
|
+ let password = '';
|
|
|
|
+ const specifyRandom = function (...arr) {
|
|
|
|
+ let str = '';
|
|
|
|
+ arr.forEach(item => {
|
|
|
|
+ str += item[Math.floor(Math.random() * item.length)];
|
|
|
|
+ });
|
|
|
|
+ return str;
|
|
|
|
+ };
|
|
|
|
+ password += specifyRandom(lowerCaseArr, blockLetterArr, numberArr, specialArr);
|
|
|
|
+ passArr.push(...lowerCaseArr, ...blockLetterArr, ...numberArr, ...specialArr);
|
|
|
|
+ const forLen = len - password.length;
|
|
|
|
+ for (let i = 0; i < forLen; i++) {
|
|
|
|
+ password += specifyRandom(passArr);
|
|
|
|
+ }
|
|
|
|
+ return password;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async checkPassword(pwd) {
|
|
|
|
+ const reg = new RegExp(/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[._~!@#$^&*])[A-Za-z0-9._~!@#$^&*]{8,16}$/g);
|
|
|
|
+ return reg.test(pwd);
|
|
|
|
+ }
|
|
}
|
|
}
|