1.5.2 • Published 5 years ago
egg-full-utils v1.5.2
egg-full-utils
Chinese 中文
Important
crypto-js@3.1.9-1 uuid@3.3.2
Install
$ npm i egg-full-utils --save
Usage
// {app_root}/config/plugin.js
exports.fullUtils = {
enable: true,
package: 'egg-full-utils',
};
Configuration
// {app_root}/config/config.default.js
exports.fullUtils = {
/**
* ctx.success() => { code: 'success', data: null }
*/
success: 'success',
};
see config/config.default.js for more detail.
Example
'use strict';
module.exports = app => {
class UserController extends app.Controller {
/**
* get: /login?name=xxx&password=123456
* response: { code: success, data: { userId }}, status: 200
*/
async login() {
const result = this.service.user.verify();
ctx.resp(result);
}
}
return UserController;
};
module.exports = app => {
class UserService extends app.Service {
async verify() {
const { ctx } = this;
const payload = ctx.request.body || {};
// get user from db or cache by payload.name
const user = {
userId: '10001',
password: '07e38ba001d5df0dd4488cdf0d5ab8ea',
salt: 'P7yRKQymchIZ5ZpP',
};
// check password
if (user.password !== ctx.helper.md5(payload.password + salt)) {
return ctx.failed('password error');
}
return ctx.success({ userId: user.userId });
}
}
return UserService;
};