User.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Log;
  5. class User extends Model
  6. {
  7. protected $table = "users";
  8. public $timestamps = false;
  9. public function CreateUser(array $params)
  10. {
  11. // todo 是否需要保证用户名唯一的功能
  12. $this->username = $params["username"];
  13. $this->password = $params["password"];
  14. $this->nickname = $params["nickname"];
  15. $this->icon = $params["icon"];
  16. $this->tel = $params["tel"];
  17. $this->email = $params["email"];
  18. if ($this->username == "") {
  19. return ["code" => EMPTY_USER_NAME];
  20. }
  21. if ($this->password == "") {
  22. return ["code" => EMPTY_USER_PASSWORD];
  23. }
  24. if ($this->nickname == "") {
  25. $this->nickname = $this->username;
  26. $params["nickname"] = $this->username;
  27. }
  28. // todo 这里需要对密码加密
  29. $this->save();
  30. unset($params["password"]);
  31. $params["id"] = $this->getQueueableId();
  32. return ["code" => SUCCESS, "data" => $params];
  33. }
  34. public function ChangePassword($uid, $oldPwd, $newPwd)
  35. {
  36. }
  37. public function ModifyUser(array $params)
  38. {
  39. $update = [];
  40. $uid = $params["id"];
  41. if ($uid == "") {
  42. return "empty user id";
  43. }
  44. // check if user exist
  45. $user = $this->where("id", $uid)->where("is_del", false)->first();
  46. if (!$user) {
  47. return INVALID_USER_ID;
  48. }
  49. if ($params["username"] != "") {
  50. $update["username"] = $params["username"];
  51. }
  52. if ($params["nickname"] != "") {
  53. $update["nickname"] = $params["nickname"];
  54. }
  55. if ($params["icon"] != "") {
  56. $update["icon"] = $params["icon"];
  57. }
  58. if ($params["tel"] != "") {
  59. $update["tel"] = $params["tel"];
  60. }
  61. if ($params["email"] != "") {
  62. $update["email"] = $params["email"];
  63. }
  64. if (count($update) == 0) {
  65. return "nothing to update";
  66. }
  67. $this->where("id", $uid)->where("is_del", false)->update($update);
  68. return SUCCESS;
  69. }
  70. public function DeleteUser($uid)
  71. {
  72. if ($uid == "") {
  73. return "empty user id";
  74. }
  75. $user = $this->where("id", $uid)->where("is_del", false)->first();
  76. if (!$user) {
  77. return 110;
  78. }
  79. $this->where("id", $uid)->where("is_del", false)->update(["is_del" => true]);
  80. return 0;
  81. }
  82. public function ListUser($page, $pageCount, $keyword)
  83. {
  84. if ($page < 1) {
  85. $page = 15;
  86. }
  87. if ($pageCount > 15 || $pageCount < 1) {
  88. $pageCount = 15;
  89. }
  90. $data = $this->select("id", "username", "nickname", "icon", "tel", "email")
  91. ->where("is_del", false)->orderBy("created_at", "asc")
  92. ->paginate($pageCount, ["*"], "page", $page);
  93. return $data;
  94. }
  95. public function ListUserByIds($ids)
  96. {
  97. $users = $this->select("id", "username", "nickname", "icon")
  98. ->whereIn("id", $ids)->where("is_del", false)->get();
  99. return $users;
  100. }
  101. public function ListUserByIds_KV($ids)
  102. {
  103. $users = $this->ListUserByIds($ids);
  104. $data = [];
  105. foreach ($users as $user) {
  106. $data[$user["id"]] = $user;
  107. }
  108. return $data;
  109. }
  110. }