UserDao.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Dao\User;
  3. use App\Models\User\User;
  4. class UserDao
  5. {
  6. /**
  7. * @param $uid
  8. * @return array
  9. */
  10. public function getUserById($uid): array
  11. {
  12. if (empty($uid)) {
  13. return [];
  14. }
  15. $result = User::where('id', $uid)->first();
  16. return $result ? $result->toArray() : [];
  17. }
  18. /**
  19. * 根据openid获取用户数据
  20. *
  21. * @param string $openId
  22. * @return array
  23. */
  24. public function getUserByOpenId(string $openId): array
  25. {
  26. if (empty($openId)) {
  27. return [];
  28. }
  29. $result = User::where('openid', $openId)->first();
  30. return $result ? $result->toArray() : [];
  31. }
  32. /**
  33. * 根据openid和unionid获取用户数据
  34. *
  35. * @param string $openId
  36. * @param string $unionId
  37. * @return array
  38. */
  39. public function getUserByOpenIdAndUnionId(string $openId, string $unionId): array
  40. {
  41. if (empty($openId) || empty($unionId)) {
  42. return [];
  43. }
  44. $result = User::where('openid', $openId)->where('unionid', $unionId)->first();
  45. return $result ? $result->toArray() : [];
  46. }
  47. /**
  48. * 根据unionid获取用户数据(多个)
  49. *
  50. * @param string $unionId
  51. * @return array
  52. */
  53. public function getUsersByUnionId(string $unionId): array
  54. {
  55. if (empty($unionId)) {
  56. return [];
  57. }
  58. $result = User::where('unionid', $unionId)->get();
  59. return $result ? $result->toArray() : [];
  60. }
  61. /**
  62. * 设置登录用户信息
  63. *
  64. * @param $openId
  65. * @param $unionId
  66. * @param $data
  67. * @return mixed
  68. */
  69. public function setUser($openId, $unionId, $data)
  70. {
  71. return User::updateOrCreate(['openid' => $openId, 'unionid' => $unionId], $data);
  72. }
  73. /**
  74. * @param $data
  75. * @return mixed
  76. */
  77. public function createUser($data)
  78. {
  79. return User::create($data);
  80. }
  81. /**
  82. * 更新用户
  83. *
  84. * @param $id
  85. * @param $data
  86. * @return bool
  87. */
  88. public function updateUserById($id, $data): bool
  89. {
  90. if (empty($id) || empty($data)) {
  91. return false;
  92. }
  93. return User::where('id', $id)->update($data);
  94. }
  95. }