Role.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 该类调用时应该在http控制层限制管理员才能调用
  6. *
  7. * Class Role
  8. * @package App\Models
  9. */
  10. class Role extends Model
  11. {
  12. protected $table = "roles";
  13. public $timestamps = false;
  14. /**
  15. * 创建一个用户的角色
  16. *
  17. * @param array $params
  18. * @return string
  19. */
  20. public function CreateRole(array $params)
  21. {
  22. $this->name = $params["name"];
  23. $this->description = $params["description"];
  24. $this->status = "normal";
  25. if ($this->name == "") {
  26. return "empty role name";
  27. }
  28. $this->save();
  29. return "success";
  30. }
  31. /**
  32. * 修改用户的角色信息
  33. *
  34. * @param array $params
  35. * @return string
  36. */
  37. public function ModifyRole(array $params)
  38. {
  39. $update = [];
  40. $id = $params["id"];
  41. if ($id == "") {
  42. return "empty role id";
  43. }
  44. if ($params["name"] != "") {
  45. $update["name"] = $params["name"];
  46. }
  47. if ($params["description"] != "") {
  48. $update["description"] = $params["description"];
  49. }
  50. if (count($update) == 0) {
  51. return "nothing to update";
  52. }
  53. $this->where("id", $id)
  54. ->where("is_del", false)
  55. ->update($update);
  56. return "success";
  57. }
  58. /**
  59. * 删除一个用户角色
  60. *
  61. * @param array $params
  62. * @return string
  63. */
  64. public function DeleteRole(array $params)
  65. {
  66. $id = $params["id"];
  67. if ($id == "") {
  68. return "empty role id";
  69. }
  70. $this->where("id", $id)
  71. ->where("is_del", false)
  72. ->update(["is_del" => true]);
  73. return "success";
  74. }
  75. /**
  76. * 列出用户角色列表
  77. *
  78. * @param array $params
  79. * @return mixed
  80. */
  81. public function ListRole(array $params)
  82. {
  83. $page = $params["page"];
  84. $pageCount = $params["pageCount"];
  85. $data = $this->where("is_del", false)
  86. ->orderBy("created_at", "asc")
  87. ->paginate($pageCount, ["*"], "page", $page)
  88. ->get();
  89. return $data;
  90. }
  91. }