Article.php 6.5 KB

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