WechatNews.php 5.5 KB

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