User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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\index\model;
  13. use think\facade\Cache;
  14. use app\index\service\User as UserService;
  15. use app\index\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. public const SHARE_PREFIX = 'shared-key-';
  27. /**
  28. * 隐藏字段
  29. * @var array
  30. */
  31. protected $hidden = [
  32. 'open_id',
  33. 'is_delete',
  34. 'store_id',
  35. 'create_time',
  36. 'update_time'
  37. ];
  38. /**
  39. * 获取器:隐藏手机号中间四位
  40. * @param string $value
  41. * @return string
  42. */
  43. public function getMobileAttr(string $value): string
  44. {
  45. return strlen($value) === 11 ? hide_mobile($value) : $value;
  46. }
  47. /**
  48. * 获取用户信息
  49. * @param int $userId
  50. * @return User|array|false|null
  51. * @throws BaseException
  52. */
  53. public static function getUserById(int $userId)
  54. {
  55. // 用户基本信息
  56. $userInfo = self::detail($userId);
  57. if (empty($userInfo) || $userInfo['is_delete']) {
  58. throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
  59. }
  60. // 获取用户关联的第三方用户信息(当前客户端)
  61. try {
  62. $userInfo['currentOauth'] = UserOauthModel::getOauth($userId, getPlatform());
  63. } catch (\Throwable $e) {
  64. throwError($e->getMessage());
  65. }
  66. return $userInfo;
  67. }
  68. /**
  69. * 绑定手机号(当前登录用户)
  70. * @param array $data
  71. * @return bool
  72. * @throws BaseException
  73. */
  74. public function bindMobile(array $data): bool
  75. {
  76. // 当前登录的用户信息
  77. $userInfo = UserService::getCurrentLoginUser(true);
  78. // 验证绑定的手机号
  79. $this->checkBindMobile($data);
  80. // 更新手机号记录
  81. return $userInfo->save(['mobile' => $data['mobile']]);
  82. }
  83. /**
  84. * 验证绑定的手机号
  85. * @param array $data
  86. * @return void
  87. * @throws BaseException
  88. */
  89. private function checkBindMobile(array $data): void
  90. {
  91. // 验证短信验证码是否匹配
  92. if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
  93. throwError('短信验证码不正确');
  94. }
  95. // 判断手机号是否已存在
  96. if (static::checkExistByMobile($data['mobile'])) {
  97. throwError('很抱歉,该手机号已绑定其他账户');
  98. }
  99. }
  100. }