token['root_org']]; $condition[] = ['publish', '=', 1]; $condition[] = ['delete_time', '=', 0]; empty($label) ?: $condition[] = ['label', '=', $label]; empty($type) ?: $condition[] = ['type', '=', $type]; empty($keyword) ?: $condition[] = ['title', 'like', '%' . $keyword . '%']; // 获取数据 $data = ArticleModel::where($condition)->withAttr('content', function ($value, $data) { $value = htmlspecialchars_decode($value); $value = preg_replace('/\s/', '', $value); $value = str_replace(" ", "", $value); $value = strip_tags($value); $text = mb_substr($value, 0, 40, "utf-8"); return $text; })->field('id,title,content,cover_img,uploadtime,shared_times,cover_share_img'); if ($order == 'clue_number') { $data = $data->select()->toArray(); } else { $data = $data->page($page, $limit)->order($order . ' ' . $desc)->select()->toArray(); } $count = ArticleModel::where($condition)->count(); $user_id = $this->request->token['uid']; if (!empty($user_id)) { $article_id = array_column($data, 'id'); $article_id = implode(',', $article_id); $user_foot = UserCollect::where([['content_type', '=', 'article'], ['content_id', 'in', $article_id], ['user_id', '=', $user_id]])->column('user_id', 'content_id'); } foreach ($data as &$val) { $val['isCollection'] = false; if (!empty($user_id)) { $val['isCollection'] = isset($user_foot[$val['id']]) ? true : false; } $val['clue_number'] = CustomerClue::where([['pipe_type', '=', 'article'], ['pipe_id', '=', $val['id']]])->count(); /** -----乐尚企业演示,线索数量进行调整按照分享量的三分之一展示,正式使用系统时删除此代码 ------*/ if(request()->token['root_org'] == 1793) { $val['clue_number'] = floor($val['shared_times'] > 0 ? $val['shared_times']/3 : 0); } /*------------------------------------------------------------------------*/ } // 排序 if ($order == 'clue_number') { $sort = $desc == 'desc' ? SORT_DESC : SORT_ASC; array_multisort(array_column($data, $order), $sort, array_column($data, 'uploadtime'), SORT_DESC, $data); $data = array_slice($data, ($page-1) * $limit, $limit); } return json(['code' => 0, 'data' => $data, 'count' => $count, 'msg' => '获取成功']); } /** * 类型获取 */ public function type() { $type = VideoType::field('id,name')->where(['pid' => 0, 'type' => 'article', 'root_id' => request()->token['root_org']])->select()->toArray(); $label = VideoType::field('id,pid,name')->where([['pid', '>', 0], ['type', '=', 'article'], ['root_id', '=', request()->token['root_org']]])->select()->toArray(); foreach ($type as &$val) { $val['sonLabel'] = []; foreach ($label as $typeVal) { if ($val['id'] == $typeVal['pid']) { $val['sonLabel'][] = $typeVal; } } } return json(['code' => 0, 'data' => $type, 'msg' => '获取成功']); } /** * 获取所有标签 */ public function get_all_label(){ $label = VideoType::field('id,pid,name')->where([['pid', '>', 0], ['type', '=', 'article'], ['root_id', '=', request()->token['root_org']]])->select()->toArray(); return json(['code' => 0, 'data' => $label, 'msg' => '获取成功']); } /** * 获取文章详情 */ public function info($article_id) { $article = ArticleModel::field('id,title,content,cover_img,cover_share_img')->where(['id' => $article_id, 'publish' => 1, 'delete_time' => 0, 'root_id' => request()->token['root_org']])->find(); if (empty($article)) { $article = [ 'title' => '该文章不存在', 'content' => '您访问的内容不存在', 'cover_img' => '' ]; } $user_id = $this->request->token['uid']; $collect = UserCollect::where(['user_id' => $user_id, 'content_type' => 'article', 'content_id' => $article_id])->count(); $article['collect'] = $collect > 0 ? true : false; return json(['code' => 0, 'msg' => '获取成功', 'data' => $article]); } /** * 收藏 */ public function collect($id) { $user_id = $this->request->token['uid']; $had = UserCollect::where(['user_id' => $user_id, 'content_type' => 'article', 'content_id' => $id])->count(); if ($had > 0) return json(['code' => 1, 'msg' => '您已收藏']); UserCollect::insert(['user_id' => $user_id, 'content_type' => 'article', 'content_id' => $id]); return json(['code' => 0, 'msg' => '收藏成功']); } /** * 取消收藏 */ public function collectCancel($id) { $user_id = $this->request->token['uid']; UserCollect::where(['user_id' => $user_id, 'content_type' => 'article', 'content_id' => $id])->delete(); return json(['code' => 0, 'msg' => '取消收藏']); } }