SystemGroupData.php 11 KB

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