ArticleV1.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\article;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\article\ArticleCategory as ArticleCategoryModel;
  14. use app\admin\model\article\ArticleContent;
  15. use service\JsonService as Json;
  16. use service\UploadService as Upload;
  17. use think\Request;
  18. use app\admin\model\article\Article;
  19. use app\admin\model\system\Recommend;
  20. use app\admin\model\system\WebRecommend;
  21. use app\admin\model\system\RecommendRelation;
  22. use app\admin\model\system\WebRecommendRelation;
  23. use think\Url;
  24. use service\FormBuilder as Form;
  25. use app\admin\model\wechat\WechatNewsCategory as WechatNewsCategoryModel;
  26. /**
  27. * 图文管理
  28. * Class WechatNews
  29. * @package app\admin\controller\wechat
  30. */
  31. class ArticleV1 extends AuthController
  32. {
  33. public function index($cid = 0)
  34. {
  35. $this->assign('cid', $cid);
  36. $this->assign('cate', ArticleCategoryModel::getTierList());
  37. $this->assign('type', $this->request->param('type', 1));
  38. return $this->fetch();
  39. }
  40. /**
  41. * 新闻列表
  42. */
  43. public function article_list($c_id)
  44. {
  45. $where = parent::getMore([
  46. ['limit', 20],
  47. ['page', 1],
  48. ['cid', 0],
  49. ['store_name', ''],
  50. ['order', ''],
  51. ['is_show', ''],
  52. ]);
  53. if (!$where['cid'] && $c_id) $where['cid'] = $c_id;
  54. return Json::successlayui(Article::getArticleLayList($where));
  55. }
  56. /**
  57. * 设置单个产品上架|下架
  58. *
  59. * @return json
  60. */
  61. public function set_show($is_show = '', $id = '')
  62. {
  63. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  64. $res = parent::getDataModification('article', $id, 'is_show', (int)$is_show);
  65. if ($res) {
  66. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  67. } else {
  68. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  69. }
  70. }
  71. /**
  72. * 快速编辑
  73. *
  74. * @return json
  75. */
  76. public function set_value($field = '', $id = '', $value = '')
  77. {
  78. ($field == '' || $id == '' || $value == '') && Json::fail('缺少参数');
  79. $res = parent::getDataModification('article', $id, $field, $value);
  80. if ($res)
  81. return Json::successful('保存成功');
  82. else
  83. return Json::fail('保存失败');
  84. }
  85. public function add_article($id = 0)
  86. {
  87. $this->assign('id', $id);
  88. if ($id) {
  89. $article = Article::get($id);
  90. $article->profile->content = htmlspecialchars_decode($article->profile->content);
  91. $this->assign('article', $article->toJson());
  92. }
  93. if (empty($all)) {
  94. $list = ArticleCategoryModel::getTierList();
  95. $all = [];
  96. foreach ($list as $menu) {
  97. $all[$menu['id']] = $menu['html'] . $menu['title'];
  98. }
  99. }
  100. $this->assign('all', json_encode($all));
  101. $this->assign('type', $this->request->param('type', 2));
  102. return $this->fetch();
  103. }
  104. public function save_article($id = 0)
  105. {
  106. $data = parent::postMore([
  107. ['title', ''],
  108. ['synopsis', ''],
  109. ['sort', 0],
  110. ['cid', 0],
  111. ['content', ''],
  112. ['image_input', ''],
  113. ['label', []],
  114. ]);
  115. if (!$data['title']) return Json::fail('请输入图文标题');
  116. if (!$data['synopsis']) return Json::fail('请输入图文简介');
  117. if (!$data['content']) return Json::fail('请输入图文内容');
  118. $data['label'] = json_encode($data['label']);
  119. $content = htmlspecialchars($data['content']);
  120. Article::beginTrans();
  121. try {
  122. if ($id) {
  123. Article::edit($data, $id, 'id');
  124. ArticleContent::edit(['content' => $content], $id, 'nid');
  125. Article::commitTrans();
  126. return Json::successful('修改成功');
  127. } else {
  128. $data['add_time'] = time();
  129. $data['is_show'] = 1;
  130. $data['visit'] = 0;
  131. $res1 = Article::set($data);
  132. $res2 = ArticleContent::set(['nid' => Article::getLastInsID(), 'content' => $content]);
  133. if ($res1 && $res2) {
  134. Article::commitTrans();
  135. return Json::successful('添加成功');
  136. } else {
  137. Article::rollbackTrans();
  138. return Json::fail('添加失败');
  139. }
  140. }
  141. } catch (\Exception $e) {
  142. Article::rollbackTrans();
  143. return Json::fail($e->getMessage());
  144. }
  145. }
  146. public function delete($id = 0)
  147. {
  148. if (!$id) return Json::fail('缺少参数');
  149. $article = Article::get($id);
  150. if (!$article) return Json::fail('没有查找到图文');
  151. Article::beginTrans();
  152. try {
  153. $res = $article->delete();
  154. $res1 = ArticleContent::where('nid', $id)->delete();
  155. if ($res1 && $res) {
  156. if ($article['hide'] == 1) {
  157. WechatNewsCategoryModel::where('new_id', $id)->delete();
  158. }
  159. Article::commitTrans();
  160. return Json::successful('删除成功');
  161. } else {
  162. Article::rollbackTrans();
  163. return Json::fail('删除失败');
  164. }
  165. } catch (\Exception $e) {
  166. Article::rollbackTrans();
  167. return Json::fail($e->getMessage());
  168. }
  169. }
  170. /**
  171. * 添加推荐
  172. * */
  173. public function recommend($article_id = 0)
  174. {
  175. if (!$article_id) $this->failed('缺少参数');
  176. $article = Article::get($article_id);
  177. if (!$article) $this->failed('没有查到此新闻');
  178. $form = Form::create(Url::build('save_recommend', ['article_id' => $article_id]), [
  179. Form::select('recommend_id', '推荐')->setOptions(function () {
  180. $list = Recommend::where(['is_show' => 1, 'type' => 1])->field('title,id')->order('sort desc,add_time desc')->select();
  181. $menus = [];
  182. foreach ($list as $menu) {
  183. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  184. }
  185. return $menus;
  186. })->filterable(1),
  187. Form::number('sort', '排序'),
  188. ]);
  189. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  190. $this->assign(compact('form'));
  191. return $this->fetch('public/form-builder');
  192. }
  193. /**
  194. * 保存推荐
  195. * */
  196. public function save_recommend($article_id = 0)
  197. {
  198. if (!$article_id) $this->failed('缺少参数');
  199. $data = parent::postMore([
  200. ['recommend_id', 0],
  201. ['sort', 0],
  202. ]);
  203. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  204. $recommend = Recommend::get($data['recommend_id']);
  205. if (!$recommend) return Json::fail('导航菜单不存在');
  206. $data['add_time'] = time();
  207. $data['type'] = 1;
  208. $data['link_id'] = $article_id;
  209. if (RecommendRelation::be(['type' => 1, 'link_id' => $article_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  210. if (RecommendRelation::set($data))
  211. return Json::successful('推荐成功');
  212. else
  213. return Json::fail('推荐失败');
  214. }
  215. /**取消推荐
  216. * @param int $id
  217. */
  218. public function cancel_recommendation($id = 0, $article_id = 0)
  219. {
  220. if (!$id || !$article_id) $this->failed('缺少参数');
  221. if (RecommendRelation::be(['id' => $id, 'link_id' => $article_id])) {
  222. $res = RecommendRelation::where(['id' => $id, 'link_id' => $article_id])->delete();
  223. if ($res)
  224. return Json::successful('取消推荐成功');
  225. else
  226. return Json::fail('取消推荐失败');
  227. } else {
  228. return Json::fail('推荐不存在');
  229. }
  230. }
  231. /**
  232. * 添加推荐
  233. * @param int $special_id
  234. * @return mixed
  235. * @throws \think\exception\DbException
  236. */
  237. public function web_recommend($article_id = 0)
  238. {
  239. if (!$article_id) $this->failed('缺少参数');
  240. $article = Article::get($article_id);
  241. if (!$article) $this->failed('没有查到此新闻');
  242. $form = Form::create(Url::build('save_web_recommend', ['article_id' => $article_id]), [
  243. Form::select('recommend_id', '推荐')->setOptions(function () {
  244. $model = WebRecommend::where(['is_show' => 1, 'type' => 4]);
  245. $list = $model->field('title,id')->order('sort desc,add_time desc')->select();
  246. $menus = [];
  247. foreach ($list as $menu) {
  248. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  249. }
  250. return $menus;
  251. })->filterable(1),
  252. Form::number('sort', '排序'),
  253. ]);
  254. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  255. $this->assign(compact('form'));
  256. return $this->fetch('public/form-builder');
  257. }
  258. /**
  259. * 保存推荐
  260. * @param int $special_id
  261. * @throws \think\exception\DbException
  262. */
  263. public function save_web_recommend($article_id = 0)
  264. {
  265. if (!$article_id) $this->failed('缺少参数');
  266. $data = parent::postMore([
  267. ['recommend_id', 0],
  268. ['sort', 0],
  269. ]);
  270. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  271. $recommend = WebRecommend::get($data['recommend_id']);
  272. if (!$recommend) return Json::fail('导航菜单不存在');
  273. $data['add_time'] = time();
  274. $data['type'] = $recommend->type;
  275. $data['link_id'] = $article_id;
  276. if (WebRecommendRelation::be(['type' => $recommend->type, 'link_id' => $article_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  277. if (WebRecommendRelation::set($data))
  278. return Json::successful('推荐成功');
  279. else
  280. return Json::fail('推荐失败');
  281. }
  282. /**取消推荐
  283. * @param int $id
  284. */
  285. public function cancel_web_recommendation($id = 0, $article_id = 0)
  286. {
  287. if (!$id || !$article_id) return Json::fail('缺少参数');
  288. if (WebRecommendRelation::be(['id' => $id, 'link_id' => $article_id])) {
  289. $res = WebRecommendRelation::where(['id' => $id, 'link_id' => $article_id])->delete();
  290. if ($res)
  291. return Json::successful('取消推荐成功');
  292. else
  293. return Json::fail('取消推荐失败');
  294. } else {
  295. return Json::fail('推荐不存在');
  296. }
  297. }
  298. }