public function register() { $this->db->begin(); $modelUser = new User(); $res = $modelUser->createUser($this->request->getPost()); if (false == $res) { $this->db->rollback(); return parent::resWithErrMsg($modelUser->getMessages()); } $this->db->commit(); return parent::success(); }
/** * @api {post} /token 登录获得token * @apiHeader {String} Accept=api-version=1.0 api版本 * @apiHeaderExample {String} Header-Example: * { * "Accept": "api-version=1.0" * } * @apiName login * @apiGroup Token * @apiVersion 1.0.0 * * @apiParam {String} username 用户名 * @apiParam {String} password 密码 * * @apiSuccess {String} token 该用户的token,两小时后失效 * * @apiSuccessExample Success-Response: * HTTP/1.1 200 OK * { * "token": "xxx" * } * * @apiUse errorExample */ public function login() { $username = $this->request->getPost('username'); $password = $this->request->getPost('password'); $userModel = new User(); $result = $userModel->login($username, $password); if (false === $result) { return parent::response($userModel->getMessages(), 406); } $roleUser = RoleUser::findFirst("user_id=" . $result->id); $token = parent::obtainToken($result->id, $roleUser->role_id); if (false === $token) { return parent::response(array('errors' => array(array('code' => 500, 'field' => null, 'message' => 'unkown error'))), 500); } return parent::success(array('token' => $token)); }
/** * @api {put} /user 更新当前登录用户信息 * @apiUse header * * @apiName updateUser * @apiGroup User * @apiVersion 1.0.0 * * @apiParam {String} username 该子会议的ID * @apiParam {String} name 该子会议名称 必选 * @apiParam {String} organization 子会议的开始时间 * @apiParam {Integer} title 子会议的结束时间 * @apiParam {String} email 子会议举行场地 * @apiParam {String} password 该子会议可接纳的人数 * * @apiSuccess {Array} empty_array 空数组 */ public function updateUser() { $token = $this->session->get('token'); // username name organization title email password $data = $this->request->get(); $dbUser = User::findFirst('id=' . $token->user_id); if (!empty($data['password'])) { $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT); } $dbUser = $dbUser->toArray(); $userModel = new User(); if (false == $userModel->save(array_merge($dbUser, $data))) { // 使用修改的数据覆盖原始的数据来达到部分更新效果 return parent::resWithErrMsg($userModel->getMessages()); } return parent::success(); }
/** * @api {delete} /admin/user/{id} 删除某个用户 * @apiUse header * * @apiName deleteUser * @apiGroup User * @apiVersion 1.0.0 * * @apiParam {String} username 该子会议的ID * @apiParam {String} name 该子会议名称 必选 * @apiParam {String} organization 子会议的开始时间 * @apiParam {Integer} title 子会议的结束时间 * @apiParam {String} email 子会议举行场地 * @apiParam {String} password 该子会议可接纳的人数 * * @apiSuccess {Array} empty_array 空数组 */ public function deleteUser($id) { if (empty($id)) { return parent::required('id'); } $where = 'id=' . $id . ' and isdeleted=0'; $user = User::findFirst($where); if (empty($user)) { return parent::invalid('id', $id); } if (false == $user->delete()) { return parent::resWithErrMsg($user->getMessages(), 406); } $roleUser = new RoleUser(); $roleUser->user_id = $id; if (false == $roleUser->delete()) { return parent::resWithErrMsg($roleUser->getMessages()); } return parent::success(); }
/** * @api {post} /user 注册接口 * @apiHeader {String} Accept=api-version=1.0 api版本 * @apiHeaderExample {String} Header-Example: * { * "Accept": "api-version=1.0" * } * @apiName register * @apiGroup User * @apiVersion 1.0.0 * * @apiSuccess {Array} empty_array 空数组 * * @apiUse errorExample */ public function register() { $this->db->begin(); $data = $this->request->getPost(); $userValidator = new UserValidator(); $messages = $userValidator->validate($data); if (0 != count($messages)) { return parent::resWithErrMsg($messages, 406); } $modelUser = new User(); $duplicate = $modelUser->findFirst("lower(username)='" . strtolower($data['username']) . "'"); if (!empty($duplicate)) { return parent::valueDuplicate('username'); } $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT); $res = $modelUser->create($data); if (false == $res) { $this->db->rollback(); return parent::resWithErrMsg($modelUser->getMessages()); } $config = $this->di->get('config'); $userRole['role_id'] = $config->role->User; $userRole['user_id'] = $modelUser->id; $roleUserModel = new RoleUser(); $res = $roleUserModel->create($userRole); if (false == $res) { $this->db->rollback(); return parent::resWithErrMsg($roleUserModel->getMessages()); } $this->db->commit(); return parent::success(); }