|
|
@@ -2,289 +2,46 @@
|
|
|
|
|
|
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\Models\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)
|
|
|
+ public function getToken()
|
|
|
{
|
|
|
- $data = User::query()->where('mobile', $mobile)->first();
|
|
|
- if (!$data || !$data->password) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
+ return SessionApp::$session_id;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @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)
|
|
|
+ public function __construct(protected int $user_id)
|
|
|
{
|
|
|
- $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
|
|
|
- * 登录
|
|
|
+ * @return static
|
|
|
*/
|
|
|
- public static function login($data)
|
|
|
+ static public function newBySession()
|
|
|
{
|
|
|
- $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);
|
|
|
+ return new static(SessionApp::getUserId());
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据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
|
|
|
- * 获取封禁信息
|
|
|
+ * @return array|int[]
|
|
|
*/
|
|
|
- public static function getBanInfo($data)
|
|
|
+ public function callData($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);
|
|
|
- }
|
|
|
+ $page = $data['page'] ?? 1;
|
|
|
+ $limit = $data['limit'] ?? 10;
|
|
|
+ unset($data['page']);
|
|
|
+ unset($data['limit']);
|
|
|
+ $where = $data;
|
|
|
return [
|
|
|
- 'type' => $banInfo->type,
|
|
|
- 'ban_time' => $banInfo->created_at->toDateTimeString(),
|
|
|
- 'end_time' => $endTime,
|
|
|
+ $page, $limit, $where,
|
|
|
];
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * @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;
|
|
|
- }
|
|
|
}
|