diff --git a/gulpfile.js b/gulpfile.js index 7f79ee0e..196cc5c6 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -12,14 +12,14 @@ const SRC = 'server/**/*.js'; function generateBabel(status) { const babelProcess = babel({ - presets: ['es2015', "stage-3"], + presets: ['es2015', 'stage-3'], plugins: ['transform-runtime'] }); babelProcess.on('error', function (e) { const restart = status ? status.count < 2 : true; - console.error(e); + console.error(e); // eslint-disable-line output('error', 'babel 编译失败!', restart); if (status) { @@ -39,7 +39,13 @@ function excuteCmd(cmd, args, opts) { }); command.stderr.on('data', data => { - output('log', `${NAME} ${data.toString()}`, true); + const message = data.toString(); + + output('log', `${NAME} ${message}`, true); + + if (~message.indexOf('building complete')) { + waitingSpinner(); + } }); return command; @@ -50,12 +56,12 @@ function output(type, message, restart = false) { if (type === 'success') { message = '✔ ' + message; - console.log(chalk.green(message)); + console.log(chalk.green(message)); // eslint-disable-line } else if (type === 'error') { message = '✖ ' + message; - console.log(chalk.red(message)); + console.log(chalk.red(message)); // eslint-disable-line } else { - console.log(message); + console.log(message); // eslint-disable-line } if (restart) { spinner.start(); @@ -63,6 +69,7 @@ function output(type, message, restart = false) { } function waitingSpinner() { + spinner.stop(); spinner = ora({ text: '等待文件变更...', spinner: 'circleQuarters', @@ -71,7 +78,7 @@ function waitingSpinner() { } gulp.task('removeDist', [], function () { - return fs.removeSync(DIST) + return fs.removeSync(DIST); }); gulp.task('initialBuild', ['removeDist'], () => { @@ -95,9 +102,14 @@ gulp.task('initialBuild', ['removeDist'], () => { }); gulp.task('default', ['initialBuild'], () => { - gulp.watch(SRC, (event) => { - let originFilePath = path.relative(path.join(__dirname, 'server'), event.path) - let distPath = path.resolve(DIST, path.join(originFilePath)) + gulp.watch('client/**/*', event => { + spinner.stop(); + spinner = ora(`正在编译 ${event.path}`).start(); + }); + + gulp.watch(SRC, event => { + let originFilePath = path.relative(path.join(__dirname, 'server'), event.path); + let distPath = path.resolve(DIST, path.join(originFilePath)); spinner.text = `正在编译 ${event.path}...`; gulp.src(event.path).pipe(generateBabel()) diff --git a/server/controllers/base.js b/server/controllers/base.js index b8b2b3d2..876eee35 100644 --- a/server/controllers/base.js +++ b/server/controllers/base.js @@ -1,20 +1,19 @@ -import yapi from '../yapi.js' -import projectModel from '../models/project.js' -import userModel from '../models/user.js' +import yapi from '../yapi.js'; +import projectModel from '../models/project.js'; +import userModel from '../models/user.js'; const jwt = require('jsonwebtoken'); - -class baseController{ - constructor(ctx){ +class baseController { + constructor(ctx) { this.ctx = ctx; //网站上线后,role对象key是不能修改的,value可以修改 this.roles = { admin: 'Admin', member: '网站会员' - } + }; } - async init(ctx){ + async init(ctx) { this.$user = null; let ignoreRouter = [ '/user/login_by_token', @@ -22,73 +21,93 @@ class baseController{ '/user/reg', '/user/status', '/user/logout' - ] - if(ignoreRouter.indexOf(ctx.path) > -1){ + ]; + if (ignoreRouter.indexOf(ctx.path) > -1) { this.$auth = true; - }else{ - await this.checkLogin(ctx) + } else { + await this.checkLogin(ctx); } - + } - getUid(ctx){ + getUid() { return parseInt(this.$uid, 10); } - async checkLogin(ctx){ + async checkLogin(ctx) { let token = ctx.cookies.get('_yapi_token'); - let uid = ctx.cookies.get('_yapi_uid'); - try{ - if(!token || !uid) return false; + let uid = ctx.cookies.get('_yapi_uid'); + + try { + if (!token || !uid) return false; let userInst = yapi.getInst(userModel); //创建user实体 let result = await userInst.findById(uid); - let decoded = jwt.verify(token, result.passsalt) - if(decoded.uid == uid){ + let decoded = jwt.verify(token, result.passsalt); + + if (decoded.uid == uid) { this.$uid = uid; this.$auth = true; - this.$user = result; + this.$user = result; return true; } + return false; - }catch(e){ + } catch (e) { return false; } } - async getLoginStatus(ctx){ - if(await this.checkLogin(ctx) === true){ - return ctx.body = yapi.commons.resReturn(yapi.commons.fieldSelect(this.$user,['_id','username','email', 'up_time', 'add_time'])); + async getLoginStatus(ctx) { + if (await this.checkLogin(ctx) === true) { + return ctx.body = yapi.commons.resReturn(yapi.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time'])); } - return ctx.body = yapi.commons.resReturn(null, 300 , 'Please login.'); - + return ctx.body = yapi.commons.resReturn(null, 300, 'Please login.'); } - getRole(){ + getRole() { return this.$user.role; } - async jungeProjectAuth(id){ + async jungeProjectAuth(id) { let model = yapi.getInst(projectModel); - if(this.getRole() === 'admin') return true; - if(!id) return false; - let result = await model.get(id); - if(result.uid === this.getUid()){ + + if (this.getRole() === 'admin') { return true; } + + if (!id) { + return false; + } + + let result = await model.get(id); + + if (result.uid === this.getUid()) { + return true; + } + return false; } - async jungeMemberAuth(id, member_uid){ + async jungeMemberAuth(id, member_uid) { let model = yapi.getInst(projectModel); - if(this.getRole() === 'admin') return true; - if(!id || !member_uid) return false; - let result = await model.checkMemberRepeat(id, member_uid); - if(result > 0){ + + if (this.getRole() === 'admin') { return true; } + + if (!id || !member_uid) { + return false; + } + + let result = await model.checkMemberRepeat(id, member_uid); + + if (result > 0) { + return true; + } + return false; } } -module.exports = baseController \ No newline at end of file +module.exports = baseController; \ No newline at end of file diff --git a/server/controllers/group.js b/server/controllers/group.js index 9dbb7db0..47aac602 100644 --- a/server/controllers/group.js +++ b/server/controllers/group.js @@ -1,14 +1,12 @@ -import groupModel from '../models/group.js' -import yapi from '../yapi.js' -import baseController from './base.js' -import projectModel from '../models/project.js' +import groupModel from '../models/group.js'; +import yapi from '../yapi.js'; +import baseController from './base.js'; +import projectModel from '../models/project.js'; -// -class groupController extends baseController{ - constructor(ctx){ - super(ctx) +class groupController extends baseController { + constructor(ctx) { + super(ctx); } - /** * 添加项目分组 @@ -23,37 +21,45 @@ class groupController extends baseController{ */ async add(ctx) { let params = ctx.request.body; + params = yapi.commons.handleParams(params, { group_name: 'string', group_desc: 'string' - }) - if(this.getRole() !== 'admin'){ - return ctx.body = yapi.commons.resReturn(null,401,'没有权限'); + }); + + if (this.getRole() !== 'admin') { + return ctx.body = yapi.commons.resReturn(null, 401, '没有权限'); } - if(!params.group_name){ + + if (!params.group_name) { return ctx.body = yapi.commons.resReturn(null, 400, '项目分组名不能为空'); } - var groupInst = yapi.getInst(groupModel); - - var checkRepeat = await groupInst.checkRepeat(params.group_name); - if(checkRepeat > 0){ - return ctx.body = yapi.commons.resReturn(null, 401, '项目分组名已存在'); + + let groupInst = yapi.getInst(groupModel); + + let checkRepeat = await groupInst.checkRepeat(params.group_name); + + if (checkRepeat > 0) { + return ctx.body = yapi.commons.resReturn(null, 401, '项目分组名已存在'); } + let data = { group_name: params.group_name, group_desc: params.group_desc, uid: this.getUid(), add_time: yapi.commons.time(), up_time: yapi.commons.time() - } - try{ + }; + + try { let result = await groupInst.save(data); + result = yapi.commons.fieldSelect(result, ['_id', 'group_name', 'group_desc', 'uid']); ctx.body = yapi.commons.resReturn(result); - }catch(e){ - ctx.body = yapi.commons.resReturn(null, 402, e.message) + } catch (e) { + ctx.body = yapi.commons.resReturn(null, 402, e.message); } - + } /** @@ -65,14 +71,13 @@ class groupController extends baseController{ * @returns {Object} * @example ./api/group/list.json */ - async list(ctx) { - try{ + try { var groupInst = yapi.getInst(groupModel); let result = await groupInst.list(); - ctx.body = yapi.commons.resReturn(result) - }catch(e){ - ctx.body = yapi.commons.resReturn(null, 402, e.message) + ctx.body = yapi.commons.resReturn(result); + } catch (e) { + ctx.body = yapi.commons.resReturn(null, 402, e.message); } } @@ -86,27 +91,30 @@ class groupController extends baseController{ * @returns {Object} * @example ./api/group/del.json */ + async del(ctx) { + if (this.getRole() !== 'admin') { + return ctx.body = yapi.commons.resReturn(null, 401, '没有权限'); + } - async del(ctx){ - if(this.getRole() !== 'admin'){ - return ctx.body = yapi.commons.resReturn(null,401,'没有权限'); - } - try{ - var groupInst = yapi.getInst(groupModel); - var projectInst = yapi.getInst(projectModel); - let id = ctx.request.body.id; - if(!id){ + try { + let groupInst = yapi.getInst(groupModel); + let projectInst = yapi.getInst(projectModel); + let id = ctx.request.body.id; + + if (!id) { return ctx.body = yapi.commons.resReturn(null, 402, 'id不能为空'); } - let count = await projectInst.countByGroupId(id); - if(count > 0){ + + let count = await projectInst.countByGroupId(id); + + if (count > 0) { return ctx.body = yapi.commons.resReturn(null, 403, '请先删除该分组下的项目'); } let result = await groupInst.del(id); - ctx.body = yapi.commons.resReturn(result) - }catch(err){ - ctx.body = yapi.commons.resReturn(null, 402, e.message) + ctx.body = yapi.commons.resReturn(result); + } catch (err) { + ctx.body = yapi.commons.resReturn(null, 402, err.message); } } @@ -122,33 +130,32 @@ class groupController extends baseController{ * @returns {Object} * @example ./api/group/up.json */ - - async up(ctx){ - if(this.getRole() !== 'admin'){ - return ctx.body = yapi.commons.resReturn(null,401,'没有权限'); + async up(ctx) { + if (this.getRole() !== 'admin') { + return ctx.body = yapi.commons.resReturn(null, 401, '没有权限'); } - try{ - + + try { + ctx.request.body = yapi.commons.handleParams(ctx.request.body, { id: 'number', group_name: 'string', group_desc: 'string' - }) - var groupInst = yapi.getInst(groupModel); + }); + let groupInst = yapi.getInst(groupModel); let id = ctx.request.body.id; let data = {}; - ctx.request.body.group_name && (data.group_name = ctx.request.body.group_name) - ctx.request.body.group_desc && (data.group_desc = ctx.request.body.group_desc) - if(Object.keys(data).length === 0 ){ + ctx.request.body.group_name && (data.group_name = ctx.request.body.group_name); + ctx.request.body.group_desc && (data.group_desc = ctx.request.body.group_desc); + if (Object.keys(data).length === 0) { ctx.body = yapi.commons.resReturn(null, 404, '分组名和分组描述不能为空'); } let result = await groupInst.up(id, data); - ctx.body = yapi.commons.resReturn(result) - }catch(err){ - ctx.body = yapi.commons.resReturn(null, 402, e.message) + ctx.body = yapi.commons.resReturn(result); + } catch (err) { + ctx.body = yapi.commons.resReturn(null, 402, err.message); } } } - -module.exports = groupController +module.exports = groupController; \ No newline at end of file