11import Application from "koa" ;
22import consola from "consola" ;
3+ import { Model } from "sequelize" ;
34import { IMiddleware } from "koa-router" ;
45import { jwt } from "./jwt" ;
56import { assert } from "./util" ;
@@ -158,14 +159,33 @@ export class Manager {
158159/**
159160 * 权限系统中的User模型
160161 */
161- export const User = db . define (
162- "lin_user" ,
162+ export class User extends Model {
163+ public id ! : number ;
164+ public nickname ! : string ;
165+ public admin ! : number ;
166+ public active ! : number ;
167+ public email ! : string ;
168+ // tslint:disable-next-line:variable-name
169+ public group_id ! : number ;
170+ public password ! : string ;
171+
172+ // tslint:disable-next-line:variable-name
173+ public create_time ! : Date ;
174+ // tslint:disable-next-line:variable-name
175+ public update_time ! : Date ;
176+ // tslint:disable-next-line:variable-name
177+ public delete_time ! : Date ;
178+ }
179+
180+ User . init (
163181 {
164182 ...UserInterface . attributes
165183 } ,
166184 merge (
167185 {
168- tableName : "lin_user"
186+ sequelize : db ,
187+ tableName : "lin_user" ,
188+ modelName : "user"
169189 } ,
170190 UserInterface . options
171191 )
@@ -188,27 +208,36 @@ User.verify = async function(nickname: string, password: string) {
188208
189209// @ts -ignore
190210User . prototype . checkPassword = function ( raw : string ) {
211+ // @ts -ignore
191212 if ( ! this . password || this . password === "" ) {
192213 return false ;
193214 }
215+ // @ts -ignore
194216 return verify ( raw , this . password ) ;
195217} ;
196218
197219// @ts -ignore
198- User . prototype . softDelete = function ( ) {
199- this . delete_time = new Date ( ) ;
200- this . save ( ) ;
201- } ;
220+ // User.prototype.softDelete = function() {
221+ // this.delete_time = new Date();
222+ // this.save();
223+ // };
202224
203225// @ts -ignore
204226User . prototype . toJSON = function ( ) {
205227 const origin = {
228+ // @ts -ignore
206229 id : this . id ,
230+ // @ts -ignore
207231 nickname : this . nickname ,
232+ // @ts -ignore
208233 admin : this . admin ,
234+ // @ts -ignore
209235 active : this . active ,
236+ // @ts -ignore
210237 email : this . email ,
238+ // @ts -ignore
211239 group_id : this . groupId ,
240+ // @ts -ignore
212241 create_time : this . create_time
213242 } ;
214243 if ( has ( this , "auths" ) ) {
@@ -223,6 +252,7 @@ User.prototype.toJSON = function() {
223252// @ts -ignore
224253User . prototype . resetPassword = function ( newPassword : string ) {
225254 // 注意,重置密码后记得提交至数据库
255+ // @ts -ignore
226256 this . password = newPassword ;
227257} ;
228258
@@ -231,7 +261,9 @@ User.prototype.changePassword = function(
231261 oldPassword : string ,
232262 newPassword : string
233263) {
264+ // @ts -ignore
234265 if ( this . checkPassword ( oldPassword ) ) {
266+ // @ts -ignore
235267 this . password = newPassword ;
236268 return true ;
237269 }
@@ -241,12 +273,21 @@ User.prototype.changePassword = function(
241273/**
242274 * 权限系统中的Group模型
243275 */
244- export const Group = db . define (
245- "lin_group" ,
246- { ...GroupInterface . attributes } ,
276+ export class Group extends Model {
277+ public id ! : number ;
278+ public name ! : string ;
279+ public info ! : string ;
280+ }
281+
282+ Group . init (
283+ {
284+ ...GroupInterface . attributes
285+ } ,
247286 merge (
248287 {
249- tableName : "lin_group"
288+ sequelize : db ,
289+ tableName : "lin_group" ,
290+ modelName : "group"
250291 } ,
251292 GroupInterface . options
252293 )
@@ -255,8 +296,11 @@ export const Group = db.define(
255296// @ts -ignore
256297Group . prototype . toJSON = function ( ) {
257298 let origin = {
299+ // @ts -ignore
258300 id : this . id ,
301+ // @ts -ignore
259302 name : this . name ,
303+ // @ts -ignore
260304 info : this . info
261305 } ;
262306 return has ( this , "auths" )
@@ -267,14 +311,23 @@ Group.prototype.toJSON = function() {
267311/**
268312 * 权限系统中的Auth模型
269313 */
270- export const Auth = db . define (
271- "lin_auth" ,
314+ export class Auth extends Model {
315+ public id ! : number ;
316+ // tslint:disable-next-line:variable-name
317+ public group_id ! : number ;
318+ public auth ! : string ;
319+ public module ! : string ;
320+ }
321+
322+ Auth . init (
272323 {
273324 ...AuthInterface . attributes
274325 } ,
275326 merge (
276327 {
277- tableName : "lin_auth"
328+ sequelize : db ,
329+ tableName : "lin_auth" ,
330+ modelName : "auth"
278331 } ,
279332 AuthInterface . options
280333 )
@@ -283,9 +336,13 @@ export const Auth = db.define(
283336// @ts -ignore
284337Auth . prototype . toJSON = function ( ) {
285338 return {
339+ // @ts -ignore
286340 id : this . id ,
341+ // @ts -ignore
287342 group_id : this . group_id ,
343+ // @ts -ignore
288344 module : this . module ,
345+ // @ts -ignore
289346 auth : this . auth
290347 } ;
291348} ;
@@ -300,26 +357,37 @@ export interface LogArgs {
300357 authority ?: string ;
301358}
302359
303- export const Log = db . define (
304- "lin_log" ,
360+ export class Log extends Model {
361+ public id ! : number ;
362+ public message ! : string ;
363+ // tslint:disable-next-line:variable-name
364+ public user_id ! : number ;
365+ // tslint:disable-next-line:variable-name
366+ public user_name ! : string ;
367+ // tslint:disable-next-line:variable-name
368+ public status_code ! : number ;
369+ public method ! : string ;
370+ public path ! : string ;
371+ public authority ! : string ;
372+ public time ! : Date ;
373+ }
374+
375+ Log . init (
305376 {
306377 ...LogInterface . attributes
307378 } ,
308379 merge (
309380 {
310- tableName : "lin_log"
381+ sequelize : db ,
382+ tableName : "lin_log" ,
383+ modelName : "log"
311384 } ,
312385 LogInterface . options
313386 )
314387) ;
315388
316389// @ts -ignore
317390Log . createLog = function ( args ?: LogArgs , commit ?: boolean ) {
318- // if (args) {
319- // Object.keys(args).forEach(arg => {
320- // set(log, arg, get(args, arg));
321- // });
322- // }
323391 const log = Log . build ( args as any ) ;
324392 // @ts -ignore
325393 commit && log . save ( ) ;
0 commit comments