/** * 编辑资料 * @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()); } }
/** * 登录 * @param $username * @param $password * @return mixed * @throws Exception */ public function login($username, $password) { $user = $this->where(array('username' => $username))->find(); if (empty($user) || $user['password'] != saltMd5($password)) { throw new Exception('用户名或密码错误'); } session('user', $user); return $user; }
/** * 管理员登录 * @param $username * @param $password * @return mixed * @throws Exception */ public function login($username, $password) { $user = $this->where(array('username' => $username))->find(); if (empty($user) || $user['password'] != saltMd5($password)) { throw new Exception('用户名或密码错误'); } session(BaseController::SESSION_KEY, $user); $data['loginAt'] = date('Y-m-d H:i:s'); $data['loginIp'] = get_client_ip(1, true); $this->where(array('adminId' => $user['adminId']))->save($data); return $user; }