123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837 |
- <?php
- namespace App\JsonRpc;
- use App\Model\Article;
- use App\Model\ArticleData;
- use App\Model\Category;
- use App\Model\Website;
- use App\Model\WebsiteCategory;
- use Hyperf\DbConnection\Db;
- use Hyperf\RpcServer\Annotation\RpcService;
- use App\Tools\Result;
- use App\Model\ArticleSurvey;
- #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
- class NewsService implements NewsServiceInterface
- {
- /**
- * 获取导航池列表
- * @param array $data
- * @return array
- */
- public function getCategoryList(array $data): array
- {
- $where = [];
- if(isset($data['name']) && $data['name']){
- array_push($where, ['category.name','like','%'.$data['name'].'%']);
- }
- if(isset($data['department_id']) && $data['department_id']){
- array_push($where, ['category.department_id','=',$data['department_id']]);
- }
- if(isset($data['city_id']) && $data['city_id']){
- array_push($where, ['category.city_id','=',$data['city_id']]);
- }
- $rep = Category::where($where)
- ->leftJoin('district','category.city_id','district.id')
- ->leftJoin('department','category.department_id','department.id')
- ->select("category.*","district.name as city_name","department.name as department_name")
- ->limit($data['pageSize'])->orderBy("category.sort","desc")->orderByDesc('category.updated_at')->offset(($data['page']-1)*$data['pageSize'])->get();
- $count = Category::where($where)->count();
- $data = [
- 'rows'=>$rep->toArray(),
- 'count'=>$count
- ];
- if(empty($rep->toArray())){
- return Result::error("没有导航池数据");
- }
- return Result::success($data);
- }
- /**
- * @param array $data
- * @return array
- */
- public function categoryList(array $data): array
- {
- $where[] = [
- 'pid','=',$data['pid']
- ];
- if(isset($data['name'])){
- array_push($where, ['category.name','like','%'.$data['name'].'%']);
- }
- var_dump($where);
- $result = Category::where($where)->select('category.*','category.id as category_id')->get();
- if(empty($result)){
- return Result::error("没有栏目数据");
- }
- return Result::success($result);
- }
- /**
- * @param array $data
- * @return array
- */
- public function addCategory(array $data): array
- {
- $id = Category::insertGetId($data);
- if(empty($id)){
- return Result::error("添加失败");
- }
- return Result::success(['id'=>$id]);
- }
- /**
- * @param array $data
- * @return array
- */
- public function delCategory(array $data): array
- {
- $categoryList = Category::where(['pid'=>$data['id']])->get();
- var_dump("分类列表:",$data,$categoryList);
- if($categoryList->toArray()){
- return Result::error("分类下面有子分类不能删除");
- }
- $articleList = Article::where(['catid'=>$data['id']])->get();
- var_dump("文章列表:",$articleList);
- if($articleList->toArray()){
- return Result::error("分类下面有资讯不能删除");
- }
- $result = Category::where($data)->delete();
- if(!$result){
- return Result::error("删除失败");
- }
- return Result::success($result);
- }
- /**
- * @param array $data
- * @return array
- */
- public function updateCategory(array $data): array
- {
- $where = [
- 'id'=>$data['id']
- ];
- $result = Category::where($where)->update($data);
- if($result){
- return Result::success($result);
- }else{
- return Result::error("更新失败");
- }
- }
- /**
- * 获取导航池信息
- * @param array $data
- * @return array
- */
- public function getCategoryInfo(array $data): array
- {
- $where = [
- 'id'=>$data['id']
- ];
- $result = Category::where($where)->first();
- if($result){
- return Result::success($result);
- }else{
- return Result::error("更新失败");
- }
- }
- /**
- * @param array $data
- * @return array
- */
- public function getArticleList(array $data): array
- {
- $where= [];
- if(isset($data['title']) && $data['title']){
- array_push($where,['article.title','like','%'.$data['title'].'%']);
- }
- if(isset($data['category_name']) && $data['category_name']){
- array_push($where,['category.name','like','%'.$data['category_name'].'%']);
- }
- if(isset($data['author']) && $data['author']){
- array_push($where,['article.author','=',$data['author']]);
- }
- if(isset($data['islink']) && $data['islink']!==""){
- array_push($where,['article.islink','=',$data['islink']]);
- }
- if(isset($data['status']) && $data['status']!==""){
- array_push($where,['article.status','=',$data['status']]);
- }
-
- $rep = Article::where($where)
- ->whereNotIn('article.status',[404])
- ->leftJoin('category','article.catid','category.id')
- ->select("article.*","category.name as category_name")
- ->orderBy("article.id","desc")
- ->limit($data['pageSize'])
- ->offset(($data['page']-1)*$data['pageSize'])->get();
- $count = Article::where($where)->whereNotIn('article.status',[404])
- ->leftJoin('category','article.catid','category.id')->count();
- $data = [
- 'rows'=>$rep->toArray(),
- 'count'=>$count
- ];
- if(empty($rep)){
- return Result::error("没有信息数据");
- }
- return Result::success($data);
- }
- /**
- * @param array $data
- * @return array
- */
- public function addArticle(array $data): array
- {
- Db::beginTransaction();
- try{
- $articleData = $data;
- unset($articleData['content']);
- $id = Article::insertGetId($articleData);
- $articleDataContent = [
- 'article_id'=>$id,
- 'content'=>$data['content']
- ];
- ArticleData::insertGetId($articleDataContent);
- Db::commit();
- } catch(\Throwable $ex){
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("创建失败",0);
- }
- return Result::success(['id'=>$id]);
- }
- /**
- * @param array $data
- * @return array
- */
- public function delArticle(array $data): array
- {
- $result = Article::where($data)->update(['status'=>404]);
- if(!$result){
- return Result::error("删除失败");
- }
- return Result::success($result);
- }
- /**
- * @param array $data
- * @return array
- */
- public function updateArticle(array $data): array
- {
- Db::beginTransaction();
- try{
- $data['cat_arr_id'] = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
- $data['tag'] = isset($data['tag'])?json_encode($data['tag']):'';
- $articleData = $data;
- unset($articleData['content']);
- unset($articleData['status_name']);
- unset($articleData['name']);
- unset($articleData['content']);
- unset($articleData['pid_arr']);
- unset($articleData['pid']);
- $id = Article::where(['id'=>$data['id']])->update($articleData);
- $articleDataContent = [
- 'content'=>$data['content']
- ];
- ArticleData::where(['article_id'=>$data['id']])->update($articleDataContent);
- } catch(\Throwable $ex){
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("更新失败",0);
- }
- return Result::success([]);
- }
- /**
- * 更新资讯状态
- * @param array $data
- * @return array
- */
- public function upArticleStatus(array $data):array
- {
- $result = Article::where(['id'=>$data['id']])->update($data);
- if($result){
- return Result::success();
- }else{
- return Result::error("更新状态失败",0);
- }
- }
- /**
- * 获取新闻详情
- * @param array $data
- * @return array
- */
- public function getArticleInfo(array $data): array
- {
- $where = [
- 'article.id'=>$data['id'],
- // 'article.status'=>1
- ];
- $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
- if(empty($result)){
- return Result::error("查询失败",0);
- }
- return Result::success($result);
- }
- /**
- * 获取头条新闻
- * @param array $data
- * @return array
- */
- public function getWebsiteArticlett(array $data): array
- {
-
- $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
- $result = [];
- if ($category) {
- $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
- $where = [
- 'status' => 1,
- ];
- var_dump($data, 'data-----------------');
- //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
- // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
- var_dump($where, 'where-----------------');
- $result = Article::where($where)
- ->whereIn("catid", $category)
- ->where(function ($query) use ($data) {
- $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
- ->orWhereNull("ignore_ids");
- })
- //$data['level'] == 4 || $data['level'] == 5 查询随机
- ->when($data['level'] == 5, function ($query) {
- $query->inRandomOrder()
- //updated_at最近三十天;
- ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
- })
- ->when($data['level'] == 4, function ($query) {
- $query->orderBy("updated_at", "desc");
- })
- ->when(!empty($data['level']), function ($query) use ($data) {
- if ($data['level'] != 4 && $data['level'] != 5) {
- $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
- ->orderBy("updated_at", "desc");
- }
- })
- ->offset($placeid)
- ->limit($data['pageSize'])
- ->get();
- if (empty($result)) {
- return Result::error("暂无头条新闻", 0);
- }
- return Result::success($result);
- } else {
- return Result::error("本网站下暂无相关栏目", 0);
- }
- }
- /**
- * 获取模块新闻
- * @param array $data
- * @return array
- */
-
- public function getWebsiteModelArticles(array $data): array
- {
- $catid = $data['catid'];
- $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
- $category = $category->toArray();
- if (!empty($category)) {
- $where = [
- 'status' => 1,
- 'catid' => $catid,
- ];
- $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
- // 1:文字新闻;2:轮播图;3:图文;
- // 级别:0:未分类
- // 3:推荐图片
- if ($data['level'] == 1) {
- $data['level'] = 0;
- }
- $result = Article::where($where)
- ->where(function ($query) use ($data) {
- $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
- ->orWhereNull("level")
- ->orWhereRaw("level = '[]'");
- })
- ->where(function ($query) use ($data) {
- $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
- ->orWhereNull("ignore_ids");
- })
- ->orderBy("updated_at", "desc")
- ->offset($placeid)
- ->limit($data['pagesize'])
- ->get();
- if (empty($result)) {
- return Result::error("此栏目暂无相关新闻", 0);
- }
- } else {
- return Result::error("此网站暂无此栏目", 0);
- }
- return Result::success($result);
- }
-
- /**
- *获取新闻列表
- * @param array $data
- * @return array
- */
- public function getWebsiteArticleList(array $data): array
- {
- $where[] = ['status', '=', 1];
- if(isset($data['keyword']) && !empty($data['keyword'])){
- array_push($where,['article.title','like','%'.$data['keyword'].'%']);
- }
- if(isset($data['catid']) && !empty($data['catid'])){
- if(is_array($data['catid'])){
- $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
- array_push($where,['catid', 'in', $data['catid']]);
- }else{
- $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
- array_push($where,['catid', '=', $data['catid']]);
- }
- if(empty($category)){
- return Result::error("此网站暂无此栏目",0);
- }
- }
- // return Result::success($where);
- $rep = Article::where(function ($query) use ($where) {
- foreach ($where as $condition) {
- if ($condition[1] === 'in') {
- $query->whereIn($condition[0], $condition[2]);
- } else {
- $query->where($condition[0], $condition[1], $condition[2]);
- }
- }
- })
- ->where(function ($query) use ($data) {
- $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
- ->orWhereNull("ignore_ids");
- })
- ->orderBy("updated_at", "desc")
- ->limit($data['pageSize'])
- ->offset(($data['page'] - 1) * $data['pageSize'])
- ->get();
- $count = Article::where(function ($query) use ($where) {
- foreach ($where as $condition) {
- if ($condition[1] === 'in') {
- $query->whereIn($condition[0], $condition[2]);
- } else {
- $query->where($condition[0], $condition[1], $condition[2]);
- }
- }
- })->count();
- $data = [
- 'rows'=>$rep->toArray(),
- 'count'=>$count
- ];
-
- if(empty($rep)){
- return Result::error("没有信息数据");
- }
- return Result::success($data);
-
- }
- /**
- * 前端-获取新闻详情
- * @param array $data
- * @return array
- */
- public function selectWebsiteArticleInfo(array $data): array
- {
- $where = [
- 'article.id'=>$data['id'],
- 'article.status'=>1
- ];
- $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
- ->where(function ($query) use ($data) {
- $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
- ->orWhereNull("ignore_ids");
- })
- ->first();
- if(empty($result)){
- return Result::error("暂无此新闻!",0);
- }
- $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
- if(empty($category)){
- return Result::error("查询失败",0);
- }
- $result['category_id'] = $category['category_id'];
- $result['cat_name'] = $category['alias'];
- return Result::success($result);
- }
- /**
- * 前端-获取网站调查问卷
- * @param array $data
- * @return array
- */
- public function getWebsiteSurvey(array $data): array
- {
- if(isset($data['website_id']) && !empty($data['website_id'])){
- $website = Website::where('id',$data['website_id'])->first();
- if(empty($website)){
- return Result::error("暂无此网站",0);
- }
- }
- if(isset($data['art_id']) && !empty($data['art_id'])){
- $article = Article::where('id',$data['art_id'])->where('status',1)->first();
- if(empty($article)){
- return Result::error("暂无此文章",0);
- }
- // return Result::error($data,0);
- $where['art_id'] = $data['art_id'];
- // $query = ArticleSurvey::where('art_id',$data['art_id']);
-
- }else{
- $survey = Article::where(function ($query) {
- $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
- ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
- })
- ->where('status',1)
- ->where('is_survey',1)
- ->select('survey_id')
- ->orderBy('updated_at','desc')
- ->first();
- if(empty($survey)){
- return Result::error("暂无调查问卷",0);
- }
- $where['sur_id'] = $survey['survey_id'];
- // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
- }
- // return Result::success($where);
- $result = ArticleSurvey::where($where)
- ->where(function ($query) {
- $query->where('is_other', 0)
- ->orWhere(function ($subQuery) {
- $subQuery->where('is_other', 1)
- ->where('other_id', 0);
- });
- })
- ->leftJoin('article', 'article_survey.art_id', 'article.id')
- ->select('article_survey.*', 'article.survey_type')
- ->get()->all();
- if(empty($result)){
- return Result::error("此文章暂无调查问卷",0);
- }
- return Result::success($result);
- }
- /**
- * 前端-添加网站调查问卷选项
- * @param array $data
- * @return array
- */
- public function addWebsiteSurveyOption(array $data): array
- {
- if(isset($data['website_id']) && !empty($data['website_id'])){
- $website = Website::where('id',$data['website_id'])->first();
- if(empty($website)){
- return Result::error("暂无此网站",0);
- }
- if(isset($data['sur_id']) && !empty($data['sur_id'])){
- $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
- if(empty($survey)){
- return Result::error("此调查问卷不可添加选项",0);
- }
- if(isset($data['choice_name']) &&!empty($data['choice_name'])){
- $choice = [
- 'art_id'=>$survey['art_id'],
- 'website_id'=>$data['website_id'],
- 'survey_name'=>$survey['survey_name'],
- 'choice_name'=>$data['choice_name'],
- 'sur_id'=>$survey['sur_id'],
- 'is_other'=>1,
- 'other_id'=>$survey['id'],
-
- ];
- $result = ArticleSurvey::insertGetId($choice);
- if(empty($result)){
- return Result::error("添加失败",0);
- }
- return Result::success($result);
- }
- }
- return Result::error("添加失败",0);
- }
- return Result::error("添加失败",0);
-
- }
- /**
- * 前端-调查问卷投票
- * @param array $data
- * @return array
- */
- public function addWebsiteSurveyVote(array $data): array
- {
- // return Result::success($data);
- if(isset($data['website_id']) && !empty($data['website_id'])){
- $website = Website::where('id',$data['website_id'])->first();
- if(empty($website)){
- return Result::error("暂无此网站",0);
- }
- if(isset($data['sur_id']) && !empty($data['sur_id'])){
- $is_survey = ArticleSurvey::where('sur_id',$data['sur_id'])->first();
- // return Result::success($survey);
- if(empty($is_survey)){
- return Result::error("此调查问卷不存在",0);
- }
- // return Result::success($survey);
- // 调查问卷类型
- if(isset($data['choice_id']) &&!empty($data['choice_id'])){
- //多选 若是json型则转化成数组类型
- if (strpos($data['choice_id'], '[') === 0) {
- $data['choice_id'] = json_decode($data['choice_id'], true);
- } else {
- // 单选 也转换成数组
- $data['choice_id'] = [$data['choice_id']];
- }
- $data['choice_id'] = array_map('intval', $data['choice_id']);
- $other = ArticleSurvey::whereIn('id',$data['choice_id'])
- ->where('website_id',$data['website_id'])
- ->where('is_other',1)
- ->where('other_id',0)
- ->first();
- if(!empty($other)){
- return Result::error("请选择已有的选项!",0);
- }
- $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
- ->where('website_id',$data['website_id'])
- ->where('is_other',1)
- ->where('other_id','!=',0)
- ->first();
- // return Result::success($choice['other']);
- $choice_id = $data['choice_id'];
- if(!empty($choice['other'])){
- // array_push($data['choice_id'],$choice['other']['other_id']);
- if(!empty($choice_id)){
- $key = array_search($choice['other']['id'], $choice_id);
- if ($key!== false) {
- unset($choice_id[$key]);
- $choice_id = array_values($choice_id);
- }
- array_push($choice_id,$choice['other']['other_id']);
- }else{
- $choice_id[0] = $choice['other']['other_id'];
- }
- array_push($data['choice_id'],$choice['other']['other_id']);
- }
- // return Result::success($data);
- $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
- ->where('website_id',$data['website_id'])
- ->increment('results', 1);
- if(empty($choice)){
- return Result::error("请选择已有的选项!",0);
- }
- $survey['data'] = ArticleSurvey::where('sur_id',$data['sur_id'])
- ->where('website_id',$data['website_id'])
- ->where('other_id', 0)
- ->get();
- $survey['choice'] = $choice_id;
- return Result::success($survey);
- }
- return Result::error("参数必填!");
- }
- return Result::error("此调查问卷不存在",0);
- }
- return Result::error("参数必填!");
- }
- /**
- * 后端-获取网站调查问卷列表
- * @param array $data
- * @return array
- */
- public function getSurveyList(array $data): array
- {
- $where = [];
- if(isset($data['survey_name']) &&!empty($data['survey_name'])){
- array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
- }
- if(isset($data['survey_type']) && $data['survey_type'] != null){
- array_push($where,['survey_type','=',$data['survey_type']]);
- }
- if(isset($data['is_survey']) && $data['is_survey'] != null){
- array_push($where,['is_survey','=',$data['is_survey']]);
- }
- // return Result::success($where);
-
- if (!empty($where)) {
- $query = Article::where($where)->where(function ($q) {
- $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
- });
- } else {
- $query = Article::where(function ($q) {
- $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
- });
- }
-
- $count = $query->count();
- $survey = $query->orderByDesc('id')
- ->limit($data['pageSize'])
- ->offset(($data['page']-1)*$data['pageSize'])
- ->get();
- if(empty($survey->toArray())){
- return Result::error("暂无调查问卷!",0);
- }
- $result = [
- 'rows'=>$survey,
- 'count'=>$count
- ];
- return Result::success($result);
- }
- /**
- * 后端-获取网站调查问卷详情
- * @param array $data
- * @return array
- */
- public function getSurveyInfo(array $data): array
- {
- if(isset($data['sur_id']) &&!empty($data['sur_id'])){
- $where = [ 'sur_id'=>$data['sur_id']];
- $choose = ArticleSurvey::where($where)->where('is_other',0)
- ->leftJoin('article','article_survey.art_id','article.id')
- ->select('article_survey.*','article.survey_type')
- ->get()->all();
- if(empty($choose)){
- return Result::error("此调查问卷不存在",0);
- }
- $resultsArray = array_column($choose, 'results');
- $total = array_sum($resultsArray);
- $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
- $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
- // $total = 0;
- if(!empty($other)){
- $total = $total + $other['results'];
- $other['choice_name'] = $other['choice_name'].'(其他)';
- if(!empty($others)){
- $other['hasChildren'] = true;
- // array_push($other,['hasChildren','=',true]);
- $other['children'] = $others;
- $other_choices = [$other->toArray()];
- $mer_choice = array_merge($choose,$other_choices);
- $value_choice = array_values($mer_choice);
- }
- else{
- // return Result::error('1111');
- $other_choices = [$other->toArray()];
- $other_choices = array_merge($choose,$other_choices);
- $value_choice = array_values($other_choices);
- // return Result::success($result);
- }
- }else{
- $value_choice = $choose;
- }
- $result = [
- 'choose'=>$value_choice,
- 'total'=>$total
- ];
- }
- return Result::success($result);
- }
- /**
- * 前端-搜索新闻列表
- * @param array $data
- * @return array
- */
- public function selectWebsiteArticle(array $data): array
- {
- $where = [];
- // 初始化查询构造器
- $category = WebsiteCategory::where('website_id',$data['website_id'])->pluck('category_id');
- $query = Article::where('status', 1)
- ->whereIn('catid', $category)
- ->where(function ($query) use ($data) {
- $query->where(function ($subQuery) use ($data) {
- $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
- })->orWhereNull("ignore_ids");
- });
- // return Result::success($all_articles);
- // 检查是否存在 cityid 参数
- if (isset($data['cityid']) && !empty($data['cityid'])) {
- $query->whereRaw("JSON_CONTAINS(city_arr_id, '".intval($data['cityid'])."')");
- }
- // 检查是否存在 department_id 参数
- if (isset($data['department_id']) && !empty($data['department_id'])) {
- $query->whereRaw("JSON_CONTAINS(department_arr_id, '".intval($data['department_id'])."')");
- }
- // 检查是否存在 keyword 参数
- if (isset($data['keyword']) && !empty($data['keyword'])) {
- $query->where('title', 'like', '%'.$data['keyword'].'%');
- }
- // 计算总数
- $count = $query->count();
- // 分页查询
- $articles = $query->orderBy("updated_at", "desc")
- ->limit($data['pageSize'])
- ->offset(($data['page'] - 1) * $data['pageSize'])
- ->get()->all();
- if (empty($articles)) {
- return Result::error("没有符合条件的资讯数据");
- }
- $data = [
- 'rows' => $articles,
- 'count' => $count
- ];
- return Result::success($data);
- }
-
- /**
- * 模块新闻加强版
- * @param array $data
- * @return array
- */
- public function getWebsiteCatidArticle(array $data): array
- {
- $where = [
- // 'category.status' => 1,
- 'website_category.category_id' => $data['catid'],
- 'website_category.website_id' => $data['website_id'],
- // 'article.status' => 1,
- ];
- // $category = WebsiteCategory::where($where);
- if (!empty($data['img_num'])) {
- $category['img'] = WebsiteCategory::where($where)
- ->leftJoin('article', 'article.catid', 'website_category.category_id')
- ->where('article.status', 1)
- ->where('article.imgurl', '!=', '')
- ->select('article.*','website_category.category_id','website_category.alias')
- ->orderBy('article.updated_at', 'desc')
- ->limit($data['img_num'])
- ->get();
- }
- if (!empty($data['text_num'])) {
- $category['text'] = WebsiteCategory::where($where)
- ->leftJoin('article', 'article.catid', 'website_category.category_id')
- ->where('article.status', 1)
- ->where(function ($query) {
- $query->whereNull('article.imgurl')
- ->orWhere('article.imgurl', '');
- })
- ->select('article.*','website_category.category_id','website_category.alias')
- ->orderBy('article.updated_at', 'desc')
- ->limit($data['text_num'])
- ->get();
- }
- // $category = $category->get();
-
- if(empty($category)){
- return Result::error("查询失败", 0);
- }
- return Result::success($category);
- }
- }
|