Modules_Manage_Controllers_SystemController.proxy.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * 系统配置.
  4. *
  5. * @author jim <jim458734573@gmail.com>
  6. * @date 2020-01-31 10:44:33
  7. */
  8. declare (strict_types=1);
  9. namespace Modules\Manage\Controllers;
  10. use App\Constants\CommonConstant;
  11. use App\Repositories\ConfigRepoistory;
  12. use App\Repositories\Criterias\ManagerLogPagesCriteria;
  13. use App\Repositories\Criterias\OrderLimitCriteria;
  14. use App\Repositories\ManagerLogRepository;
  15. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  16. use Modules\Manage\Responses\CommonDetailResponse;
  17. use Modules\Manage\Responses\CommonPagesResponse;
  18. use Modules\Manage\Responses\CommonSuccessPkResponse;
  19. use Modules\Manage\Responses\SystemLogPagesResponse;
  20. class SystemController extends BaseController
  21. {
  22. use \Hyperf\Di\Aop\ProxyTrait;
  23. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  24. function __construct()
  25. {
  26. if (method_exists(parent::class, '__construct')) {
  27. parent::__construct(...func_get_args());
  28. }
  29. $this->__handlePropertyHandler(__CLASS__);
  30. }
  31. private $configSgroup = 'sys';
  32. /**
  33. * 读取配置信息.
  34. * @return mixed
  35. */
  36. public function configInfo()
  37. {
  38. $inputData = $this->getJsonData();
  39. $sgroup = !empty($inputData['sgroup']) ? $inputData['sgroup'] : $this->configSgroup;
  40. /*$cacheKey = 'config_'. $this->configSgroup;
  41. $cache = make(CacheInterface::class);
  42. $data = $cache->rememberForever($cacheKey, function () {
  43. return make(ConfigRepoistory::class)->pickByKeys([], $this->configSgroup);
  44. });*/
  45. $data = make(ConfigRepoistory::class)->pickByKeys('', $sgroup);
  46. // $data = make(ConfigRepoistory::class)->get('asdasd', $this->configSgroup, '');
  47. return new CommonDetailResponse($data);
  48. }
  49. /**
  50. * 配置保存.
  51. * @return mixed
  52. */
  53. public function configSave()
  54. {
  55. $inputData = $this->getJsonData();
  56. $sgroup = data_get($inputData, 'sgroup', '');
  57. $items = data_get($inputData, 'input', []);
  58. if (!empty($sgroup) && !empty($items)) {
  59. $repo = make(ConfigRepoistory::class);
  60. $repo->saveByKeys($items, $sgroup);
  61. // 保存后同时更新缓存.
  62. $repo->updateConfigCache($sgroup);
  63. }
  64. // 操作日志.
  65. $logSubject = '保存系统配置';
  66. $logSvalue = '';
  67. $logDbData = ['after_items' => [], 'before_items' => []];
  68. $this->log($logSubject, $logSvalue, $logDbData);
  69. return new CommonSuccessPkResponse('');
  70. }
  71. /**
  72. * 获取所有分组.
  73. * @return mixed
  74. */
  75. public function configType()
  76. {
  77. $arrData = [];
  78. $items = make(ConfigRepoistory::class)->getAllGroup();
  79. if ($items->count()) {
  80. foreach ($items as $k => $v) {
  81. $arrData[$v->sgroup] = $v->sgroup;
  82. }
  83. }
  84. return json_success($arrData);
  85. }
  86. /**
  87. * 配置缓存更新.
  88. * @return mixed
  89. */
  90. public function configUpdateCache()
  91. {
  92. make(ConfigRepoistory::class)->updateConfigCache($this->configSgroup);
  93. // make(ConfigRepoistory::class)->clearAllCache();
  94. // 操作日志.
  95. $logSubject = '更新系统配置缓存';
  96. $logSvalue = '';
  97. $logDbData = ['after_items' => [], 'before_items' => []];
  98. $this->log($logSubject, $logSvalue, $logDbData);
  99. return new CommonSuccessPkResponse('');
  100. }
  101. /**
  102. * 清除所有缓存.
  103. * @return CommonSuccessPkResponse
  104. */
  105. public function clearAllCache()
  106. {
  107. make(ConfigRepoistory::class)->clearAllCache();
  108. // 操作日志.
  109. $logSubject = '清楚所有缓存';
  110. $logSvalue = '';
  111. $logDbData = ['after_items' => [], 'before_items' => []];
  112. $this->log($logSubject, $logSvalue, $logDbData);
  113. return new CommonSuccessPkResponse('');
  114. }
  115. /**
  116. * 管理日志列表.
  117. * @return mixed|CommonPagesResponse
  118. */
  119. public function logLists()
  120. {
  121. $inputData = $this->getJsonData();
  122. $rules = ['page' => 'nullable|integer', 'page_size' => 'nullable|integer', 'username' => 'nullable|max:60', 'nick' => 'nullable|max:60', 'subject' => 'nullable|max:700', 'module' => 'nullable|max:30', 'ip' => 'nullable|max:40'];
  123. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  124. if ($validator->fails()) {
  125. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  126. return json_fail($validator->errors()->first());
  127. }
  128. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  129. if ($pageSize > 500) {
  130. $pageSize = CommonConstant::PAGE_SIZE;
  131. }
  132. $page = format_page($inputData['page']);
  133. $orders = [['created_at', 'DESC']];
  134. // 注意,单例分页一定要克隆一个(保证前面没用使用到paginate);
  135. $lists = make(ManagerLogRepository::class)->pushCriteria(new ManagerLogPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  136. return new SystemLogPagesResponse($lists);
  137. }
  138. }