Article.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * 显示后台管理员添加的图文
  21. * @return mixed
  22. */
  23. public function index($pid = 0)
  24. {
  25. $where = Util::getMore([
  26. ['title',''],
  27. ['cid','']
  28. ],$this->request);
  29. $pid = $this->request->param('pid');
  30. $this->assign('where',$where);
  31. $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  32. $catlist = ArticleCategoryModel::where('is_del',0)->select()->toArray();
  33. //获取分类列表
  34. if($catlist){
  35. $tree = Phptree::makeTreeForHtml($catlist);
  36. $this->assign(compact('tree'));
  37. if($pid){
  38. $pids = Util::getChildrenPid($tree,$pid);
  39. $where['cid'] = ltrim($pid.$pids);
  40. }
  41. }else{
  42. $tree = [];
  43. $this->assign(compact('tree'));
  44. }
  45. $this->assign('cate',ArticleCategoryModel::getTierList());
  46. $this->assign(ArticleModel::getAll($where));
  47. return $this->fetch();
  48. }
  49. /**
  50. * 展示页面 添加和删除
  51. * @return mixed
  52. */
  53. public function create(){
  54. $id = input('id');
  55. $cid = input('cid');
  56. $news = array();
  57. $news['id'] = '';
  58. $news['image_input'] = '';
  59. $news['title'] = '';
  60. $news['author'] = '';
  61. $news['is_banner'] = '';
  62. $news['is_hot'] = '';
  63. $news['content'] = '';
  64. $news['synopsis'] = '';
  65. $news['url'] = '';
  66. $news['cid'] = array();
  67. if($id){
  68. $news = ArticleModel::where('n.id',$id)->alias('n')->field('n.*,c.content')->join('ArticleContent c','c.nid=n.id')->find();
  69. if(!$news) return $this->failedNotice('数据不存在!');
  70. $news['cid'] = explode(',',$news['cid']);
  71. }
  72. $all = array();
  73. $select = 0;
  74. if(!$cid)
  75. $cid = '';
  76. else {
  77. if($id){
  78. $all = ArticleCategoryModel::where('id',$cid)->where('hidden','neq',0)->column('id,title');
  79. $select = 1;
  80. }else{
  81. $all = ArticleCategoryModel::where('id',$cid)->column('id,title');
  82. $select = 1;
  83. }
  84. }
  85. if(empty($all)){
  86. $select = 0;
  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',$all);
  94. $this->assign('news',$news);
  95. $this->assign('cid',$cid);
  96. $this->assign('select',$select);
  97. return $this->fetch();
  98. }
  99. /**
  100. * 上传图文图片
  101. * @return \think\response\Json
  102. */
  103. public function upload_image(){
  104. $res = Upload::Image($_POST['file'],'wechat/image/'.date('Ymd'));
  105. //产品图片上传记录
  106. $fileInfo = $res->fileInfo->getinfo();
  107. SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(),$fileInfo['size'],$fileInfo['type'],$res->dir,'',5);
  108. if(!$res->status) return Json::fail($res->error);
  109. return Json::successful('上传成功!',['url'=>$res->filePath]);
  110. }
  111. /**
  112. * 添加和修改图文
  113. * @param Request $request
  114. * @return \think\response\Json
  115. */
  116. public function add_new(Request $request){
  117. $post = $request->post();
  118. $data = Util::postMore([
  119. ['id',0],
  120. ['cid',[]],
  121. 'title',
  122. 'author',
  123. 'image_input',
  124. 'content',
  125. 'synopsis',
  126. 'share_title',
  127. 'share_synopsis',
  128. ['visit',0],
  129. ['sort',0],
  130. 'url',
  131. ['is_banner',0],
  132. ['is_hot',0],
  133. ['status',1],],$request);
  134. $data['cid'] = implode(',',$data['cid']);
  135. $content = $data['content'];
  136. unset($data['content']);
  137. if($data['id']){
  138. $id = $data['id'];
  139. unset($data['id']);
  140. ArticleModel::beginTrans();
  141. $res1 = ArticleModel::edit($data,$id,'id');
  142. $res2 = ArticleModel::setContent($id,$content);
  143. if($res1 && $res2)
  144. $res = true;
  145. else
  146. $res =false;
  147. // dump($res);
  148. // exit();
  149. ArticleModel::checkTrans($res);
  150. if($res)
  151. return Json::successful('修改图文成功!',$id);
  152. else
  153. return Json::fail('修改图文失败!',$id);
  154. }else{
  155. $data['add_time'] = time();
  156. $data['admin_id'] = $this->adminId;
  157. ArticleModel::beginTrans();
  158. $res1 = ArticleModel::set($data);
  159. $res2 = false;
  160. if($res1)
  161. $res2 = ArticleModel::setContent($res1->id,$content);
  162. if($res1 && $res2)
  163. $res = true;
  164. else
  165. $res =false;
  166. ArticleModel::checkTrans($res);
  167. if($res)
  168. return Json::successful('添加图文成功!',$res1->id);
  169. else
  170. return Json::successful('添加图文失败!',$res1->id);
  171. }
  172. }
  173. /**
  174. * 删除图文
  175. * @param $id
  176. * @return \think\response\Json
  177. */
  178. public function delete($id)
  179. {
  180. $res = ArticleModel::del($id);
  181. if(!$res)
  182. return Json::fail('删除失败,请稍候再试!');
  183. else
  184. return Json::successful('删除成功!');
  185. }
  186. public function merchantIndex(){
  187. $where = Util::getMore([
  188. ['title','']
  189. ],$this->request);
  190. $this->assign('where',$where);
  191. $where['cid'] = input('cid');
  192. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  193. $this->assign(ArticleModel::getAll($where));
  194. return $this->fetch();
  195. }
  196. }