Base.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\SystemBase;
  4. use think\admin\Controller;
  5. use think\admin\helper\QueryHelper;
  6. /**
  7. * 数据字典管理
  8. * Class Base
  9. * @package app\admin\controller
  10. */
  11. class Base extends Controller
  12. {
  13. /**
  14. * 绑定数据表
  15. * @var string
  16. */
  17. protected $table = 'SystemBase';
  18. /**
  19. * 数据字典管理
  20. * @auth true
  21. * @menu true
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function index()
  27. {
  28. $this->_query(SystemBase::class)->layTable(function () {
  29. $this->title = '数据字典管理';
  30. $this->types = (new SystemBase)->types();
  31. $this->type = input('get.type') ?: ($this->types[0] ?? '-');
  32. }, function (QueryHelper $query) {
  33. $query->where(['deleted' => 0])->equal('type')->like('code,name,status')->dateBetween('create_at');
  34. });
  35. }
  36. /**
  37. * 添加数据字典
  38. * @auth true
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function add()
  44. {
  45. $this->_form($this->table, 'form');
  46. }
  47. /**
  48. * 编辑数据字典
  49. * @auth true
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function edit()
  55. {
  56. $this->_form($this->table, 'form');
  57. }
  58. /**
  59. * 表单数据处理
  60. * @param array $data
  61. */
  62. protected function _form_filter(array &$data)
  63. {
  64. if ($this->request->isGet()) {
  65. $this->types = (new SystemBase)->types();
  66. $this->types[] = '--- 新增类型 ---';
  67. $this->type = input('get.type') ?: ($this->types[0] ?? '-');
  68. } else {
  69. $map = [];
  70. $map[] = ['deleted', '=', 0];
  71. $map[] = ['code', '=', $data['code']];
  72. $map[] = ['type', '=', $data['type']];
  73. if (isset($data['id'])) $map[] = ['id', '<>', $data['id']];
  74. if ($this->app->db->name($this->table)->where($map)->count() > 0) {
  75. $this->error("同类型的数据编码已经存在!");
  76. }
  77. }
  78. }
  79. /**
  80. * 修改数据状态
  81. * @auth true
  82. * @throws \think\db\exception\DbException
  83. */
  84. public function state()
  85. {
  86. $this->_save($this->table);
  87. }
  88. /**
  89. * 删除数据记录
  90. * @auth true
  91. * @throws \think\db\exception\DbException
  92. */
  93. public function remove()
  94. {
  95. $this->_delete($this->table);
  96. }
  97. }