User.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\model\mysql\User as UserModel;
  12. class User
  13. {
  14. public $userObj = null;
  15. public function __construct()
  16. {
  17. $this->userObj = new UserModel();
  18. }
  19. public function login(array $data)
  20. {
  21. $redisCode = cache(config('redis.code_pre') . $data['phoneNumber']);
  22. if (empty($redisCode) || $redisCode !== $data['code']) {
  23. throw new \think\Exception('验证码不存在', -1009);
  24. }
  25. // 需要去判断表 是否有 用户记录phoneNumber
  26. // 生成token
  27. $user = $this->userObj->getUserByPhoneNumber($data['phoneNumber']);
  28. if (!$user) {
  29. $username = "singewa粉-{$data['phoneNumber']}";
  30. $userData = [
  31. 'username' => $username,
  32. 'phone_number' => $data['phoneNumber'],
  33. 'type' => $data['type'],
  34. 'status' => config('status.mysql.table_normal'),
  35. ];
  36. try {
  37. $this->userObj->save($userData);
  38. $userId = $this->userObj->id;
  39. }catch (\Exception $e){
  40. throw new \think\Exception('数据库内部异常');
  41. }
  42. } else {
  43. // 用户存在,更新表数据
  44. }
  45. return true;
  46. }
  47. }