Bladeren bron

修改eslint

郑国榕 8 jaren geleden
bovenliggende
commit
1dd0200409

+ 0 - 8
api.md

@@ -1,8 +0,0 @@
-##API地址速查
-
-### 用户相关
-
-> 用户登录 /auth/login
-
-> 用户注册 /auth/register
-

+ 75 - 75
api/pages/pages.controller.js

@@ -6,123 +6,123 @@ var Pages = require('./pages.model')
 var tools = require('../../util/tools')
 
 const respondWithResult = (res, statusCode) => {
-    statusCode = statusCode || 200
-    return function (entity) {
-        if (entity) {
-            return res.status(statusCode).json(entity)
-        }
-        return null
+  statusCode = statusCode || 200
+  return function (entity) {
+    if (entity) {
+      return res.status(statusCode).json(entity)
     }
+    return null
+  }
 }
 
 const patchUpdates = (patches) => {
-    return function (entity) {
-        try {
-            jsonpatch.apply(entity, patches, /*validate*/ true)
-        } catch (err) {
-            return Promise.reject(err)
-        }
-
-        return entity.save()
+  return function (entity) {
+    try {
+      jsonpatch.apply(entity, patches, true)
+    } catch (err) {
+      return Promise.reject(err)
     }
+
+    return entity.save()
+  }
 }
 
 const removeEntity = (res) => {
-    return function (entity) {
-        if (entity) {
-            return entity.remove()
-                .then(() => {
-                    res.status(204).end()
-                })
-        }
+  return function (entity) {
+    if (entity) {
+      return entity.remove()
+        .then(() => {
+          res.status(204).end()
+        })
     }
+  }
 }
 
 const handleEntityNotFound = (res) => {
-    return function (entity) {
-        if (!entity) {
-            res.status(404).end()
-            return null
-        }
-        return entity
+  return function (entity) {
+    if (!entity) {
+      res.status(404).end()
+      return null
     }
+    return entity
+  }
 }
 
 const handleError = (res, statusCode) => {
-    statusCode = statusCode || 500
-    return function (err) {
-        res.status(statusCode).send(err)
-    }
+  statusCode = statusCode || 500
+  return function (err) {
+    res.status(statusCode).send(err)
+  }
 }
 
 module.exports.index = (req, res) => {
-    return Pages.find().exec()
-        .then(respondWithResult(res))
-        .catch(handleError(res))
+  return Pages.find().exec()
+    .then(respondWithResult(res))
+    .catch(handleError(res))
 }
 
 module.exports.findByLoginId = (req, res) => {
-    var loginId = req.user.loginId
-    var type = req.query.type;
-    return Pages.find({ loginId: loginId, type: type }).exec()
-        .then(respondWithResult(res))
-        .catch(handleError(res))
+  var loginId = req.user.loginId
+  var type = req.query.type;
+  return Pages.find({ loginId: loginId, type: type }).exec()
+    .then(respondWithResult(res))
+    .catch(handleError(res))
 }
 
 // Gets a single Pages from the DB
 module.exports.show = (req, res) => {
-    return Pages.findById(req.params.id).exec()
-        .then(handleEntityNotFound(res))
-        .then(respondWithResult(res))
-        .catch(handleError(res))
+  return Pages.findById(req.params.id).exec()
+    .then(handleEntityNotFound(res))
+    .then(respondWithResult(res))
+    .catch(handleError(res))
 }
 
 // Creates a new Pages in the DB
 module.exports.create = (req, res) => {
-    //添加作者信息
-    req.body.loginId = req.user.loginId
-    return Pages.create(req.body)
-        .then(respondWithResult(res, 201))
-        .catch(handleError(res))
+  //添加作者信息
+  req.body.loginId = req.user.loginId
+  return Pages.create(req.body)
+    .then(respondWithResult(res, 201))
+    .catch(handleError(res))
 }
 
 // Upserts the given Pages in the DB at the specified ID
 module.exports.update = (req, res) => {
-    if (req.body._id) {
-        delete req.body._id
-    }
-    if (req.body.type === 'h5') {
-        tools.renderFile('template.html', req.body, (html) => {
-            tools.saveFile(req.params.id + '.html', html)
-        })
-    } else if (req.body.type === 'spa') {
-        tools.renderFile('spa.html', req.body, (html) => {
-            tools.saveFile(req.params.id + '.html', html)
-        })
-    }
+  if (req.body._id) {
+    delete req.body._id
+  }
+  if (req.body.type === 'h5') {
+    tools.renderFile('template.html', req.body, (html) => {
+      tools.saveFile(req.params.id + '.html', html)
+    })
+  } else if (req.body.type === 'spa') {
+    tools.renderFile('spa.html', req.body, (html) => {
+      tools.saveFile(req.params.id + '.html', html)
+    })
+  }
 
-    return Pages.findOneAndUpdate({ _id: req.params.id }, req.body, { upsert: true, setDefaultsOnInsert: true, runValidators: true }).exec()
-        .then(respondWithResult(res))
-        .catch(handleError(res))
+  return Pages.findOneAndUpdate({ _id: req.params.id }, req.body, { upsert: true, setDefaultsOnInsert: true, runValidators: true }).exec()
+    .then(respondWithResult(res))
+    .catch(handleError(res))
 }
 
 
 // Updates an existing Pages in the DB
 module.exports.patch = (req, res) => {
-    if (req.body._id) {
-        delete req.body._id
-    }
-    return Pages.findById(req.params.id).exec()
-        .then(handleEntityNotFound(res))
-        .then(patchUpdates(req.body))
-        .then(respondWithResult(res))
-        .catch(handleError(res))
+  if (req.body._id) {
+    delete req.body._id
+  }
+  return Pages.findById(req.params.id).exec()
+    .then(handleEntityNotFound(res))
+    .then(patchUpdates(req.body))
+    .then(respondWithResult(res))
+    .catch(handleError(res))
 }
 
 // Deletes a Pages from the DB
 module.exports.destroy = (req, res) => {
-    return Pages.findById(req.params.id).exec()
-        .then(handleEntityNotFound(res))
-        .then(removeEntity(res))
-        .catch(handleError(res))
+  return Pages.findById(req.params.id).exec()
+    .then(handleEntityNotFound(res))
+    .then(removeEntity(res))
+    .catch(handleError(res))
 }

+ 14 - 14
api/pages/pages.model.js

@@ -5,19 +5,19 @@ const mongoose = require('mongoose')
 mongoose.Promise = require('bluebird')
 
 var PageSchema = new mongoose.Schema({
-    pages: {
-        type: Array,
-        required: true
-    },
-    title: String,
-    description: String,
-    html: String,
-    createDate: { type: Number, default: new Date().getTime() },
-    loginId: String,
-    type: {
-        type: String, required: true, default: 'h5', enum: ['h5', 'spa'] // 页面是单页还是多页 
-    },
-    canvasHeight: Number
+  pages: {
+    type: Array,
+    required: true
+  },
+  title: String,
+  description: String,
+  html: String,
+  createDate: { type: Number, default: new Date().getTime() },
+  loginId: String,
+  type: {
+    type: String, required: true, default: 'h5', enum: ['h5', 'spa'] // 页面是单页还是多页
+  },
+  canvasHeight: Number
 })
 
-module.exports = mongoose.model('Page', PageSchema)
+module.exports = mongoose.model('Page', PageSchema)

+ 7 - 7
api/user/index.js

@@ -7,11 +7,11 @@ const auth = require('../../auth/auth.service')
 
 var router = new express.Router()
 
-router.get('/', auth.hasRole('admin'), controller.index);
-router.delete('/:id', auth.hasRole('admin'), controller.destroy);
-router.get('/me', auth.isAuthenticated(), controller.me);
-router.put('/:id/password', auth.isAuthenticated(), controller.changePassword);
-router.get('/:id', auth.isAuthenticated(), controller.show);
-router.post('/', controller.create);
+router.get('/', auth.hasRole('admin'), controller.index)
+router.delete('/:id', auth.hasRole('admin'), controller.destroy)
+router.get('/me', auth.isAuthenticated(), controller.me)
+router.put('/:id/password', auth.isAuthenticated(), controller.changePassword)
+router.get('/:id', auth.isAuthenticated(), controller.show)
+router.post('/', controller.create)
 
-module.exports = router
+module.exports = router

+ 85 - 86
api/user/user.controller.js

@@ -12,29 +12,29 @@ const jwt = require('jsonwebtoken')
  * @returns {Function}
  */
 const validationError = (res, statusCode) => {
-    statusCode = statusCode || 422;
-    return function (err) {
-        return res.status(statusCode).json(err);
-    };
+  statusCode = statusCode || 422
+  return function (err) {
+    return res.status(statusCode).json(err)
+  }
 }
 
 const handleError = (res, statusCode) => {
-    statusCode = statusCode || 500;
-    return function (err) {
-        return res.status(statusCode).send(err);
-    };
+  statusCode = statusCode || 500
+  return function (err) {
+    return res.status(statusCode).send(err)
+  }
 }
 
 module.exports.index = (req, res) => {
-    return User.find({}, '-salt -password').exec()
-        .then(users => {
-            res.status(200).json(users);
-        })
-        .catch(handleError(res));
+  return User.find({}, '-salt -password').exec()
+    .then(users => {
+      res.status(200).json(users)
+    })
+    .catch(handleError(res))
 }
 
 module.exports.findByToken = (token) => {
-    return User.findOne({ token: token }).exec()
+  return User.findOne({ token: token }).exec()
 }
 
 /**
@@ -43,36 +43,36 @@ module.exports.findByToken = (token) => {
  * @param res
  */
 module.exports.create = (req, res) => {
-    let newUser = new User(req.body)
-    newUser.provider = 'local'
-    newUser.role = 'user'
-    newUser.save()
-        .then((user) => {
-            let token = jwt.sign({ _id: user._id }, config.secrets.session, {
-                expiresIn: 60 * 60 * 5
-            })
-            user.token = token
-            var updateUser = JSON.parse(JSON.stringify(user))
-            delete updateUser._id
-            User.findOneAndUpdate({ _id: user._id }, updateUser).exec()
-            res.json({ token })
-        })
-        .catch(validationError(res))
+  let newUser = new User(req.body)
+  newUser.provider = 'local'
+  newUser.role = 'user'
+  newUser.save()
+    .then((user) => {
+      let token = jwt.sign({ _id: user._id }, config.secrets.session, {
+        expiresIn: 60 * 60 * 5
+      })
+      user.token = token
+      var updateUser = JSON.parse(JSON.stringify(user))
+      delete updateUser._id
+      User.findOneAndUpdate({ _id: user._id }, updateUser).exec()
+      res.json({ token })
+    })
+    .catch(validationError(res))
 }
 
 /**
  * 获取单个用户信息
  */
 module.exports.show = (req, res, next) => {
-    let userId = req.params.id
-    return User.findById(userId).exec()
-        .then(user => {
-            if (!user) {
-                return res.status(400).end()
-            }
-            res.json(user.profile)
-        })
-        .catch(err => next(err))
+  let userId = req.params.id
+  return User.findById(userId).exec()
+    .then(user => {
+      if (!user) {
+        return res.status(400).end()
+      }
+      res.json(user.profile)
+    })
+    .catch(err => next(err))
 }
 
 /**
@@ -82,11 +82,11 @@ module.exports.show = (req, res, next) => {
  * @returns {Promise.<TResult>|Promise}
  */
 module.exports.destroy = (req, res) => {
-    return User.findByIdAndRemove(req.params.id).exec()
-        .then(() => {
-            res.status(204).end()
-        })
-        .catch(handleError(res))
+  return User.findByIdAndRemove(req.params.id).exec()
+    .then(() => {
+      res.status(204).end()
+    })
+    .catch(handleError(res))
 }
 
 /**
@@ -96,25 +96,24 @@ module.exports.destroy = (req, res) => {
  * @returns {Promise.<TResult>}
  */
 module.exports.changePassword = (req, res) => {
-    var uesrId = req.user._id
-    var oldPass = String(req.body.oldPassword)
-    var newPass = String(req.body.newPassword)
-    return User.findById(uesrId).exec()
-        .then(user => {
-            if (user.authenticate(oldPass)) {
-                user.password = newPass
-                return user.save()
-                    .then(() => {
-                        res.status(204).end()
-                    })
-                    .catch(validationError(res))
-            } else {
-                return res.status(403).end()
-            }
-        })
+  var uesrId = req.user._id
+  var oldPass = String(req.body.oldPassword)
+  var newPass = String(req.body.newPassword)
+  return User.findById(uesrId).exec()
+    .then(user => {
+      if (user.authenticate(oldPass)) {
+        user.password = newPass
+        return user.save()
+          .then(() => {
+            res.status(204).end()
+          })
+          .catch(validationError(res))
+      } else {
+        return res.status(403).end()
+      }
+    })
 }
 
-
 /**
  * 用户登陆
  * @param req
@@ -122,24 +121,24 @@ module.exports.changePassword = (req, res) => {
  * @returns {Promise.<TResult>}
  */
 module.exports.login = (req, res) => {
-    var loginId = req.body.loginId
-    var password = req.body.password
-    let token
-    return User.findOne({ loginId: loginId }).exec()
-        .then(user => {
-            if (user && user.authenticate(password)) {
-                token = jwt.sign({ _id: user._id }, config.secrets.session, {
-                    expiresIn: 60 * 60 * 5
-                })
-                user.token = token
-                var updateUser = JSON.parse(JSON.stringify(user))
-                delete updateUser._id
-                User.findOneAndUpdate({ _id: user._id }, updateUser).exec()
-                res.status(200).json({ token }).end()
-            } else {
-                return res.status(401).end()
-            }
+  var loginId = req.body.loginId
+  var password = req.body.password
+  let token
+  return User.findOne({ loginId: loginId }).exec()
+    .then(user => {
+      if (user && user.authenticate(password)) {
+        token = jwt.sign({ _id: user._id }, config.secrets.session, {
+          expiresIn: 60 * 60 * 5
         })
+        user.token = token
+        var updateUser = JSON.parse(JSON.stringify(user))
+        delete updateUser._id
+        User.findOneAndUpdate({ _id: user._id }, updateUser).exec()
+        res.status(200).json({ token }).end()
+      } else {
+        return res.status(401).end()
+      }
+    })
 }
 
 /**
@@ -150,13 +149,13 @@ module.exports.login = (req, res) => {
  * @returns {Promise.<TResult>|Promise}
  */
 module.exports.me = (req, res, next) => {
-    var userId = req.user._id
-    return User.findOne({ _id: userId }, '-salt -password').exec()
-        .then(user => { // don't ever give out the password or salt
-            if (!user) {
-                return res.status(401).end();
-            }
-            res.json(user);
-        })
-        .catch(err => next(err));
+  var userId = req.user._id
+  return User.findOne({ _id: userId }, '-salt -password').exec()
+    .then(user => { // don't ever give out the password or salt
+      if (!user) {
+        return res.status(401).end()
+      }
+      res.json(user)
+    })
+    .catch(err => next(err))
 }

+ 156 - 156
api/user/user.model.js

@@ -3,24 +3,24 @@ const mongoose = require('mongoose')
 mongoose.Promise = require('bluebird')
 
 var UserSchema = new mongoose.Schema({
-    name: String,
-    loginId: {
-        type: String,
-        lowercase: true,
-        required: true
-    },
-    role: {
-        type: String,
-        default: 'user'
-    },
-    password: {
-        type: String,
-        required: true
-    },
-    provider: String,
-    salt: String,
-    token: String
-});
+  name: String,
+  loginId: {
+    type: String,
+    lowercase: true,
+    required: true
+  },
+  role: {
+    type: String,
+    default: 'user'
+  },
+  password: {
+    type: String,
+    required: true
+  },
+  provider: String,
+  salt: String,
+  token: String
+})
 
 
 /**
@@ -29,168 +29,168 @@ var UserSchema = new mongoose.Schema({
 
 // Validate empty email
 UserSchema
-    .path('loginId')
-    .validate((loginId) => {
-        return loginId.length;
-    }, '登陆名不能空');
+  .path('loginId')
+  .validate((loginId) => {
+    return loginId.length
+  }, '登陆名不能空')
 
 // Validate empty password
 UserSchema
-    .path('password')
-    .validate((password) => {
-        return password.length;
-    }, '密码不能空');
+  .path('password')
+  .validate((password) => {
+    return password.length
+  }, '密码不能空')
 
 // Validate loginId is not taken
 UserSchema
-    .path('loginId')
-    .validate(function (value, respond) {
-        return this.constructor.findOne({loginId: value}).exec()
-            .then(user => {
-                if (user) {
-                    if (this.id === user.id) {
-                        return respond(true);
-                    }
-                    return respond(false);
-                }
-                return respond(true);
-            })
-            .catch((err) => {
-                throw err;
-            });
-    }, '该用户已存在');
+  .path('loginId')
+  .validate(function (value, respond) {
+    return this.constructor.findOne({ loginId: value }).exec()
+      .then(user => {
+        if (user) {
+          if (this.id === user.id) {
+            return respond(true)
+          }
+          return respond(false)
+        }
+        return respond(true)
+      })
+      .catch((err) => {
+        throw err
+      })
+  }, '该用户已存在')
 
 var validatePresenceOf = (value) => {
-    return value && value.length;
-};
+  return value && value.length
+}
 
 /**
  * Pre-save hook
  */
 UserSchema
-    .pre('save', function (next) {
-        // Handle new/update passwords
-        if (!this.isModified('password')) {
-            return next();
-        }
+  .pre('save', function (next) {
+    // Handle new/update passwords
+    if (!this.isModified('password')) {
+      return next()
+    }
 
-        if (!validatePresenceOf(this.password)) {
-            return next(new Error('密码错误'));
-        }
+    if (!validatePresenceOf(this.password)) {
+      return next(new Error('密码错误'))
+    }
 
-        // Make salt with a callback
-        this.makeSalt((saltErr, salt) => {
-            if (saltErr) {
-                return next(saltErr);
-            }
-            this.salt = salt;
-            this.encryptPassword(this.password, (encryptErr, hashedPassword) => {
-                if (encryptErr) {
-                    return next(encryptErr);
-                }
-                this.password = hashedPassword;
-                return next();
-            });
-        });
-    });
+    // Make salt with a callback
+    this.makeSalt((saltErr, salt) => {
+      if (saltErr) {
+        return next(saltErr)
+      }
+      this.salt = salt
+      this.encryptPassword(this.password, (encryptErr, hashedPassword) => {
+        if (encryptErr) {
+          return next(encryptErr)
+        }
+        this.password = hashedPassword
+        return next()
+      })
+    })
+  })
 
 /**
  * Methods
  */
 UserSchema.methods = {
-    /**
-     * Authenticate - check if the passwords are the same
-     *
-     * @param {String} password
-     * @param {Function} callback
-     * @return {Boolean}
-     * @api public
-     */
-    authenticate(password, callback) {
-        if (!callback) {
-            return this.password === this.encryptPassword(password);
-        }
-
-        this.encryptPassword(password, (err, pwdGen) => {
-            if (err) {
-                return callback(err);
-            }
-
-            if (this.password === pwdGen) {
-                return callback(null, true);
-            } else {
-                return callback(null, false);
-            }
-        });
-    },
-
-    /**
-     * Make salt
-     *
-     * @param {Number} [byteSize] - Optional salt byte size, default to 16
-     * @param {Function} callback
-     * @return {String}
-     * @api public
-     */
-    makeSalt(byteSize, callback) {
-        var defaultByteSize = 16;
-
-        if (typeof arguments[0] === 'function') {
-            callback = arguments[0];
-            byteSize = defaultByteSize;
-        } else if (typeof arguments[1] === 'function') {
-            callback = arguments[1];
-        } else {
-            throw new Error('却少回调方法');
-        }
+  /**
+   * Authenticate - check if the passwords are the same
+   *
+   * @param {String} password
+   * @param {Function} callback
+   * @return {Boolean}
+   * @api public
+   */
+  authenticate(password, callback) {
+    if (!callback) {
+      return this.password === this.encryptPassword(password)
+    }
 
-        if (!byteSize) {
-            byteSize = defaultByteSize;
-        }
+    this.encryptPassword(password, (err, pwdGen) => {
+      if (err) {
+        return callback(err)
+      }
+
+      if (this.password === pwdGen) {
+        return callback(null, true)
+      } else {
+        return callback(null, false)
+      }
+    })
+  },
+
+  /**
+   * Make salt
+   *
+   * @param {Number} [byteSize] - Optional salt byte size, default to 16
+   * @param {Function} callback
+   * @return {String}
+   * @api public
+   */
+  makeSalt(byteSize, callback) {
+    var defaultByteSize = 16
+
+    if (typeof arguments[0] === 'function') {
+      callback = arguments[0]
+      byteSize = defaultByteSize;
+    } else if (typeof arguments[1] === 'function') {
+      callback = arguments[1]
+    } else {
+      throw new Error('却少回调方法')
+    }
 
-        return crypto.randomBytes(byteSize, (err, salt) => {
-            if (err) {
-                return callback(err);
-            } else {
-                return callback(null, salt.toString('base64'));
-            }
-        });
-    },
-
-    /**
-     * Encrypt password
-     *
-     * @param {String} password
-     * @param {Function} callback
-     * @return {String}
-     * @api public
-     */
-    encryptPassword(password, callback) {
-        if (!password || !this.salt) {
-            if (!callback) {
-                return null;
-            } else {
-                return callback('却少密码或者加密内容');
-            }
-        }
+    if (!byteSize) {
+      byteSize = defaultByteSize
+    }
 
-        var defaultIterations = 10000;
-        var defaultKeyLength = 64;
-        var salt = new Buffer(this.salt, 'base64');
+    return crypto.randomBytes(byteSize, (err, salt) => {
+      if (err) {
+        return callback(err)
+      } else {
+        return callback(null, salt.toString('base64'))
+      }
+    })
+  },
+
+  /**
+   * Encrypt password
+   *
+   * @param {String} password
+   * @param {Function} callback
+   * @return {String}
+   * @api public
+   */
+  encryptPassword(password, callback) {
+    if (!password || !this.salt) {
+      if (!callback) {
+        return null
+      } else {
+        return callback('却少密码或者加密内容')
+      }
+    }
 
-        if (!callback) {
-            return crypto.pbkdf2Sync(password, salt, defaultIterations, defaultKeyLength)
-                .toString('base64');
-        }
+    var defaultIterations = 10000
+    var defaultKeyLength = 64
+    var salt = new Buffer(this.salt, 'base64')
 
-        return crypto.pbkdf2(password, salt, defaultIterations, defaultKeyLength, (err, key) => {
-            if (err) {
-                return callback(err);
-            } else {
-                return callback(null, key.toString('base64'));
-            }
-        });
+    if (!callback) {
+      return crypto.pbkdf2Sync(password, salt, defaultIterations, defaultKeyLength)
+        .toString('base64')
     }
-};
 
-module.exports = mongoose.model('User', UserSchema);
+    return crypto.pbkdf2(password, salt, defaultIterations, defaultKeyLength, (err, key) => {
+      if (err) {
+        return callback(err)
+      } else {
+        return callback(null, key.toString('base64'))
+      }
+    })
+  }
+}
+
+module.exports = mongoose.model('User', UserSchema)

+ 2 - 0
webapp/src/components/HeaderBar.vue

@@ -43,6 +43,8 @@ export default {
     this.navList.forEach((element) => {
       if (element.path === this.$route.name) {
         element.active = true
+      } else if (this.$route.name === 'index') {
+        this.navList[0].active = true
       }
     })
   }

+ 1 - 1
webapp/src/util/appConst.js

@@ -1,7 +1,7 @@
 
 let BACKEND_DOMAIN = 'http://localhost:3000'
 if (process.env.NODE_ENV === 'production') {
-  BACKEND_DOMAIN = 'http://node.bluemoon.com.cn'
+  BACKEND_DOMAIN = '.'
 } else if (process.env.NODE_ENV === 'development') {
   BACKEND_DOMAIN = 'http://localhost:3000'
 }