SystemDictDataController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\controller\system;
  8. use plugin\saiadmin\basic\BaseController;
  9. use plugin\saiadmin\app\logic\system\SystemDictDataLogic;
  10. use plugin\saiadmin\app\validate\system\SystemDictDataValidate;
  11. use support\Cache;
  12. use support\Request;
  13. use support\Response;
  14. /**
  15. * 字典数据控制器
  16. */
  17. class SystemDictDataController extends BaseController
  18. {
  19. /**
  20. * 构造
  21. */
  22. public function __construct()
  23. {
  24. $this->logic = new SystemDictDataLogic();
  25. $this->validate = new SystemDictDataValidate;
  26. parent::__construct();
  27. }
  28. /**
  29. * 数据列表
  30. * @param Request $request
  31. * @return Response
  32. */
  33. public function index(Request $request) : Response
  34. {
  35. $where = $request->more([
  36. ['label', ''],
  37. ['value', ''],
  38. ['type_id', ''],
  39. ['status', ''],
  40. ]);
  41. $query = $this->logic->search($where);
  42. $data = $this->logic->getList($query);
  43. return $this->success($data);
  44. }
  45. /**
  46. * 修改状态
  47. * @param Request $request
  48. * @return Response
  49. */
  50. public function changeStatus(Request $request) : Response
  51. {
  52. $id = $request->input('id', '');
  53. $status = $request->input('status', 1);
  54. $model = $this->logic->findOrEmpty($id);
  55. if ($model->isEmpty()) {
  56. return $this->fail('未查找到信息');
  57. }
  58. $result = $model->save(['status' => $status]);
  59. if ($result) {
  60. $this->afterChange('changeStatus', $model);
  61. return $this->success('操作成功');
  62. } else {
  63. return $this->fail('操作失败');
  64. }
  65. }
  66. /**
  67. * 数据改变后执行
  68. * @param $type
  69. * @param $args
  70. * @return void
  71. */
  72. protected function afterChange($type, $args): void
  73. {
  74. if (in_array($type, ['save', 'update'])) {
  75. Cache::delete(request()->input('code'));
  76. }
  77. if ($type === 'changeStatus') {
  78. $id = request()->input('id', '');
  79. $info = $this->logic->findOrEmpty($id);
  80. if (!$info->isEmpty()) {
  81. Cache::delete($info->code);
  82. }
  83. }
  84. if ($type === 'destroy') {
  85. $ids = request()->input('ids', '');
  86. if (is_array($ids)) {
  87. $id = $ids[0];
  88. $info = $this->logic->findOrEmpty($id);
  89. if (!$info->isEmpty()) {
  90. Cache::delete($info->code);
  91. }
  92. }
  93. }
  94. }
  95. }