User.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\api\model;
  13. use think\facade\Cache;
  14. use yiovo\captcha\facade\CaptchaApi;
  15. use app\api\service\User as UserService;
  16. use app\common\model\User as UserModel;
  17. use cores\exception\BaseException;
  18. /**
  19. * 用户模型类
  20. * Class User
  21. * @package app\api\model
  22. */
  23. class User extends UserModel
  24. {
  25. /**
  26. * 隐藏字段
  27. * @var array
  28. */
  29. protected $hidden = [
  30. 'open_id',
  31. 'is_delete',
  32. 'store_id',
  33. 'create_time',
  34. 'update_time'
  35. ];
  36. /**
  37. * 获取器:隐藏手机号中间四位
  38. * @param string $value
  39. * @return string
  40. */
  41. public function getMobileAttr(string $value): string
  42. {
  43. return strlen($value) === 11 ? hide_mobile($value) : $value;
  44. }
  45. /**
  46. * 获取用户信息
  47. * @param string $token
  48. * @return User|array|false|null
  49. * @throws BaseException
  50. */
  51. public static function getUserByToken(string $token)
  52. {
  53. // 检查登录态是否存在
  54. if (!Cache::has($token)) {
  55. return false;
  56. }
  57. // 获取用户的ID
  58. $userId = Cache::get($token)['user']['user_id'];
  59. // 获取用户基本信息
  60. $userInfo = self::detail($userId);
  61. if (empty($userInfo) || $userInfo['is_delete']) {
  62. throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
  63. }
  64. return $userInfo;
  65. }
  66. /**
  67. * 绑定手机号(当前登录用户)
  68. * @param array $data
  69. * @return bool
  70. * @throws BaseException
  71. */
  72. public function bindMobile(array $data): bool
  73. {
  74. // 当前登录的用户信息
  75. $userInfo = UserService::getCurrentLoginUser(true);
  76. // 验证绑定的手机号
  77. $this->checkBindMobile($data);
  78. // 更新手机号记录
  79. return $userInfo->save(['mobile' => $data['mobile']]);
  80. }
  81. /**
  82. * 验证绑定的手机号
  83. * @param array $data
  84. * @return void
  85. * @throws BaseException
  86. */
  87. private function checkBindMobile(array $data): void
  88. {
  89. // 验证短信验证码是否匹配
  90. if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
  91. throwError('短信验证码不正确');
  92. }
  93. // 判断手机号是否已存在
  94. if (static::checkExistByMobile($data['mobile'])) {
  95. throwError('很抱歉,该手机号已绑定其他账户');
  96. }
  97. }
  98. }