SystemGroupData.php 12 KB

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