Article.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\admin\controller\article;
  3. use app\admin\controller\AuthController;
  4. use crmeb\services\UtilService as Util;
  5. use crmeb\services\PHPTreeService as Phptree;
  6. use crmeb\services\JsonService as Json;
  7. use crmeb\services\UploadService as Upload;
  8. use app\admin\model\article\ArticleCategory as ArticleCategoryModel;
  9. use app\admin\model\article\Article as ArticleModel;
  10. use app\admin\model\system\SystemAttachment;
  11. /**
  12. * 图文管理
  13. * Class WechatNews
  14. * @package app\admin\controller\wechat
  15. */
  16. class Article extends AuthController
  17. {
  18. /**
  19. * TODO 显示后台管理员添加的图文
  20. * @return mixed
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function index()
  26. {
  27. $where = Util::getMore([
  28. ['title',''],
  29. ['cid','']
  30. ],$this->request);
  31. $pid = $this->request->param('pid');
  32. $this->assign('where',$where);
  33. $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  34. $cateList = ArticleCategoryModel::getArticleCategoryList();
  35. $tree = [];
  36. //获取分类列表
  37. if(count($cateList)){
  38. $tree = Phptree::makeTreeForHtml($cateList);
  39. if($pid){
  40. $pids = Util::getChildrenPid($tree,$pid);
  41. $where['cid'] = ltrim($pid.$pids);
  42. }
  43. }
  44. $this->assign(compact('tree'));
  45. $this->assign(ArticleModel::getAll($where));
  46. return $this->fetch();
  47. }
  48. /**
  49. * TODO 文件添加和修改
  50. * @return mixed
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. */
  55. public function create(){
  56. $id = $this->request->param('id');
  57. $cid = $this->request->param('cid');
  58. $news = [];
  59. $all = [];
  60. $news['id'] = '';
  61. $news['image_input'] = '';
  62. $news['title'] = '';
  63. $news['author'] = '';
  64. $news['is_banner'] = '';
  65. $news['is_hot'] = '';
  66. $news['content'] = '';
  67. $news['synopsis'] = '';
  68. $news['url'] = '';
  69. $news['cid'] = [];
  70. $select = 0;
  71. if($id){
  72. $news = ArticleModel::where('n.id',$id)->alias('n')->field('n.*,c.content')->join('ArticleContent c','c.nid=n.id','left')->find();
  73. if(!$news) return $this->failed('数据不存在!');
  74. $news['cid'] = explode(',',$news['cid']);
  75. }
  76. if($cid && in_array($cid, ArticleCategoryModel::getArticleCategoryInfo(0,'id'))){
  77. $all = ArticleCategoryModel::getArticleCategoryInfo($cid);
  78. $select = 1;
  79. }
  80. if(!$select){
  81. $list = ArticleCategoryModel::getTierList();
  82. foreach ($list as $menu){
  83. $all[$menu['id']] = $menu['html'].$menu['title'];
  84. }
  85. }
  86. $this->assign('all',$all);
  87. $this->assign('news',$news);
  88. $this->assign('cid',$cid);
  89. $this->assign('select',$select);
  90. return $this->fetch();
  91. }
  92. /**
  93. * 上传图文图片
  94. * @return \think\response\Json
  95. */
  96. public function upload_image(){
  97. $res = Upload::Image($_POST['file'],'wechat/image/'.date('Ymd'));
  98. if(!is_array($res)) return Json::fail($res);
  99. SystemAttachment::attachmentAdd($res['name'],$res['size'],$res['type'],$res['dir'],$res['thumb_path'],5,$res['image_type'],$res['time']);
  100. return Json::successful('上传成功!',['url'=>$res['dir']]);
  101. }
  102. /**
  103. * 添加和修改图文
  104. */
  105. public function add_new(){
  106. $data = Util::postMore([
  107. ['id',0],
  108. ['cid',[]],
  109. 'title',
  110. 'author',
  111. 'image_input',
  112. 'content',
  113. 'synopsis',
  114. 'share_title',
  115. 'share_synopsis',
  116. ['visit',0],
  117. ['sort',0],
  118. 'url',
  119. ['is_banner',0],
  120. ['is_hot',0],
  121. ['status',1],]);
  122. $data['cid'] = implode(',',$data['cid']);
  123. $content = $data['content'];
  124. unset($data['content']);
  125. if($data['id']){
  126. $id = $data['id'];
  127. unset($data['id']);
  128. $res = false;
  129. ArticleModel::beginTrans();
  130. $res1 = ArticleModel::edit($data,$id,'id');
  131. $res2 = ArticleModel::setContent($id,$content);
  132. if($res1 && $res2){
  133. $res = true;
  134. }
  135. ArticleModel::checkTrans($res);
  136. if($res)
  137. return Json::successful('修改图文成功!',$id);
  138. else
  139. return Json::fail('修改图文失败,您并没有修改什么!',$id);
  140. }else{
  141. $data['add_time'] = time();
  142. $data['admin_id'] = $this->adminId;
  143. $res = false;
  144. ArticleModel::beginTrans();
  145. $res1 = ArticleModel::create($data);
  146. $res2 = false;
  147. if($res1)
  148. $res2 = ArticleModel::setContent($res1->id,$content);
  149. if($res1 && $res2){
  150. $res = true;
  151. }
  152. ArticleModel::checkTrans($res);
  153. if($res)
  154. return Json::successful('添加图文成功!',$res1->id);
  155. else
  156. return Json::successful('添加图文失败!',$res1->id);
  157. }
  158. }
  159. /**
  160. * 删除图文
  161. * @param $id
  162. * @return \think\response\Json
  163. */
  164. public function delete($id)
  165. {
  166. $res = ArticleModel::del($id);
  167. if(!$res)
  168. return Json::fail('删除失败,请稍候再试!');
  169. else
  170. return Json::successful('删除成功!');
  171. }
  172. public function merchantIndex(){
  173. $where = Util::getMore([
  174. ['title','']
  175. ],$this->request);
  176. $this->assign('where',$where);
  177. $where['cid'] = input('cid');
  178. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  179. $this->assign(ArticleModel::getAll($where));
  180. return $this->fetch();
  181. }
  182. }