40) { return ResultWrapper::fail('name字段超出了40个字'); } $password = $info['password']; if (empty($password)) { return ResultWrapper::fail('password字段不能为空'); } $password = Math::md5_16($password); $email = $info['email']; // if (empty($email)) { // return ResultWrapper::fail('email字段不能为空'); // } # 这是数据表存储上的逻辑 if (mb_strlen($email) > 50) { return ResultWrapper::fail('email字段超出了50个字'); } if (isset($info['register_time']) && Verify::int($info['register_time'])) { $register_time = $info['register_time']; } else { $register_time = time(); } $tableInfo = array_merge($info, array( 'name' => $name, 'password' => $password, 'email' => $email, 'register_time' => $register_time, )); $uid = parent::add($tableInfo, self::PARAM_CREATE_ACTION_INSERT_IGNORE); if (!$uid) { return ResultWrapper::fail('插入用户库失败'); } if (!Verify::int($uid)) { return ResultWrapper::fail("用户名 {$name} 已存在"); } $user = $tableInfo; $user[$this->primaryKey] = $uid; return ResultWrapper::success($user); } /** * * 根据用户名、密码获取用户信息 * @param string $name * @param string $password * @return ResultWrapper */ public function getByNameAndPassword($name, $password) { $user = $this->getByName(mb_strtolower($name, 'UTF-8')); if (!$user) { return ResultWrapper::fail("用户 {$name} 不存在"); } if ($user['password'] != Math::md5_16($password)) { return ResultWrapper::fail("密码 {$password} 不正确"); } return ResultWrapper::success($user); } public function getByName($name) { $uid = $this->getIdByName($name); return $this->get($uid); } /** * * 获取指定用户名 $name 的用户id * @param string $name * @return false | int */ public function getIdByName($name) { if (empty($name)) { return false; } $condition = array( 'name' => $name, ); $result = $this->fetchOne($condition, $this->primaryKey); if (!$result) { return false; } return array_pop($result); } /** * 修改用户密码 * @param int $uid * @param string $oldPassword * @param string $newPassword * @return ResultWrapper */ public function modifyPassword($uid, $oldPassword, $newPassword) { if (!Verify::unsignedInt($uid)) { return ResultWrapper::fail('无效uid'); } $user = $this->get($uid); if ($user['password'] != Math::md5_16($oldPassword)) { return ResultWrapper::fail('原始密码错误'); } $newPassword = trim($newPassword); if (empty($newPassword)) { return ResultWrapper::fail('新密码不能为空'); } $newPassword = Math::md5_16($newPassword); $info = array( 'uid' => $uid, 'password' => $newPassword, ); $cas_token = $user['cas_token']; $tmpResult = $this->modify($info, null, $cas_token); if (!$tmpResult->isSuccess()) { return ResultWrapper::fail('修改密码失败。原因:'.$tmpResult->getData()); } return ResultWrapper::success(); } }