| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- /**
- * 系统配置.
- *
- * @author nj
- * @date 2021-01-31 10:44:33
- */
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Repositories\Criterias\ManagerLogPagesCriteria;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use App\Repositories\ConfigRepoistory;
- use App\Repositories\ManagerLogRepository;
- use Modules\ManageApi\Responses\CommonDetailResponse;
- use Modules\ManageApi\Responses\CommonSuccessPkResponse;
- use Modules\ManageApi\Responses\SystemLogPagesResponse;
- use Validator;
- class SystemController extends BaseController
- {
- /**
- * 读取配置信息.
- * @return mixed
- */
- public function configInfo()
- {
- /**
- * @var Validator $validatorFactory
- */
- $validatorFactory = app('validator');
- /**
- * @var ConfigRepoistory $configRepository
- */
- $configRepository = make(ConfigRepoistory::class);
- $inputData = $this->getJsonData();
- $rules = [
- 'sgroup' => 'required|string',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $data = $configRepository->pickByKeys([], $inputData['sgroup']);
- return new CommonDetailResponse($data);
- }
- /**
- * 配置保存.
- * @return \Illuminate\Http\JsonResponse|CommonSuccessPkResponse
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function configSave()
- {
- /**
- * @var Validator $validatorFactory
- */
- $validatorFactory = app('validator');
- /**
- * @var ConfigRepoistory $configRepository
- */
- $configRepository = make(ConfigRepoistory::class);
- $inputData = $this->getJsonData();
- $rules = [
- 'sgroup' => 'required|string',
- 'input' => 'required',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $repo = $configRepository;
- $repo->saveByKeys($inputData['input'], $inputData['sgroup']);
- // 保存后同时更新缓存.
- $repo->updateConfigCache($inputData['sgroup']);
- // 操作日志.
- $logSubject = '保存系统配置';
- $logSvalue = '';
- $logDbData = [
- 'after_items' => [],
- 'before_items' => [],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessPkResponse('');
- }
- /**
- * 配置缓存更新.
- * @return \Illuminate\Http\JsonResponse|CommonSuccessPkResponse
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function configUpdateCache()
- {
- /**
- * @var Validator $validatorFactory
- */
- $validatorFactory = app('validator');
- /**
- * @var ConfigRepoistory $configRepository
- */
- $configRepository = make(ConfigRepoistory::class);
- $inputData = $this->getJsonData();
- $rules = [
- 'sgroup' => 'required|string',
- ];
- $validator =$validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $configRepository->updateConfigCache($inputData['sgroup']);
- // 操作日志.
- $logSubject = '更新系统配置缓存';
- $logSvalue = '';
- $logDbData = [
- 'after_items' => [],
- 'before_items' => [],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessPkResponse('');
- }
- /**
- * 管理日志列表.
- * @return mixed
- */
- public function logLists()
- {
- /**
- * @var Validator $validatorFactory
- */
- $validatorFactory = app('validator');
- /**
- * @var ConfigRepoistory $configRepository
- */
- $configRepository = make(ConfigRepoistory::class);
- /**
- * @var ManagerLogRepository $managerLogRepository
- */
- $managerLogRepository = make(ManagerLogRepository::class);
- $inputData = $this->getJsonData();
- $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',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page'] ?? 0);
- $orders = [
- ['created_at', 'DESC'],
- ];
- // 注意,单例分页一定要克隆一个(保证前面没用使用到paginate);
- $lists = $managerLogRepository->pushCriteria(new ManagerLogPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
- return new SystemLogPagesResponse($lists);
- }
- }
|