SystemGroupData.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\ump\StoreSeckill;
  5. use crmeb\services\{
  6. CacheService,
  7. FormBuilder as Form,
  8. JsonService as Json,
  9. UtilService as Util
  10. };
  11. use think\facade\Route as Url;
  12. use app\admin\model\system\{
  13. SystemAttachment, SystemGroup as GroupModel, SystemGroupData as GroupDataModel
  14. };
  15. /**
  16. * 数据列表控制器 在组合数据中
  17. * Class SystemGroupData
  18. * @package app\admin\controller\system
  19. */
  20. class SystemGroupData extends AuthController
  21. {
  22. /**
  23. * 显示资源列表
  24. * @return \think\Response
  25. */
  26. public function index($gid = 0)
  27. {
  28. $where = Util::getMore([
  29. ['gid', 0],
  30. ['status', ''],
  31. ], $this->request);
  32. if ($gid) $where['gid'] = $gid;
  33. $this->assign('where', $where);
  34. $this->assign(compact("gid"));
  35. $this->assign(GroupModel::getField($gid));
  36. $this->assign(GroupDataModel::getList($where));
  37. return $this->fetch();
  38. }
  39. /**
  40. * 显示创建资源表单页.
  41. * @return \think\Response
  42. */
  43. public function create($gid)
  44. {
  45. $Fields = GroupModel::getField($gid);
  46. $f = array();
  47. foreach ($Fields["fields"] as $key => $value) {
  48. $info = [];
  49. if (isset($value["param"])) {
  50. $value["param"] = str_replace("\r\n", "\n", $value["param"]);//防止不兼容
  51. $params = explode("\n", $value["param"]);
  52. if (is_array($params) && !empty($params)) {
  53. foreach ($params as $index => $v) {
  54. $vl = explode('=>', $v);
  55. if (isset($vl[0]) && isset($vl[1])) {
  56. $info[$index]["value"] = $vl[0];
  57. $info[$index]["label"] = $vl[1];
  58. }
  59. }
  60. }
  61. }
  62. switch ($value["type"]) {
  63. case 'input':
  64. $f[] = Form::input($value["title"], $value["name"]);
  65. break;
  66. case 'textarea':
  67. $f[] = Form::input($value["title"], $value["name"])->type('textarea')->placeholder($value['param']);
  68. break;
  69. case 'radio':
  70. $f[] = Form::radio($value["title"], $value["name"], $info[0]["value"] ?? '')->options($info);
  71. break;
  72. case 'checkbox':
  73. $f[] = Form::checkbox($value["title"], $value["name"], $info[0] ?? '')->options($info);
  74. break;
  75. case 'select':
  76. $f[] = Form::select($value["title"], $value["name"], $info[0] ?? '')->options($info)->multiple(false);
  77. break;
  78. case 'upload':
  79. $f[] = Form::frameImageOne($value["title"], $value["name"], Url::buildUrl('admin/widget.images/index', array('fodder' => $value["title"], 'big' => 1)))->icon('image')->width('100%')->height('500px');
  80. break;
  81. case 'uploads':
  82. $f[] = Form::frameImages($value["title"], $value["name"], Url::buildUrl('admin/widget.images/index', array('fodder' => $value["title"], 'big' => 1)))->maxLength(5)->icon('images')->width('100%')->height('500px')->spin(0);
  83. break;
  84. case 'number':
  85. $f[] = Form::number($value["title"], $value["name"])->precision('int');
  86. break;
  87. default:
  88. $f[] = Form::input($value["title"], $value["name"]);
  89. break;
  90. }
  91. }
  92. $f[] = Form::number('sort', '排序', 1);
  93. $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  94. $form = Form::make_post_form('添加数据', $f, Url::buildUrl('save', compact('gid')), 2);
  95. $this->assign(compact('form'));
  96. return $this->fetch('public/form-builder');
  97. }
  98. /**
  99. * 保存新建的资源
  100. *
  101. * @param \think\Request $request
  102. * @return \think\Response
  103. */
  104. public function save($gid)
  105. {
  106. $Fields = GroupModel::getField($gid);
  107. $params = request()->post();
  108. foreach ($params as $key => $param) {
  109. foreach ($Fields['fields'] as $index => $field) {
  110. if ($key == $field["title"]) {
  111. // if($param == "" || count($param) == 0)
  112. if ($param == "")
  113. return Json::fail($field["name"] . "不能为空!");
  114. else {
  115. $value[$key]["type"] = $field["type"];
  116. $value[$key]["value"] = $param;
  117. }
  118. }
  119. }
  120. }
  121. $data = array("gid" => $gid, "add_time" => time(), "value" => htmlspecialchars_decode(json_encode($value)), "sort" => $params["sort"], "status" => $params["status"]);
  122. GroupDataModel::create($data);
  123. CacheService::clear();
  124. return Json::successful('添加数据成功!');
  125. }
  126. /**
  127. * 显示指定的资源
  128. *
  129. * @param int $id
  130. * @return \think\Response
  131. */
  132. public function read($id)
  133. {
  134. //
  135. }
  136. /**
  137. * 显示编辑资源表单页.
  138. *
  139. * @param int $id
  140. * @return \think\Response
  141. */
  142. public function edit($gid, $id)
  143. {
  144. $GroupData = GroupDataModel::get($id);
  145. $GroupDataValue = json_decode($GroupData["value"], true);
  146. $Fields = GroupModel::getField($gid);
  147. $f = array();
  148. if (!isset($Fields['fields'])) return $this->failed('数据解析失败!');
  149. foreach ($Fields['fields'] as $key => $value) {
  150. $info = [];
  151. if (isset($value["param"])) {
  152. $value["param"] = str_replace("\r\n", "\n", $value["param"]);//防止不兼容
  153. $params = explode("\n", $value["param"]);
  154. if (is_array($params) && !empty($params)) {
  155. foreach ($params as $index => $v) {
  156. $vl = explode('=>', $v);
  157. if (isset($vl[0]) && isset($vl[1])) {
  158. $info[$index]["value"] = $vl[0];
  159. $info[$index]["label"] = $vl[1];
  160. }
  161. }
  162. }
  163. }
  164. $fvalue = isset($GroupDataValue[$value['title']]['value']) ? $GroupDataValue[$value['title']]['value'] : '';
  165. switch ($value['type']) {
  166. case 'input':
  167. $f[] = Form::input($value['title'], $value['name'], $fvalue);
  168. break;
  169. case 'textarea':
  170. $f[] = Form::input($value['title'], $value['name'], $fvalue)->type('textarea');
  171. break;
  172. case 'radio':
  173. $f[] = Form::radio($value['title'], $value['name'], $fvalue)->options($info);
  174. break;
  175. case 'checkbox':
  176. $f[] = Form::checkbox($value['title'], $value['name'], $fvalue)->options($info);
  177. break;
  178. case 'upload':
  179. if (!empty($fvalue)) {
  180. $image = is_string($fvalue) ? $fvalue : $fvalue[0];
  181. } else {
  182. $image = '';
  183. }
  184. $f[] = Form::frameImageOne($value['title'], $value['name'], Url::buildUrl('admin/widget.images/index', array('fodder' => $value['title'], 'big' => 1)), $image)->icon('image')->width('100%')->height('500px');
  185. break;
  186. case 'uploads':
  187. $images = !empty($fvalue) ? $fvalue : [];
  188. $f[] = Form::frameImages($value['title'], $value['name'], Url::buildUrl('admin/widget.images/index', array('fodder' => $value['title'], 'big' => 1)), $images)->maxLength(5)->icon('images')->width('100%')->height('500px')->spin(0);
  189. break;
  190. case 'select':
  191. $f[] = Form::select($value['title'], $value['name'], $fvalue)->setOptions($info);
  192. break;
  193. case 'number':
  194. $f[] = Form::number($value["title"], $value["name"])->precision('int');
  195. break;
  196. default:
  197. $f[] = Form::input($value['title'], $value['name'], $fvalue);
  198. break;
  199. }
  200. }
  201. $f[] = Form::number('sort', '排序', $GroupData["sort"]);
  202. $f[] = Form::radio('status', '状态', $GroupData["status"])->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  203. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('update', compact('id')), 2);
  204. $this->assign(compact('form'));
  205. return $this->fetch('public/form-builder');
  206. }
  207. /**
  208. * 保存更新的资源
  209. *
  210. * @param $id
  211. */
  212. public function update($id)
  213. {
  214. $GroupData = GroupDataModel::get($id);
  215. $Fields = GroupModel::getField($GroupData["gid"]);
  216. $params = request()->post();
  217. foreach ($params as $key => $param) {
  218. foreach ($Fields['fields'] as $index => $field) {
  219. if ($key == $field["title"]) {
  220. if (trim($param) == '')
  221. return Json::fail($field["name"] . "不能为空!");
  222. else {
  223. $value[$key]["type"] = $field["type"];
  224. $value[$key]["value"] = $param;
  225. }
  226. }
  227. }
  228. }
  229. $data = array("value" => htmlspecialchars_decode(json_encode($value)), "sort" => $params["sort"], "status" => $params["status"]);
  230. GroupDataModel::edit($data, $id);
  231. CacheService::clear();
  232. return Json::successful('修改成功!');
  233. }
  234. /**
  235. * 删除指定资源
  236. *
  237. * @param int $id
  238. * @return \think\Response
  239. */
  240. public function delete($id)
  241. {
  242. $gid = GroupDataModel::where('id', $id)->value('gid');
  243. $config_name = GroupModel::where('id', $gid)->value('config_name');
  244. if ($config_name == 'routine_seckill_time') {
  245. if (!StoreSeckill::where('is_del', 0)->find()) {
  246. if (!GroupDataModel::del($id))
  247. return Json::fail(GroupDataModel::getErrorInfo('删除失败,请稍候再试!'));
  248. else {
  249. CacheService::clear();
  250. return Json::successful('删除成功!');
  251. }
  252. } else {
  253. return Json::fail('有秒杀活动,不能删除秒杀时段,请先删除活动');
  254. }
  255. } else {
  256. if (!GroupDataModel::del($id))
  257. return Json::fail(GroupDataModel::getErrorInfo('删除失败,请稍候再试!'));
  258. else {
  259. CacheService::clear();
  260. return Json::successful('删除成功!');
  261. }
  262. }
  263. }
  264. }