Aftermarket.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Aftermarket as AftermarketModel;
  4. use addons\qingdongams\model\AftermarketType;
  5. use addons\qingdongams\model\Message;
  6. use think\Db;
  7. use think\Log;
  8. /**
  9. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  10. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  11. * @desc 售后微信:qingdong_crm
  12. */
  13. /**
  14. * 文章
  15. */
  16. class Aftermarket extends StaffApi
  17. {
  18. protected $noNeedLogin = [];
  19. protected $noNeedRight = [];
  20. /**
  21. * 类型列表
  22. */
  23. public function typeList(){
  24. $pid = input('pid',0);
  25. $where['pid'] = $pid;
  26. $model = new AftermarketType();
  27. $list = collection($model->order('id desc')->where($where)->field('id,name,pid')->select())->toArray();
  28. $this->success('',$list);
  29. }
  30. //添加文章
  31. public function addArticle()
  32. {
  33. $params = $this->request->post();
  34. if(!$params['title'] || !$params['content']){
  35. $this->error('请上传文章标题或者内容呢');
  36. }
  37. if(!isset($params['img']) || empty($params['img'])){
  38. $this->error('请上传缩略图');
  39. }
  40. $params['content'] = html_entity_decode($params['content']);
  41. $img = \addons\qingdongams\model\File::where('id',$params['img'])->value('file_path');
  42. $data = [
  43. 'title' => $params['title'],
  44. 'content' => $params['content'],
  45. 'img' =>$img,
  46. 'type_ids' => ','.$params['type_ids'].',',
  47. 'product_ids' => $params['product_ids'],
  48. 'method' => $params['method']?? '',
  49. 'desc' => $params['desc']?? '',
  50. 'create_staff_id' => $this->auth->id,
  51. ];
  52. $commentModel = new AftermarketModel();
  53. if($commentModel->allowField(true)->isUpdate(false)->save($data)){
  54. $this->success('文章添加成功');
  55. }else{
  56. $this->error('文章添加失败');
  57. }
  58. }
  59. // 修改文章
  60. public function editArticle()
  61. {
  62. $params = $this->request->post();
  63. if(!isset($params['id']) || empty($params['id'])){
  64. $this->error('文章id呢?');
  65. }
  66. $commentModel = new AftermarketModel();
  67. $row = $commentModel->get($params['id']);
  68. if(!$row){
  69. $this->error('暂无此文章');
  70. }
  71. if(!isset($params['type_ids']) || empty($params['type_ids'])){
  72. $this->error('请上传文章类型');
  73. }
  74. if(!isset($params['product_ids']) || empty($params['product_ids'])){
  75. $this->error('请上传产品ID');
  76. }
  77. if(!$params['title'] || !$params['content']){
  78. $this->error('请上传文章标题或者内容呢');
  79. }
  80. Log::write('接受前------'.$params['content']);
  81. $params['content'] = html_entity_decode($params['content']);
  82. Log::write('接受后------'.$params['content']);
  83. $data = [
  84. 'type_ids' => ','.$params['type_ids'].',',
  85. 'product_ids' => $params['product_ids'],
  86. 'title' => $params['title'],
  87. 'content' => $params['content'],
  88. 'method' => $params['method']?? '',
  89. 'desc' => $params['desc']?? '',
  90. ];
  91. if(isset($params['img']) && !empty($params['img']) && is_numeric($params['img'])){
  92. $img = \addons\qingdongams\model\File::where('id',$params['img'])->value('file_path');
  93. $data['img'] = $img;
  94. }
  95. if($commentModel->isUpdate(true)->save($data,['id'=>$params['id']])){
  96. $this->success('文章修改成功');
  97. }else{
  98. $this->error('文章修改失败');
  99. }
  100. }
  101. //文章列表
  102. public function articleList()
  103. {
  104. $limit = input("limit/d", 10);
  105. $params = $this->request->post();
  106. $where = [];
  107. $followWhere=[];
  108. if(isset($params['type_ids']) && $params['type_ids']){
  109. $type_ids=explode(',',$params['type_ids']);
  110. if(count($type_ids) > 1){
  111. foreach ($type_ids as $tid){
  112. $tid=intval($tid);
  113. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' .$tid. ',type_ids)')];
  114. }
  115. }else{
  116. $where['type_ids'] = ['like','%,'.$params['type_ids'].',%'];
  117. }
  118. }
  119. if(isset($params['product_ids'])){
  120. $where['product_ids'] = $params['product_ids'];
  121. }
  122. if(isset($params['name'])){
  123. $where['title'] = ['like','%'.$params['name'].'%'];
  124. }
  125. $comments = AftermarketModel::where($where)->with('staff')->field('id,create_staff_id,product_ids,type_ids,title,content,img,desc,method,click_number,comment_num,createtime')->paginate($limit);
  126. $this->success('请求成功', $comments);
  127. }
  128. public function detail(){
  129. $id = input("id");
  130. if(!isset($id) || !$id){
  131. $this->error('参数缺失');
  132. }
  133. //标记通知已读
  134. Message::setRead(Message::ARTICLE_TYPE, $id, $this->auth->id);
  135. $detail = AftermarketModel::where('id',$id)->with('staff')->field('id,annex,create_staff_id,product_ids,type_ids,title,content,img,desc,method,click_number,comment_num,createtime')->find();
  136. preg_match_all('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$detail['content'],$match);
  137. foreach ($match[1] as $v){
  138. $detail['content']=str_replace($v,cdnurl($v,true),$detail['content']);
  139. }
  140. $this->success('请求成功', $detail);
  141. }
  142. /**
  143. * 文章浏览次数
  144. * @throws \think\Exception
  145. */
  146. public function viewArticle(){
  147. $id = input('id');
  148. if(!$id){
  149. $this->error('文章id呢?');
  150. }
  151. $articleModel = new AftermarketModel();
  152. $row = $articleModel->get($id);
  153. if(!$row){
  154. $this->error('文章不存在');
  155. }
  156. if($articleModel->where(['id'=>$id])->setInc('click_number')){
  157. $this->success('成功');
  158. }else{
  159. $this->error('失败');
  160. }
  161. }
  162. }