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 all 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
35 changes: 17 additions & 18 deletions packages/toolkits/pro/src/lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,48 +67,48 @@ const getProjectInfo = (): Promise<ProjectInfo> => {
],
default: 'mysql',
prefix: '*',
when: (answers) => answers.serverFramework !== ServerFrameworks.Skip
when: (answers) => answers.serverFramework !== ServerFrameworks.Skip,
},
{
type: 'input',
name: 'host',
message: '请输入数据库地址:',
default: 'localhost',
prefix: '*',
when: (answers) => answers.dialect
when: (answers) => answers.dialect,
},
{
type: 'input',
name: 'port',
message: '请输入数据库端口:',
default: 3306,
prefix: '*',
when: (answers) => answers.host
when: (answers) => answers.host,
},
{
type: 'input',
name: 'database',
message: '请输入数据库名称:',
prefix: '*',
validate: (input: string) => Boolean(input),
when: (answers) => answers.host
when: (answers) => answers.host,
},
{
type: 'input',
name: 'username',
message: '请输入登录用户名:',
default: 'root',
prefix: '*',
when: (answers) => answers.host
when: (answers) => answers.host,
},
{
type: 'password',
name: 'password',
message: '请输入密码:',
prefix: '*',
when: (answers) => answers.host
when: (answers) => answers.host,
},
]
];
return inquirer.prompt(question);
};

Expand All @@ -122,14 +122,15 @@ const createServerSync = (answers: ProjectInfo) => {
// 复制服务端相关目录
const serverFrom = utils.getTemplatePath(`server/${serverFramework}`);
const serverTo = utils.getDistPath('server');
const defaultConfig = { // 在未配置数据库信息时,使用默认值替换ejs模板
const defaultConfig = {
// 在未配置数据库信息时,使用默认值替换ejs模板
dialect: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '123456',
database: 'tiny_pro_server'
}
database: 'tiny_pro_server',
};
fs.copyTpl(serverFrom, serverTo, dialect ? answers : defaultConfig, {
overwrite: true,
});
Expand Down Expand Up @@ -158,23 +159,21 @@ const createProjectSync = (answers: ProjectInfo) => {
fs.readFileSync(packageJsonPath, { encoding: 'utf8' })
);
packageJson = { ...packageJson, name, description };
fs.writeFileSync(
packageJsonPath,
JSON.stringify(packageJson, null, 2),
{ encoding: 'utf8' }
);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), {
encoding: 'utf8',
});
} catch (e) {
log.error('配置项目信息创失败');
}

// 如果不对接服务端,默认开启mock
// 如果不对接服务端,全部接口采用mock
if (!serverFramework) {
try {
const envPath = path.join(to, '.env');
const envConfig = dotenv.parse(
fs.readFileSync(envPath, { encoding: 'utf8' })
);
envConfig.VITE_USE_MOCK = 'true';
delete envConfig.VITE_MOCK_IGNORE;
const config = Object.keys(envConfig)
.map((key) => `${key} = ${envConfig[key]}`)
.join('\n');
Expand All @@ -184,7 +183,7 @@ const createProjectSync = (answers: ProjectInfo) => {
log.info('请手动配置env信息');
}
} else {
// 如果对接服务端,执行文件复制及相关配置
// 如果对接服务端,执行文件复制及相关配置( WIP: 后台接口暂未全量完成,部分接口还是使用mock )
createServerSync(answers);
}
};
Expand Down
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, 'InternalError');
}
}
}
65 changes: 53 additions & 12 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,40 +22,41 @@ 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) {
await transaction.rollback();
ctx.helper.commonJson(ctx, {}, 500, 'InterError');
ctx.helper.commonJson(ctx, {}, 500, 'InternalError');
}
}

// 获取用户信息
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;
}
ctx.helper.commonJson(ctx, userInfo, 200);
} catch (error) {
ctx.helper.commonJson(ctx, {}, 500, 'InterError');
ctx.helper.commonJson(ctx, {}, 500, 'InternalError');
}
}

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,9 +97,49 @@ 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, 'InternalError');
}
}

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');
ctx.helper.commonJson(ctx, {}, 500, 'InternalError');
}
}
}
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
Loading