BaseController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare (strict_types = 1);
  11. namespace app\common\controller;
  12. use app\common\event\AppEvent;
  13. use app\common\listener\WriteLog;
  14. use think\facade\View;
  15. use think\Request;
  16. use think\Response;
  17. /**
  18. * 后台控制器基类
  19. */
  20. class BaseController
  21. {
  22. /**
  23. * Request实例
  24. * @var \think\Request
  25. */
  26. protected $request;
  27. public function __construct(Request $request)
  28. {
  29. $this->request = $request;
  30. event('write_log',WriteLog::BEGIN);
  31. $this->_initialize();
  32. }
  33. /**
  34. * 初始化方法
  35. * @return void
  36. */
  37. protected function _initialize()
  38. {
  39. }
  40. protected function assign(string|array $name, mixed $value = null)
  41. {
  42. return View::assign($name,$value);
  43. }
  44. protected function fetch(string $template = '', array $vars = [])
  45. {
  46. return View::fetch($template,$vars);
  47. }
  48. /**
  49. * 返回成功的操作
  50. * @param string $msg
  51. * @param mixed|null $data
  52. */
  53. protected function success(string $msg='',mixed $data=null)
  54. {
  55. $type='json';
  56. if(!$this->request->isAjax()) {
  57. $type = 'html';
  58. }
  59. $this->result($msg,$data,1,$type);
  60. }
  61. /**
  62. * 返回失败的操作
  63. * @param string $msg
  64. * @param mixed|null $data
  65. */
  66. protected function error(string $msg='',mixed $data=null)
  67. {
  68. $type='json';
  69. if(!$this->request->isAjax()) {
  70. $type = 'html';
  71. }
  72. $this->result($msg,$data,0,$type);
  73. }
  74. /**
  75. * 返回请求结果,当ajax请求时返回json,其他请求时返回html
  76. * @param string $msg 提示消息
  77. * @param mixed $data 返回数据
  78. * @param int $code 错误码1为成功,0为失败
  79. * @param string|null $type 输出类型
  80. * @param array $header 发送的 header 信息
  81. * @param array $options Response 输出参数
  82. */
  83. protected function result(string $msg, mixed $data = null, int $code, string $type = null, array $header = [], array $options = [])
  84. {
  85. $result = [
  86. 'code' => $code,
  87. 'msg' => $msg,
  88. 'data' => $data,
  89. ];
  90. $statuscode = 200;
  91. if (isset($header['statuscode'])) {
  92. $statuscode = $header['statuscode'];
  93. unset($header['statuscode']);
  94. }
  95. if(!$this->request->isAjax()) {
  96. $modulename=app('http')->getName();
  97. $result['modulename'] = $modulename;
  98. $result['modulealis'] = get_module_alis($modulename);
  99. View::layout(false);
  100. View::assign('result',$result);
  101. $result=View::fetch('common@/msg');
  102. }
  103. $response = Response::create($result, $type, $statuscode)->header($header)->options($options);
  104. $response->send();
  105. event('write_log',WriteLog::END);
  106. exit;
  107. }
  108. }