add sendMail and install website

This commit is contained in:
suxiaoxin 2017-07-11 16:50:17 +08:00
parent 2b3ce248b2
commit b584b85b20
26 changed files with 458 additions and 227 deletions

2
.gitignore vendored
View File

@ -34,3 +34,5 @@ Thumbs.db
# *.tar.gz
node_modules/
runtime/init.lock
runtime/log

View File

@ -31,7 +31,7 @@
"mongoose": "4.10.8",
"mongoose-auto-increment": "^5.0.1",
"node-sass-china": "^4.5.0",
"sha1": "^1.1.1",
"nodemailer": "^4.0.1",
"redux": "^3.7.1",
"sha1": "^1.1.1",
"ykit-config-antd": "^0.1.3",

19
runtime/config.dev.json Normal file
View File

@ -0,0 +1,19 @@
{
"port": "3000",
"webhost": "127.0.0.1",
"adminAccount": "admin@admin.com",
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi",
"port": 27017
},
"mail": {
"host": "smtp.163.com",
"port": 465,
"auth": {
"user": "hellosean1025@163.com",
"pass": "helloqunar123"
}
}
}

18
runtime/config.json Normal file
View File

@ -0,0 +1,18 @@
{
"port": "80",
"webhost": "127.0.0.1",
"adminAccount": "admin@admin.com",
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi",
"port": 27017
},
"mail": {
"host": "smtp.163.com",
"port": 465,
"auth": {
"user": "***********@163.com",
"pass": "*********"
}
}
}

View File

@ -1,15 +1,8 @@
import yapi from './yapi.js';
import commons from './utils/commons';
yapi.commons = commons;
import dbModule from './utils/db.js';
import mockServer from './middleware/mockServer.js'
import Koa from 'koa'
import convert from 'koa-convert'
import koaStatic from 'koa-static'
@ -31,8 +24,7 @@ commons.log(`the server is start at port ${yapi.WEBCONFIG.port}`)
yapi.fs.ensureDirSync(yapi.WEBROOT_RUNTIME);
yapi.fs.ensureDirSync(yapi.WEBROOT_LOG);

View File

@ -1,9 +0,0 @@
{
"port": "3000",
"webhost": "127.0.0.1",
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi",
"port": 27017
}
}

View File

@ -1,9 +0,0 @@
{
"port":"80",
"webhost": "127.0.0.1",
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi",
"port": "27017"
}
}

View File

