/** * 用户端点。 */ "use strict"; let ObjectUtil = require('../../util/object.util'); let ControllerUtil = require('../../util/controller.util'); let Users = require('../../models/user/users'); let express = require('express'); let router = express.Router(); let http = require('http'); let configFile = require('../../include/commons').CONFIG_FILE; let config = require('../../resources/config/' + configFile); let log = require("../../util/log.js"); const APIv2 = require('../../include/endpoints').APIv2; /** * 登录。用户登录后,更新数据库中的在线状态。 * * 请求URL: * /users/login?user_id=sand&token=0PFWlKmLBN9YzhCfFWVgYA&client_id=H6FYbDejks6VjMmW3uH7V6&platform=0 * * 返回值: * 登录后的token. * * 参数说明: * user_id:用户ID * token:个推的token * client_id:个推的client id * platform:平台类型,0为iOS,1为Android */ router.post(APIv2.Users.Login, function (req, res) { let appStatus = req.body; let testing = ObjectUtil.fieldsCheck(appStatus, "user_id", "token", "client_id", "platform"); if (!testing.pass) { throw {httpStatus: 406, message: testing.message} } let users = new Users(); ControllerUtil.regModelEventHandler(users, res); users.login(appStatus.user_id, appStatus.platform, appStatus.token, appStatus.client_id); }); /** * 更新客户端状态。 * * 请求URL: * /users/:user_id/status * * POST参数格式: * {status: 1}, app状态,0在后台,1在前台 */ router.put(APIv2.Users.UserStatus, function (req, res) { let userId = req.params.user_id; let status = req.body; if (!ObjectUtil.isJsonObject(status)) { throw {httpStatus: 406, message: "Problems parsing json."}; } if (userId === null) { throw {httpStatus: 406, message: 'Validation Failed. Missing field(s): user_id.'}; } let users = new Users(); ControllerUtil.regModelEventHandler(users, res); users.updateAppStatus(userId, status.app_in_bg); }); /** * 退出。 * * 请求URL: * /users/logout?user_id=sand * * 参数: * user_id:用户ID */ router.delete(APIv2.Users.Logout, function (req, res) { let userId = req.query.user_id; if (userId == null) { throw {httpStatus: 406, message: 'Logout Failed. Missing field: user_id.'}; } let userStatus = new Users(); ControllerUtil.regModelEventHandler(userStatus, res); userStatus.logout(userId); }); module.exports = router;