| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- namespace App\Module\User\Services;
- use App\Module\App\Mnemon;
- use App\Module\App\SessionApp;
- use App\Module\Ulogic\Model\UserBans;
- use App\Module\Ulogic\Model\UserWord;
- use App\Module\User\Model\User;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- class UserService
- {
- // 默认头像
- const AVATAR = 'https://www.api.xuyoo.cc/images/default_avatar.jpg';
- // 登录错误次数key
- const LOGIN_ERROR_NUM = 'LOGIN_ERROR_NUM_';
- // 登录错误次数最大值
- const ERROR_NUM = 5;
- /**
- * @param $mobile
- * @return bool
- * 查询用户信息
- */
- public static function getUserByMobile($mobile)
- {
- $data = User::query()->where('mobile', $mobile)->first();
- if (!$data || !$data->password) {
- return false;
- }
- return true;
- }
- /**
- * @param $mobile
- * @return \App\Module\Ulogic\Unit\User|array|false
- * @throws \\Exception\LogicException
- * 注册
- */
- public static function register($mobile)
- {
- // 判断手机号是否已存在
- $model = User::getUserInfoByCondition('mobile', $mobile);
- if (!$model) {
- // user表插入数据,返回token
- $model = User::createUser($mobile, self::AVATAR);
- if (!$model) {
- return false;
- }
- }
- // 生成token
- $data = SessionApp::setLogin($model, true);
- return $data;
- }
- /**
- * @param $inviteCode
- * @return array|false
- * 获取邀请信息
- */
- public static function getInviteInfo($inviteCode)
- {
- $data = User::getUserInfoByCondition('invite_code', $inviteCode);
- if (empty($data)) {
- return false;
- }
- return [
- 'nickname' => $data['nickname'],
- 'avatar' => $data['avatar'],
- ];
- }
- /**
- * @param $mobile
- * @param $password
- * @return \App\Module\Ulogic\Unit\User|array|false
- * @throws \FurqanSiddiqui\BIP39\Exception\Bip39MnemonicException
- * @throws \\Exception\LogicException
- * 登录
- */
- public static function login($data)
- {
- $mobile = $data['mobile'];
- $password = $data['password'];
- $userData = User::getUserInfoByCondition('mobile', $mobile);
- if (empty($userData)) {
- throw new ValidateException(null, '用户不存在');
- }
- // 验证封禁状态
- if ($userData->is_prohibit != 0) {
- return [
- 'isProhibit' => 1,
- 'token' => ''
- ];
- }
- $check = self::checkPassword($userData, $password);
- $key = self::LOGIN_ERROR_NUM.$userData->user_id;
- if (!$check) {
- // 记录用户错误次数
- if (Cache::has($key)) {
- Cache::increment($key);
- // 错误达到5次封号
- $errorNum = Cache::get($key);
- if ($errorNum >= self::ERROR_NUM) {
- DB::beginTransaction();
- try {
- // 封号
- UserBans::create($userData->user_id, 1, 0, time() + 86400);
- // 修改用户窗台
- User::updateByUserId($userData->user_id, 'is_prohibit', '1');
- DB::commit();
- throw new ValidateException(null, '助记词错误,已临时冻结,请在24小时后尝试');
- } catch (\Exception $e) {
- DB::rollBack();
- throw new ValidateException(null, $e->getMessage());
- }
- }
- } else {
- Cache::put($key, 1);
- }
- throw new ValidateException(null, '登录失败');
- }
- // 设置token
- Cache::delete($key);
- return SessionApp::setLogin($userData, true);
- }
- /**
- * 根据UserId登陆
- *
- * @param $user_id
- * @return \App\Module\Ulogic\Unit\User|array
- * @throws \\Exception\LogicException
- */
- public static function loginById($user_id)
- {
- $userData = User::query()->find($user_id);
- return SessionApp::setLogin($userData, true);
- }
- /**
- * @param $data
- * @return array
- * @throws ValidateException
- * 获取封禁信息
- */
- public static function getBanInfo($data)
- {
- $userData = User::getUserInfoByCondition('mobile', $data['mobile']);
- $check = self::checkPassword($userData, $data['password']);
- if (!$check) {
- throw new ValidateException(null, '助记词错误');
- }
- if ($userData->is_prohibit == 0) {
- throw new ValidateException(null, '该用户状态正常');
- }
- $banInfo = UserBans::getRow($userData->user_id, $userData->is_prohibit);
- $diff = $banInfo->end_time - time();
- $endTime = 0;
- if ($diff > 0) {
- $endTime = gmdate('h:i:s', $diff);
- }
- return [
- 'type' => $banInfo->type,
- 'ban_time' => $banInfo->created_at->toDateTimeString(),
- 'end_time' => $endTime,
- ];
- }
- /**
- * @param $userData
- * @param $password
- * @return bool|\FurqanSiddiqui\BIP39\Mnemonic|string
- * 验证助记词
- */
- private static function checkPassword($userData, $password)
- {
- // 判断新老用户(user_word表里有没有记录)
- $userWord = UserWord::getData($userData->user_id);
- if (empty($userWord)) {
- // 新用户 BIP39验证
- $check = Mnemon::wordsToMnemonic($password, $userData->user_id, false);
- } else {
- // 老用户 md5验证
- $password = strtolower($password);
- $password = str_replace(",", '","', $password);
- $password = '["'.$password.'"]';
- $password = md5(md5($password));
- $check = false;
- if ($password === $userData->password) {
- $check = true;
- }
- }
- return $check;
- }
- /**
- * @param $userId
- * @return User|false
- * 获取用户信息(user-id)
- */
- public static function getInfoByUserId($userId)
- {
- $data = User::query()->where('user_id', $userId)->first();
- if (!$data || !$data->password) {
- return false;
- }
- return $data;
- }
- /**
- * @param $userId
- * @param $password
- * @return mixed
- * 修改安全密码
- */
- public static function resetSecretPassword($userId, $password)
- {
- // 定义查询条件(唯一标识字段)
- $conditions = ['user_id' => $userId];
- // 需要更新或插入的数据
- $data = [
- 'secret_password' => password_hash($password, PASSWORD_DEFAULT)
- ];
- $res = UserPassword::updateOrCreate($conditions,$data);
- // 输出结果
- if ($res->id) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * @param $userId
- * @return bool
- * 查询用户是否绑定谷歌验证器
- */
- public static function isBindGoogle($userId)
- {
- $userPasswordData = UserPassword::query()->where('user_id', $userId)->first();
- if (!$userPasswordData->google2fa_secret) {
- return false;
- }
- return true;
- }
- /**
- * @param $userId
- * @param $password
- * @return bool
- * 验证用户安全密码
- */
- public static function checkSecretPassword($userId, $password)
- {
- $userPasswordData = UserPassword::query()
- ->where('user_id', $userId)
- ->first();
- if (!$userPasswordData) {
- return false;
- }
- if (!password_verify($password, $userPasswordData->secret_password)) {
- return false;
- }
- return true;
- }
- }
|