ArticleV1.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\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\RecommendRelation;
  21. use think\Url;
  22. use service\FormBuilder as Form;
  23. use app\admin\model\wechat\WechatNewsCategory as WechatNewsCategoryModel;
  24. /**
  25. * 图文管理
  26. * Class WechatNews
  27. * @package app\admin\controller\wechat
  28. */
  29. class ArticleV1 extends AuthController
  30. {
  31. public function index($cid = 0)
  32. {
  33. $this->assign('cid',$cid);
  34. $this->assign('cate',ArticleCategoryModel::getTierList());
  35. $this->assign('type', $this->request->param('type', 1));
  36. return $this->fetch();
  37. }
  38. public function article_list()
  39. {
  40. $where = parent::getMore([
  41. ['limit', 20],
  42. ['page', 1],
  43. ['cid', $this->request->param('cid')],
  44. ['store_name', ''],
  45. ['order', ''],
  46. ['is_show', ''],
  47. ]);
  48. return Json::successlayui(Article::getArticleLayList($where));
  49. }
  50. /**
  51. * 设置单个产品上架|下架
  52. *
  53. * @return json
  54. */
  55. public function set_show($is_show = '', $id = '')
  56. {
  57. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  58. $res = Article::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  59. if ($res) {
  60. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  61. } else {
  62. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  63. }
  64. }
  65. /**
  66. * 快速编辑
  67. *
  68. * @return json
  69. */
  70. public function set_value($field = '', $id = '', $value = '')
  71. {
  72. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  73. if (Article::where(['id' => $id])->update([$field => $value]))
  74. return Json::successful('保存成功');
  75. else
  76. return Json::fail('保存失败');
  77. }
  78. public function add_article($id=0)
  79. {
  80. $this->assign('id', $id);
  81. if ($id) {
  82. $article = Article::get($id);
  83. $article->profile->content = htmlspecialchars_decode($article->profile->content);
  84. $this->assign('article', $article->toJson());
  85. }
  86. if(empty($all)){
  87. $list = ArticleCategoryModel::getTierList();
  88. $all = [];
  89. foreach ($list as $menu){
  90. $all[$menu['id']] = $menu['html'].$menu['title'];
  91. }
  92. }
  93. $this->assign('all', json_encode($all));
  94. $this->assign('type', $this->request->param('type', 2));
  95. return $this->fetch();
  96. }
  97. public function save_article($id = 0)
  98. {
  99. $data = parent::postMore([
  100. ['title', ''],
  101. ['synopsis', ''],
  102. ['sort', 0],
  103. ['cid', 0],
  104. ['content', ''],
  105. ['image_input', ''],
  106. ['label', []],
  107. ]);
  108. if (!$data['title']) return Json::fail('请输入图文标题');
  109. if (!$data['synopsis']) return Json::fail('请输入图文简介');
  110. if (!$data['content']) return Json::fail('请输入图文内容');
  111. $data['label'] = json_encode($data['label']);
  112. $content = htmlspecialchars($data['content']);
  113. Article::beginTrans();
  114. try {
  115. if ($id) {
  116. Article::update($data, ['id' => $id]);
  117. ArticleContent::update(['content' => $content], ['nid' => $id]);
  118. Article::commitTrans();
  119. return Json::successful('修改成功');
  120. } else {
  121. $data['add_time'] = time();
  122. $data['is_show'] = 1;
  123. $res1 = Article::set($data);
  124. $res2 = ArticleContent::set(['nid' => Article::getLastInsID(), 'content' => $content]);
  125. if ($res1 && $res2) {
  126. Article::commitTrans();
  127. return Json::successful('添加成功');
  128. } else {
  129. Article::rollbackTrans();
  130. return Json::fail('添加失败');
  131. }
  132. }
  133. } catch (\Exception $e) {
  134. Article::rollbackTrans();
  135. return Json::fail($e->getMessage());
  136. }
  137. }
  138. public function delete($id = 0)
  139. {
  140. if (!$id) return Json::fail('缺少参数');
  141. $article = Article::get($id);
  142. if (!$article) return Json::fail('没有查找到图文');
  143. Article::beginTrans();
  144. try {
  145. $res = $article->delete();
  146. $res1 = ArticleContent::where('nid', $id)->delete();
  147. if ($res1 && $res) {
  148. if($article['hide']==1){
  149. WechatNewsCategoryModel::where('new_id',$id)->delete();
  150. }
  151. Article::commitTrans();
  152. return Json::successful('删除成功');
  153. } else {
  154. Article::rollbackTrans();
  155. return Json::fail('删除失败');
  156. }
  157. } catch (\Exception $e) {
  158. Article::rollbackTrans();
  159. return Json::fail($e->getMessage());
  160. }
  161. }
  162. /*
  163. * 添加推荐
  164. * */
  165. public function recommend($article_id = 0)
  166. {
  167. if (!$article_id) $this->failed('缺少参数');
  168. $article = Article::get($article_id);
  169. if (!$article) $this->failed('没有查到此专题');
  170. $form = Form::create(Url::build('save_recommend', ['article_id' => $article_id]), [
  171. Form::select('recommend_id', '推荐')->setOptions(function () {
  172. $list = Recommend::where(['is_show' => 1, 'type' => 1])->field('title,id')->order('sort desc,add_time desc')->select();
  173. $menus = [['value' => 0, 'label' => '顶级菜单']];
  174. foreach ($list as $menu) {
  175. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  176. }
  177. return $menus;
  178. })->filterable(1),
  179. Form::number('sort', '排序'),
  180. ]);
  181. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  182. $this->assign(compact('form'));
  183. return $this->fetch('public/form-builder');
  184. }
  185. /*
  186. * 保存推荐
  187. * */
  188. public function save_recommend($article_id = 0)
  189. {
  190. if (!$article_id) $this->failed('缺少参数');
  191. $data = parent::postMore([
  192. ['recommend_id', 0],
  193. ['sort', 0],
  194. ]);
  195. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  196. $data['add_time'] = time();
  197. $data['type'] = 1;
  198. $data['link_id'] = $article_id;
  199. if (RecommendRelation::be(['type' => 1, 'link_id' => $article_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  200. if (RecommendRelation::set($data))
  201. return Json::successful('推荐成功');
  202. else
  203. return Json::fail('推荐失败');
  204. }
  205. }