BaseController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * 返回Json成功的操作
  63. * @param string $msg
  64. * @param mixed|null $data
  65. */
  66. protected function jsonSuccess(string $msg='',mixed $data=null)
  67. {
  68. return json(['code' => 1,'msg' => 1111, 'data' => $data]);
  69. }
  70. /**
  71. * 返回失败的操作
  72. * @param string $msg
  73. * @param mixed|null $data
  74. */
  75. protected function error(string $msg='',mixed $data=null)
  76. {
  77. $type='json';
  78. if(!$this->request->isAjax()) {
  79. $type = 'html';
  80. }
  81. $this->result($msg,$data,0,$type);
  82. }
  83. /**
  84. * 返回Json成功的操作
  85. * @param string $msg
  86. * @param mixed|null $data
  87. */
  88. protected function jsonError(string $msg='',mixed $data=null)
  89. {
  90. return json(['code' => 0,'msg' => $msg, 'data' => $data]);
  91. }
  92. /**
  93. * 返回请求结果,当ajax请求时返回json,其他请求时返回html
  94. * @param string $msg 提示消息
  95. * @param mixed $data 返回数据
  96. * @param int $code 错误码1为成功,0为失败
  97. * @param string|null $type 输出类型
  98. * @param array $header 发送的 header 信息
  99. * @param array $options Response 输出参数
  100. */
  101. protected function result(string $msg, mixed $data = null, int $code, string $type = null, array $header = [], array $options = [])
  102. {
  103. $result = [
  104. 'code' => $code,
  105. 'msg' => $msg,
  106. 'data' => $data,
  107. ];
  108. $statuscode = 200;
  109. if (isset($header['statuscode'])) {
  110. $statuscode = $header['statuscode'];
  111. unset($header['statuscode']);
  112. }
  113. if(!$this->request->isAjax()) {
  114. $modulename=app('http')->getName();
  115. $result['modulename'] = $modulename;
  116. $result['modulealis'] = get_module_alis($modulename);
  117. View::layout(false);
  118. View::assign('result',$result);
  119. $result=View::fetch('common@/msg');
  120. }
  121. $response = Response::create($result, $type, $statuscode)->header($header)->options($options);
  122. $response->send();
  123. event('write_log',WriteLog::END);
  124. exit;
  125. }
  126. }