User.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User:林志杰
  5. * Email:[email protected]
  6. * Motto:纵有疾风起,人生不言弃!
  7. * Time:2020/2/8 1:24
  8. */
  9. declare(strict_types=1);
  10. namespace app\common\business;
  11. use app\common\lib\Str;
  12. use app\common\lib\Time;
  13. use app\common\model\mysql\User as UserModel;
  14. class User
  15. {
  16. public $userObj = null;
  17. public function __construct()
  18. {
  19. $this->userObj = new UserModel();
  20. }
  21. /**
  22. * 用户登录
  23. * @param array $data
  24. * @return array|bool
  25. * @throws \think\Exception
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function login(array $data)
  31. {
  32. $redisCode = cache(config('redis.code_pre') . $data['phoneNumber']);
  33. if (empty($redisCode) || $redisCode !== $data['code']) {
  34. throw new \think\Exception('验证码不存在', -1009);
  35. }
  36. // 需要去判断表 是否有 用户记录phoneNumber
  37. // 生成token
  38. $user = $this->userObj->getUserByPhoneNumber($data['phoneNumber']);
  39. if (!$user) {
  40. $username = "singewa粉-{$data['phoneNumber']}";
  41. $userData = [
  42. 'username' => $username,
  43. 'phone_number' => $data['phoneNumber'],
  44. 'type' => $data['type'],
  45. 'status' => config('status.mysql.table_normal'),
  46. ];
  47. try {
  48. $this->userObj->save($userData);
  49. $userId = $this->userObj->id;
  50. } catch (\Exception $e) {
  51. throw new \think\Exception('数据库内部异常');
  52. }
  53. } else {
  54. // 用户存在,更新表数据
  55. $userId = $user->id;
  56. $username = $user->username;
  57. }
  58. $token = Str::getLoginToken($data['phoneNumber']);
  59. $redisData = [
  60. 'id' => $userId,
  61. 'username' => $username,
  62. ];
  63. $res = cache(config('redis.token_pre') . $token, $redisData, Time::userLoginExpiresTime($data['type']));
  64. return $res ? ['token' => $token, 'username' => $username] : false;
  65. }
  66. /**
  67. * 返回正常用户数据
  68. * @param int $id
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function getNormalUserById(int $id): array
  75. {
  76. $user = $this->userObj->getUserById($id);
  77. if (!$user || $user->status !== config('status.mysql.table_normal')) {
  78. return [];
  79. }
  80. return $user->toArray();
  81. }
  82. public function getNormalUserByUsername(string $username): array
  83. {
  84. $user = $this->userObj->getUserByUsername($username);
  85. if (!$user || $user->status !== config('status.mysql.table_normal')) {
  86. return [];
  87. }
  88. return $user->toArray();
  89. }
  90. public function update(int $id, array $data)
  91. {
  92. $user = $this->getNormalUserById($id);
  93. if (!$user) {
  94. throw new \think\Exception('不存在该用户');
  95. }
  96. // 检查用户名是否存在
  97. $userResult = $this->getNormalUserByUsername($data['username']);
  98. if ($userResult && $userResult['id'] !== $id) {
  99. throw new \think\Exception('该用户已经存在请重新设置');
  100. }
  101. return $this->userObj->updateById($id, $data);
  102. }
  103. }