浏览代码

用户管理员改为强密码

lrf 7 月之前
父节点
当前提交
b0ceb07b70
共有 2 个文件被更改,包括 69 次插入3 次删除
  1. 5 1
      src/controller/home.controller.ts
  2. 64 2
      src/service/serviceUtil.service.ts

+ 5 - 1
src/controller/home.controller.ts

@@ -1,12 +1,16 @@
 import { Controller, Get, Inject, Query } from '@midwayjs/core';
 import { Controller, Get, Inject, Query } from '@midwayjs/core';
 import { InitFourService } from '../service/initData/initFour.service';
 import { InitFourService } from '../service/initData/initFour.service';
+import { ServiceUtilService } from '../service/serviceUtil.service';
 @Controller('/')
 @Controller('/')
 export class HomeController {
 export class HomeController {
   @Inject()
   @Inject()
   initFour: InitFourService;
   initFour: InitFourService;
+  @Inject()
+  serviceUtil: ServiceUtilService;
   @Get('/')
   @Get('/')
   async home(@Query() query: object): Promise<any> {
   async home(@Query() query: object): Promise<any> {
-    const res = await this.initFour.initData();
+    // const res = await this.initFour.initData();
+    const res = await this.serviceUtil.initPassword();
     return res;
     return res;
     // return 'starting...';
     // return 'starting...';
   }
   }

+ 64 - 2
src/service/serviceUtil.service.ts

@@ -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);
+  }
 }
 }