BaseController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\admin\controller;
  3. use Webman\Http\Request;
  4. class BaseController
  5. {
  6. protected $request;
  7. protected $validate;
  8. public function beforeAction(Request $request)
  9. {
  10. $this->request = $request;
  11. }
  12. /**
  13. * Notes:获取分页请求参数
  14. * @return array
  15. * User: yym
  16. * Date: 2022/7/27
  17. */
  18. public function getPage()
  19. {
  20. $data = $this->request->only(['page', 'limit']);
  21. $page = 1;
  22. $limit = 10;
  23. if(isset($data['limit']) && !empty($data['limit']))
  24. {
  25. $limit = (int)$data['limit'];
  26. }
  27. if(isset($data['page']) && !empty($data['page']))
  28. {
  29. $page = (int)$data['page'];
  30. }
  31. return [(int)$page, (int)$limit];
  32. }
  33. /**
  34. * Notes:验证器简化处理
  35. * @param $validate
  36. * @param $data
  37. * @param string $scene
  38. * User: yym
  39. * Date: 2022/7/28
  40. */
  41. public function validateCheck($validate, $data, $scene = '')
  42. {
  43. $classname = 'app\\admin\\validate\\' . $validate;
  44. $model = new $classname;
  45. if(!empty($scene) && $model->hasScene($scene))
  46. {
  47. $model->scene($scene)->check($data);
  48. }else{
  49. $model->check($data);
  50. }
  51. }
  52. /**
  53. * Notes:密码加密随机字符串
  54. * @param $validate
  55. * @param $data
  56. * @param string $scene
  57. * User: ZQ
  58. * Date: 2022/9/27
  59. */
  60. public function passwordSalf($length){
  61. $string = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
  62. 't', 'u', 'v', 'w', 'x', 'y','z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  63. // 在 $str 中随机取 $length 个数组元素键名
  64. $keys = array_rand($string, $length);
  65. $password = '';
  66. for($i = 0; $i < $length; $i++)
  67. {
  68. // 将 $length 个数组元素连接成字符串
  69. $password .= $string[$keys[$i]];
  70. }
  71. return $password;
  72. }
  73. }