User.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class User extends Model
  5. {
  6. protected $table = "users";
  7. public $timestamps = false;
  8. public function CreateUser(array $params)
  9. {
  10. // todo 是否需要保证用户名唯一的功能
  11. $this->username = $params["username"];
  12. $this->password = $params["password"];
  13. $this->nickname = $params["nickname"];
  14. $this->icon = $params["icon"];
  15. $this->tel = $params["tel"];
  16. $this->email = $params["email"];
  17. $this->status = "normal";
  18. if ($this->username == "" || $this->password == "") {
  19. return "empty username or password";
  20. }
  21. if ($this->nickname == "") {
  22. $this->nickname = $this->username;
  23. }
  24. // todo 这里需要对密码加密
  25. $this->save();
  26. return "success";
  27. }
  28. public function ChangePassword($uid, $oldPwd, $newPwd)
  29. {
  30. }
  31. public function ModifyUser(array $params)
  32. {
  33. $update = [];
  34. $uid = $params["uid"];
  35. if ($uid == "") {
  36. return "empty user id";
  37. }
  38. if ($params["username"] != "") {
  39. $update["username"] = $params["username"];
  40. }
  41. if ($params["nickname"] != "") {
  42. $update["nickname"] = $params["nickname"];
  43. }
  44. if ($params["icon"] != "") {
  45. $update["icon"] = $params["icon"];
  46. }
  47. if ($params["tel"] != "") {
  48. $update["tel"] = $params["tel"];
  49. }
  50. if ($params["email"] != "") {
  51. $update["email"] = $params["email"];
  52. }
  53. if (count($update) == 0) {
  54. return "nothing to update";
  55. }
  56. $result = $this->where("id", $uid)
  57. ->where("is_del", false)
  58. ->update($update);
  59. return $result > 0 ? "success" : "fail";
  60. }
  61. public function DeleteUser($uid)
  62. {
  63. if ($uid == "") {
  64. return "empty user id";
  65. }
  66. $result = $this->where("id", $uid)
  67. ->where("is_del", false)
  68. ->update(["is_del" => true]);
  69. return $result>0?"success": "fail";
  70. }
  71. public function ListUser($page, $pageCount)
  72. {
  73. $data = $this->where("is_del", false)
  74. ->paginate($pageCount, ["*"], "page", $page)
  75. ->get();
  76. return $data;
  77. }
  78. }