SystemDictDataController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\app\cache\DictCache;
  9. use plugin\saiadmin\basic\BaseController;
  10. use plugin\saiadmin\app\logic\system\SystemDictDataLogic;
  11. use plugin\saiadmin\app\validate\system\SystemDictDataValidate;
  12. use plugin\saiadmin\service\Permission;
  13. use support\Request;
  14. use support\Response;
  15. /**
  16. * 字典数据控制器
  17. */
  18. class SystemDictDataController extends BaseController
  19. {
  20. /**
  21. * 构造
  22. */
  23. public function __construct()
  24. {
  25. $this->logic = new SystemDictDataLogic();
  26. $this->validate = new SystemDictDataValidate;
  27. parent::__construct();
  28. }
  29. /**
  30. * 数据列表
  31. * @param Request $request
  32. * @return Response
  33. */
  34. #[Permission('数据字典列表', 'core:dict:index')]
  35. public function index(Request $request): Response
  36. {
  37. $where = $request->more([
  38. ['label', ''],
  39. ['value', ''],
  40. ['type_id', ''],
  41. ['status', ''],
  42. ]);
  43. $this->logic->setOrderField('sort');
  44. $this->logic->setOrderType('desc');
  45. $query = $this->logic->search($where);
  46. $data = $this->logic->getList($query);
  47. return $this->success($data);
  48. }
  49. /**
  50. * 保存数据
  51. * @param Request $request
  52. * @return Response
  53. */
  54. #[Permission('数据字典管理', 'core:dict:edit')]
  55. public function save(Request $request): Response
  56. {
  57. $data = $request->post();
  58. $this->validate('save', $data);
  59. $result = $this->logic->add($data);
  60. if ($result) {
  61. DictCache::clear();
  62. return $this->success('添加成功');
  63. } else {
  64. return $this->fail('添加失败');
  65. }
  66. }
  67. /**
  68. * 更新数据
  69. * @param Request $request
  70. * @return Response
  71. */
  72. #[Permission('数据字典管理', 'core:dict:edit')]
  73. public function update(Request $request): Response
  74. {
  75. $data = $request->post();
  76. $this->validate('update', $data);
  77. $result = $this->logic->edit($data['id'], $data);
  78. if ($result) {
  79. DictCache::clear();
  80. return $this->success('修改成功');
  81. } else {
  82. return $this->fail('修改失败');
  83. }
  84. }
  85. /**
  86. * 删除数据
  87. * @param Request $request
  88. * @return Response
  89. */
  90. #[Permission('数据字典管理', 'core:dict:edit')]
  91. public function destroy(Request $request): Response
  92. {
  93. $ids = $request->post('ids', '');
  94. if (empty($ids)) {
  95. return $this->fail('请选择要删除的数据');
  96. }
  97. $result = $this->logic->destroy($ids);
  98. if ($result) {
  99. DictCache::clear();
  100. return $this->success('删除成功');
  101. } else {
  102. return $this->fail('删除失败');
  103. }
  104. }
  105. }