WechatNews.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\article;
  12. use app\admin\controller\AuthController;
  13. use service\JsonService as Json;
  14. use service\UploadService as Upload;
  15. use think\Request;
  16. use app\admin\model\article\ArticleCategory as ArticleCategoryModel;
  17. use app\admin\model\article\Article as ArticleModel;
  18. use app\admin\model\system\SystemAttachment;
  19. /**
  20. * 图文管理
  21. * Class WechatNews
  22. * @package app\admin\controller\wechat
  23. */
  24. class WechatNews extends AuthController
  25. {
  26. /**
  27. * 显示后台管理员添加的图文
  28. * @return mixed
  29. */
  30. public function index($cid = 0)
  31. {
  32. $where = parent::getMore([
  33. ['title','']
  34. ],$this->request);
  35. if($cid){
  36. $where['cid'] = $cid;
  37. }else{
  38. $where['cid'] = '';
  39. }
  40. $this->assign('where',$where);
  41. $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  42. $this->assign('cid',$cid);
  43. $this->assign(ArticleModel::getAll($where));
  44. return $this->fetch();
  45. }
  46. /**
  47. * 展示页面 添加和删除
  48. * @return mixed
  49. */
  50. public function create(){
  51. $id = input('id');
  52. $cid = input('cid');
  53. $news = array();
  54. $news['id'] = '';
  55. $news['image_input'] = '';
  56. $news['title'] = '';
  57. $news['author'] = '';
  58. $news['content'] = '';
  59. $news['synopsis'] = '';
  60. $news['url'] = '';
  61. $news['cid'] = array();
  62. if($id){
  63. $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();
  64. if(!$news) return $this->failedNotice('数据不存在!');
  65. $news['cid'] = explode(',',$news['cid']);
  66. }
  67. $all = array();
  68. $select = 0;
  69. if(!$cid) {
  70. $cid = '';
  71. }else {
  72. if($id){
  73. $all = ArticleCategoryModel::where('id',$cid)->where('hidden','neq',0)->column('id,title');
  74. $select = 1;
  75. }else{
  76. $all = ArticleCategoryModel::where('id',$cid)->column('id,title');
  77. $select = 1;
  78. }
  79. }
  80. if(empty($all)){
  81. $all = ArticleCategoryModel::getField('id,title');//新闻分类
  82. $select = 0;
  83. }
  84. $this->assign('all',$all);
  85. $this->assign('news',$news);
  86. $this->assign('cid',$cid);
  87. $this->assign('select',$select);
  88. return $this->fetch();
  89. }
  90. /**
  91. * 上传图文图片
  92. * @return \think\response\Json
  93. */
  94. public function upload_image(){
  95. $res = Upload::Image($_POST['file'],'wechat/image/'.date('Ymd'));
  96. //产品图片上传记录
  97. $fileInfo = $res->fileInfo->getinfo();
  98. SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(),$fileInfo['size'],$fileInfo['type'],$res->dir,'',5);
  99. if(!$res->status) return Json::fail($res->error);
  100. return Json::successful('上传成功!',['url'=>$res->filePath]);
  101. }
  102. /**
  103. * 添加和修改图文
  104. * @param Request $request
  105. * @return \think\response\Json
  106. */
  107. public function add_new(Request $request){
  108. $post = $request->post();
  109. $data = parent::postMore([
  110. ['id',0],
  111. ['cid',[]],
  112. 'title',
  113. 'author',
  114. 'image_input',
  115. 'content',
  116. 'synopsis',
  117. 'share_title',
  118. 'share_synopsis',
  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. }
  137. ArticleModel::checkTrans($res);
  138. if($res) {
  139. return Json::successful('修改图文成功!', $id);
  140. }else {
  141. return Json::fail('修改图文失败!', $id);
  142. }
  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. }
  152. if($res1 && $res2) {
  153. $res = true;
  154. }else {
  155. $res = false;
  156. }
  157. ArticleModel::checkTrans($res);
  158. if($res) {
  159. return Json::successful('添加图文成功!', $res1->id);
  160. }else {
  161. return Json::successful('添加图文失败!', $res1->id);
  162. }
  163. }
  164. }
  165. /**
  166. * 删除图文
  167. * @param $id
  168. * @return \think\response\Json
  169. */
  170. public function delete($id)
  171. {
  172. $res = ArticleModel::del($id);
  173. if(!$res) {
  174. return Json::fail('删除失败,请稍候再试!');
  175. }else {
  176. return Json::successful('删除成功!');
  177. }
  178. }
  179. public function merchantIndex(){
  180. $where = parent::getMore([
  181. ['title','']
  182. ],$this->request);
  183. $this->assign('where',$where);
  184. $where['cid'] = input('cid');
  185. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  186. $this->assign(ArticleModel::getAll($where));
  187. return $this->fetch();
  188. }
  189. }