/** * 编辑资料 * @param $id */ public function update($id) { try { $model = new UserModel(); $user = $model->find($id); unset($user['password']); if (empty($user)) { throw new Exception('用户不存在'); } if (IS_POST) { $data = $_POST; $unsets = array('userId', 'username', 'createdAt', 'createdIp', 'postCount'); if (empty($data['password'])) { $unsets[] = 'password'; } else { $data['password'] = saltMd5($data['password']); } foreach ($unsets as $unset) { unset($data[$unset]); } $result = $model->where(array('userId' => $id))->save($data); if ($result === false) { throw new Exception('编辑失败'); } $this->redirect('index'); } else { $this->assign('user', $user); $this->assign('pageTitle', '编辑用户'); $this->display(); } } catch (Exception $e) { $this->error($e->getMessage()); } }
/** * 用户主页 */ public function home() { $this->checkLogin(); $model = new UserModel(); if (IS_POST) { $avatar = I('avatar'); $nickname = I('nickname'); $password = I('password'); if (!$model->create()) { $this->error($model->getError()); } $data = ['avatar' => $avatar, 'nickname' => $nickname]; if (!empty($password)) { $data['password'] = $password; } if ($model->where(array('userId' => $this->user['userId']))->save() === false) { $this->error('编辑失败'); } $this->success('编辑成功'); } else { $user = $model->find($this->user['userId']); $this->assign('user', $user); $this->display(); } }