PluginsBase.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app;
  3. use think\App;
  4. use think\View;
  5. class PluginsBase
  6. {
  7. public ?View $view = null;
  8. public ?\think\Request $request = null;
  9. public ?App $app = null;
  10. function __construct(App $app)
  11. {
  12. $this->request = $app->request;
  13. // 视图对象
  14. $this->view = new View($app);
  15. $this->app = $app;
  16. $this->_initialize();
  17. }
  18. function _initialize()
  19. {
  20. }
  21. function assign($key, $view)
  22. {
  23. $this->view->assign($key, $view);
  24. }
  25. function fetch($view, $opt = []): string
  26. {
  27. $view = plugins_path("view/" . $view);
  28. return $this->view->fetch($view, $opt);
  29. }
  30. public function getAdmin()
  31. {
  32. $info = new BaseController($this->app);
  33. return $info->getAdmin();
  34. }
  35. public function getUser(bool $must = false)
  36. {
  37. $info = new BaseController($this->app);
  38. return $info->getUser($must);
  39. }
  40. public function success($msg, $data = []): \think\response\Json
  41. {
  42. if (is_array($msg)) {
  43. return json(['msg' => '', 'code' => 1, 'data' => $msg]);
  44. }
  45. return json(['msg' => $msg, 'code' => 1, 'data' => $data]);
  46. }
  47. public function error($msg, $data = []): \think\response\Json
  48. {
  49. if (is_array($msg)) {
  50. return json(['msg' => '', 'code' => 0, 'data' => $msg]);
  51. }
  52. return json(['msg' => $msg, 'code' => 0, 'data' => $data]);
  53. }
  54. }