User.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use think\facade\Cache;
  14. use app\api\service\User as UserService;
  15. use app\api\model\UserOauth as UserOauthModel;
  16. use app\common\model\User as UserModel;
  17. use cores\exception\BaseException;
  18. use yiovo\captcha\facade\CaptchaApi;
  19. /**
  20. * 用户模型类
  21. * Class User
  22. * @package app\api\model
  23. */
  24. class User extends UserModel
  25. {
  26. /**
  27. * 隐藏字段
  28. * @var array
  29. */
  30. protected $hidden = [
  31. 'currentOauth',
  32. 'is_delete',
  33. 'store_id',
  34. 'create_time',
  35. 'update_time'
  36. ];
  37. /**
  38. * 获取器:隐藏手机号中间四位
  39. * @param string $value
  40. * @return string
  41. */
  42. public function getMobileAttr(string $value): string
  43. {
  44. return strlen($value) === 11 ? hide_mobile($value) : $value;
  45. }
  46. /**
  47. * 获取用户信息
  48. * @param string $token
  49. * @return User|array|false|null
  50. * @throws BaseException
  51. */
  52. public static function getUserByToken(string $token)
  53. {
  54. // 检查登录态是否存在
  55. if (!Cache::has($token)) {
  56. return false;
  57. }
  58. // 用户的ID
  59. $userId = (int)Cache::get($token)['user']['user_id'];
  60. // 用户基本信息
  61. $userInfo = self::detail($userId);
  62. if (empty($userInfo) || $userInfo['is_delete']) {
  63. throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
  64. }
  65. // 获取用户关联的第三方用户信息(当前客户端)
  66. try {
  67. $userInfo['currentOauth'] = UserOauthModel::getOauth($userId, getPlatform());
  68. } catch (\Throwable $e) {
  69. throwError($e->getMessage());
  70. }
  71. return $userInfo;
  72. }
  73. /**
  74. * 绑定手机号(当前登录用户)
  75. * @param array $data
  76. * @return bool
  77. * @throws BaseException
  78. */
  79. public function bindMobile(array $data): bool
  80. {
  81. // 当前登录的用户信息
  82. $userInfo = UserService::getCurrentLoginUser(true);
  83. // 验证绑定的手机号
  84. $this->checkBindMobile($data);
  85. // 更新手机号记录
  86. return $userInfo->save(['mobile' => $data['mobile']]);
  87. }
  88. /**
  89. * 修改个人信息(头像昵称)
  90. * @param array $form
  91. * @return bool
  92. * @throws BaseException
  93. */
  94. public function personal(array $form): bool
  95. {
  96. // 当前登录的用户信息
  97. $userInfo = UserService::getCurrentLoginUser(true);
  98. // 默认数据
  99. $data['avatar_id'] = $form['avatarId'] ?: $userInfo['avatar_id'];
  100. $data['nick_name'] = $form['nickName'] ?: $userInfo['nick_name'];
  101. // 更新用户记录
  102. return $userInfo->save($data);
  103. }
  104. /**
  105. * 验证绑定的手机号
  106. * @param array $data
  107. * @return void
  108. * @throws BaseException
  109. */
  110. private function checkBindMobile(array $data): void
  111. {
  112. // 验证短信验证码是否匹配
  113. try {
  114. CaptchaApi::checkSms($data['smsCode'], $data['mobile']);
  115. } catch (\Exception $e) {
  116. throwError($e->getMessage() ?: '短信验证码不正确');
  117. }
  118. // 判断手机号是否已存在
  119. if (static::checkExistByMobile($data['mobile'])) {
  120. throwError('很抱歉,该手机号已绑定其他账户');
  121. }
  122. }
  123. }