12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154 |
- <?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'] ='(其他)';
- 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
- {
- // return Result::success($data);
- $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(isset($data['child_catnum']) && !empty($data['child_catnum'])){
- $child_catnum = $data['child_catnum']?? 1;
- $category['child'] = WebsiteCategory::where('pid',$data['catid'])->where('website_id',$data['website_id'])->select('category_id','alias')->limit($child_catnum)->get()->toArray();
- $childCategoryIds = array_column($category['child'], 'category_id');
- if(empty($childCategoryIds)){
- return Result::error("暂无子栏目",0);
- }
- $imgArticles = [];
- $textArticles = [];
- // return Result::success($childCategoryIds);
- if(isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum']!=0){
- // 初始化子分类图片新闻和文字新闻数组
-
- // 查询所有子级栏目的图文新闻
- $imgArticles = Article::where('catid', $childCategoryIds[0])
- ->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");
- })
- ->where('status', 1)
- ->where('imgurl', '!=', '')
- ->select('*')
- ->orderBy('updated_at', 'desc')
- ->limit($data['child_imgnum'])
- ->get();
- }
- if( isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum']!=0){
- // 查询所有子级栏目的文字新闻
- $textArticles = Article::where('catid', $childCategoryIds[0])
- ->where('status', 1)
- ->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");
- })
- // ->where(function ($query) {
- // $query->whereNull('imgurl')
- // ->orWhere('imgurl', '');
- // })
- ->select('*')
- ->orderBy('updated_at', 'desc')
- ->limit($data['child_textnum'])
- ->get();
- }
- // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
- $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
- $child['img'] = $imgArticles? $imgArticles->where('catid', $child['category_id']) : [];
- $child['text'] = $textArticles? $textArticles->where('catid', $child['category_id']) : [];
- return $child;
- }, $category['child']);
-
- }
- // }
- if (isset($data['img_num']) && !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 (isset($data['text_num']) && !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);
- }
- /**
- * 模块新闻加强plus版
- * @param array $data
- * @return array
- */
- public function getWebsiteAllArticle(array $data): array
- {
- // 修正传入的字符串,将单引号替换为双引号
- $input['id'] = $data['id'];
- $input['website_id'] = $data['website_id'];
- // 将 JSON 字符串转换为 PHP 数组
- $data = json_decode($input['id'], true);
- // 使用 array_map 处理每个元素
- $result = array_map(function ($item) use ($input) {
- list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
- $website = [
- 'website_id' => $input['website_id'],
- ];
- // 查询栏目名称
- $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
- if(empty($category)){
- $imgArticles = [];
- $textArticles = [];
- // return Result::error("暂无此栏目",0);
- }else{
- $parent_alias = $category->alias ?? '';
- $parent_pinyin = $category->aLIas_pinyin ?'/'.$category->aLIas_pinyin.'/' : null;
- // 查询图片新闻
- $imgArticles = Article::where('catid', $parentCatId)
- ->where('status', 1)
- ->where(function ($query) use ($website) {
- $query->where(function ($subQuery) use ($website) {
- $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
- })->orWhereNull("ignore_ids");
- })
- ->where('imgurl', '!=', '')
- ->select(
- 'article.id',
- 'article.title',
- 'article.imgurl',
- 'article.author',
- 'article.updated_at',
- 'article.introduce',
- 'article.islink',
- 'article.linkurl',
- 'article.copyfrom')
- ->orderBy('updated_at', 'desc')
- ->limit($parentImgNum)
- ->get()->all();
- // 查询文字新闻
- $textArticles = Article::where('catid', $parentCatId)
- ->where('status', 1)
- ->where(function ($query) use ($website) {
- $query->where(function ($subQuery) use ($website) {
- $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
- })->orWhereNull("ignore_ids");
- })
- ->select(
- 'article.id',
- 'article.title',
- 'article.author',
- 'article.updated_at',
- 'article.introduce',
- 'article.islink',
- 'article.linkurl',
- 'article.copyfrom')
- ->orderBy('updated_at', 'desc')
- ->limit($parentTextNum)
- ->get()->all();
- }
- $resultItem = [
- 'alias' => $parent_alias,
- 'category_id' => $parentCatId,
- 'pinyin' => $parent_pinyin,
- 'imgnum' => $imgArticles,
- 'textnum' => $textArticles,
-
- ];
- if (!empty($item['child']) && $item['child'] != "") {
- $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin : '';
- $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
- ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin, '/') as aLIas_pinyin", [$parent_pinyin_str])
- ->get()->all();
- if ($childCategory) {
- list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
- // 查询子栏目名称
- $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
- ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin, '/') as aLIas_pinyin", [$parent_pinyin])
- ->first();
- if(empty($childCategoryInfo)){
- $childImgArticles = [];
- $childTextArticles = [];
- }else{
- // 查询子栏目图片新闻
- $childImgArticles = Article::where('catid', $childCatId)
- ->where('status', 1)
- ->where(function ($query) use ($website) {
- $query->where(function ($subQuery) use ($website) {
- $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
- })->orWhereNull("ignore_ids");
- })
- ->select(
- 'article.id',
- 'article.title',
- 'article.imgurl',
- 'article.author',
- 'article.updated_at',
- 'article.introduce',
- 'article.islink',
- 'article.linkurl',
- 'article.copyfrom')
- ->where('imgurl', '!=', '')
- ->orderBy('updated_at', 'desc')
- ->limit($childImgNum)
- ->get()->all();
- // 查询子栏目文字新闻
- $childTextArticles = Article::where('catid', $childCatId)
- ->where('status', 1)
- ->where(function ($query) use ($website) {
- $query->where(function ($subQuery) use ($website) {
- $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
- })->orWhereNull("ignore_ids");
- })
- ->select(
- 'article.id',
- 'article.title',
- 'article.author',
- 'article.updated_at',
- 'article.introduce',
- 'article.islink',
- 'article.linkurl',
- 'article.copyfrom')
- ->orderBy('updated_at', 'desc')
- ->limit($childTextNum)
- ->get()->all();
- }
- $resultItem['child'] = [
- 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
- 'category_id' => $childCatId,
- 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
- 'all_childcat' => $childCategory,
- 'imgnum' => $childImgArticles,
- 'textnum' => $childTextArticles,
-
- ];
- // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
- }
- }
- return $resultItem;
- }, $data);
- return Result::success($result);
- // return Result::success($data);
- }
- /**
- * 乡村网-获取特殊新闻模块
- * @param array $data
- * @return array
- */
- public function getWebsiteArticles(array $data): array
- {
- $input['id'] = $data['id'];
- $input['website_id'] = $data['website_id'];
- $data = json_decode($input['id'], true);
- // 使用 array_map 处理每个元素
- $result = array_map(function ($item) use ($input) {
- list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
- $website = [
- 'website_id' => $input['website_id']
- ];
- // 查询栏目名称
- $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
- $pinyin = $category->aLIas_pinyin ? '/'.$category->aLIas_pinyin.'/' : null;
- if(empty($category)){
- $imgArticles = [];
- $textArticles = [];
- // return Result::error("暂无此栏目",0);
- }else{
- // return Result::success($website);
- // 查询图片新闻
- // 合并查询条件
- $baseQuery = Article::where(function ($query) use ($website) {
- $query->where(function ($subQuery) use ($website) {
- $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
- })->orWhereNull("ignore_ids");
- })
- ->whereRaw("JSON_CONTAINS(category_arr_id, '" . intval($parentCatId) . "')")
- ->leftJoin('website_category', 'website_category.category_id', 'article.catid')
- ->where('website_category.website_id', $website['website_id'])
- ->where('article.status', 1);
-
- // 查询文字新闻
- $textArticles = clone $baseQuery;
- $textArticles = $textArticles
- ->select(
- 'article.id',
- 'article.title',
- // 'article.imgurl',
- 'article.author',
- 'article.updated_at',
- 'article.introduce',
- 'article.islink',
- 'article.linkurl',
- 'article.copyfrom',
- 'website_category.category_id',
- 'website_category.alias',
- 'website_category.aLIas_pinyin'
- )
- ->selectRaw("CONCAT(?, aLIas_pinyin, '/') as aLIas_pinyin", [$pinyin])
- ->orderBy('article.updated_at', 'desc')
- ->limit($parentTextNum)
- ->get()
- ->all();
- // 查询图片新闻
- $imgArticles = clone $baseQuery;
- $imgArticles = $imgArticles
- ->select(
- 'article.id',
- 'article.title',
- 'article.imgurl',
- 'article.author',
- 'article.updated_at',
- 'article.introduce',
- 'article.islink',
- 'article.linkurl',
- 'article.copyfrom',
- 'website_category.category_id',
- 'website_category.alias',
- 'website_category.aLIas_pinyin'
- )
- ->selectRaw("CONCAT(?, aLIas_pinyin, '/') as aLIas_pinyin", [$pinyin])
- ->orderBy('article.updated_at', 'desc')
- ->where('imgurl', '!=', '')
- ->limit($parentImgNum)
- ->get()
- ->all();
-
- }
-
- $resultItem = [
- 'alias' => $category ? $category->alias : null,
- 'category_id' => $parentCatId,
- 'pinyin' => $pinyin ? $pinyin : null,
- 'imgnum' => $imgArticles ?? [],
- 'textnum' => $textArticles ?? [],
-
- ];
- return $resultItem;
- }, $data);
- return Result::success($result);
- }
-
- }
|