SystemGroup.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\setting;
  12. use EasyWeChat\ShakeAround\Group;
  13. use service\JsonService as Json;
  14. use think\Request;
  15. use think\Url;
  16. use app\admin\model\system\SystemGroup as GroupModel;
  17. use app\admin\model\system\SystemGroupData as GroupDataModel;
  18. use app\admin\controller\AuthController;
  19. /**
  20. * 组合数据控制器
  21. * Class SystemGroup
  22. * @package app\admin\controller\system
  23. */
  24. class SystemGroup extends AuthController
  25. {
  26. /**
  27. * 显示资源列表
  28. * @return \think\Response
  29. */
  30. public function index()
  31. {
  32. $type = $this->request->param('type', "");
  33. $where_type = [];
  34. if (isset($type) && $type) {
  35. switch ($type) {
  36. case "bank":
  37. $where_type['config_name'] = ['extract_bank', 'extract_rule'];
  38. break;
  39. }
  40. }
  41. $list = GroupModel::getGroupDataByType($where_type);
  42. $this->assign($list);
  43. return $this->fetch();
  44. }
  45. /**
  46. * 显示创建资源表单页.
  47. *
  48. * @return \think\Response
  49. */
  50. public function create()
  51. {
  52. $this->assign(['title' => '添加数据组', 'save' => Url::build('save')]);
  53. return $this->fetch();
  54. }
  55. /**
  56. * 保存新建的资源
  57. *
  58. * @param \think\Request $request
  59. * @return \think\Response
  60. */
  61. public function save(Request $request)
  62. {
  63. $params = parent::postMore([
  64. ['id', ''],
  65. ['name', ''],
  66. ['config_name', ''],
  67. ['info', ''],
  68. ['typelist', []],
  69. ], $this->request);
  70. //数据组名称判断
  71. if (!$params['name']) return Json::fail('请输入数据组名称!');
  72. if (!$params['config_name']) return Json::fail('请输入配置名称!');
  73. //判断ID是否存在,存在就是编辑,不存在就是添加
  74. if (!$params['id']) {
  75. if (GroupModel::be($params['config_name'], 'config_name')) return Json::fail('数据关键字已存在!');
  76. }
  77. $data["name"] = $params['name'];
  78. $data["config_name"] = $params['config_name'];
  79. $data["info"] = $params['info'];
  80. //字段信息判断
  81. if (!count($params['typelist']))
  82. return Json::fail('字段至少存在一个!');
  83. else {
  84. $validate = ["name", "type", "title", "description"];
  85. foreach ($params["typelist"] as $key => $value) {
  86. foreach ($value as $name => $field) {
  87. if (empty($field["value"]) && in_array($name, $validate))
  88. return Json::fail("字段" . ($key + 1) . ":" . $field["placeholder"] . "不能为空!");
  89. else
  90. $data["fields"][$key][$name] = $field["value"];
  91. }
  92. }
  93. }
  94. $data["fields"] = json_encode($data["fields"]);
  95. //判断ID是否存在,存在就是编辑,不存在就是添加
  96. if (!$params['id']) {
  97. GroupModel::set($data);
  98. return Json::successful('添加数据组成功!');
  99. } else {
  100. GroupModel::edit($data, $params['id']);
  101. return Json::successful('编辑数据组成功!');
  102. }
  103. }
  104. /**编辑数组
  105. * @param $id
  106. */
  107. public function edit($id)
  108. {
  109. $Groupinfo = GroupModel::get($id);
  110. $fields = json_decode($Groupinfo['fields'], true);
  111. $typelist = [];
  112. foreach ($fields as $key => $v) {
  113. $typelist[$key]['name']['value'] = $v['name'];
  114. $typelist[$key]['title']['value'] = $v['title'];
  115. $typelist[$key]['type']['value'] = $v['type'];
  116. $typelist[$key]['param']['value'] = $v['param'];
  117. }
  118. $Groupinfo['fields'] = json_encode($typelist);
  119. $this->assign(compact('Groupinfo'));
  120. $this->assign(['title' => '添加数据组', 'save' => Url::build('save')]);
  121. return $this->fetch();
  122. }
  123. /**
  124. * 删除指定资源
  125. *
  126. * @param int $id
  127. * @return \think\Response
  128. */
  129. public function delete($id)
  130. {
  131. if (!GroupModel::del($id))
  132. return Json::fail(GroupModel::getErrorInfo('删除失败,请稍候再试!'));
  133. else {
  134. GroupDataModel::del(["gid" => $id]);
  135. return Json::successful('删除成功!');
  136. }
  137. }
  138. }