Article.php 6.2 KB

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