Base.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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->applyTypes();
  43. $this->title = '数据字典管理';
  44. }, function (QueryHelper $query) {
  45. $query->where(['deleted' => 0])->equal('type')->like('code,name,status')->dateBetween('create_at');
  46. });
  47. }
  48. /**
  49. * 添加数据字典
  50. * @auth true
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function add()
  56. {
  57. $this->_form($this->table, 'form');
  58. }
  59. /**
  60. * 编辑数据字典
  61. * @auth true
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function edit()
  67. {
  68. $this->_form($this->table, 'form');
  69. }
  70. /**
  71. * 表单数据处理
  72. * @param array $data
  73. */
  74. protected function _form_filter(array &$data)
  75. {
  76. if ($this->request->isGet()) {
  77. $this->applyTypes(true);
  78. } else {
  79. $map = [['type', '=', $data['type']], ['code', '=', $data['code']], ['deleted', '=', 0]];
  80. if (isset($data['id'])) $map[] = ['id', '<>', $data['id']];
  81. if ($this->app->db->name($this->table)->where($map)->count() > 0) {
  82. $this->error("同类型的数据编码已经存在!");
  83. }
  84. }
  85. }
  86. /**
  87. * 修改数据状态
  88. * @auth true
  89. * @throws \think\db\exception\DbException
  90. */
  91. public function state()
  92. {
  93. $this->_save($this->table);
  94. }
  95. /**
  96. * 删除数据记录
  97. * @auth true
  98. * @throws \think\db\exception\DbException
  99. */
  100. public function remove()
  101. {
  102. $this->_delete($this->table);
  103. }
  104. /**
  105. * 初始化数据类型
  106. * @param false $add
  107. */
  108. private function applyTypes(bool $add = false)
  109. {
  110. $query = $this->app->db->name($this->table)->where(['deleted' => 0]);
  111. $this->types = $query->distinct(true)->order('id asc')->column('type');
  112. if (empty($this->types)) $this->types = ['身份权限'];
  113. $this->type = input('get.type') ?: ($this->types[0] ?? '-');
  114. if ($add) $this->types[] = '--- 新增类型 ---';
  115. }
  116. }