BaseLikeAdminController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. declare(strict_types=1);
  15. namespace app\common\controller;
  16. use app\BaseController;
  17. use app\common\lists\BaseDataLists;
  18. use app\common\service\JsonService;
  19. use think\facade\App;
  20. /**
  21. * 控制器基类
  22. * Class BaseLikeAdminController
  23. * @package app\common\controller
  24. */
  25. class BaseLikeAdminController extends BaseController
  26. {
  27. public array $notNeedLogin = [];
  28. /**
  29. * @notes 操作成功
  30. * @param string $msg
  31. * @param array $data
  32. * @param int $code
  33. * @param int $show
  34. * @return \think\response\Json
  35. * @author 段誉
  36. * @date 2021/12/27 14:21
  37. */
  38. protected function success(string $msg = 'success', array $data = [], int $code = 1, int $show = 0)
  39. {
  40. return JsonService::success($msg, $data, $code, $show);
  41. }
  42. /**
  43. * @notes 数据返回
  44. * @param $data
  45. * @return \think\response\Json
  46. * @author 段誉
  47. * @date 2021/12/27 14:21\
  48. */
  49. protected function data($data)
  50. {
  51. return JsonService::data($data);
  52. }
  53. /**
  54. * @notes 列表数据返回
  55. * @param \app\common\lists\BaseDataLists|null $lists
  56. * @return \think\response\Json
  57. * @author 令狐冲
  58. * @date 2021/7/8 00:40
  59. */
  60. protected function dataLists(BaseDataLists $lists = null)
  61. {
  62. //列表类和控制器一一对应,"app/应用/controller/控制器的方法" =》"app\应用\lists\"目录下
  63. //(例如:"app/adminapi/controller/auth/AdminController.php的lists()方法" =》 "app/adminapi/lists/auth/AminLists.php")
  64. //当对象为空时,自动创建列表对象
  65. if (is_null($lists)) {
  66. $listName = str_replace('.', '\\', App::getNamespace() . '\\lists\\' . $this->request->controller() . ucwords($this->request->action()));
  67. $lists = invoke($listName);
  68. }
  69. return JsonService::dataLists($lists);
  70. }
  71. /**
  72. * @notes 操作失败
  73. * @param string $msg
  74. * @param array $data
  75. * @param int $code
  76. * @param int $show
  77. * @return \think\response\Json
  78. * @author 段誉
  79. * @date 2021/12/27 14:21
  80. */
  81. protected function fail(string $msg = 'fail', array $data = [], int $code = 0, int $show = 1)
  82. {
  83. return JsonService::fail($msg, $data, $code, $show);
  84. }
  85. /**
  86. * @notes 是否免登录验证
  87. * @return bool
  88. * @author 段誉
  89. * @date 2021/12/27 14:21
  90. */
  91. public function isNotNeedLogin() : bool
  92. {
  93. $notNeedLogin = $this->notNeedLogin;
  94. if (empty($notNeedLogin)) {
  95. return false;
  96. }
  97. $action = $this->request->action();
  98. if (!in_array(trim($action), $notNeedLogin)) {
  99. return false;
  100. }
  101. return true;
  102. }
  103. }