UserController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Validator;
  6. // todo 返回值为 数字代码还是json还是其他格式待定
  7. class UserController extends Controller
  8. {
  9. public function CreateUser(Request $request)
  10. {
  11. $validator = Validator::make($request->all(), [
  12. "username" => "required|between:1,32",
  13. "password" => "required|between:8,16",
  14. "nickname" => "max:32",
  15. "icon" => "max:255",
  16. "tel" => "max:32",
  17. "email" => "max:64",
  18. ]);
  19. if ($validator->fails()) {
  20. return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
  21. }
  22. $params = [];
  23. $params["username"] = $request->input("username");
  24. $params["password"] = $request->input("password");
  25. $params["nickname"] = $request->input("nickname");
  26. $params["icon"] = $request->input("icon");
  27. $params["tel"] = $request->input("tel");
  28. $params["email"] = $request->input("email");
  29. $user = new Models\User();
  30. $result = $user->CreateUser($params);
  31. if ($result["code"] == 0) {
  32. return $this->success($result["data"]);
  33. }
  34. return $this->fail($result["code"], $this->error[$result["code"]]);
  35. }
  36. public function ModifyUser(Request $request)
  37. {
  38. $validator = Validator::make($request->all(), [
  39. "id" => "required|integer|min:1",
  40. "nickname" => "max:32",
  41. "icon" => "max:255",
  42. "tel" => "max:32",
  43. "email" => "max:64",
  44. ]);
  45. if ($validator->fails()) {
  46. return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
  47. }
  48. $params = [];
  49. $params["id"] = $request->input("id");
  50. $params["username"] = $request->input("username");
  51. $params["password"] = $request->input("password");
  52. $params["nickname"] = $request->input("nickname");
  53. $params["icon"] = $request->input("icon");
  54. $params["tel"] = $request->input("tel");
  55. $params["email"] = $request->input("email");
  56. $user = new Models\User();
  57. $code = $user->ModifyUser($params);
  58. if ($code == 0) {
  59. return $this->success("success");
  60. } else {
  61. return $this->fail($code, $this->error[$code]);
  62. }
  63. }
  64. public function DeleteUser(Request $request)
  65. {
  66. $validator = Validator::make($request->all(), [
  67. "id" => "required|integer|min:1"
  68. ]);
  69. if ($validator->fails()) {
  70. return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
  71. }
  72. $uid = $request->input("id");
  73. $user = new Models\User();
  74. $code = $user->DeleteUser($uid);
  75. if ($code == 0) {
  76. return $this->success("success");
  77. }
  78. return $this->fail($code, $this->error[$code]);
  79. }
  80. // todo, 列出用户要根据业务在中间件添加权限拦截
  81. public function ListUser(Request $request)
  82. {
  83. $validator = Validator::make($request->all(), [
  84. "page" => "integer|min:1",
  85. "pageCount" => "integer|min:1",
  86. ]);
  87. if ($validator->fails()) {
  88. return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
  89. }
  90. $page = $request->input("page");
  91. $pageCount = $request->input("pageCount");
  92. $keyword = $request->input("keyword");
  93. $user = new Models\User();
  94. $result = $user->ListUser($page, $pageCount, $keyword);
  95. return $result;
  96. }
  97. }