SystemGroup.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use crmeb\services\UtilService as Util;
  4. use crmeb\services\JsonService as Json;
  5. use think\facade\Route as Url;
  6. use app\admin\model\system\SystemGroup as GroupModel;
  7. use app\admin\model\system\SystemGroupData as GroupDataModel;
  8. use app\admin\controller\AuthController;
  9. /**
  10. * 组合数据控制器
  11. * Class SystemGroup
  12. * @package app\admin\controller\system
  13. */
  14. class SystemGroup extends AuthController
  15. {
  16. /**
  17. * 显示资源列表
  18. *
  19. * @return \think\Response
  20. */
  21. public function index()
  22. {
  23. $this->assign(GroupModel::page());
  24. return $this->fetch();
  25. }
  26. /**
  27. * 显示创建资源表单页.
  28. *
  29. * @return \think\Response
  30. */
  31. public function create()
  32. {
  33. $this->assign(['title'=>'添加数据组','save'=>Url::buildUrl('save')]);
  34. return $this->fetch();
  35. }
  36. /**
  37. * 保存新建的资源
  38. *
  39. * @param \think\Request $request
  40. * @return \think\Response
  41. */
  42. public function save()
  43. {
  44. $params = Util::postMore([
  45. ['id',''],
  46. ['name',''],
  47. ['config_name',''],
  48. ['info',''],
  49. ['typelist',[]],
  50. ],$this->request);
  51. //数据组名称判断
  52. if(!$params['name'])return Json::fail('请输入数据组名称!');
  53. if(!$params['config_name'])return Json::fail('请输入配置名称!');
  54. //判断ID是否存在,存在就是编辑,不存在就是添加
  55. if(!$params['id']){
  56. if(GroupModel::be($params['config_name'],'config_name')) return Json::fail('数据关键字已存在!');
  57. }
  58. $data["name"] = $params['name'];
  59. $data["config_name"] = $params['config_name'];
  60. $data["info"] = $params['info'];
  61. //字段信息判断
  62. if(!count($params['typelist']))
  63. return Json::fail('字段至少存在一个!');
  64. else{
  65. $validate = ["name","type","title","description"];
  66. foreach ($params["typelist"] as $key => $value) {
  67. foreach ($value as $name => $field) {
  68. if(empty($field["value"]) && in_array($name,$validate))
  69. return Json::fail("字段".($key + 1).":".$field["placeholder"]."不能为空!");
  70. else
  71. $data["fields"][$key][$name] = $field["value"];
  72. }
  73. }
  74. }
  75. $data["fields"] = htmlspecialchars_decode(json_encode($data["fields"]));
  76. //判断ID是否存在,存在就是编辑,不存在就是添加
  77. if(!$params['id']) {
  78. GroupModel::create($data);
  79. return Json::successful('添加数据组成功!');
  80. }else{
  81. GroupModel::edit($data,$params['id']);
  82. return Json::successful('编辑数据组成功!');
  83. }
  84. }
  85. /**编辑数组
  86. * @param $id
  87. */
  88. public function edit($id)
  89. {
  90. $Groupinfo = GroupModel::get($id);
  91. $fields = json_decode($Groupinfo['fields'],true);
  92. $typelist = [];
  93. foreach ($fields as $key => $v){
  94. $typelist[$key]['name']['value'] = $v['name'];
  95. $typelist[$key]['title']['value'] = $v['title'];
  96. $typelist[$key]['type']['value'] = $v['type'];
  97. $typelist[$key]['param']['value'] = $v['param'];
  98. }
  99. $Groupinfo['fields'] = json_encode($typelist);
  100. $this->assign(compact('Groupinfo'));
  101. $this->assign(['title'=>'添加数据组','save'=>Url::buildUrl('save')]);
  102. return $this->fetch();
  103. }
  104. /**
  105. * 删除指定资源
  106. *
  107. * @param int $id
  108. * @return \think\Response
  109. */
  110. public function delete($id)
  111. {
  112. if(!GroupModel::del($id))
  113. return Json::fail(GroupModel::getErrorInfo('删除失败,请稍候再试!'));
  114. else{
  115. GroupDataModel::del(["gid"=>$id]);
  116. return Json::successful('删除成功!');
  117. }
  118. }
  119. }