feat: 修改用户部分
This commit is contained in:
parent
b6d6737751
commit
14572ce6fa
@ -1,7 +1,7 @@
|
|||||||
import { useConfig } from '@kevisual/use-config';
|
import { useConfig } from '@kevisual/use-config';
|
||||||
import { sequelize } from '@/modules/sequelize.ts';
|
import { sequelize } from '@/modules/sequelize.ts';
|
||||||
import { DataTypes, Model, Op } from 'sequelize';
|
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 { cryptPwd } from '@kevisual/auth';
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
import { CustomError } from '@kevisual/router';
|
import { CustomError } from '@kevisual/router';
|
||||||
@ -36,11 +36,22 @@ export class User extends Model {
|
|||||||
* @param uid
|
* @param uid
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async createToken(uid?: string, loginType?: 'default' | 'plugin') {
|
async createToken(uid?: string, loginType?: 'default' | 'plugin' | 'month' | 'season' | 'year' ) {
|
||||||
const { id, username, type } = this;
|
const { id, username, type } = this;
|
||||||
let expireTime = 60 * 60 * 24 * 7; // 7 days
|
let expireTime = 60 * 60 * 24 * 7; // 7 days
|
||||||
if (loginType === 'plugin') {
|
switch (loginType) {
|
||||||
|
case 'plugin':
|
||||||
|
expireTime = 60 * 60 * 24 * 30 * 12; // 365 days
|
||||||
|
break;
|
||||||
|
case 'month':
|
||||||
expireTime = 60 * 60 * 24 * 30; // 30 days
|
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 now = new Date().getTime();
|
||||||
const token = await createToken({ id, username, uid, type }, config.tokenSecret);
|
const token = await createToken({ id, username, uid, type }, config.tokenSecret);
|
||||||
@ -96,6 +107,7 @@ export class User extends Model {
|
|||||||
return {
|
return {
|
||||||
id: this.id,
|
id: this.id,
|
||||||
username: this.username,
|
username: this.username,
|
||||||
|
nickname: this.nickname,
|
||||||
description: this.description,
|
description: this.description,
|
||||||
needChangePassword: this.needChangePassword,
|
needChangePassword: this.needChangePassword,
|
||||||
type: this.type,
|
type: this.type,
|
||||||
@ -243,3 +255,16 @@ export const CreateDemoUser = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
// initializeUser();
|
// 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 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,3 +2,5 @@ import './list.ts';
|
|||||||
import './org.ts';
|
import './org.ts';
|
||||||
|
|
||||||
import './me.ts';
|
import './me.ts';
|
||||||
|
|
||||||
|
import './update.ts'
|
||||||
|
56
src/routes/user/update.ts
Normal file
56
src/routes/user/update.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import { app } from '@/app.ts'
|
||||||
|
import { User } from '@/models/user.ts'
|
||||||
|
|
||||||
|
app
|
||||||
|
.route({
|
||||||
|
path: 'user',
|
||||||
|
key: 'getUpdateInfo',
|
||||||
|
middleware: ['auth']
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const tokenUser = ctx.state?.tokenUser || {}
|
||||||
|
const user = await User.findByPk(tokenUser.id)
|
||||||
|
if (!user) {
|
||||||
|
ctx.throw(500, 'user not found')
|
||||||
|
}
|
||||||
|
ctx.body = {
|
||||||
|
nickname: user.nickname,
|
||||||
|
avatar: user.avatar,
|
||||||
|
data: user.data
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.addTo(app)
|
||||||
|
app
|
||||||
|
.route('user', 'updateInfo', {
|
||||||
|
middleware: ['auth']
|
||||||
|
})
|
||||||
|
.define(async (ctx) => {
|
||||||
|
const { nickname, avatar, data } = ctx.query.data || {}
|
||||||
|
const tokenUser = ctx.state?.tokenUser || {}
|
||||||
|
const { id } = tokenUser
|
||||||
|
const user = await User.findByPk(id)
|
||||||
|
let updateData: any = {}
|
||||||
|
if (!user) {
|
||||||
|
ctx.throw(500, 'user not found')
|
||||||
|
}
|
||||||
|
if (nickname) {
|
||||||
|
updateData.nickname = nickname
|
||||||
|
}
|
||||||
|
if (avatar) {
|
||||||
|
updateData.avatar = avatar
|
||||||
|
}
|
||||||
|
await user.update(
|
||||||
|
{
|
||||||
|
...updateData,
|
||||||
|
data: {
|
||||||
|
...user.data,
|
||||||
|
...data
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: ['nickname', 'avatar', 'data']
|
||||||
|
}
|
||||||
|
)
|
||||||
|
ctx.body = await user.getInfo()
|
||||||
|
})
|
||||||
|
.addTo(app)
|
Loading…
x
Reference in New Issue
Block a user