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