Consult.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\FormBuilder as Form;
  14. use service\JsonService as Json;
  15. use service\UploadService as Upload;
  16. use think\Request;
  17. use think\Url;
  18. use app\admin\model\article\ArticleContent as ArticleContentContentModel;
  19. use app\admin\model\article\Article as WechatNewsModel;
  20. use traits\CurdControllerTrait;
  21. /**
  22. * 活动咨询控制器
  23. * Class Consult
  24. * @package app\admin\controller\wechat
  25. */
  26. class Consult extends AuthController
  27. {
  28. use CurdControllerTrait;
  29. protected $bindModel = ArticleContentContentModel::class;
  30. public function edit_content($id,$type='content'){
  31. if(!$id) return $this->failed('数据不存在');
  32. $news = WechatNewsModel::get($id);
  33. if(!$news) return Json::fail('数据不存在!');
  34. $this->assign([
  35. 'content'=>ArticleContentContentModel::where('nid',$id)->value($type),
  36. 'field'=>$type,
  37. 'action'=>Url::build('consult_field',['id'=>$id,'field'=>$type])
  38. ]);
  39. return $this->fetch('public/edit_content');
  40. }
  41. public function index(){
  42. $where = parent::getMore([
  43. ['consult_type',0],
  44. ['title',''],
  45. ],$this->request);
  46. $this->assign('where',$where);
  47. $this->assign(WechatNewsModel::getConsultList($where));
  48. return $this->fetch();
  49. }
  50. public function create(){
  51. $field = [
  52. Form::text('title','文章标题'),
  53. Form::frameImages('consult_image','产品轮播图(640*640px)',Url::build('admin/widget.images/index',array('fodder'=>'consult_image')))->maxLength(5)->icon('images')->width('100%')->height('550px')->spin(0),
  54. Form::number('visit','浏览量',0),
  55. Form::number('sort','排序',0),
  56. Form::radio('status','状态',0)->options([['label'=>'显示','value'=>1],['label'=>'隐藏','value'=>0]])->col(8)
  57. ];
  58. $form = Form::create(Url::build('save'));
  59. $form->setMethod('post')->setTitle('编辑文章')->components($field)->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload();');
  60. $this->assign(compact('form'));
  61. return $this->fetch('public/form-builder');
  62. }
  63. /**
  64. * s上传图片
  65. * */
  66. public function upload(){
  67. $res = Upload::image('file','article');
  68. $thumbPath = Upload::thumb($res->dir);
  69. if($res->status == 200)
  70. return Json::successful('图片上传成功!',['name'=>$res->fileInfo->getSaveName(),'url'=>Upload::pathToUrl($thumbPath)]);
  71. else
  72. return Json::fail($res->error);
  73. }
  74. public function save(Request $request){
  75. $data = parent::postMore([
  76. ['title',''],
  77. ['consult_image',[]],
  78. ['visit',0],
  79. ['sort',0],
  80. ['consult_type',1],
  81. ['status',0],
  82. ],$request);
  83. if(!strlen(trim($data['title']))) return Json::fail('请输入文章名称');
  84. if(!count($data['consult_image'])) return Json::fail('请上传图片');
  85. if($data['sort'] < 0) return Json::fail('排序不能是负数');
  86. $data['add_time'] = time();
  87. $data['is_consult'] = 1;
  88. $data['hide'] = 0;
  89. $data['is_hot'] = 1;
  90. $data['status'] = (int)$data['status'];
  91. $data['consult_image'] = implode(',',$data['consult_image']);
  92. $res = WechatNewsModel::set($data);
  93. if(!$res) return Json::fail('文章添加失败');
  94. return Json::successful('添加文章成功!');
  95. }
  96. public function edit($id){
  97. $article = WechatNewsModel::get($id);
  98. if(!$article) return Json::fail('数据不存在!');
  99. $form = Form::create(Url::build('update',array('id'=>$id)),[
  100. Form::text('title','文章标题',$article->getData('title')),
  101. Form::frameImages('consult_image','产品轮播图(640*640px)',Url::build('admin/widget.images/index',array('fodder'=>'consult_image')),explode(',',$article->getData('consult_image')))->maxLength(5)->icon('images')->width('100%')->height('550px')->spin(0),
  102. Form::number('visit','浏览量',$article->getData('visit')),
  103. Form::number('sort','排序',$article->getData('sort')),
  104. Form::radio('status','状态',$article->getData('status'))->options([['label'=>'显示','value'=>1],['label'=>'隐藏','value'=>0]])->col(8)
  105. ]);
  106. $form->setMethod('post')->setTitle('编辑文章')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload();');
  107. $this->assign(compact('form'));
  108. return $this->fetch('public/form-builder');
  109. }
  110. public function update(Request $request, $id)
  111. {
  112. $data = parent::postMore([
  113. ['title',''],
  114. ['consult_image',[]],
  115. ['visit',0],
  116. ['sort',0],
  117. ['consult_type',1],
  118. ['status',0],
  119. ],$request);
  120. if(!strlen(trim($data['title']))) return Json::fail('请输入文章名称');
  121. if(!count($data['consult_image'])) return Json::fail('请上传图片');
  122. if($data['sort'] < 0) return Json::fail('排序不能是负数');
  123. $data['consult_image'] = implode(',',$data['consult_image']);
  124. $data['status'] = (int)$data['status'];
  125. if(!WechatNewsModel::get($id)) return Json::fail('编辑的记录不存在!');
  126. $res = WechatNewsModel::edit($data,$id);
  127. if(!$res) return Json::fail('修改失败');
  128. return Json::successful('修改成功!');
  129. }
  130. public function delete($id)
  131. {
  132. $res = WechatNewsModel::edit(['hide'=>1],$id);
  133. if(!$res) return Json::fail('删除失败,请稍候再试!');
  134. else return Json::successful('删除成功!');
  135. }
  136. }