SystemGroup.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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\controller\admin\v1\system\config;
  12. use app\controller\admin\AuthController;
  13. use app\services\system\config\SystemGroupDataServices;
  14. use app\services\system\config\SystemGroupServices;
  15. use think\facade\App;
  16. /**
  17. * 组合数据
  18. * Class SystemGroup
  19. * @package app\controller\admin\v1\setting
  20. */
  21. class SystemGroup extends AuthController
  22. {
  23. /**
  24. * 构造方法
  25. * SystemGroup constructor.
  26. * @param App $app
  27. * @param SystemGroupServices $services
  28. */
  29. public function __construct(App $app, SystemGroupServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 显示资源列表
  36. *
  37. * @return \think\Response
  38. */
  39. public function index()
  40. {
  41. $where = $this->request->getMore([
  42. ['title', '']
  43. ]);
  44. return $this->success($this->services->getGroupList($where));
  45. }
  46. /**
  47. * 显示创建资源表单页.
  48. *
  49. * @return \think\Response
  50. */
  51. public function create()
  52. {
  53. //
  54. }
  55. /**
  56. * 保存新建的资源
  57. *
  58. * @return \think\Response
  59. */
  60. public function save()
  61. {
  62. $params = $this->request->postMore([
  63. ['name', ''],
  64. ['config_name', ''],
  65. [['cate_id', 'd'], 0],
  66. ['info', ''],
  67. ['typelist', []],
  68. ]);
  69. //数据组名称判断
  70. if (!$params['name']) {
  71. return $this->fail('请输入数据组名称!');
  72. }
  73. if (!$params['config_name']) {
  74. return $this->fail('请输入配置名称!');
  75. }
  76. $data["name"] = $params['name'];
  77. $data["config_name"] = $params['config_name'];
  78. $data["info"] = $params['info'];
  79. $data["cate_id"] = $params['cate_id'];
  80. //字段信息判断
  81. if (!count($params['typelist']))
  82. return $this->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 $this->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. $this->services->save($data);
  96. \crmeb\services\CacheService::clear();
  97. return $this->success('添加数据组成功!');
  98. }
  99. /**
  100. * 显示指定的资源
  101. *
  102. * @param int $id
  103. * @return \think\Response
  104. */
  105. public function read($id)
  106. {
  107. $info = $this->services->get($id);
  108. $fields = json_decode($info['fields'], true);
  109. $type_list = [];
  110. foreach ($fields as $key => $v) {
  111. $type_list[$key]['name']['value'] = $v['name'];
  112. $type_list[$key]['title']['value'] = $v['title'];
  113. $type_list[$key]['type']['value'] = $v['type'];
  114. $type_list[$key]['param']['value'] = $v['param'];
  115. }
  116. $info['typelist'] = $type_list;
  117. unset($info['fields']);
  118. return $this->success(compact('info'));
  119. }
  120. /**
  121. * 显示编辑资源表单页.
  122. *
  123. * @param int $id
  124. * @return \think\Response
  125. */
  126. public function edit($id)
  127. {
  128. //
  129. }
  130. /**
  131. * 保存更新的资源
  132. *
  133. * @param int $id
  134. * @return \think\Response
  135. */
  136. public function update($id)
  137. {
  138. $params = $this->request->postMore([
  139. ['name', ''],
  140. ['config_name', ''],
  141. [['cate_id', 'd'], 0],
  142. ['info', ''],
  143. ['typelist', []],
  144. ]);
  145. //数据组名称判断
  146. if (!$params['name']) return $this->fail('请输入数据组名称!');
  147. if (!$params['config_name']) return $this->fail('请输入配置名称!');
  148. //判断ID是否存在,存在就是编辑,不存在就是添加
  149. if (!$id) {
  150. if ($this->services->count(['config_name' => $params['config_name']])) {
  151. return $this->fail('数据关键字已存在!');
  152. }
  153. }
  154. $data["name"] = $params['name'];
  155. $data["config_name"] = $params['config_name'];
  156. $data["info"] = $params['info'];
  157. $data["cate_id"] = $params['cate_id'];
  158. //字段信息判断
  159. if (!count($params['typelist']))
  160. return $this->fail('字段至少存在一个!');
  161. else {
  162. $validate = ["name", "type", "title", "description"];
  163. foreach ($params["typelist"] as $key => $value) {
  164. foreach ($value as $name => $field) {
  165. if (empty($field["value"]) && in_array($name, $validate))
  166. return $this->fail("字段" . ($key + 1) . ":" . $field["placeholder"] . "不能为空!");
  167. else
  168. $data["fields"][$key][$name] = $field["value"];
  169. }
  170. }
  171. }
  172. $data["fields"] = json_encode($data["fields"]);
  173. $this->services->update($id, $data);
  174. \crmeb\services\CacheService::clear();
  175. return $this->success('编辑数据组成功!');
  176. }
  177. /**
  178. * 删除指定资源
  179. *
  180. * @param int $id
  181. * @return \think\Response
  182. */
  183. public function delete($id, SystemGroupDataServices $services)
  184. {
  185. if (!$this->services->delete($id))
  186. return $this->fail('删除失败,请稍候再试!');
  187. else {
  188. $services->delete($id, 'gid');
  189. return $this->success('删除成功!');
  190. }
  191. }
  192. /**
  193. * 获取组合数据
  194. * @return mixed
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\DbException
  197. * @throws \think\db\exception\ModelNotFoundException
  198. */
  199. public function getGroup()
  200. {
  201. return $this->success($this->services->getGroupList(['cate_id' => 1], ['id', 'name', 'config_name'])['list']);
  202. }
  203. }