WechatNews.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace app\admin\controller\wechat;
  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\wechat\ArticleCategory as ArticleCategoryModel;
  9. use app\admin\model\wechat\WechatNews as WechatNewsModel;
  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(WechatNewsModel::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. //产品图片上传记录
  89. $fileInfo = $res->fileInfo->getinfo();
  90. SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(),$fileInfo['size'],$fileInfo['type'],$res->dir,'',5);
  91. if(!$res->status) return Json::fail($res->error);
  92. return Json::successful('上传成功!',['url'=>$res->filePath]);
  93. }
  94. /**
  95. * 添加和修改图文
  96. * @param Request $request
  97. * @return \think\response\Json
  98. */
  99. public function add_new(Request $request){
  100. $post = $request->post();
  101. $data = Util::postMore([
  102. ['id',0],
  103. ['cid',[]],
  104. 'title',
  105. 'author',
  106. 'image_input',
  107. 'content',
  108. 'synopsis',
  109. 'share_title',
  110. 'share_synopsis',
  111. ['visit',0],
  112. ['sort',0],
  113. 'url',
  114. ['status',1],],$request);
  115. $data['cid'] = implode(',',$data['cid']);
  116. $content = $data['content'];
  117. unset($data['content']);
  118. if($data['id']){
  119. $id = $data['id'];
  120. unset($data['id']);
  121. WechatNewsModel::beginTrans();
  122. $res1 = WechatNewsModel::edit($data,$id,'id');
  123. $res2 = WechatNewsModel::setContent($id,$content);
  124. if($res1 && $res2)
  125. $res = true;
  126. else
  127. $res =false;
  128. // dump($res);
  129. // exit();
  130. WechatNewsModel::checkTrans($res);
  131. if($res)
  132. return Json::successful('修改图文成功!',$id);
  133. else
  134. return Json::fail('修改图文失败!',$id);
  135. }else{
  136. $data['add_time'] = time();
  137. $data['admin_id'] = $this->adminId;
  138. WechatNewsModel::beginTrans();
  139. $res1 = WechatNewsModel::set($data);
  140. $res2 = false;
  141. if($res1)
  142. $res2 = WechatNewsModel::setContent($res1->id,$content);
  143. if($res1 && $res2)
  144. $res = true;
  145. else
  146. $res =false;
  147. WechatNewsModel::checkTrans($res);
  148. if($res)
  149. return Json::successful('添加图文成功!',$res1->id);
  150. else
  151. return Json::successful('添加图文失败!',$res1->id);
  152. }
  153. }
  154. /**
  155. * 删除图文
  156. * @param $id
  157. * @return \think\response\Json
  158. */
  159. public function delete($id)
  160. {
  161. $res = WechatNewsModel::del($id);
  162. if(!$res)
  163. return Json::fail('删除失败,请稍候再试!');
  164. else
  165. return Json::successful('删除成功!');
  166. }
  167. public function merchantIndex(){
  168. $where = Util::getMore([
  169. ['title','']
  170. ],$this->request);
  171. $this->assign('where',$where);
  172. $where['cid'] = input('cid');
  173. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  174. $this->assign(WechatNewsModel::getAll($where));
  175. return $this->fetch();
  176. }
  177. }