AbstractLogic.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic;
  8. use plugin\saiadmin\basic\contracts\LogicInterface;
  9. /**
  10. * 抽象逻辑层基类
  11. * 定义通用属性和方法签名,具体实现由各 ORM 驱动完成
  12. */
  13. abstract class AbstractLogic implements LogicInterface
  14. {
  15. /**
  16. * 模型注入
  17. * @var object
  18. */
  19. protected $model;
  20. /**
  21. * 管理员信息
  22. * @var array
  23. */
  24. protected array $adminInfo;
  25. /**
  26. * 排序字段
  27. * @var string
  28. */
  29. protected string $orderField = '';
  30. /**
  31. * 排序方式
  32. * @var string
  33. */
  34. protected string $orderType = 'ASC';
  35. /**
  36. * 初始化
  37. * @param $user
  38. * @return void
  39. */
  40. public function init($user): void
  41. {
  42. $this->adminInfo = $user;
  43. }
  44. /**
  45. * 设置排序字段
  46. * @param string $field
  47. * @return static
  48. */
  49. public function setOrderField(string $field): static
  50. {
  51. $this->orderField = $field;
  52. return $this;
  53. }
  54. /**
  55. * 设置排序方式
  56. * @param string $type
  57. * @return static
  58. */
  59. public function setOrderType(string $type): static
  60. {
  61. $this->orderType = $type;
  62. return $this;
  63. }
  64. /**
  65. * 获取模型实例
  66. * @return object
  67. */
  68. public function getModel(): object
  69. {
  70. return $this->model;
  71. }
  72. /**
  73. * 获取上传的导入文件
  74. * @param $file
  75. * @return string
  76. */
  77. public function getImport($file): string
  78. {
  79. $full_dir = runtime_path() . '/resource/';
  80. if (!is_dir($full_dir)) {
  81. mkdir($full_dir, 0777, true);
  82. }
  83. $ext = $file->getUploadExtension() ?: null;
  84. $full_path = $full_dir . md5(time()) . '.' . $ext;
  85. $file->move($full_path);
  86. return $full_path;
  87. }
  88. /**
  89. * 方法调用代理到模型
  90. * @param string $name
  91. * @param array $arguments
  92. * @return mixed
  93. */
  94. public function __call(string $name, array $arguments): mixed
  95. {
  96. return call_user_func_array([$this->model, $name], $arguments);
  97. }
  98. }