@ -6,12 +6,17 @@ const jwt = require('jsonwebtoken');
class baseController{
constructor(ctx){
//网站上线后role对象key是不能修改的value可以修改
this.roles = {
admin: 'Admin',
member: '网站会员'
}
}
async init(ctx){
this.$user = null;
if(ctx.path === '/user/login' || ctx.path === '/user/reg' || ctx.path === '/user/status'){
if(ctx.path === '/user/login' || ctx.path === '/user/reg' || ctx.path === '/user/status' || ctx.path === '/user/logout'){
this.$auth = true;
}else{
await this.checkLogin(ctx)
@ -34,7 +39,6 @@ class baseController{
if(decoded.uid == uid){
this.$uid = uid;
this.$auth = true;
console.log(11111)
this.$user = result;
return true;
}
@ -49,12 +53,12 @@ class baseController{
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, 400 , 'Please login.');
return ctx.body = yapi.commons.resReturn(null, 300 , 'Please login.');
}
getRole(){
return 'admin'
return this.$user.role;
}
async jungeProjectAuth(id){

View File

@ -8,7 +8,6 @@ const jwt = require('jsonwebtoken');
class userController extends baseController{
constructor(ctx){
super(ctx)
console.log('user constructor...')
}
/**
* 添加项目分组
@ -27,7 +26,7 @@ class userController extends baseController{
let password = ctx.request.body.password;
if(!email){
return ctx.body = yapi.commons.resReturn(null,400,'用户名不能为空');
return ctx.body = yapi.commons.resReturn(null,400,'email不能为空');
}
if(!password){
return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空');
@ -41,17 +40,25 @@ class userController extends baseController{
}else if(yapi.commons.generatePassword(password, result.passsalt) === result.password){
let token = jwt.sign({uid: result._id},result.passsalt,{expiresIn: '7 days'});
ctx.cookies.set('_yapi_token', token, {
expires: yapi.commons.expireDate(7)
expires: yapi.commons.expireDate(7),
httpOnly: true
})
ctx.cookies.set('_yapi_uid', result._id, {
expires: yapi.commons.expireDate(7)
expires: yapi.commons.expireDate(7),
httpOnly: true
})
return ctx.body = yapi.commons.resReturn(null,200,'ok');
return ctx.body = yapi.commons.resReturn(null, 0, 'logout success...');
}else{
return ctx.body = yapi.commons.resReturn(null, 405, '密码错误');
}
}
async logout(ctx){
ctx.cookies.set('_yapi_token', null);
ctx.cookies.set('_yapi_uid', null);
ctx.body = yapi.commons.resReturn('ok');
}
async reg(ctx){ //注册
@ -75,7 +82,7 @@ class userController extends baseController{
password: yapi.commons.generatePassword(params.password, passsalt),//加密
email: params.email,
passsalt: passsalt,
role: params.role,
role: 'member',
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
}
@ -83,11 +90,15 @@ class userController extends baseController{
let user = await userInst.save(data);
user = yapi.commons.fieldSelect(user,['id','username','email'])
ctx.body = yapi.commons.resReturn(user);
yapi.commons.sendMail({
to: params.email,
contents: `欢迎注册,您的账号 ${params.email} 已经注册成功`
})
}catch(e){
ctx.body = yapi.commons.resReturn(null, 401, e.message);
}
}
async list(ctx){ //获取用户列表并分页
async list(ctx){
var userInst = yapi.getInst(userModel);
try{
let user = await userInst.list();
@ -97,9 +108,12 @@ class userController extends baseController{
}
}
async findById(ctx){ //根据id获取用户信息
try{
try{
var userInst = yapi.getInst(userModel);
let id = ctx.request.body.id;
if(this.getUid() != id){
return ctx.body = yapi.commons.resReturn(null, 402, 'Without permission.');
}
let result = await userInst.findById(id);
return ctx.body = yapi.commons.resReturn(result);
}catch(e){
@ -108,6 +122,9 @@ class userController extends baseController{
}
async del(ctx){ //根据id删除一个用户
try{
if(this.getRole() !== 'admin'){
return ctx.body = yapi.commons.resReturn(null, 402, 'Without permission.');
}
var userInst = yapi.getInst(userModel);
let id = ctx.request.body.id;
let result = await userInst.del(id);
@ -119,15 +136,10 @@ class userController extends baseController{
async update(ctx){ //更新用户信息
try{
var userInst = yapi.getInst(userModel);
let id = ctx.request.body.id;
let id = this.getUid();
let data ={};
ctx.request.body.username && (data.username = ctx.request.body.username)
ctx.request.body.password && (data.password = ctx.request.body.password)
ctx.request.body.email && (data.email = ctx.request.body.email)
ctx.request.body.role && (data.role = ctx.request.body.role)
if (Object.keys(data).length===0){
ctx.body = yapi.commons.resReturn(null,404,'用户名、密码、Email、role都为空');
}
let result = await userInst.update(id,data);
ctx.body = yapi.commons.resReturn(result);
}catch(e){

42
server/install.js Normal file
View File

@ -0,0 +1,42 @@
import yapi from './yapi.js';
import commons from './utils/commons';
yapi.commons = commons;
import dbModule from './utils/db.js';
import userModel from './models/user.js'
yapi.connect = dbModule.connect()
function install(){
let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'))
if(exist){
return yapi.commons.log('runtime/init.lock文件已存在请确认您是否已安装。如果需要重新安装请删掉runtime/init.lock文件');
process.exit(1);
}
setupSql();
}
function setupSql(){
let userInst = yapi.getInst(userModel);
let passsalt = yapi.commons.randStr();
let result = userInst.save({
email: yapi.WEBCONFIG.adminAccount,
password: yapi.commons.generatePassword('qunar.com', passsalt),
passsalt: passsalt,
role: 'admin',
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
})
result.then(function(success){
console.log(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 成功`);
yapi.fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
process.exit(1)
}, function(err){
console.log(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 失败, ${err.message}`);
process.exit(1)
})
}
install();

View File

@ -34,7 +34,7 @@ class userModel extends baseModel{
})
}
list(){
return this.model.find().select("username_id username email role add_time up_time").exec() //显示id name email role
return this.model.find().select("_id username email role add_time up_time").exec() //显示id name email role
}
findByEmail(email){
return this.model.findOne({email: email})
@ -54,7 +54,6 @@ class userModel extends baseModel{
_id: id
},{
username: data.username,
password: data.password,
email: data.email,
role: data.role,
up_time: yapi.commons.time()

View File

@ -42,6 +42,7 @@ createAction('user', 'findById', 'post', 'findById')
createAction('user', 'update', 'post', 'update')
createAction('user', 'del', 'post', 'del')
createAction('user', 'status', 'get', 'getLoginStatus')
createAction('user', 'logout', 'get', 'logout')
//project
@ -72,7 +73,6 @@ function createAction(controller, path, method, action){
router[method](INTERFACE_CONFIG[controller].prefix + path, async (ctx) => {
let inst = new INTERFACE_CONFIG[controller].controller(ctx);
await inst.init(ctx);
console.log(22222)
if(inst.$auth === true){
await inst[action].call(inst, ctx);
}else{

View File

@ -94,4 +94,23 @@ exports.expireDate = (day) => {
let date = new Date();
date.setTime(date.getTime() + day * 86400000);
return date;
}
exports.sendMail = (options,cb) => {
if(!yapi.mail) return false;
options.subject = options.subject? options.subject + '-yapi平台' : 'ypai平台';
cb = cb || function(err, info){
if(err){
yapi.commons.log('send mail ' + options.to +' error,'+ err.message, 'error');
}else{
yapi.commons.log('send mail ' + options.to +' success');
}
}
yapi.mail.sendMail({
from: yapi.WEBCONFIG.mail.auth.user,
to : options.to,
subject: 'yapi平台',
html: options.contents
}, cb)
}

View File

@ -26,17 +26,9 @@ function connect(){
autoIncrement.initialize(db);
checkDatabase();
return db;
}
function checkDatabase(){
let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'))
if(!exist){
yapi.commons.log('lock is not exist')
}
}
yapi.db = model;

View File

@ -1,10 +1,12 @@
import path from 'path'
import fs from 'fs-extra'
import prdConfig from './config.json'
import devConfig from './config.dev.json'
import nodemailer from 'nodemailer';
import prdConfig from '../runtime/config.json'
import devConfig from '../runtime/config.dev.json'
let args = process.argv.splice(2);
let isDev = args[0] === 'dev' ? true : false;
var insts = new Map();
let mail;
const config = isDev ? devConfig : prdConfig;
const WEBROOT = path.resolve(__dirname, '..'); //路径
@ -13,6 +15,14 @@ const WEBROOT_RUNTIME = path.join(WEBROOT, 'runtime');
const WEBROOT_LOG = path.join(WEBROOT_RUNTIME, 'log');
const WEBCONFIG = config;
fs.ensureDirSync(WEBROOT_LOG);
if(WEBCONFIG.mail){
mail = nodemailer.createTransport(WEBCONFIG.mail)
}
/**
* 获取一个model实例如果不存在则创建一个新的返回
* @param {*} m class
@ -34,7 +44,7 @@ function delInst(m){
}
}
module.exports = {
let r = {
fs: fs,
path: path,
WEBROOT: WEBROOT,
@ -45,4 +55,7 @@ module.exports = {
getInst: getInst,
delInst: delInst,
getInsts: insts
}
}
if(mail) r.mail = mail;
module.exports = r;

View File

@ -40,6 +40,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
_yapi2.default.commons = _commons2.default;
_yapi2.default.connect = _db2.default.connect();
var app = new _koa2.default();
@ -49,7 +50,4 @@ app.use(_router2.default.routes());
app.use(_router2.default.allowedMethods());
app.use((0, _koaStatic2.default)(_yapi2.default.path.join(_yapi2.default.WEBROOT, 'static')));
app.listen(_yapi2.default.WEBCONFIG.port);
_commons2.default.log('the server is start at port ' + _yapi2.default.WEBCONFIG.port);
_yapi2.default.fs.ensureDirSync(_yapi2.default.WEBROOT_RUNTIME);
_yapi2.default.fs.ensureDirSync(_yapi2.default.WEBROOT_LOG);
_commons2.default.log('the server is start at port ' + _yapi2.default.WEBCONFIG.port);

View File

@ -1,9 +1,19 @@
{
"port": "3000",
"webhost": "127.0.0.1",
"adminAccount": "admin@admin.com",
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi",
"port": 27017
},
"mail": {
"host": "smtp.163.com",
"port": 465,
"auth": {
"user": "hellosean1025@163.com",
"pass": "helloqunar123"
}
}
}

View File

@ -1,9 +1,18 @@
{
"port":"80",
"port": "80",
"webhost": "127.0.0.1",
"adminAccount": "admin@admin.com",
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi",
"port": "27017"
"port": 27017
},
"mail": {
"host": "smtp.163.com",
"port": 465,
"auth": {
"user": "***********@163.com",
"pass": "*********"
}
}
}

View File

@ -35,6 +35,13 @@ var jwt = require('jsonwebtoken');
var baseController = function () {
function baseController(ctx) {
(0, _classCallCheck3.default)(this, baseController);
//网站上线后role对象key是不能修改的value可以修改
this.roles = {
admin: 'Admin',
member: '网站会员'
};
}
(0, _createClass3.default)(baseController, [{
@ -47,7 +54,7 @@ var baseController = function () {
case 0:
this.$user = null;
if (!(ctx.path === '/user/login' || ctx.path === '/user/reg' || ctx.path === '/user/status')) {
if (!(ctx.path === '/user/login' || ctx.path === '/user/reg' || ctx.path === '/user/status' || ctx.path === '/user/logout')) {
_context.next = 5;
break;
}
@ -110,30 +117,29 @@ var baseController = function () {
decoded = jwt.verify(token, result.passsalt);
if (!(decoded.uid == uid)) {
_context2.next = 16;
_context2.next = 15;
break;
}
this.$uid = uid;
this.$auth = true;
console.log(11111);
this.$user = result;
return _context2.abrupt('return', true);
case 16:
case 15:
return _context2.abrupt('return', false);
case 19:
_context2.prev = 19;
case 18:
_context2.prev = 18;
_context2.t0 = _context2['catch'](2);
return _context2.abrupt('return', false);
case 22:
case 21:
case 'end':
return _context2.stop();
}
}
}, _callee2, this, [[2, 19]]);
}, _callee2, this, [[2, 18]]);
}));
function checkLogin(_x2) {
@ -164,7 +170,7 @@ var baseController = function () {
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(_yapi2.default.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time'])));
case 5:
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'Please login.'));
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 300, 'Please login.'));
case 6:
case 'end':
@ -183,7 +189,7 @@ var baseController = function () {
}, {
key: 'getRole',
value: function getRole() {
return 'admin';
return this.$user.role;
}
}, {
key: 'jungeProjectAuth',

View File

@ -1,9 +1,5 @@
'use strict';
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
@ -57,11 +53,7 @@ var userController = function (_baseController) {
function userController(ctx) {
(0, _classCallCheck3.default)(this, userController);
var _this = (0, _possibleConstructorReturn3.default)(this, (userController.__proto__ || (0, _getPrototypeOf2.default)(userController)).call(this, ctx));
console.log('user constructor...');
return _this;
return (0, _possibleConstructorReturn3.default)(this, (userController.__proto__ || (0, _getPrototypeOf2.default)(userController)).call(this, ctx));
}
/**
* 添加项目分组
@ -96,7 +88,7 @@ var userController = function (_baseController) {
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '用户名不能为空'));
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'email不能为空'));
case 5:
if (password) {
@ -129,12 +121,14 @@ var userController = function (_baseController) {
token = jwt.sign({ uid: result._id }, result.passsalt, { expiresIn: '7 days' });
ctx.cookies.set('_yapi_token', token, {
expires: _yapi2.default.commons.expireDate(7)
expires: _yapi2.default.commons.expireDate(7),
httpOnly: true
});
ctx.cookies.set('_yapi_uid', result._id, {
expires: _yapi2.default.commons.expireDate(7)
expires: _yapi2.default.commons.expireDate(7),
httpOnly: true
});
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 200, 'ok'));
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 0, 'logout success...'));
case 21:
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '密码错误'));
@ -154,46 +148,72 @@ var userController = function (_baseController) {
return login;
}()
}, {
key: 'reg',
key: 'logout',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) {
var userInst, params, checkRepeat, passsalt, data, user;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
ctx.cookies.set('_yapi_token', null);
ctx.cookies.set('_yapi_uid', null);
ctx.body = _yapi2.default.commons.resReturn('ok');
case 3:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function logout(_x2) {
return _ref2.apply(this, arguments);
}
return logout;
}()
}, {
key: 'reg',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
var userInst, params, checkRepeat, passsalt, data, user;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
//注册
userInst = _yapi2.default.getInst(_user2.default);
params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码
if (params.email) {
_context2.next = 4;
_context3.next = 4;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '邮箱不能为空'));
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '邮箱不能为空'));
case 4:
if (params.password) {
_context2.next = 6;
_context3.next = 6;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空'));
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空'));
case 6:
_context2.next = 8;
_context3.next = 8;
return userInst.checkRepeat(params.email);
case 8:
checkRepeat = _context2.sent;
checkRepeat = _context3.sent;
if (!(checkRepeat > 0)) {
_context2.next = 11;
_context3.next = 11;
break;
}
return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册'));
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册'));
case 11:
passsalt = _yapi2.default.commons.randStr();
@ -202,38 +222,42 @@ var userController = function (_baseController) {
password: _yapi2.default.commons.generatePassword(params.password, passsalt), //加密
email: params.email,
passsalt: passsalt,
role: params.role,
role: 'member',
add_time: _yapi2.default.commons.time(),
up_time: _yapi2.default.commons.time()
};
_context2.prev = 13;
_context2.next = 16;
_context3.prev = 13;
_context3.next = 16;
return userInst.save(data);
case 16:
user = _context2.sent;
user = _context3.sent;
user = _yapi2.default.commons.fieldSelect(user, ['id', 'username', 'email']);
ctx.body = _yapi2.default.commons.resReturn(user);
_context2.next = 24;
_yapi2.default.commons.sendMail({
to: params.email,
contents: '\u6B22\u8FCE\u6CE8\u518C\uFF0C\u60A8\u7684\u8D26\u53F7 ' + params.email + ' \u5DF2\u7ECF\u6CE8\u518C\u6210\u529F'
});
_context3.next = 25;
break;
case 21:
_context2.prev = 21;
_context2.t0 = _context2['catch'](13);
case 22:
_context3.prev = 22;
_context3.t0 = _context3['catch'](13);
ctx.body = _yapi2.default.commons.resReturn(null, 401, _context2.t0.message);
ctx.body = _yapi2.default.commons.resReturn(null, 401, _context3.t0.message);
case 24:
case 25:
case 'end':
return _context2.stop();
return _context3.stop();
}
}
}, _callee2, this, [[13, 21]]);
}, _callee3, this, [[13, 22]]);
}));
function reg(_x2) {
return _ref2.apply(this, arguments);
function reg(_x3) {
return _ref3.apply(this, arguments);
}
return reg;
@ -241,81 +265,42 @@ var userController = function (_baseController) {
}, {
key: 'list',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
var userInst, user;
return _regenerator2.default.wrap(function _callee3$(_context3) {
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context3.prev = _context3.next) {
switch (_context4.prev = _context4.next) {
case 0:
//获取用户列表并分页
userInst = _yapi2.default.getInst(_user2.default);
_context3.prev = 1;
_context3.next = 4;
_context4.prev = 1;
_context4.next = 4;
return userInst.list();
case 4:
user = _context3.sent;
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(user));
user = _context4.sent;
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(user));
case 8:
_context3.prev = 8;
_context3.t0 = _context3['catch'](1);
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context3.t0.message));
_context4.prev = 8;
_context4.t0 = _context4['catch'](1);
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t0.message));
case 11:
case 'end':
return _context3.stop();
return _context4.stop();
}
}
}, _callee3, this, [[1, 8]]);
}, _callee4, this, [[1, 8]]);
}));
function list(_x3) {
return _ref3.apply(this, arguments);
function list(_x4) {
return _ref4.apply(this, arguments);
}
return list;
}()
}, {
key: 'findById',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
var userInst, id, result;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.prev = 0;
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
_context4.next = 5;
return userInst.findById(id);
case 5:
result = _context4.sent;
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result));
case 9:
_context4.prev = 9;
_context4.t0 = _context4['catch'](0);
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t0.message));
case 12:
case 'end':
return _context4.stop();
}
}
}, _callee4, this, [[0, 9]]);
}));
function findById(_x4) {
return _ref4.apply(this, arguments);
}
return findById;
}()
}, {
key: 'del',
value: function () {
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) {
var userInst, id, result;
@ -326,83 +311,134 @@ var userController = function (_baseController) {
_context5.prev = 0;
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
_context5.next = 5;
return userInst.del(id);
if (!(this.getUid() != id)) {
_context5.next = 5;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'Without permission.'));
case 5:
_context5.next = 7;
return userInst.findById(id);
case 7:
result = _context5.sent;
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result));
ctx.body = _yapi2.default.commons.resReturn(result);
_context5.next = 12;
break;
case 9:
_context5.prev = 9;
case 11:
_context5.prev = 11;
_context5.t0 = _context5['catch'](0);
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context5.t0.message));
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context5.t0.message);
case 12:
case 14:
case 'end':
return _context5.stop();
}
}
}, _callee5, this, [[0, 9]]);
}, _callee5, this, [[0, 11]]);
}));
function del(_x5) {
function findById(_x5) {
return _ref5.apply(this, arguments);
}
return findById;
}()
}, {
key: 'del',
value: function () {
var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) {
var userInst, id, result;
return _regenerator2.default.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.prev = 0;
if (!(this.getRole() !== 'admin')) {
_context6.next = 3;
break;
}
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'Without permission.'));
case 3:
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
_context6.next = 7;
return userInst.del(id);
case 7:
result = _context6.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context6.next = 14;
break;
case 11:
_context6.prev = 11;
_context6.t0 = _context6['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context6.t0.message);
case 14:
case 'end':
return _context6.stop();
}
}
}, _callee6, this, [[0, 11]]);
}));
function del(_x6) {
return _ref6.apply(this, arguments);
}
return del;
}()
}, {
key: 'update',
value: function () {
var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) {
var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) {
var userInst, id, data, result;
return _regenerator2.default.wrap(function _callee6$(_context6) {
return _regenerator2.default.wrap(function _callee7$(_context7) {
while (1) {
switch (_context6.prev = _context6.next) {
switch (_context7.prev = _context7.next) {
case 0:
_context6.prev = 0;
_context7.prev = 0;
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
id = this.getUid();
data = {};
ctx.request.body.username && (data.username = ctx.request.body.username);
ctx.request.body.password && (data.password = ctx.request.body.password);
ctx.request.body.email && (data.email = ctx.request.body.email);
ctx.request.body.role && (data.role = ctx.request.body.role);
if ((0, _keys2.default)(data).length === 0) {
ctx.body = _yapi2.default.commons.resReturn(null, 404, '用户名、密码、Email、role都为空');
}
_context6.next = 11;
_context7.next = 8;
return userInst.update(id, data);
case 11:
result = _context6.sent;
case 8:
result = _context7.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context6.next = 18;
_context7.next = 15;
break;
case 12:
_context7.prev = 12;
_context7.t0 = _context7['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t0.message);
case 15:
_context6.prev = 15;
_context6.t0 = _context6['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context6.t0.message);
case 18:
case 'end':
return _context6.stop();
return _context7.stop();
}
}
}, _callee6, this, [[0, 15]]);
}, _callee7, this, [[0, 12]]);
}));
function update(_x6) {
return _ref6.apply(this, arguments);
function update(_x7) {
return _ref7.apply(this, arguments);
}
return update;

56
server_dist/install.js Normal file
View File

@ -0,0 +1,56 @@
'use strict';
var _yapi = require('./yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _commons = require('./utils/commons');
var _commons2 = _interopRequireDefault(_commons);
var _db = require('./utils/db.js');
var _db2 = _interopRequireDefault(_db);
var _user = require('./models/user.js');
var _user2 = _interopRequireDefault(_user);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_yapi2.default.commons = _commons2.default;
_yapi2.default.connect = _db2.default.connect();
function install() {
var exist = _yapi2.default.commons.fileExist(_yapi2.default.path.join(_yapi2.default.WEBROOT_RUNTIME, 'init.lock'));
if (exist) {
return _yapi2.default.commons.log('runtime/init.lock文件已存在请确认您是否已安装。如果需要重新安装请删掉runtime/init.lock文件');
process.exit(1);
}
setupSql();
}
function setupSql() {
var userInst = _yapi2.default.getInst(_user2.default);
var passsalt = _yapi2.default.commons.randStr();
var result = userInst.save({
email: _yapi2.default.WEBCONFIG.adminAccount,
password: _yapi2.default.commons.generatePassword('qunar.com', passsalt),
passsalt: passsalt,
role: 'admin',
add_time: _yapi2.default.commons.time(),
up_time: _yapi2.default.commons.time()
});
result.then(function (success) {
console.log('\u521D\u59CB\u5316\u7BA1\u7406\u5458\u8D26\u53F7 "' + _yapi2.default.WEBCONFIG.adminAccount + '" \u6210\u529F');
_yapi2.default.fs.ensureFileSync(_yapi2.default.path.join(_yapi2.default.WEBROOT_RUNTIME, 'init.lock'));
process.exit(1);
}, function (err) {
console.log('\u521D\u59CB\u5316\u7BA1\u7406\u5458\u8D26\u53F7 "' + _yapi2.default.WEBCONFIG.adminAccount + '" \u5931\u8D25, ' + err.message);
process.exit(1);
});
}
install();

View File

@ -82,7 +82,7 @@ var userModel = function (_baseModel) {
}, {
key: 'list',
value: function list() {
return this.model.find().select("username_id username email role add_time up_time").exec(); //显示id name email role
return this.model.find().select("_id username email role add_time up_time").exec(); //显示id name email role
}
}, {
key: 'findByEmail',
@ -110,7 +110,6 @@ var userModel = function (_baseModel) {
_id: id
}, {
username: data.username,
password: data.password,
email: data.email,
role: data.role,
up_time: _yapi2.default.commons.time()

View File

@ -69,6 +69,7 @@ createAction('user', 'findById', 'post', 'findById');
createAction('user', 'update', 'post', 'update');
createAction('user', 'del', 'post', 'del');
createAction('user', 'status', 'get', 'getLoginStatus');
createAction('user', 'logout', 'get', 'logout');
//project
createAction('project', 'add', 'post', 'add');
@ -108,24 +109,22 @@ function createAction(controller, path, method, action) {
return inst.init(ctx);
case 3:
console.log(22222);
if (!(inst.$auth === true)) {
_context.next = 9;
_context.next = 8;
break;
}
_context.next = 7;
_context.next = 6;
return inst[action].call(inst, ctx);
case 7:
_context.next = 10;
case 6:
_context.next = 9;
break;
case 9:
case 8:
ctx.body = _yapi2.default.commons.resReturn(null, 400, 'Without Permission.');
case 10:
case 9:
case 'end':
return _context.stop();
}

View File

@ -116,4 +116,22 @@ exports.expireDate = function (day) {
var date = new Date();
date.setTime(date.getTime() + day * 86400000);
return date;
};
exports.sendMail = function (options, cb) {
if (!_yapi2.default.mail) return false;
options.subject = options.subject ? options.subject + '-yapi平台' : 'ypai平台';
cb = cb || function (err, info) {
if (err) {
_yapi2.default.commons.log('send mail ' + options.to + ' error,' + err.message, 'error');
} else {
_yapi2.default.commons.log('send mail ' + options.to + ' success');
}
};
_yapi2.default.mail.sendMail({
from: _yapi2.default.WEBCONFIG.mail.auth.user,
to: options.to,
subject: 'yapi平台',
html: options.contents
}, cb);
};

View File

@ -37,17 +37,9 @@ function connect() {
_mongooseAutoIncrement2.default.initialize(db);
checkDatabase();
return db;
}
function checkDatabase() {
var exist = _yapi2.default.commons.fileExist(_yapi2.default.path.join(_yapi2.default.WEBROOT_RUNTIME, 'init.lock'));
if (!exist) {
_yapi2.default.commons.log('lock is not exist');
}
}
_yapi2.default.db = model;
module.exports = {

View File

@ -12,11 +12,15 @@ var _fsExtra = require('fs-extra');
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _config = require('./config.json');
var _nodemailer = require('nodemailer');
var _nodemailer2 = _interopRequireDefault(_nodemailer);
var _config = require('../runtime/config.json');
var _config2 = _interopRequireDefault(_config);
var _configDev = require('./config.dev.json');
var _configDev = require('../runtime/config.dev.json');
var _configDev2 = _interopRequireDefault(_configDev);
@ -25,6 +29,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
var args = process.argv.splice(2);
var isDev = args[0] === 'dev' ? true : false;
var insts = new _map2.default();
var mail = void 0;
var config = isDev ? _configDev2.default : _config2.default;
var WEBROOT = _path2.default.resolve(__dirname, '..'); //路径
@ -33,6 +38,12 @@ var WEBROOT_RUNTIME = _path2.default.join(WEBROOT, 'runtime');
var WEBROOT_LOG = _path2.default.join(WEBROOT_RUNTIME, 'log');
var WEBCONFIG = config;
_fsExtra2.default.ensureDirSync(WEBROOT_LOG);
if (WEBCONFIG.mail) {
mail = _nodemailer2.default.createTransport(WEBCONFIG.mail);
}
/**
* 获取一个model实例如果不存在则创建一个新的返回
* @param {*} m class
@ -58,7 +69,7 @@ function delInst(m) {
}
}
module.exports = {
var r = {
fs: _fsExtra2.default,
path: _path2.default,
WEBROOT: WEBROOT,
@ -69,4 +80,7 @@ module.exports = {
getInst: getInst,
delInst: delInst,
getInsts: insts
};
};
if (mail) r.mail = mail;
module.exports = r;