feat: 修改用户部分

This commit is contained in:
2025-02-20 11:51:43 +08:00
parent b6d6737751
commit 14572ce6fa
3 changed files with 87 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
import { useConfig } from '@kevisual/use-config';
import { sequelize } from '@/modules/sequelize.ts';
import { DataTypes, Model, Op } from 'sequelize';
import { createToken, checkToken } from '@kevisual/auth/token';
import { createToken, checkToken } from '@kevisual/auth';
import { cryptPwd } from '@kevisual/auth';
import { nanoid } from 'nanoid';
import { CustomError } from '@kevisual/router';
@@ -36,11 +36,22 @@ export class User extends Model {
* @param uid
* @returns
*/
async createToken(uid?: string, loginType?: 'default' | 'plugin') {
async createToken(uid?: string, loginType?: 'default' | 'plugin' | 'month' | 'season' | 'year' ) {
const { id, username, type } = this;
let expireTime = 60 * 60 * 24 * 7; // 7 days
if (loginType === 'plugin') {
expireTime = 60 * 60 * 24 * 30; // 30 days
switch (loginType) {
case 'plugin':
expireTime = 60 * 60 * 24 * 30 * 12; // 365 days
break;
case 'month':
expireTime = 60 * 60 * 24 * 30; // 30 days
break;
case 'season':
expireTime = 60 * 60 * 24 * 30 * 3; // 90 days
break;
case 'year':
expireTime = 60 * 60 * 24 * 30 * 12; // 365 days
break;
}
const now = new Date().getTime();
const token = await createToken({ id, username, uid, type }, config.tokenSecret);
@@ -96,6 +107,7 @@ export class User extends Model {
return {
id: this.id,
username: this.username,
nickname: this.nickname,
description: this.description,
needChangePassword: this.needChangePassword,
type: this.type,
@@ -243,3 +255,16 @@ export const CreateDemoUser = async () => {
}
};
// initializeUser();
export class UserServices extends User {
static async loginByPhone(phone: string) {
let user = await User.findOne({ where: { username: phone } });
let isNew = false;
if (!user) {
user = await User.createUser(phone, phone.slice(-6));
isNew = true;
}
const token = await user.createToken(null, 'season');
return { ...token, isNew };
}
}