UserModel.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Model\Model;
  5. use App\Utils\Common;
  6. use Hyperf\DbConnection\Db;
  7. class UserModel extends Model
  8. {
  9. /**
  10. * The table associated with the model.
  11. *
  12. * @var ?string
  13. */
  14. protected ?string $table = 'user';
  15. protected ?string $dateFormat = 'U';
  16. public bool $timestamps = false;
  17. /**
  18. * 默认查询字段
  19. *
  20. * @var array|string[]
  21. */
  22. public array $select = [
  23. '*'
  24. ];
  25. protected int $is_status_search = 1;// 默认使用 status = 1 筛选
  26. protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
  27. /**
  28. * 中间件中获取用户信息
  29. * @param int $user_id
  30. * @return array
  31. */
  32. public function searchMobileAttribute($query, $value, array $params): mixed
  33. {
  34. if (!isset($value)) {
  35. return $query;
  36. }
  37. return $query->where('mobile', $value);
  38. }
  39. public function searchIdsAttribute($query, $value, array $params): mixed
  40. {
  41. if (!isset($value)) {
  42. return $query;
  43. }
  44. return $query->whereIn('id', $value);
  45. }
  46. public function authUserInfo(int $user_id)
  47. {
  48. return (new UserModel())->getDetail(params: ['id' => $user_id]);
  49. }
  50. public function getByMobile(string $mobile)
  51. {
  52. $this->is_status_search = 0;
  53. return $this->getDetail(params: ['mobile' => $mobile]);
  54. }
  55. /**
  56. * 注册
  57. * @param string $mobile
  58. * @return bool
  59. */
  60. public function register(string $mobile)
  61. {
  62. $time = time();
  63. $params = [
  64. 'avatar' => '',
  65. 'nickname' => Common::getRandNickName(),
  66. 'mobile' => $mobile,
  67. 'status' => 1,
  68. 'createtime' => $time
  69. ];
  70. //账号注册时需要开启事务,避免出现垃圾数据
  71. Db::beginTransaction();
  72. if (!$user_id = $this->query()->insertGetId($params)){
  73. Db::rollBack();
  74. return $this->error('注册失败');
  75. }
  76. //注册钱包
  77. if (!Db::table('user_wallet')->insert(['user_id'=>$user_id])){
  78. Db::rollBack();
  79. return $this->error('注册失败');
  80. }
  81. Db::commit();
  82. return $this->success('注册成功',array_merge($params,[
  83. 'id' => $user_id
  84. ]));
  85. }
  86. // 开播点赞
  87. public function roomLike()
  88. {
  89. return $this->hasOne(LiveRoomLogLikeModel::class, 'user_id', 'id');
  90. }
  91. }