Article.php 6.2 KB

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