Skip to content

Commit

Permalink
Merge pull request #3 from tomatobang/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
CShame authored Jul 19, 2018
2 parents 2c83d4b + a4033bf commit 41a9747
Show file tree
Hide file tree
Showing 21 changed files with 515 additions and 238 deletions.
38 changes: 19 additions & 19 deletions app/controller/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import { Controller } from 'egg';
export default class BaseController extends Controller {
select_field;
validateRule;

/**
* 按条件查找
* search with conditions
*/
async list() {
const { ctx } = this;
let conditions:any;
let conditions: any;
conditions = {};
const query = ctx.request.query;
ctx.logger.info('ctx.request:', ctx.request['currentUser']);
// 按用户筛选
// filter with logged userinfo
if (ctx.request['currentUser']) {
conditions.userid =ctx.request['currentUser'].username;
conditions.userid = ctx.request['currentUser'].username;
}
if (query.conditions) {
conditions = JSON.parse(query.conditions);
}
const result = await this.service.findAll(query, conditions);
// ctx.logger.info('message', result);
ctx.body = result;
ctx.status = 200;
}

/**
* 按 id 查找
* search by id
*/
async findById() {
const { ctx } = this;
Expand All @@ -38,13 +38,13 @@ export default class BaseController extends Controller {
}

/**
* 创建
* create record
*/
async create() {
const { ctx, app } = this;
// 存储用户编号/username
// filter with logged userinfo
if (ctx.request['currentUser']) {
ctx.request.body.userid =ctx.request['currentUser'].username;
ctx.request.body.userid = ctx.request['currentUser'].username;
}
if (this.validateRule) {
const invalid = app['validator'].validate(
Expand All @@ -61,7 +61,7 @@ export default class BaseController extends Controller {
}

/**
* 删除
* delete record by id
*/
async deleteById() {
const { ctx } = this;
Expand All @@ -71,7 +71,7 @@ export default class BaseController extends Controller {
}

/**
* 按 id 更新
* update record by id
*/
async updateById() {
const { ctx } = this;
Expand All @@ -82,7 +82,7 @@ export default class BaseController extends Controller {
}

/**
* 按 id 替换
* replace record by id
*/
async replaceById() {
const { ctx } = this;
Expand All @@ -94,14 +94,14 @@ export default class BaseController extends Controller {
}

/**
* 关键词查找
* load record by pagination
*/
async pagination() {
const { ctx } = this;
const current = ctx.request.body.current;
let userid = '';
if (ctx.request['currentUser']) {
userid =ctx.request['currentUser'].username;
userid = ctx.request['currentUser'].username;
}
if (!this.select_field) {
ctx.status = 403;
Expand Down Expand Up @@ -133,16 +133,16 @@ export default class BaseController extends Controller {
}

/**
* 统一错误处理
* @param {*} type 错误类型
* @param {*} message 错误消息
* error util function
* @param {*} type error type
* @param {*} message error message
*/
async throwBizError(type, message) {
if (type === 'DB:ERR') {
throw new Error('发生了数据库错误');
throw new Error('DB:ERR');
}
if (type === 'FILE:ERR') {
throw new Error('发生了文件类型错误');
throw new Error('FILE:ERR');
} else {
throw new Error(message);
}
Expand Down
4 changes: 2 additions & 2 deletions app/controller/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class MessageController extends BaseController {
}

/**
* 加载用户未读消息
* load user unread messages
*/
async loadUnreadMessages() {
const { ctx, app } = this;
Expand Down Expand Up @@ -47,7 +47,7 @@ export default class MessageController extends BaseController {
}

/**
* 更新消息已读状态
* update user messages state by message id
*/
async updateMessageState() {
const { ctx } = this;
Expand Down
2 changes: 1 addition & 1 deletion app/controller/other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Controller } from 'egg';
export default class OtherController extends Controller {
/**
* 获取七牛客户端上传 token
* load qi-niu upload token
*/
async getQiniuUploadToken() {
const { ctx, app } = this;
Expand Down
6 changes: 4 additions & 2 deletions app/controller/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default class TaskController extends BaseController {
this.validateRule = taskValidationRule;
}

/**
* update task voice media url
*/
async updateVoiceUrl() {
const { ctx, app } = this;
const invalid = app['validator'].validate(
Expand All @@ -19,14 +22,13 @@ export default class TaskController extends BaseController {
if (invalid) {
ctx.body = {
status: 'fail',
description: '请求参数错误!',
description: 'request parame(s) error!',
};
return;
}
const taskid = ctx.request.body.taskid;
const relateUrl = ctx.request.body.relateUrl;
await ctx.service.task.updateVoiceUrl(taskid, relateUrl);
// 此处写法待重构
ctx.status = 200;
ctx.body = {
success: true,
Expand Down
7 changes: 2 additions & 5 deletions app/controller/tomato.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export default class TomatoController extends BaseController {
ctx.status = 200;
ctx.body = [];
}
// 获取本月的第一天
const starDate = ctx.helper.dateHelper.getCurrentMonthFirst(date);
// 获取本月的最后一天
const endDate = ctx.helper.dateHelper.getNextMonthFirst(date);
const ret = await ctx.service.tomato.statistics(
userid,
Expand All @@ -43,7 +41,7 @@ export default class TomatoController extends BaseController {
}

/**
* 筛选今日番茄钟
* load today tomatoes
*/
async tomatoToday() {
const { ctx } = this;
Expand Down Expand Up @@ -73,13 +71,12 @@ export default class TomatoController extends BaseController {
}

/**
* 关键词查找
* search by keyword
*/
async search() {
const { ctx } = this;
let keywords = ctx.request.body.keywords;
keywords = ctx.helper.escape(keywords);
ctx.logger.info('keywords2', keywords);
const ret = await ctx.service.tomato.findAll(
{},
{
Expand Down
24 changes: 14 additions & 10 deletions app/controller/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class UserController extends BaseController {
}

/**
* 查找用户
* search user by keyword
*/
async searchUsers() {
const { ctx } = this;
Expand All @@ -37,6 +37,9 @@ export default class UserController extends BaseController {
ctx.body = users;
}

/**
* to see is user has login
*/
async auth() {
const { ctx } = this;
if (ctx.request['currentUser']) {
Expand All @@ -50,8 +53,9 @@ export default class UserController extends BaseController {
};
}
}

/**
* 登录
* login
*/
async login() {
const { ctx, app } = this;
Expand Down Expand Up @@ -117,7 +121,7 @@ export default class UserController extends BaseController {
}

/**
* 登出
* logout
*/
async logout() {
const { ctx, app } = this;
Expand All @@ -131,7 +135,7 @@ export default class UserController extends BaseController {
}

/**
* 邮箱验证
* validate username and email
*/
async emailUserNameVerify() {
const { ctx, app } = this;
Expand Down Expand Up @@ -176,7 +180,7 @@ export default class UserController extends BaseController {
}

/**
* 更新头像路径
* update head image( file url )
*/
async updateHeadImg() {
const { ctx } = this;
Expand All @@ -191,7 +195,7 @@ export default class UserController extends BaseController {
}

/**
* 更新性别
* update sex info
*/
async updateSex() {
const { ctx, app } = this;
Expand All @@ -213,7 +217,7 @@ export default class UserController extends BaseController {
}

/**
* 更新昵称
* update nick name
*/
async updateDisplayName() {
const { ctx } = this;
Expand All @@ -224,7 +228,7 @@ export default class UserController extends BaseController {
}

/**
* 更新邮箱
* update email
*/
async updateEmail() {
const { ctx, app } = this;
Expand All @@ -247,7 +251,7 @@ export default class UserController extends BaseController {
}

/**
* 更新位置
* update position
*/
async updateLocation() {
const { ctx } = this;
Expand All @@ -258,7 +262,7 @@ export default class UserController extends BaseController {
}

/**
* 更新签名
* update bio
*/
async updateBio() {
const { ctx } = this;
Expand Down
8 changes: 4 additions & 4 deletions app/controller/userFriend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class User_friendController extends BaseController {
}

/**
* @api {getFriendReqList} /api/user_friend [获取好友列表]
* @api {getFriendReqList} /api/user_friend [load user friend list]
*/
async getUserFriends() {
const { ctx } = this;
Expand All @@ -36,7 +36,7 @@ export default class User_friendController extends BaseController {
}

/**
* @api {getFriendReqList} /api/user_friend/request_add_friend [请求添加为好友]
* @api {getFriendReqList} /api/user_friend/request_add_friend [request friend list]
*/
async getFriendReqList() {
const { ctx } = this;
Expand All @@ -55,7 +55,7 @@ export default class User_friendController extends BaseController {
}

/**
* @api {requestAddFriend} /api/user_friend/request_add_friend [请求添加为好友]
* @api {requestAddFriend} /api/user_friend/request_add_friend [request add friend]
*/
async requestAddFriend() {
const { ctx, app } = this;
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class User_friendController extends BaseController {
}

/**
* @api {responseAddFriend} /api/user_friend/response_add_friend [回复添加好友请求]
* @api {responseAddFriend} /api/user_friend/response_add_friend [response friend request]
*/
async responseAddFriend() {
const { ctx, app } = this;
Expand Down
2 changes: 1 addition & 1 deletion app/controller/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class VersionController extends BaseController {
}

/**
* 查找最新版本信息
* find latest version info
*/
async findLatestVersion() {
const { ctx } = this;
Expand Down
18 changes: 9 additions & 9 deletions app/extend/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as moment from 'moment';
exports.relativeTime = time => moment(new Date(time * 1000)).fromNow();

/**
* 日期处理 Util
* date Util
*/
exports.dateHelper = {
getCurrentMonthFirst(date) {
Expand All @@ -28,16 +28,16 @@ exports.dateHelper = {
const nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
return this.format(new Date(nextMonthFirstDay), 'yyyy-MM-dd');
},
// 参考: https://www.cnblogs.com/tugenhua0707/p/3776808.html
// refer: https://www.cnblogs.com/tugenhua0707/p/3776808.html
format(datetime, fmt) {
const o = {
'M+': datetime.getMonth() + 1, // 月份
'd+': datetime.getDate(), // 日
'h+': datetime.getHours(), // 小时
'm+': datetime.getMinutes(), // 分
's+': datetime.getSeconds(), // 秒
'q+': Math.floor((datetime.getMonth() + 3) / 3), // 季度
S: datetime.getMilliseconds(), // 毫秒
'M+': datetime.getMonth() + 1,
'd+': datetime.getDate(),
'h+': datetime.getHours(),
'm+': datetime.getMinutes(),
's+': datetime.getSeconds(),
'q+': Math.floor((datetime.getMonth() + 3) / 3), // quarter
S: datetime.getMilliseconds(),
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
Expand Down
Loading

0 comments on commit 41a9747

Please sign in to comment.