Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复pro/vue中部分问题,对接eggjs用户相关接口 #58

Merged
merged 21 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@ import { Controller } from 'egg';
export default class EmployeeController extends Controller {
public async getEmployee() {
const { ctx } = this;
const { query } = ctx;
const { pageIndex = '1', pageSize = '20', searchInfo = '{}' } = query;
const [ offset, limit ] = ctx.helper.getPagination(pageIndex, pageSize);
const payload = ctx.request.body || {};
const { pageIndex = 1, pageSize = 20, searchInfo = {} } = payload;
const [offset, limit] = ctx.helper.getPagination(pageIndex, pageSize);

try {
const where = ctx.helper.getWhere(JSON.parse(searchInfo));
let { sortField, sortType } = query;

[sortField, sortType] = ctx.helper.getSortFieldAndType(
sortField,
sortType,
);
const where = ctx.helper.getWhere(searchInfo);
let { sortField, sortType } = payload;

[sortField, sortType] = ctx.helper.getSortFieldAndType(sortField, sortType);
const options = {
where,
limit,
offset,
order: [[sortField, sortType]],
raw: true,
};

this.logger.info('[ controller | employee ] getEmployee : 进入getEmployee方法');
const result = await ctx.service.employee.getEmployee(options);
ctx.helper.commonJson(
Expand All @@ -34,9 +31,7 @@ export default class EmployeeController extends Controller {
200,
);
} catch (error) {
console.log('error', error)
// ctx.helper.exceptionHandling(ctx, error);
ctx.helper.commonJson(ctx, {}, 500, 'CM087');
ctx.helper.commonJson(ctx, {}, 500, 'InterError');
wjiangwang marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
59 changes: 50 additions & 9 deletions packages/toolkits/pro/template/server/eggJs/app/controller/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class UserController extends Controller {
this.logger.info('[ controller | user ] registerUser : 进入registerUser方法');
// 校验参数
const registerUserRule = {
user_name: { type: 'email' },
username: { type: 'email' },
password: {
type: 'string',
format: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/,
Expand All @@ -22,17 +22,17 @@ export default class UserController extends Controller {
ctx.helper.commonJson(ctx, {}, 500, 'InvalidParameter');
return;
}
const { user_name, password } = payload;
const { username, password } = payload;
// 判断用户是否已经存在
const user = await ctx.service.user.getUserByName(user_name);
const user = await ctx.service.user.getUserByName(username);
if (user) {
ctx.helper.commonJson(ctx, {}, 500, 'UserAlreadyExist');
return;
}
const hash = BcryptUtils.genHash(password);
// 创建用户
const { id } = await ctx.service.user.createUser({ user_name, password: hash }, { transaction });
const userInfo = await ctx.service.user.createUserInfo({ user_name, user_id: id }, { transaction });
const { id: userId } = await ctx.service.user.createUser({ username, password: hash }, { transaction });
const userInfo = await ctx.service.user.createUserInfo({ username, userId }, { transaction });
await transaction.commit();
ctx.helper.commonJson(ctx, userInfo, 200);
} catch (error) {
Expand All @@ -45,10 +45,11 @@ export default class UserController extends Controller {
public async getUserInfo() {
const { ctx } = this;
const { id } = ctx.params;
const { userId } = ctx.state.user || {};

try {
this.logger.info('[ controller | user ] getUserInfo : 进入getUserInfo方法');
const userInfo = await ctx.service.user.getUserInfoById(id);
const userInfo = await ctx.service.user.getUserInfoById(id || userId);
if (!userInfo) {
ctx.helper.commonJson(ctx, {}, 500, 'UserNotFound');
return;
Expand All @@ -68,7 +69,7 @@ export default class UserController extends Controller {
// 校验参数格式
const err = app.validator.validate(
{
user_name: { type: 'email' },
username: { type: 'email' },
password: { type: 'string' },
},
payload,
Expand All @@ -79,7 +80,7 @@ export default class UserController extends Controller {
}

// 用户是否存在
const user = await ctx.service.user.getUserByName(payload.user_name);
const user = await ctx.service.user.getUserByName(payload.username);
if (!user) {
ctx.helper.commonJson(ctx, {}, 500, 'UserNotFound');
return;
Expand All @@ -96,7 +97,47 @@ export default class UserController extends Controller {
const { secret, sign } = this.app.config.jwt;
const userInfo = await ctx.service.user.getUserInfoById(user.id);
const token = this.app.jwt.sign(userInfo, secret, sign);
ctx.helper.commonJson(ctx, { token }, 200);
ctx.helper.commonJson(ctx, { token, userInfo }, 200);
} catch (error) {
ctx.helper.commonJson(ctx, {}, 500, 'InterError');
}
}

public async updateUserInfo() {
const { ctx, app } = this;
const { userId } = ctx.state.user || {};
const payload = ctx.request.body || {};
try {
this.logger.info('[ controller | user ] updateUserInfo : 进入updateUserInfo方法');

// 校验参数
const registerUserRule = {
userId: { type: 'string?' },
department: { type: 'string' },
employeeType: { type: 'string' },
job: { type: 'string' },
probationStart: { type: 'string' },
probationEnd: { type: 'string' },
probationDuration: { type: 'string' },
protocolStart: { type: 'string' },
protocolEnd: { type: 'string' },
};
const err = app.validator.validate(registerUserRule, payload);
if (err?.length) {
ctx.helper.commonJson(ctx, {}, 500, 'InvalidParameter');
return;
}
const id = payload.userId || userId;
const user = await ctx.service.user.getUserInfoById(id);
if (!user) {
ctx.helper.commonJson(ctx, {}, 500, 'UserNotFound');
return;
}

// 修改用户信息
await ctx.service.user.updateUserInfo(id, payload);
const userInfo = await ctx.service.user.getUserInfoById(id);
ctx.helper.commonJson(ctx, userInfo, 200);
} catch (error) {
ctx.helper.commonJson(ctx, {}, 500, 'InterError');
}
Expand Down
23 changes: 18 additions & 5 deletions packages/toolkits/pro/template/server/eggJs/app/model/employee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,69 @@ module.exports = (app: any) => {
'employee',
{
id: {
field: 'id',
type: DataTypes.INTEGER(16),
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: {
field: 'name',
type: DataTypes.STRING(20),
allowNull: false,
},
employee_no: {
employeeNo: {
field: 'employee_no',
type: DataTypes.STRING(50),
allowNull: false,
},
department: {
field: 'department',
type: DataTypes.STRING(50),
allowNull: false,
},
department_level: {
departmentLevel: {
field: 'department_level',
type: DataTypes.STRING(50),
allowNull: false,
},
status: {
field: 'status',
type: DataTypes.STRING(10),
allowNull: false,
},
workbench_name: {
workbenchName: {
field: 'workbench_name',
type: DataTypes.STRING(50),
allowNull: false,
},
project: {
field: 'project',
type: DataTypes.STRING(50),
allowNull: false,
},
type: {
field: 'type',
type: DataTypes.STRING(50),
allowNull: false,
},
address: {
field: 'address',
type: DataTypes.STRING(50),
allowNull: false,
},
roles: {
field: 'roles',
type: DataTypes.STRING(50),
allowNull: false,
},
last_update_user: {
lastUpdateUser: {
field: 'last_update_user',
type: DataTypes.STRING(50),
allowNull: false,
},
create_time: {
createTime: {
field: 'create_time',
type: DataTypes.TIME,
allowNull: true,
defaultValue: DataTypes.literal('CURRENT_TIMESTAMP'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ module.exports = (app: any) => {
'RegisterUser',
{
id: {
field: 'id',
type: DataTypes.INTEGER(20).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
user_name: {
username: {
field: 'user_name',
type: DataTypes.STRING(32),
allowNull: false,
},
password: {
field: 'password',
type: DataTypes.STRING(60),
allowNull: false,
},
register_type: {
registerType: {
field: 'register_type',
type: DataTypes.ENUM('email'),
allowNull: false,
defaultValue: 'email',
Expand Down
34 changes: 26 additions & 8 deletions packages/toolkits/pro/template/server/eggJs/app/model/userInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,75 @@ module.exports = (app: any) => {
'UserInfo',
{
id: {
field: 'id',
type: DataTypes.INTEGER(20).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
user_name: {
username: {
field: 'user_name',
type: DataTypes.STRING(32),
allowNull: false,
},
user_id: {
userId: {
field: 'user_id',
type: DataTypes.INTEGER(20),
allowNull: false,
},
department: {
field: 'department',
type: DataTypes.STRING(32),
allowNull: false,
defaultValue: '',
},
employee_type: {
employeeType: {
field: 'employee_type',
type: DataTypes.STRING(32),
defaultValue: null,
},
job: {
field: 'job',
type: DataTypes.STRING(32),
defaultValue: null,
},
roles: {
field: 'roles',
type: DataTypes.STRING(32),
defaultValue: null,
},
probation_start: {
probationStart: {
field: 'probation_start',
type: DataTypes.DATE,
defaultValue: null,
},
probation_end: {
probationEnd: {
field: 'probation_end',
type: DataTypes.DATE,
defaultValue: null,
},
probation_duration: {
probationDuration: {
field: 'probation_duration',
type: DataTypes.INTEGER(11).UNSIGNED,
defaultValue: null,
},
protocol_start: {
protocolStart: {
field: 'protocol_start',
type: DataTypes.DATE,
defaultValue: null,
},
protocol_end: {
protocolEnd: {
field: 'protocol_end',
type: DataTypes.DATE,
defaultValue: null,
},
address: {
field: 'address',
type: DataTypes.STRING(32),
defaultValue: null,
},
status: {
field: 'status',
type: DataTypes.STRING(32),
defaultValue: null,
},
Expand Down
4 changes: 4 additions & 0 deletions packages/toolkits/pro/template/server/eggJs/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ export default (app: Application) => {

router.get('/v1/user/userInfo/:id', controller.user.getUserInfo);

router.get('/v1/user/userInfo', controller.user.getUserInfo);

router.post('/v1/user/login', controller.user.login);

router.put('/v1/user/userInfo', controller.user.updateUserInfo);
};
10 changes: 7 additions & 3 deletions packages/toolkits/pro/template/server/eggJs/app/service/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ export default class User extends Service {
return this.ctx.model.UserInfo.findOne({ where: { user_id }, raw: true });
}

public async getUserByName(user_name: string): Promise<any> {
return this.ctx.model.RegisterUser.findOne({ where: { user_name } });
public async getUserByName(username: string): Promise<any> {
return this.ctx.model.RegisterUser.findOne({ where: { username } });
}

public async createUser(params: { user_name: string; password: string }, options?): Promise<any> {
public async createUser(params: { username: string; password: string }, options?): Promise<any> {
return this.ctx.model.RegisterUser.create(params, options);
}

public async createUserInfo(info, options?): Promise<any> {
return this.ctx.model.UserInfo.create(info, options);
}

public async updateUserInfo(userId: string, info): Promise<any> {
return this.ctx.model.UserInfo.update(info, { where: { userId } });
}
}
Loading