Service.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace addons\adminlogin\library;
  3. use think\exception\HttpResponseException;
  4. use think\Request;
  5. use think\Response;
  6. use think\response\Redirect;
  7. use think\Session;
  8. class Service
  9. {
  10. public static function getUrl()
  11. {
  12. $url = \request()->get('url', 'index/index', 'url_clean');
  13. // 返回地址不能是 login/logout
  14. $arr = [
  15. 'index/login',
  16. 'index/logout',
  17. 'adminlogin/index'
  18. ];
  19. foreach ($arr as $item) {
  20. if ($url == url($item)) {
  21. $url = 'index/index';
  22. break;
  23. }
  24. }
  25. return $url;
  26. }
  27. /**
  28. * URL 重定向
  29. * @access protected
  30. * @param string $url 跳转的 URL 表达式
  31. * @param array|int $params 其它 URL 参数
  32. * @param int $code http code
  33. * @param array $with 隐式传参
  34. * @return void
  35. * @throws HttpResponseException
  36. */
  37. public static function redirect($url='', $params = [], $code = 302, $with = [])
  38. {
  39. $url = 'adminlogin/index';
  40. if (empty($params)) {
  41. $goUrl = Session::get('referer');
  42. $goUrl = $goUrl ? $goUrl : request()->url();
  43. $params['url'] = $goUrl;
  44. }
  45. if (is_integer($params)) {
  46. $code = $params;
  47. $params = [];
  48. }
  49. $response = new Redirect($url);
  50. $response->code($code)->params($params)->with($with);
  51. throw new HttpResponseException($response);
  52. die;
  53. }
  54. /**
  55. * 返回错误消息
  56. */
  57. public static function error()
  58. {
  59. if (request()->isAjax()) {
  60. $data = [
  61. 'code' => 0,
  62. 'msg' => __('Please login first'),
  63. 'data' => [],
  64. 'time' => time(),
  65. ];
  66. $response = \think\Response::create($data, 'json', 200);
  67. throw new HttpResponseException($response);
  68. } else {
  69. static::redirect();
  70. }
  71. }
  72. }