Article.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ['visit',0],
  118. ['sort',0],
  119. 'url',
  120. ['status',1],],$request);
  121. $data['cid'] = implode(',',$data['cid']);
  122. $content = $data['content'];
  123. unset($data['content']);
  124. if($data['id']){
  125. $id = $data['id'];
  126. unset($data['id']);
  127. ArticleModel::beginTrans();
  128. $res1 = ArticleModel::edit($data,$id,'id');
  129. $res2 = ArticleModel::setContent($id,$content);
  130. if($res1 && $res2)
  131. $res = true;
  132. else
  133. $res =false;
  134. // dump($res);
  135. // exit();
  136. ArticleModel::checkTrans($res);
  137. if($res)
  138. return Json::successful('修改图文成功!',$id);
  139. else
  140. return Json::fail('修改图文失败!',$id);
  141. }else{
  142. $data['add_time'] = time();
  143. $data['admin_id'] = $this->adminId;
  144. ArticleModel::beginTrans();
  145. $res1 = ArticleModel::set($data);
  146. $res2 = false;
  147. if($res1)
  148. $res2 = ArticleModel::setContent($res1->id,$content);
  149. if($res1 && $res2)
  150. $res = true;
  151. else
  152. $res =false;
  153. ArticleModel::checkTrans($res);
  154. if($res)
  155. return Json::successful('添加图文成功!',$res1->id);
  156. else
  157. return Json::successful('添加图文失败!',$res1->id);
  158. }
  159. }
  160. /**
  161. * 删除图文
  162. * @param $id
  163. * @return \think\response\Json
  164. */
  165. public function delete($id)
  166. {
  167. $res = ArticleModel::del($id);
  168. if(!$res)
  169. return Json::fail('删除失败,请稍候再试!');
  170. else
  171. return Json::successful('删除成功!');
  172. }
  173. public function merchantIndex(){
  174. $where = Util::getMore([
  175. ['title','']
  176. ],$this->request);
  177. $this->assign('where',$where);
  178. $where['cid'] = input('cid');
  179. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  180. $this->assign(ArticleModel::getAll($where));
  181. return $this->fetch();
  182. }
  183. }