WechatNews.php 5.6 KB

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