StoreInfoMana.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemAttachment;
  5. use think\facade\Route as Url;
  6. use app\admin\model\wechat\WechatNews as WechatNewsModel;
  7. use app\admin\model\article\ArticleCategory as ArticleCategoryModel;
  8. use crmeb\services\{FormBuilder, UtilService as Util, JsonService as Json};
  9. /**
  10. * 资讯管理 控制器
  11. * Class StoreInfoMana
  12. * @package app\admin\controller\store
  13. */
  14. class StoreInfoMana extends AuthController
  15. {
  16. /**
  17. * 新闻管理
  18. * */
  19. public function index($is_list = 0)
  20. {
  21. if (!$is_list) return $this->failed('数据不存在');
  22. echo $is_list;
  23. exit();
  24. $where = Util::getMore([
  25. ['status', ''],
  26. ['title', ''],
  27. ], $this->request);
  28. $this->assign('where', $where);
  29. $this->assign(ArticleCategoryModel::systemPage($where));
  30. return $this->fetch();
  31. }
  32. /**
  33. * 添加分类管理
  34. * */
  35. public function create()
  36. {
  37. FormBuilder::text('title', '分类昵称');
  38. FormBuilder::textarea('intr', '分类简介');
  39. FormBuilder::select('new_id', '图文列表', function () {
  40. $list = \app\admin\model\wechat\WechatNews::getNews();
  41. $options = [];
  42. foreach ($list as $id => $roleName) {
  43. $options[] = ['label' => $roleName, 'value' => $id];
  44. }
  45. return $options;
  46. })->multiple()->filterable();
  47. FormBuilder::frameImageOne('image', '分类图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
  48. FormBuilder::number('sort', '排序', 0);
  49. FormBuilder::radio('status', '状态', [['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']], 1);
  50. $rules = FormBuilder::builder()->getContent();
  51. $this->assign(['title' => '编辑菜单', 'rules' => $rules, 'save' => Url::buildUrl('save')]);
  52. return $this->fetch();
  53. }
  54. /**
  55. * 保存分类管理
  56. * */
  57. public function save()
  58. {
  59. $data = Util::postMore([
  60. 'title',
  61. 'intr',
  62. ['new_id', []],
  63. ['image', []],
  64. ['sort', 0],
  65. 'status',]);
  66. if (!$data['title']) return Json::fail('请输入分类名称');
  67. if (count($data['image']) != 1) return Json::fail('请选择分类图片,并且只能上传一张');
  68. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  69. $data['add_time'] = time();
  70. $data['image'] = $data['image'][0];
  71. $new_id = $data['new_id'];
  72. unset($data['new_id']);
  73. $res = ArticleCategoryModel::create($data);
  74. if (!WechatNewsModel::saveBatchCid($res['id'], implode(',', $new_id))) return Json::fail('文章列表添加失败');
  75. return Json::successful('添加分类成功!');
  76. }
  77. /**
  78. * 修改分类
  79. * */
  80. public function edit($id)
  81. {
  82. $this->assign(['title' => '编辑菜单', 'read' => Url::buildUrl('read', array('id' => $id)), 'update' => Url::buildUrl('update', array('id' => $id))]);
  83. return $this->fetch();
  84. }
  85. public function read($id)
  86. {
  87. $article = ArticleCategoryModel::get($id)->getData();
  88. if (!$article) return Json::fail('数据不存在!');
  89. FormBuilder::text('title', '分类昵称', $article['title']);
  90. FormBuilder::textarea('intr', '分类简介', $article['intr']);
  91. $arr = ArticleCategoryModel::getArticle($id, 'id,title,cid');//子文章
  92. $new_id = array();
  93. foreach ($arr as $k => $v) {
  94. $new_id[$k] = $k;
  95. }
  96. FormBuilder::select('new_id', '文章列表', function () {
  97. $list = \app\admin\model\wechat\WechatNews::getNews();
  98. $options = [];
  99. foreach ($list as $id => $roleName) {
  100. $options[] = ['label' => $roleName, 'value' => $id];
  101. }
  102. return $options;
  103. }, $new_id)->multiple();
  104. FormBuilder::upload('image', '分类图片')->defaultFileList($article['image']);
  105. FormBuilder::number('sort', '排序', $article['sort']);
  106. FormBuilder::radio('status', '状态', [['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']], $article['status']);
  107. return FormBuilder::builder();
  108. }
  109. public function update($id)
  110. {
  111. $data = Util::postMore([
  112. 'title',
  113. 'intr',
  114. ['new_id', []],
  115. ['image', []],
  116. ['sort', 0],
  117. 'status',]);
  118. if (!$data['title']) return Json::fail('请输入分类名称');
  119. if (count($data['image']) != 1) return Json::fail('请选择分类图片,并且只能上传一张');
  120. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  121. $data['image'] = $data['image'][0];
  122. // dump($data);
  123. // exit;
  124. if (!ArticleCategoryModel::get($id)) return Json::fail('编辑的记录不存在!');
  125. if (!WechatNewsModel::saveBatchCid($id, implode(',', $data['new_id']))) return Json::fail('文章列表添加失败');
  126. unset($data['new_id']);
  127. ArticleCategoryModel::edit($data, $id);
  128. return Json::successful('修改成功!');
  129. }
  130. /**
  131. * 删除分类
  132. * */
  133. public function delete($id)
  134. {
  135. $res = ArticleCategoryModel::delArticleCategory($id);
  136. if (!$res)
  137. return Json::fail(ArticleCategoryModel::getErrorInfo('删除失败,请稍候再试!'));
  138. else
  139. return Json::successful('删除成功!');
  140. }
  141. }