Base.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\admin\controller;
  16. use app\admin\model\SystemBase;
  17. use think\admin\Controller;
  18. use think\admin\helper\QueryHelper;
  19. /**
  20. * 数据字典管理
  21. * Class Base
  22. * @package app\admin\controller
  23. */
  24. class Base extends Controller
  25. {
  26. /**
  27. * 绑定数据表
  28. * @var string
  29. */
  30. protected $table = 'SystemBase';
  31. /**
  32. * 数据字典管理
  33. * @auth true
  34. * @menu true
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function index()
  40. {
  41. $this->_query(SystemBase::class)->layTable(function () {
  42. $this->title = '数据字典管理';
  43. $this->types = (new SystemBase)->types();
  44. $this->type = input('get.type') ?: ($this->types[0] ?? '-');
  45. }, function (QueryHelper $query) {
  46. $query->where(['deleted' => 0])->equal('type')->like('code,name,status')->dateBetween('create_at');
  47. });
  48. }
  49. /**
  50. * 添加数据字典
  51. * @auth true
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function add()
  57. {
  58. $this->_form($this->table, 'form');
  59. }
  60. /**
  61. * 编辑数据字典
  62. * @auth true
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function edit()
  68. {
  69. $this->_form($this->table, 'form');
  70. }
  71. /**
  72. * 表单数据处理
  73. * @param array $data
  74. */
  75. protected function _form_filter(array &$data)
  76. {
  77. if ($this->request->isGet()) {
  78. $this->types = (new SystemBase)->types();
  79. $this->types[] = '--- 新增类型 ---';
  80. $this->type = input('get.type') ?: ($this->types[0] ?? '-');
  81. } else {
  82. $map = [];
  83. $map[] = ['deleted', '=', 0];
  84. $map[] = ['code', '=', $data['code']];
  85. $map[] = ['type', '=', $data['type']];
  86. if (isset($data['id'])) $map[] = ['id', '<>', $data['id']];
  87. if ($this->app->db->name($this->table)->where($map)->count() > 0) {
  88. $this->error("同类型的数据编码已经存在!");
  89. }
  90. }
  91. }
  92. /**
  93. * 修改数据状态
  94. * @auth true
  95. * @throws \think\db\exception\DbException
  96. */
  97. public function state()
  98. {
  99. $this->_save($this->table);
  100. }
  101. /**
  102. * 删除数据记录
  103. * @auth true
  104. * @throws \think\db\exception\DbException
  105. */
  106. public function remove()
  107. {
  108. $this->_delete($this->table);
  109. }
  110. }