SystemGroupData.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 app\admin\model\special\Grade;
  13. use app\admin\model\special\RecommendBanner;
  14. use app\admin\model\special\Special;
  15. use app\admin\model\system\Recommend;
  16. use app\admin\model\system\RecommendRelation;
  17. use app\admin\model\user\Group;
  18. use service\FormBuilder as Form;
  19. use service\JsonService as Json;
  20. use service\UploadService as Upload;
  21. use think\Request;
  22. use think\Url;
  23. use app\admin\model\system\SystemGroup as GroupModel;
  24. use app\admin\model\system\SystemGroupData as GroupDataModel;
  25. use app\admin\controller\AuthController;
  26. use app\admin\model\system\SystemAttachment;
  27. /**
  28. * 数据列表控制器 在组合数据中
  29. * Class SystemGroupData
  30. * @package app\admin\controller\system
  31. */
  32. class SystemGroupData extends AuthController
  33. {
  34. /**
  35. * 显示资源列表
  36. * @return \think\Response
  37. */
  38. public function index($gid)
  39. {
  40. $where = parent::getMore([
  41. ['status', '']
  42. ], $this->request);
  43. $this->assign('where', $where);
  44. $this->assign(compact("gid"));
  45. $this->assign(GroupModel::getField($gid));
  46. $where['gid'] = $gid;
  47. $this->assign(GroupDataModel::getList($where));
  48. return $this->fetch();
  49. }
  50. /**
  51. * 显示创建资源表单页.
  52. * @return \think\Response
  53. */
  54. public function create($gid)
  55. {
  56. $Fields = GroupModel::getField($gid);
  57. $f = array();
  58. foreach ($Fields["fields"] as $key => $value) {
  59. $info = [];
  60. if (!empty($value["param"])) {
  61. $value["param"] = str_replace("\r\n", "\n", $value["param"]);//防止不兼容
  62. $params = explode("\n", $value["param"]);
  63. if (is_array($params) && !empty($params)) {
  64. foreach ($params as $index => $v) {
  65. if (strstr($v, '=>') !== false) {
  66. list($left, $right) = explode('=>', $v);
  67. } else if (strstr($v, '=') !== false) {
  68. list($left, $right) = explode('=', $v);
  69. }
  70. $val["value"] = $left;
  71. $val["label"] = $right;
  72. $info[] = $val;
  73. }
  74. }
  75. }
  76. switch ($value["type"]) {
  77. case 'input':
  78. $f[] = Form::input($value["title"], $value["name"]);
  79. break;
  80. case 'textarea':
  81. $f[] = Form::input($value["title"], $value["name"])->type('textarea')->placeholder($value['param']);
  82. break;
  83. case 'radio':
  84. $f[] = Form::radio($value["title"], $value["name"], isset($info[0]["value"]) ? $info[0]["value"] : '')->options($info);
  85. break;
  86. case 'checkbox':
  87. $f[] = Form::checkbox($value["title"], $value["name"], isset($info[0]) ? $info[0] : '')->options($info);
  88. break;
  89. case 'select':
  90. $f[] = Form::select($value["title"], $value["name"], isset($info[0]) ? $info[0] : '')->options($info)->multiple(false);
  91. break;
  92. case 'upload':
  93. $f[] = Form::frameImageOne($value["title"], $value["name"], Url::build('admin/widget.images/index', array('fodder' => $value["title"])))->icon('image')->width('100%')->height('500px');
  94. break;
  95. case 'uploads':
  96. $f[] = Form::frameImages($value["title"], $value["name"], Url::build('admin/widget.images/index', array('fodder' => $value["title"])))->maxLength(5)->icon('images')->width('100%')->height('500px')->spin(0);
  97. break;
  98. default:
  99. $f[] = Form::input($value["title"], $value["name"]);
  100. break;
  101. }
  102. }
  103. $f[] = Form::number('sort', '排序', 1);
  104. $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  105. $form = Form::make_post_form('添加数据', $f, Url::build('save', compact('gid')), 2);
  106. $this->assign(compact('form'));
  107. return $this->fetch('public/form-builder');
  108. }
  109. /**
  110. * 保存新建的资源
  111. *
  112. * @param \think\Request $request
  113. * @return \think\Response
  114. */
  115. public function save(Request $request, $gid)
  116. {
  117. $Fields = GroupModel::getField($gid);
  118. $params = $request->post();
  119. $value = array();
  120. foreach ($params as $key => $param) {
  121. foreach ($Fields['fields'] as $index => $field) {
  122. if ($key == $field["title"]) {
  123. if($param == "")
  124. return Json::fail($field["name"]."不能为空!");
  125. else {
  126. $value[$key]["type"] = $field["type"];
  127. $value[$key]["value"] = $param;
  128. }
  129. }
  130. }
  131. }
  132. $data = array("gid" => $gid, "add_time" => time(), "value" => json_encode($value), "sort" => $params["sort"], "status" => $params["status"]);
  133. GroupDataModel::set($data);
  134. return Json::successful('添加数据成功!');
  135. }
  136. /**
  137. * 显示指定的资源
  138. *
  139. * @param int $id
  140. * @return \think\Response
  141. */
  142. public function read($id)
  143. {
  144. }
  145. /**
  146. * 显示编辑资源表单页.
  147. *
  148. * @param int $id
  149. * @return \think\Response
  150. */
  151. public function edit($gid, $id)
  152. {
  153. $GroupData = GroupDataModel::get($id);
  154. $GroupDataValue = json_decode($GroupData["value"], true);
  155. $Fields = GroupModel::getField($gid);
  156. $f = array();
  157. foreach ($Fields['fields'] as $key => $value) {
  158. $info = [];
  159. if (!empty($value["param"])) {
  160. $value["param"] = str_replace("\r\n", "\n", $value["param"]);//防止不兼容
  161. $params = explode("\n", $value["param"]);
  162. if (is_array($params) && !empty($params)) {
  163. foreach ($params as $index => $v) {
  164. if (strstr($v, '=>') !== false) {
  165. list($left, $right) = explode('=>', $v);
  166. } else if (strstr($v, '=') !== false) {
  167. list($left, $right) = explode('=', $v);
  168. }
  169. $val["value"] = $left;
  170. $val["label"] = $right;
  171. $info[] = $val;
  172. }
  173. }
  174. }
  175. switch ($value['type']) {
  176. case 'input':
  177. $f[] = Form::input($value['title'], $value['name'], $GroupDataValue[$value['title']]['value']);
  178. break;
  179. case 'textarea':
  180. $f[] = Form::input($value['title'], $value['name'], $GroupDataValue[$value['title']]['value'])->type('textarea');
  181. break;
  182. case 'radio':
  183. $f[] = Form::radio($value['title'], $value['name'], $GroupDataValue[$value['title']]['value'])->options($info);
  184. break;
  185. case 'checkbox':
  186. if(array_key_exists($value['title'],$GroupDataValue)){
  187. $f[] = Form::checkbox($value['title'], $value['name'], $GroupDataValue[$value['title']]['value'])->options($info);
  188. }else{
  189. $f[] = Form::checkbox($value["title"], $value["name"], isset($info[0]) ? $info[0] : '')->options($info);
  190. }
  191. break;
  192. case 'upload':
  193. if (!empty($GroupDataValue[$value['title']]['value'])) {
  194. $image = is_string($GroupDataValue[$value['title']]['value']) ? $GroupDataValue[$value['title']]['value'] : $GroupDataValue[$value['title']]['value'][0];
  195. } else {
  196. $image = '';
  197. }
  198. $f[] = Form::frameImageOne($value['title'], $value['name'], Url::build('admin/widget.images/index', array('fodder' => $value['title'])), $image)->icon('image')->width('100%')->height('500px');
  199. break;
  200. case 'uploads':
  201. $images = !empty($GroupDataValue[$value['title']]['value']) ? $GroupDataValue[$value['title']]['value'] : [];
  202. $f[] = Form::frameImages($value['title'], $value['name'], Url::build('admin/widget.images/index', array('fodder' => $value['title'])), $images)->maxLength(5)->icon('images')->width('100%')->height('550px')->spin(0);
  203. break;
  204. case 'select':
  205. $f[] = Form::select($value['title'], $value['name'], $GroupDataValue[$value['title']]['value'])->setOptions($info);
  206. break;
  207. default:
  208. $f[] = Form::input($value['title'], $value['name'], $GroupDataValue[$value['title']]['value']);
  209. break;
  210. }
  211. }
  212. $f[] = Form::input('sort', '排序', $GroupData["sort"]);
  213. $f[] = Form::radio('status', '状态', $GroupData["status"])->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  214. $form = Form::make_post_form('编辑', $f, Url::build('update', compact('id')), 2);
  215. $this->assign(compact('form'));
  216. return $this->fetch('public/form-builder');
  217. }
  218. /**
  219. * 保存更新的资源
  220. *
  221. * @param \think\Request $request
  222. * @param int $id
  223. * @return \think\Response
  224. */
  225. public function update(Request $request, $id)
  226. {
  227. $GroupData = GroupDataModel::get($id);
  228. $Fields = GroupModel::getField($GroupData["gid"]);
  229. $params = $request->post();
  230. foreach ($params as $key => $param) {
  231. foreach ($Fields['fields'] as $index => $field) {
  232. if ($key == $field["title"]) {
  233. if ($param == "" || !$param || empty($param))
  234. return Json::fail($field["name"] . "不能为空!");
  235. else {
  236. $value[$key]["type"] = $field["type"];
  237. $value[$key]["value"] = $param;
  238. }
  239. }
  240. }
  241. }
  242. $data = array("value" => json_encode($value), "sort" => $params["sort"], "status" => $params["status"]);
  243. GroupDataModel::edit($data, $id);
  244. return Json::successful('修改成功!');
  245. }
  246. /**
  247. * 删除指定资源
  248. *
  249. * @param int $id
  250. * @return \think\Response
  251. */
  252. public function delete($id)
  253. {
  254. if (!GroupDataModel::del($id))
  255. return Json::fail(GroupDataModel::getErrorInfo('删除失败,请稍候再试!'));
  256. else
  257. return Json::successful('删除成功!');
  258. }
  259. public function upload()
  260. {
  261. $res = Upload::image('file', 'common');
  262. $thumbPath = Upload::thumb($res->dir);
  263. //产品图片上传记录
  264. $fileInfo = $res->fileInfo->getinfo();
  265. SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(), $fileInfo['size'], $fileInfo['type'], $res->dir, $thumbPath, 6);
  266. if ($res->status == 200)
  267. return Json::successful('图片上传成功!', ['name' => $res->fileInfo->getSaveName(), 'url' => Upload::pathToUrl($thumbPath)]);
  268. else
  269. return Json::fail($res->error);
  270. }
  271. public function recommend()
  272. {
  273. $this->assign('fixedList', Recommend::fixedList());
  274. return $this->fetch();
  275. }
  276. public function recommend_list()
  277. {
  278. $where = parent::getMore([
  279. ['page', 1],
  280. ['limit', 20],
  281. ['order', ''],
  282. ['is_fixed', $this->request->param('is_fixed', 0)]
  283. ]);
  284. return Json::successlayui(Recommend::getRecommendList($where));
  285. }
  286. public function create_recemmend($id = 0)
  287. {
  288. if ($id) $this->assign('recemmend', Recommend::get($id));
  289. $this->assign('is_fixed', 1);
  290. $this->assign('grade_list', Grade::getAll());
  291. $this->assign('id', $id);
  292. return $this->fetch();
  293. }
  294. public function create_recemmend_v1($id = 0)
  295. {
  296. if ($id) $this->assign('recemmend', Recommend::get($id));
  297. $this->assign('is_fixed', 0);
  298. $this->assign('grade_list', Grade::getAll());
  299. $this->assign('is_fixed', 0);
  300. $this->assign('id', $id);
  301. return $this->fetch();
  302. }
  303. public function save_recemmend($id = 0)
  304. {
  305. $post = parent::postMore([
  306. ['icon', ''],
  307. ['image', ''],
  308. ['title', ''],
  309. ['type', ''],
  310. ['sort', 0],
  311. ['is_fixed', 0],
  312. ['is_show', 0],
  313. ['grade_id', 0],
  314. ['show_count', 0],
  315. ['typesetting', ''],
  316. ]);
  317. if ($id) {
  318. $post['is_show'] = $post['is_fixed'] ? 1 : $post['is_show'];
  319. $rescomm = Recommend::get($id);
  320. if (!$rescomm) return Json::fail('修改的信息不存在');
  321. if($post['typesetting']==5){
  322. unset($post['type'],$post['icon'],$post['image'],$post['is_fixed'],$post['is_show'],$post['grade_id'],$post['show_count']);
  323. }
  324. Recommend::update($post, ['id' => $id]);
  325. return Json::successful('修改成功');
  326. } else {
  327. $post['add_time'] = time();
  328. if (Recommend::set($post))
  329. return Json::successful('保存成功');
  330. else
  331. return Json::fail('保存失败');
  332. }
  333. }
  334. public function recemmend_content($id = 0)
  335. {
  336. if (!$id) return Json::fail('缺少参数');
  337. if ($this->request->isAjax()) {
  338. $where = parent::getMore([
  339. ['page', 1],
  340. ['limit', 20],
  341. ]);
  342. return Json::successlayui(RecommendRelation::getAll($where, $id));
  343. } else {
  344. $this->assign('id', $id);
  345. return $this->fetch();
  346. }
  347. }
  348. public function recemmed_delete($id = 0)
  349. {
  350. if (!$id) return Json::fail('缺少参数');
  351. if (RecommendRelation::del($id))
  352. return Json::successful('删除成功');
  353. else
  354. return Json::fail('删除失败');
  355. }
  356. /**
  357. * 设置单个产品上架|下架
  358. *
  359. * @return json
  360. */
  361. public function set_show($is_show = '', $id = '')
  362. {
  363. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  364. $res = Recommend::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  365. if ($res) {
  366. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  367. } else {
  368. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  369. }
  370. }
  371. public function set_show_banner($is_show = '', $id = '')
  372. {
  373. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  374. $res = RecommendBanner::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  375. if ($res) {
  376. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  377. } else {
  378. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  379. }
  380. }
  381. /**
  382. * 快速编辑
  383. *
  384. * @return json
  385. */
  386. public function set_value($field = '', $id = '', $value = '')
  387. {
  388. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  389. if (Recommend::where(['id' => $id])->update([$field => $value]))
  390. return Json::successful('保存成功');
  391. else
  392. return Json::fail('保存失败');
  393. }
  394. /**
  395. * 快速编辑
  396. *
  397. * @return json
  398. */
  399. public function set_recemmend_value($field = '', $id = '', $value = '')
  400. {
  401. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  402. if(bcsub($value,0,0)<0) Json::fail('不能为负数');
  403. if (RecommendRelation::where(['id' => $id])->update([$field => $value]))
  404. return Json::successful('保存成功');
  405. else
  406. return Json::fail('保存失败');
  407. }
  408. public function set_value_banner($field = '', $id = '', $value = '')
  409. {
  410. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  411. if (RecommendBanner::where(['id' => $id])->update([$field => $value]))
  412. return Json::successful('保存成功');
  413. else
  414. return Json::fail('保存失败');
  415. }
  416. /**
  417. * 删除指定资源
  418. *
  419. * @param int $id
  420. * @return \think\Response
  421. */
  422. public function delete_recomm($id)
  423. {
  424. if (RecommendBanner::be(['recommend_id' => $id])) return Json::fail('删除失败,请先删除Banner图');
  425. if (RecommendRelation::be(['recommend_id' => $id])) return Json::fail('删除失败,请先删除内容管理里面的列表');
  426. if (!Recommend::del($id))
  427. return Json::fail('删除失败');
  428. else
  429. return Json::successful('删除成功!');
  430. }
  431. /**
  432. * 删除导航推荐
  433. * @param string $id
  434. */
  435. public function delete_banner($id = '')
  436. {
  437. if (!RecommendBanner::del($id))
  438. return Json::fail('删除失败');
  439. else
  440. return Json::successful('删除成功!');
  441. }
  442. public function recemmend_banner($id = '')
  443. {
  444. if ($id == '') return $this->failed('缺少参数');
  445. $this->assign('id', $id);
  446. $this->assign('type', 1);
  447. return $this->fetch();
  448. }
  449. public function recemmend_banner_list()
  450. {
  451. $where = parent::getMore([
  452. ['id', ''],
  453. ['page', ''],
  454. ['limit', ''],
  455. ]);
  456. if ($where['id'] == '') return Json::fail('缺少参数');
  457. return Json::successlayui(RecommendBanner::getRecemmodBannerList($where));
  458. }
  459. /*
  460. * 创建banner图
  461. * */
  462. public function create_recemmend_banner($id = '', $banner_id = 0)
  463. {
  464. $this->assign('id', $id);
  465. if ($banner_id) {
  466. $banner = RecommendBanner::get($banner_id);
  467. if (!$banner) return $this->failed('缺少修改的banner');
  468. $banner['pic_key'] = get_key_attr($banner['pic'], false);
  469. $this->assign('banner', $banner);
  470. }
  471. $this->assign('banner_id', (int)$banner_id);
  472. $this->assign('type', 2);
  473. return $this->fetch();
  474. }
  475. public function save_recemmend_banner($id = '', $banner_id = '')
  476. {
  477. $post = parent::postMore([
  478. ['url', ''],
  479. ['sort', ''],
  480. ['is_show', 0],
  481. ['pic', ''],
  482. ]);
  483. if ($id == '') return Json::fail('缺少参数');
  484. if ($post['pic'] == '') return Json::fail('请上传封面图!');
  485. if ($post['is_show'] == 'on') $post['is_show'] = 1;
  486. else $post['is_show'] = 0;
  487. $post['recommend_id'] = $id;
  488. if ($banner_id) {
  489. RecommendBanner::edit($post, $banner_id);
  490. return Json::successful('修改成功');
  491. } else {
  492. $post['add_time'] = time();
  493. RecommendBanner::set($post);
  494. return Json::successful('保存成功');
  495. }
  496. }
  497. /**
  498. * 首页导航固定跳转添加
  499. * @return mixed
  500. */
  501. public function navigation()
  502. {
  503. return $this->fetch();
  504. }
  505. /**
  506. * 自定义跳转导航添加和修改页面
  507. * @param int $id
  508. * @return mixed|void
  509. * @throws \FormBuilder\exception\FormBuilderException
  510. * @throws \think\exception\DbException
  511. */
  512. public function create_recemmend_custom($id = 0)
  513. {
  514. if ($id) {
  515. $recommend = Recommend::get($id);
  516. if (!$recommend) {
  517. return $this->failed('您修改的导航不存在');
  518. }
  519. }
  520. $f[] = Form::input('title', '导航名称', isset($recommend) ? $recommend->title : '');
  521. $f[] = Form::frameImageOne('icon', '图标(100*100px)', get_image_Url('icon'), isset($recommend) ? $recommend->icon : '')->icon('image')->width('100%')->height('500px');
  522. $f[] = Form::input('link', '跳转路径', isset($recommend) ? $recommend->link : '');
  523. $f[] = Form::input('sort', '排序', isset($recommend) ? $recommend->sort : 0);
  524. $f[] = Form::radio('is_show', '状态', isset($recommend) ? $recommend->is_show : 0)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  525. $form = Form::make_post_form('编辑', $f, Url::build('save_recemmend_custom', compact('id')), 2);
  526. $this->assign(compact('form'));
  527. return $this->fetch('public/form-builder');
  528. }
  529. /**
  530. * 保存自定义导航链接
  531. * @param int $id
  532. */
  533. public function save_recemmend_custom($id = 0)
  534. {
  535. $data = parent::postMore([
  536. ['title', ''],
  537. ['icon', ''],
  538. ['link', ''],
  539. ['sort', 0],
  540. ['is_show', 0],
  541. ['type', 3],
  542. ['is_fixed', 1],
  543. ]);
  544. if (!$data['title']) {
  545. return Json::fail('请填写导航名称');
  546. }
  547. if (!$data['icon']) {
  548. return Json::fail('请选择导航图标');
  549. }
  550. if (!$data['link']) {
  551. return Json::fail('请填写导航跳转地址');
  552. }
  553. if ($id) {
  554. Recommend::where('id', $id)->update($data);
  555. return Json::successful('修改成功');
  556. } else {
  557. $data['add_time'] = time();
  558. $res = Recommend::set($data);
  559. if ($res) {
  560. return Json::successful('添加成功');
  561. } else {
  562. return Json::fail('修改失败');
  563. }
  564. }
  565. }
  566. /**
  567. * 显示资源列表
  568. * @return \think\Response
  569. */
  570. public function index_v1($gid)
  571. {
  572. $this->assign(compact("gid"));
  573. return $this->fetch();
  574. }
  575. /**
  576. * 获取某个组合数据列表
  577. * @param int $gid
  578. * @param int $page
  579. * @param int $limit
  580. * @throws \think\Exception
  581. */
  582. public function get_group_data_list($gid = 0, $status = '', $page = 1, $limit = 10)
  583. {
  584. $model = GroupDataModel::where(function ($query) use ($gid, $status) {
  585. $query->where('gid', $gid);
  586. if ($status != '') {
  587. $query->where('status', $status);
  588. }
  589. });
  590. $data = $model->order('sort desc,id desc')->page($page, $limit)->select();
  591. $data = count($data) ? $data->toArray() : [];
  592. foreach ($data as &$item) {
  593. $value = json_decode($item['value'], true);
  594. foreach ($value as $key => $val) {
  595. $item[$key] = $value[$key]['value'];
  596. }
  597. }
  598. $count = $model->count();
  599. return Json::successlayui(compact('data', 'count'));
  600. }
  601. /**
  602. * 修改某个字段
  603. * @param string $field
  604. * @param int $id
  605. * @param string $value
  606. */
  607. public function set_group_data($field = '', $id = 0, $value = '')
  608. {
  609. if ('id' == $field) {
  610. return Json::fail('修改失败,主键不允许修改');
  611. }
  612. if (!$field && !$value) {
  613. return Json::fail('缺少修改参数');
  614. }
  615. $info = GroupDataModel::where('id', $id)->find();
  616. if (!$info) {
  617. return Json::fail('修改的信息不存在');
  618. }
  619. if (in_array($field, ['sort', 'status'])) {
  620. $info->{$field} = $value;
  621. $res = $info->save();
  622. } else {
  623. $infoVale = json_decode($info->value, true);
  624. $infoVale[$field]['value'] = $value;
  625. $info->value = json_encode($infoVale);
  626. $res = $info->save();
  627. }
  628. if ($res) {
  629. return Json::successful('修改成功');
  630. } else {
  631. return Json::fail('修改失败');
  632. }
  633. }
  634. /**
  635. * 添加组合数据页面
  636. * @return mixed
  637. */
  638. public function create_v1($id = 0)
  639. {
  640. $this->assign([
  641. 'specialList' => json_encode(Special::PreWhere()->field(['id', 'title'])->order('sort desc,id desc')->select()),
  642. 'cateList' => json_encode(Grade::where(['is_del' => 0,'is_show'=>1])->field(['id', 'name as title'])->order('sort desc,id desc')->select()),
  643. ]);
  644. if ($id) {
  645. $info = GroupDataModel::get($id);
  646. if ($info) {
  647. $infoValue = json_decode($info->value, true);
  648. $this->assign('data', [
  649. 'title' => isset($infoValue['title']['value']) ? $infoValue['title']['value'] : "",
  650. 'pic' => isset($infoValue['pic']['value']) ? $infoValue['pic']['value'] : "",
  651. 'info' => isset($infoValue['info']['value']) ? $infoValue['info']['value'] : '',
  652. 'sort' => $info->sort,
  653. 'status' => $info->status,
  654. 'type' => isset($infoValue['type']['value']) ? $infoValue['type']['value'] : "''",
  655. 'select_id' => isset($infoValue['select_id']['value']) ? $infoValue['select_id']['value'] : "''",
  656. 'id' => $id
  657. ]);
  658. }
  659. }
  660. return $this->fetch();
  661. }
  662. public function save_group_data($name = '')
  663. {
  664. $data = parent::postMore([
  665. ['title', ''],
  666. ['id', ''],
  667. ['image', ''],
  668. ['info', ''],
  669. ['type', 0],
  670. ['select_id', 0],
  671. ['sort', 0],
  672. ['status', 0],
  673. ]);
  674. $gid = GroupModel::where(['config_name' => $name])->value('id');
  675. if (!isset($data['id']) || !$data['id']) {
  676. if (GroupDataModel::where('gid', $gid)->count() >= 3) {
  677. return Json::fail('最多能添加3条信息');
  678. }
  679. }
  680. if (!$data['title']) {
  681. return Json::fail('请输入标题');
  682. }
  683. if (!$data['image']) {
  684. return Json::fail('请选择图片');
  685. }
  686. if (!$data['info']) {
  687. return Json::fail('请输入简介');
  688. }
  689. if (!$data['select_id']) {
  690. return Json::fail('请选择' . ($data['type'] ? '分类' : "专题"));
  691. }
  692. $info = '{"pic":{"type":"upload","value":""},"title":{"type":"input","value":""},"info":{"type":"input","value":""},"wap_link":{"type":"select","value":""}}';
  693. $info = json_decode($info, true);
  694. $info['pic']['value'] = $data['image'];
  695. $info['title']['value'] = $data['title'];
  696. $info['info']['value'] = $data['info'];
  697. $info['select_id']['value'] = $data['select_id'];
  698. $info['select_id']['type'] = 'select';
  699. $info['type']['type'] = 'radio';
  700. $info['type']['value'] = $data['type'];
  701. if ($data['type']) {
  702. $info['wap_link']['value'] = '/wap/special/special_cate?cate_id=' . $data['select_id'];
  703. } else {
  704. $info['wap_link']['value'] = '/wap/special/details?id=' . $data['select_id'];
  705. }
  706. if (isset($data['id']) && $data['id']) {
  707. $res = GroupDataModel::update([
  708. 'gid' => $gid,
  709. 'value' => json_encode($info),
  710. 'sort' => $data['sort'],
  711. 'status' => $data['status'],
  712. ],['id' => $data['id']]);
  713. }else{
  714. $res = GroupDataModel::set([
  715. 'gid' => $gid,
  716. 'value' => json_encode($info),
  717. 'add_time' => time(),
  718. 'sort' => $data['sort'],
  719. 'status' => $data['status'],
  720. ]);
  721. }
  722. if ($res) {
  723. return Json::successful('编辑成功');
  724. } else {
  725. return Json::fail('编辑失败');
  726. }
  727. }
  728. }