User.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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. 'password',
  38. ];
  39. /**
  40. * 获取器:隐藏手机号中间四位
  41. * @param string $value
  42. * @return string
  43. */
  44. public function getMobileAttr(string $value): string
  45. {
  46. return strlen($value) === 11 ? hide_mobile($value) : $value;
  47. }
  48. /**
  49. * 获取用户信息
  50. * @param string $token
  51. * @return User|array|false|null
  52. * @throws BaseException
  53. */
  54. public static function getUserByToken(string $token)
  55. {
  56. // 检查登录态是否存在
  57. if (!Cache::has($token)) {
  58. return false;
  59. }
  60. // 用户的ID
  61. $userId = (int)Cache::get($token)['user']['user_id'];
  62. // 用户基本信息
  63. $userInfo = self::detail($userId);
  64. if (empty($userInfo) || $userInfo['is_delete']) {
  65. throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
  66. }
  67. // 获取用户关联的第三方用户信息(当前客户端)
  68. try {
  69. $userInfo['currentOauth'] = UserOauthModel::getOauth($userId, getPlatform());
  70. } catch (\Throwable $e) {
  71. throwError($e->getMessage());
  72. }
  73. return $userInfo;
  74. }
  75. /**
  76. * 绑定手机号(当前登录用户)
  77. * @param array $data
  78. * @return bool
  79. * @throws BaseException
  80. */
  81. public function bindMobile(array $data): bool
  82. {
  83. // 当前登录的用户信息
  84. $userInfo = UserService::getCurrentLoginUser(true);
  85. // 验证绑定的手机号
  86. $this->checkBindMobile($data);
  87. // 更新手机号记录
  88. return $userInfo->save(['mobile' => $data['mobile']]);
  89. }
  90. /**
  91. * 修改个人信息(头像昵称)
  92. * @param array $form
  93. * @return bool
  94. * @throws BaseException
  95. */
  96. public function personal(array $form): bool
  97. {
  98. // 当前登录的用户信息
  99. $userInfo = UserService::getCurrentLoginUser(true);
  100. // 默认数据
  101. $data['avatar_id'] = $form['avatarId'] ?: $userInfo['avatar_id'];
  102. $data['nick_name'] = $form['nickName'] ?: $userInfo['nick_name'];
  103. // 更新用户记录
  104. return $userInfo->save($data);
  105. }
  106. /**
  107. * 验证绑定的手机号
  108. * @param array $data
  109. * @return void
  110. * @throws BaseException
  111. */
  112. private function checkBindMobile(array $data): void
  113. {
  114. // 验证短信验证码是否匹配
  115. try {
  116. CaptchaApi::checkSms($data['smsCode'], $data['mobile']);
  117. } catch (\Exception $e) {
  118. throwError($e->getMessage() ?: '短信验证码不正确');
  119. }
  120. // 判断手机号是否已存在
  121. if (static::checkExistByMobile($data['mobile'])) {
  122. throwError('很抱歉,该手机号已绑定其他账户');
  123. }
  124. }
  125. }