NewsService.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\ArticleData;
  5. use App\Model\Category;
  6. use App\Model\Website;
  7. use App\Model\WebsiteCategory;
  8. use App\Model\Good;
  9. use Hyperf\DbConnection\Db;
  10. use Hyperf\RpcServer\Annotation\RpcService;
  11. use App\Tools\Result;
  12. use App\Model\ArticleSurvey;
  13. use App\Model\District;
  14. use App\Tools\buildMenuTree;
  15. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  16. class NewsService implements NewsServiceInterface
  17. {
  18. /**
  19. * 获取导航池列表
  20. * @param array $data
  21. * @return array
  22. */
  23. public function getCategoryList(array $data): array
  24. {
  25. $where = [];
  26. if(isset($data['name']) && $data['name']){
  27. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  28. }
  29. if(isset($data['department_id']) && $data['department_id']){
  30. array_push($where, ['category.department_id','=',$data['department_id']]);
  31. }
  32. if(isset($data['city_id']) && $data['city_id']){
  33. array_push($where, ['category.city_id','=',$data['city_id']]);
  34. }
  35. $rep = Category::where($where)
  36. ->leftJoin('district','category.city_id','district.id')
  37. ->leftJoin('department','category.department_id','department.id')
  38. ->select("category.*","district.name as city_name","department.name as department_name")
  39. ->limit($data['pageSize'])->orderBy("category.sort","desc")->orderByDesc('category.updated_at')->offset(($data['page']-1)*$data['pageSize'])->get();
  40. $count = Category::where($where)->count();
  41. $data = [
  42. 'rows'=>$rep->toArray(),
  43. 'count'=>$count
  44. ];
  45. if(empty($rep->toArray())){
  46. return Result::error("没有导航池数据");
  47. }
  48. return Result::success($data);
  49. }
  50. /**
  51. * @param array $data
  52. * @return array
  53. */
  54. public function categoryList(array $data): array
  55. {
  56. $where[] = [
  57. 'pid','=',$data['pid']
  58. ];
  59. if(isset($data['name'])){
  60. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  61. }
  62. var_dump($where);
  63. $result = Category::where($where)->select('category.*','category.id as category_id')->get();
  64. if(empty($result)){
  65. return Result::error("没有栏目数据");
  66. }
  67. return Result::success($result);
  68. }
  69. /**
  70. * @param array $data
  71. * @return array
  72. */
  73. public function addCategory(array $data): array
  74. {
  75. $id = Category::insertGetId($data);
  76. if(empty($id)){
  77. return Result::error("添加失败");
  78. }
  79. return Result::success(['id'=>$id]);
  80. }
  81. /**
  82. * @param array $data
  83. * @return array
  84. */
  85. public function delCategory(array $data): array
  86. {
  87. $categoryList = Category::where(['pid'=>$data['id']])->get();
  88. var_dump("分类列表:",$data,$categoryList);
  89. if($categoryList->toArray()){
  90. return Result::error("分类下面有子分类不能删除");
  91. }
  92. $articleList = Article::where(['catid'=>$data['id']])->get();
  93. var_dump("文章列表:",$articleList);
  94. if($articleList->toArray()){
  95. return Result::error("分类下面有资讯不能删除");
  96. }
  97. $result = Category::where($data)->delete();
  98. if(!$result){
  99. return Result::error("删除失败");
  100. }
  101. return Result::success($result);
  102. }
  103. /**
  104. * @param array $data
  105. * @return array
  106. */
  107. public function updateCategory(array $data): array
  108. {
  109. $where = [
  110. 'id'=>$data['id']
  111. ];
  112. $result = Category::where($where)->update($data);
  113. if($result){
  114. return Result::success($result);
  115. }else{
  116. return Result::error("更新失败");
  117. }
  118. }
  119. /**
  120. * 获取导航池信息
  121. * @param array $data
  122. * @return array
  123. */
  124. public function getCategoryInfo(array $data): array
  125. {
  126. $where = [
  127. 'id'=>$data['id']
  128. ];
  129. $result = Category::where($where)->first();
  130. if($result){
  131. return Result::success($result);
  132. }else{
  133. return Result::error("更新失败");
  134. }
  135. }
  136. /**
  137. * @param array $data
  138. * @return array
  139. */
  140. public function getArticleList(array $data): array
  141. {
  142. $where= [];
  143. if(isset($data['title']) && $data['title']){
  144. array_push($where,['article.title','like','%'.$data['title'].'%']);
  145. }
  146. if(isset($data['category_name']) && $data['category_name']){
  147. array_push($where,['category.name','like','%'.$data['category_name'].'%']);
  148. }
  149. if(isset($data['author']) && $data['author']){
  150. array_push($where,['article.author','=',$data['author']]);
  151. }
  152. if(isset($data['islink']) && $data['islink']!==""){
  153. array_push($where,['article.islink','=',$data['islink']]);
  154. }
  155. if(isset($data['status']) && $data['status']!==""){
  156. array_push($where,['article.status','=',$data['status']]);
  157. }
  158. $rep = Article::where($where)
  159. ->whereNotIn('article.status',[404])
  160. ->leftJoin('category','article.catid','category.id')
  161. ->select("article.*","category.name as category_name")
  162. ->orderBy("article.id","desc")
  163. ->limit($data['pageSize'])
  164. ->offset(($data['page']-1)*$data['pageSize'])->get();
  165. $count = Article::where($where)->whereNotIn('article.status',[404])
  166. ->leftJoin('category','article.catid','category.id')->count();
  167. $data = [
  168. 'rows'=>$rep->toArray(),
  169. 'count'=>$count
  170. ];
  171. if(empty($rep)){
  172. return Result::error("没有信息数据");
  173. }
  174. return Result::success($data);
  175. }
  176. /**
  177. * @param array $data
  178. * @return array
  179. */
  180. public function addArticle(array $data): array
  181. {
  182. Db::beginTransaction();
  183. try{
  184. $articleData = $data;
  185. unset($articleData['content']);
  186. $id = Article::insertGetId($articleData);
  187. $articleDataContent = [
  188. 'article_id'=>$id,
  189. 'content'=>$data['content']
  190. ];
  191. ArticleData::insertGetId($articleDataContent);
  192. Db::commit();
  193. } catch(\Throwable $ex){
  194. Db::rollBack();
  195. var_dump($ex->getMessage());
  196. return Result::error("创建失败",0);
  197. }
  198. return Result::success(['id'=>$id]);
  199. }
  200. /**
  201. * @param array $data
  202. * @return array
  203. */
  204. public function delArticle(array $data): array
  205. {
  206. $result = Article::where($data)->update(['status'=>404]);
  207. if(!$result){
  208. return Result::error("删除失败");
  209. }
  210. return Result::success($result);
  211. }
  212. /**
  213. * @param array $data
  214. * @return array
  215. */
  216. public function updateArticle(array $data): array
  217. {
  218. Db::beginTransaction();
  219. try{
  220. $data['cat_arr_id'] = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
  221. $data['tag'] = isset($data['tag'])?json_encode($data['tag']):'';
  222. $articleData = $data;
  223. unset($articleData['content']);
  224. unset($articleData['status_name']);
  225. unset($articleData['name']);
  226. unset($articleData['content']);
  227. unset($articleData['pid_arr']);
  228. unset($articleData['pid']);
  229. $id = Article::where(['id'=>$data['id']])->update($articleData);
  230. $articleDataContent = [
  231. 'content'=>$data['content']
  232. ];
  233. ArticleData::where(['article_id'=>$data['id']])->update($articleDataContent);
  234. } catch(\Throwable $ex){
  235. Db::rollBack();
  236. var_dump($ex->getMessage());
  237. return Result::error("更新失败",0);
  238. }
  239. return Result::success([]);
  240. }
  241. /**
  242. * 更新资讯状态
  243. * @param array $data
  244. * @return array
  245. */
  246. public function upArticleStatus(array $data):array
  247. {
  248. $result = Article::where(['id'=>$data['id']])->update($data);
  249. if($result){
  250. return Result::success();
  251. }else{
  252. return Result::error("更新状态失败",0);
  253. }
  254. }
  255. /**
  256. * 获取新闻详情
  257. * @param array $data
  258. * @return array
  259. */
  260. public function getArticleInfo(array $data): array
  261. {
  262. $where = [
  263. 'article.id'=>$data['id'],
  264. // 'article.status'=>1
  265. ];
  266. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  267. if(empty($result)){
  268. return Result::error("查询失败",0);
  269. }
  270. return Result::success($result);
  271. }
  272. /**
  273. * 获取头条新闻
  274. * @param array $data
  275. * @return array
  276. */
  277. public function getWebsiteArticlett(array $data): array
  278. {
  279. // return Result::success($data['website_id']);
  280. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  281. $category = array_values(array_unique($category->toArray()));
  282. if ($category) {
  283. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  284. $where = [
  285. 'status' => 1,
  286. ];
  287. var_dump($data, 'data-----------------');
  288. // level=7 根据文章key来匹配文章
  289. if (isset($data['level']) && $data['level'] == 7) {
  290. // 根据文章id获取key
  291. // $data['id'] = 50142;
  292. if (isset($data['id']) && !empty($data['id'])) {
  293. $keyword = Article::where('id', $data['id'])->value('keyword');
  294. $keywordArray = explode(',', $keyword);
  295. $whereL7 = [];
  296. foreach ($keywordArray as $k => $v) {
  297. $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
  298. }
  299. // 原始查询
  300. $result['level7'] = Article::where($whereL7)
  301. ->when(!empty($data['imgnum']), function ($query) use ($data) {
  302. $query->where('article.imgurl', '!=', '')
  303. ->select('article.*')
  304. ->offset($data['placeid'])
  305. ->limit($data['imgnum']);
  306. })
  307. ->when(!empty($data['textnum']), function ($query) use ($data) {
  308. $query->select('article.*')
  309. ->offset($data['placeid'])
  310. ->limit($data['textnum']);
  311. })
  312. ->orderBy('updated_at', 'desc')
  313. ->get()
  314. ->map(function ($article ) use ($data) {
  315. $catid = $article->catid;
  316. $pinyin = '';
  317. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  318. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  319. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  320. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  321. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  322. }
  323. } else {
  324. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  325. }
  326. $article->pinyin = $pinyin;
  327. return $article;
  328. });
  329. if (empty($result)) {
  330. return Result::success();
  331. }
  332. return Result::success($result);
  333. } else {
  334. return Result::error("参数错误level=7,id不能为空", 0);
  335. }
  336. }
  337. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  338. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  339. var_dump($where, 'where-----------------');
  340. // 初始化基础查询
  341. $query = Article::where($where)
  342. ->whereIn("catid", $category)
  343. ->where(function ($query) use ($data) {
  344. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  345. ->orWhereNull("ignore_ids");
  346. });
  347. // 处理 level 相关查询条件
  348. $query->when($data['level'] == 5, function ($query) {
  349. $query->inRandomOrder()
  350. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  351. })->when($data['level'] == 4, function ($query) {
  352. $query->orderBy("article.updated_at", "desc");
  353. })->when(!empty($data['level']), function ($query) use ($data) {
  354. if ($data['level'] != 4 && $data['level'] != 5) {
  355. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  356. ->orderBy("article.updated_at", "desc");
  357. }
  358. });
  359. // 初始化结果数组
  360. $result = [];
  361. // $input = $data['website_id'];
  362. // 处理图片新闻查询
  363. if (!empty($data['imgnum'])) {
  364. // 原错误提示表明 cleanBindings 方法需要传入数组类型的参数,而实际传入了 null。
  365. // 这里克隆查询构建器,确保绑定参数为数组,避免该错误。
  366. $imgQuery = clone $query;
  367. if ($imgQuery->getBindings() === null) {
  368. $imgQuery->setBindings([]);
  369. }
  370. $result['img'] = $imgQuery->where('article.imgurl', '!=', '')
  371. ->select('article.id',
  372. 'article.title',
  373. 'article.imgurl',
  374. 'article.author',
  375. 'article.updated_at',
  376. 'article.introduce',
  377. 'article.islink',
  378. 'article.linkurl',
  379. 'article.copyfrom',
  380. 'article.catid')
  381. ->offset($data['placeid'])
  382. ->limit($data['imgnum'])
  383. ->get()
  384. ->map(function ($article ) use ($data) {
  385. $catid = $article->catid;
  386. $pinyin = '';
  387. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  388. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  389. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  390. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  391. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  392. }
  393. } else {
  394. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  395. }
  396. $article->pinyin = $pinyin;
  397. return $article;
  398. });
  399. // $result['img'] = $this->processArticles($img_article, $input);
  400. }
  401. // 处理文字新闻查询
  402. if (!empty($data['textnum'])) {
  403. $textQuery = clone $query;
  404. $result['text'] = $textQuery
  405. ->select('article.id',
  406. 'article.title',
  407. 'article.imgurl',
  408. 'article.author',
  409. 'article.updated_at',
  410. 'article.introduce',
  411. 'article.islink',
  412. 'article.linkurl',
  413. 'article.copyfrom',
  414. 'article.catid')
  415. ->offset($data['placeid'])
  416. ->limit($data['textnum'])
  417. ->get()
  418. ->map(function ($article ) use ($data) {
  419. $catid = $article->catid;
  420. $pinyin = '';
  421. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  422. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  423. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  424. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  425. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  426. }
  427. } else {
  428. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  429. }
  430. $article->pinyin = $pinyin;
  431. return $article;
  432. });
  433. // $result['text'] = $this->processArticles($text_article, $input);
  434. }
  435. if (empty($result) || count($result) == 0) {
  436. return Result::error("暂无头条新闻");
  437. }
  438. return Result::success($result);
  439. } else {
  440. return Result::error("本网站下暂无相关栏目", 0);
  441. }
  442. }
  443. /**
  444. * 获取模块新闻
  445. * @param array $data
  446. * @return array
  447. */
  448. public function getWebsiteModelArticles(array $data): array
  449. {
  450. $catid = $data['catid'];
  451. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  452. $category = $category->toArray();
  453. if (!empty($category)) {
  454. $where = [
  455. 'status' => 1,
  456. 'catid' => $catid,
  457. ];
  458. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  459. // 1:文字新闻;2:轮播图;3:图文;
  460. // 级别:0:未分类
  461. // 3:推荐图片
  462. if ($data['level'] == 1) {
  463. $data['level'] = 0;
  464. }
  465. $result = Article::where($where)
  466. ->where(function ($query) use ($data) {
  467. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  468. ->orWhereNull("level")
  469. ->orWhereRaw("level = '[]'");
  470. })
  471. ->where(function ($query) use ($data) {
  472. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  473. ->orWhereNull("ignore_ids");
  474. })
  475. ->orderBy("updated_at", "desc")
  476. ->offset($placeid)
  477. ->limit($data['pagesize'])
  478. ->get();
  479. if (empty($result)) {
  480. return Result::error("此栏目暂无相关新闻", 0);
  481. }
  482. } else {
  483. return Result::error("此网站暂无此栏目", 0);
  484. }
  485. return Result::success($result);
  486. }
  487. /**
  488. *获取新闻列表
  489. * @param array $data
  490. * @return array
  491. */
  492. public function getWebsiteArticleList(array $data): array
  493. {
  494. // return Result::success($data);
  495. $where[] = ['status', '=', 1];
  496. if (isset($data['catid']) && !empty($data['catid'])) {
  497. if (is_array($data['catid'])) {
  498. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  499. array_push($where, ['catid', 'in', $data['catid']]);
  500. } else {
  501. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  502. array_push($where, ['catid', '=', $data['catid']]);
  503. }
  504. if (empty($category)) {
  505. return Result::error("此网站暂无此栏目", 0);
  506. }
  507. }
  508. // return Result::success($where);
  509. $rep = Article::where(function ($query) use ($where) {
  510. foreach ($where as $condition) {
  511. if ($condition[1] === 'in') {
  512. $query->whereIn($condition[0], $condition[2]);
  513. } else {
  514. $query->where($condition[0], $condition[1], $condition[2]);
  515. }
  516. }
  517. })
  518. ->where(function ($query) use ($data) {
  519. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  520. ->orWhereNull("ignore_ids");
  521. })
  522. ->select(
  523. 'article.id',
  524. 'article.title',
  525. 'article.imgurl',
  526. 'article.author',
  527. 'article.updated_at',
  528. 'article.introduce',
  529. 'article.islink',
  530. 'article.linkurl',
  531. 'article.copyfrom',
  532. 'article.catid')
  533. ->orderBy("updated_at", "desc")
  534. ->offset(($data['page'] - 1) * $data['pageSize'])
  535. ->limit($data['pageSize'])
  536. ->get()
  537. ->map(function ($article ) use ($data) {
  538. $catid = $article->catid ?? 0;
  539. $pinyin = '';
  540. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  541. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  542. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  543. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  544. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  545. }
  546. $article->pinyin = $pinyin;
  547. return $article;
  548. });
  549. $count = Article::where(function ($query) use ($where) {
  550. foreach ($where as $condition) {
  551. if ($condition[1] === 'in') {
  552. $query->whereIn($condition[0], $condition[2]);
  553. } else {
  554. $query->where($condition[0], $condition[1], $condition[2]);
  555. }
  556. }
  557. })->count();
  558. $data = [
  559. 'rows' => $rep->toArray(),
  560. 'count' => $count,
  561. ];
  562. if (empty($rep)) {
  563. return Result::error("没有信息数据");
  564. }
  565. return Result::success($data);
  566. }
  567. /**
  568. * 前端-获取新闻详情
  569. * @param array $data
  570. * @return array
  571. */
  572. public function selectWebsiteArticleInfo(array $data): array
  573. {
  574. $where = [
  575. 'article.id'=>$data['id'],
  576. 'article.status'=>1
  577. ];
  578. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
  579. ->where(function ($query) use ($data) {
  580. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  581. ->orWhereNull("ignore_ids");
  582. })
  583. ->first();
  584. if(empty($result)){
  585. return Result::error("暂无此新闻!",0);
  586. }
  587. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  588. if(empty($category)){
  589. return Result::error("查询失败",0);
  590. }
  591. $result['category_id'] = $category['category_id'];
  592. $result['cat_name'] = $category['alias'];
  593. return Result::success($result);
  594. }
  595. /**
  596. * 前端-获取网站调查问卷
  597. * @param array $data
  598. * @return array
  599. */
  600. public function getWebsiteSurvey(array $data): array
  601. {
  602. if(isset($data['website_id']) && !empty($data['website_id'])){
  603. $website = Website::where('id',$data['website_id'])->first();
  604. if(empty($website)){
  605. return Result::error("暂无此网站",0);
  606. }
  607. }
  608. if(isset($data['art_id']) && !empty($data['art_id'])){
  609. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  610. if(empty($article)){
  611. return Result::error("暂无此文章",0);
  612. }
  613. // return Result::error($data,0);
  614. $where['art_id'] = $data['art_id'];
  615. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  616. }else{
  617. $survey = Article::where(function ($query) {
  618. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  619. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  620. })
  621. ->where('status',1)
  622. ->where('is_survey',1)
  623. ->select('survey_id')
  624. ->orderBy('updated_at','desc')
  625. ->first();
  626. if(empty($survey)){
  627. return Result::error("暂无调查问卷",0);
  628. }
  629. $where['sur_id'] = $survey['survey_id'];
  630. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  631. }
  632. // return Result::success($where);
  633. $result = ArticleSurvey::where($where)
  634. ->where(function ($query) {
  635. $query->where('is_other', 0)
  636. ->orWhere(function ($subQuery) {
  637. $subQuery->where('is_other', 1)
  638. ->where('other_id', 0);
  639. });
  640. })
  641. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  642. ->select('article_survey.*', 'article.survey_type')
  643. ->get()->all();
  644. if(empty($result)){
  645. return Result::error("此文章暂无调查问卷",0);
  646. }
  647. return Result::success($result);
  648. }
  649. /**
  650. * 前端-添加网站调查问卷选项
  651. * @param array $data
  652. * @return array
  653. */
  654. public function addWebsiteSurveyOption(array $data): array
  655. {
  656. if(isset($data['website_id']) && !empty($data['website_id'])){
  657. $website = Website::where('id',$data['website_id'])->first();
  658. if(empty($website)){
  659. return Result::error("暂无此网站",0);
  660. }
  661. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  662. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('is_other',1)->where('other_id',0)->first();
  663. if(empty($survey)){
  664. return Result::error("此调查问卷不可添加选项",0);
  665. }
  666. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  667. $choice = [
  668. 'art_id'=>$survey['art_id'],
  669. 'website_id'=>$data['website_id'],
  670. 'survey_name'=>$survey['survey_name'],
  671. 'choice_name'=>$data['choice_name'],
  672. 'sur_id'=>$survey['sur_id'],
  673. 'is_other'=>1,
  674. 'other_id'=>$survey['id'],
  675. ];
  676. $result = ArticleSurvey::insertGetId($choice);
  677. if(empty($result)){
  678. return Result::error("添加失败",0);
  679. }
  680. return Result::success($result);
  681. }
  682. }
  683. return Result::error("添加失败",0);
  684. }
  685. return Result::error("添加失败",0);
  686. }
  687. /**
  688. * 前端-调查问卷投票
  689. * @param array $data
  690. * @return array
  691. */
  692. public function addWebsiteSurveyVote(array $data): array
  693. {
  694. // return Result::success($data);
  695. if(isset($data['website_id']) && !empty($data['website_id'])){
  696. $website = Website::where('id',$data['website_id'])->first();
  697. if(empty($website)){
  698. return Result::error("暂无此网站",0);
  699. }
  700. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  701. $is_survey = ArticleSurvey::where('sur_id',$data['sur_id'])->first();
  702. // return Result::success($survey);
  703. if(empty($is_survey)){
  704. return Result::error("此调查问卷不存在",0);
  705. }
  706. // return Result::success($survey);
  707. // 调查问卷类型
  708. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  709. //多选 若是json型则转化成数组类型
  710. if (strpos($data['choice_id'], '[') === 0) {
  711. $data['choice_id'] = json_decode($data['choice_id'], true);
  712. } else {
  713. // 单选 也转换成数组
  714. $data['choice_id'] = [$data['choice_id']];
  715. }
  716. $data['choice_id'] = array_map('intval', $data['choice_id']);
  717. $other = ArticleSurvey::whereIn('id',$data['choice_id'])
  718. // ->where('website_id',$data['website_id'])
  719. ->where('is_other',1)
  720. ->where('other_id',0)
  721. ->first();
  722. if(!empty($other)){
  723. return Result::error("请选择已有的选项!",0);
  724. }
  725. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  726. // ->where('website_id',$data['website_id'])
  727. ->where('is_other',1)
  728. ->where('other_id','!=',0)
  729. ->first();
  730. // return Result::success($choice['other']);
  731. $choice_id = $data['choice_id'];
  732. if(!empty($choice['other'])){
  733. // array_push($data['choice_id'],$choice['other']['other_id']);
  734. if(!empty($choice_id)){
  735. $key = array_search($choice['other']['id'], $choice_id);
  736. if ($key!== false) {
  737. unset($choice_id[$key]);
  738. $choice_id = array_values($choice_id);
  739. }
  740. array_push($choice_id,$choice['other']['other_id']);
  741. }else{
  742. $choice_id[0] = $choice['other']['other_id'];
  743. }
  744. array_push($data['choice_id'],$choice['other']['other_id']);
  745. }
  746. // return Result::success($data);
  747. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  748. // ->where('website_id',$data['website_id'])
  749. ->increment('results', 1);
  750. if(empty($choice)){
  751. return Result::error("请选择已有的选项!",0);
  752. }
  753. $survey['data'] = ArticleSurvey::where('sur_id',$data['sur_id'])
  754. // ->where('website_id',$data['website_id'])
  755. ->where('other_id', 0)
  756. ->get();
  757. $survey['choice'] = $choice_id;
  758. return Result::success($survey);
  759. }
  760. return Result::error("参数必填!");
  761. }
  762. return Result::error("此调查问卷不存在",0);
  763. }
  764. return Result::error("参数必填!");
  765. }
  766. /**
  767. * 后端-获取网站调查问卷列表
  768. * @param array $data
  769. * @return array
  770. */
  771. public function getSurveyList(array $data): array
  772. {
  773. $where = [];
  774. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  775. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  776. }
  777. if(isset($data['survey_type']) && $data['survey_type'] != null){
  778. array_push($where,['survey_type','=',$data['survey_type']]);
  779. }
  780. if(isset($data['is_survey']) && $data['is_survey'] != null){
  781. array_push($where,['is_survey','=',$data['is_survey']]);
  782. }
  783. // return Result::success($where);
  784. if (!empty($where)) {
  785. $query = Article::where($where)->where(function ($q) {
  786. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  787. });
  788. } else {
  789. $query = Article::where(function ($q) {
  790. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  791. });
  792. }
  793. $count = $query->count();
  794. $survey = $query->orderByDesc('id')
  795. ->limit($data['pageSize'])
  796. ->offset(($data['page']-1)*$data['pageSize'])
  797. ->get();
  798. if(empty($survey->toArray())){
  799. return Result::error("暂无调查问卷!",0);
  800. }
  801. $result = [
  802. 'rows'=>$survey,
  803. 'count'=>$count
  804. ];
  805. return Result::success($result);
  806. }
  807. /**
  808. * 后端-获取网站调查问卷详情
  809. * @param array $data
  810. * @return array
  811. */
  812. public function getSurveyInfo(array $data): array
  813. {
  814. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  815. $where = [ 'sur_id'=>$data['sur_id']];
  816. $choose = ArticleSurvey::where($where)->where('is_other',0)
  817. ->leftJoin('article','article_survey.art_id','article.id')
  818. ->select('article_survey.*','article.survey_type')
  819. ->get()->all();
  820. if(empty($choose)){
  821. return Result::error("此调查问卷不存在",0);
  822. }
  823. $resultsArray = array_column($choose, 'results');
  824. $total = array_sum($resultsArray);
  825. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  826. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  827. // $total = 0;
  828. if(!empty($other)){
  829. $total = $total + $other['results'];
  830. $other['choice_name'] ='(其他)';
  831. if(!empty($others)){
  832. $other['hasChildren'] = true;
  833. // array_push($other,['hasChildren','=',true]);
  834. $other['children'] = $others;
  835. $other_choices = [$other->toArray()];
  836. $mer_choice = array_merge($choose,$other_choices);
  837. $value_choice = array_values($mer_choice);
  838. }
  839. else{
  840. // return Result::error('1111');
  841. $other_choices = [$other->toArray()];
  842. $other_choices = array_merge($choose,$other_choices);
  843. $value_choice = array_values($other_choices);
  844. // return Result::success($result);
  845. }
  846. }else{
  847. $value_choice = $choose;
  848. }
  849. $result = [
  850. 'choose'=>$value_choice,
  851. 'total'=>$total
  852. ];
  853. }
  854. return Result::success($result);
  855. }
  856. /**
  857. * 前端-搜索新闻列表
  858. * @param array $data
  859. * @return array
  860. */
  861. public function selectWebsiteArticle(array $data): array
  862. {
  863. $where = [];
  864. // 初始化查询构造器
  865. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  866. $query = Article::where('status', 1)
  867. ->whereIn('catid', $category)
  868. ->where(function ($query) use ($data) {
  869. $query->where(function ($subQuery) use ($data) {
  870. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  871. })->orWhereNull("ignore_ids");
  872. });
  873. // return Result::success($all_articles);
  874. // 检查是否存在 cityid 参数
  875. if (isset($data['cityid']) && !empty($data['cityid'])) {
  876. $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
  877. }
  878. // 检查是否存在 department_id 参数
  879. if (isset($data['department_id']) && !empty($data['department_id'])) {
  880. $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
  881. }
  882. // 检查是否存在 keyword 参数
  883. if (isset($data['keyword']) && !empty($data['keyword'])) {
  884. $query->where('title', 'like', '%' . $data['keyword'] . '%');
  885. }
  886. // 计算总数
  887. $count = $query->count();
  888. // 分页查询
  889. $articles = $query
  890. ->select(
  891. 'article.id',
  892. 'article.title',
  893. 'article.imgurl',
  894. 'article.author',
  895. 'article.updated_at',
  896. 'article.introduce',
  897. 'article.islink',
  898. 'article.linkurl',
  899. 'article.copyfrom',
  900. 'article.catid',
  901. 'article.department_arr_id',
  902. 'article.city_arr_id',)
  903. ->orderBy("updated_at", "desc")
  904. ->offset(($data['page'] - 1) * $data['pageSize'])
  905. ->limit($data['pageSize'])
  906. ->get()
  907. ->map(function ($article ) use ($data) {
  908. $catid = $article->catid ?? 0;
  909. $pinyin = '';
  910. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  911. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  912. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  913. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  914. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  915. }
  916. $article->pinyin = $pinyin;
  917. return $article;
  918. });
  919. if (empty($articles)) {
  920. return Result::error("没有符合条件的资讯数据");
  921. }
  922. $data = [
  923. 'rows' => $articles,
  924. 'count' => $count,
  925. ];
  926. return Result::success($data);
  927. }
  928. /**
  929. * 模块新闻加强版
  930. * @param array $data
  931. * @return array
  932. */
  933. public function getWebsiteCatidArticle(array $data): array
  934. {
  935. // return Result::success($data);
  936. $where = [
  937. // 'category.status' => 1,
  938. 'website_category.category_id' => $data['catid'],
  939. 'website_category.website_id' => $data['website_id'],
  940. // 'article.status' => 1,
  941. ];
  942. // $category = WebsiteCategory::where($where);
  943. if(isset($data['child_catnum']) && !empty($data['child_catnum'])){
  944. $child_catnum = $data['child_catnum']?? 1;
  945. $category['child'] = WebsiteCategory::where('pid',$data['catid'])->where('website_id',$data['website_id'])->select('category_id','alias')->limit($child_catnum)->get()->toArray();
  946. $childCategoryIds = array_column($category['child'], 'category_id');
  947. if(empty($childCategoryIds)){
  948. return Result::error("暂无子栏目",0);
  949. }
  950. $imgArticles = [];
  951. $textArticles = [];
  952. // return Result::success($childCategoryIds);
  953. if(isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum']!=0){
  954. // 初始化子分类图片新闻和文字新闻数组
  955. // 查询所有子级栏目的图文新闻
  956. $imgArticles = Article::where('catid', $childCategoryIds[0])
  957. ->where(function ($query) use ($data) {
  958. $query->where(function ($subQuery) use ($data) {
  959. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  960. })->orWhereNull("ignore_ids");
  961. })
  962. ->where('status', 1)
  963. ->where('imgurl', '!=', '')
  964. ->select('*')
  965. ->orderBy('updated_at', 'desc')
  966. ->limit($data['child_imgnum'])
  967. ->get();
  968. }
  969. if( isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum']!=0){
  970. // 查询所有子级栏目的文字新闻
  971. $textArticles = Article::where('catid', $childCategoryIds[0])
  972. ->where('status', 1)
  973. ->where(function ($query) use ($data) {
  974. $query->where(function ($subQuery) use ($data) {
  975. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  976. })->orWhereNull("ignore_ids");
  977. })
  978. // ->where(function ($query) {
  979. // $query->whereNull('imgurl')
  980. // ->orWhere('imgurl', '');
  981. // })
  982. ->select('*')
  983. ->orderBy('updated_at', 'desc')
  984. ->limit($data['child_textnum'])
  985. ->get();
  986. }
  987. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  988. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  989. $child['img'] = $imgArticles? $imgArticles->where('catid', $child['category_id']) : [];
  990. $child['text'] = $textArticles? $textArticles->where('catid', $child['category_id']) : [];
  991. return $child;
  992. }, $category['child']);
  993. }
  994. // }
  995. if (isset($data['img_num']) && !empty($data['img_num'])) {
  996. $category['img'] = WebsiteCategory::where($where)
  997. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  998. ->where('article.status', 1)
  999. ->where('article.imgurl', '!=', '')
  1000. ->select('article.*','website_category.category_id','website_category.alias')
  1001. ->orderBy('article.updated_at', 'desc')
  1002. ->limit($data['img_num'])
  1003. ->get();
  1004. }
  1005. if (isset($data['text_num']) && !empty($data['text_num'])) {
  1006. $category['text'] = WebsiteCategory::where($where)
  1007. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1008. ->where('article.status', 1)
  1009. // ->where(function ($query) {
  1010. // $query->whereNull('article.imgurl')
  1011. // ->orWhere('article.imgurl', '');
  1012. // })
  1013. ->select('article.*','website_category.category_id','website_category.alias')
  1014. ->orderBy('article.updated_at', 'desc')
  1015. ->limit($data['text_num'])
  1016. ->get();
  1017. }
  1018. // $category = $category->get();
  1019. if(empty($category)){
  1020. return Result::error("查询失败", 0);
  1021. }
  1022. return Result::success($category);
  1023. }
  1024. /**
  1025. * 模块新闻加强plus版
  1026. * @param array $data
  1027. * @return array
  1028. */
  1029. public function getWebsiteAllArticle(array $data): array
  1030. {
  1031. // 修正传入的字符串,将单引号替换为双引号
  1032. $input['id'] = $data['id'];
  1033. $input['website_id'] = $data['website_id'];
  1034. // 将 JSON 字符串转换为 PHP 数组
  1035. $data = json_decode($input['id'], true);
  1036. // 使用 array_map 处理每个元素
  1037. $result = array_map(function ($item) use ($input) {
  1038. // 检查parent元素是否存在且不是undefined
  1039. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  1040. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1041. $website = [
  1042. 'website_id' => $input['website_id'],
  1043. ];
  1044. // 查询栏目名称
  1045. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1046. if(empty($category)){
  1047. $parent_alias = '';
  1048. $parent_pinyin = null;
  1049. $imgArticles = [];
  1050. $textArticles = [];
  1051. }else{
  1052. $parent_alias = $category->alias ?? '';
  1053. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1054. // 查找子分类ID数组
  1055. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1056. array_push($childCategoryIds,$parentCatId);
  1057. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  1058. if($parentImgNum!=0){
  1059. // 查询图片新闻
  1060. $imgArticles = Article::where('article.status', 1)
  1061. ->where(function($query) use ($parentCatId) {
  1062. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1063. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1064. })
  1065. ->where(function ($query) use ($website) {
  1066. $query->where(function ($subQuery) use ($website) {
  1067. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1068. ->orWhereNull("ignore_ids");
  1069. });
  1070. })
  1071. ->where('imgurl', '!=', '')
  1072. ->leftJoin('website_category', function($join) use ($website) {
  1073. $join->on('article.catid', '=', 'website_category.category_id')
  1074. ->where('website_category.website_id', '=', $website['website_id']);
  1075. })
  1076. ->select(
  1077. 'article.id',
  1078. 'article.title',
  1079. 'article.imgurl',
  1080. 'article.author',
  1081. 'article.updated_at',
  1082. 'article.introduce',
  1083. 'article.islink',
  1084. 'article.linkurl',
  1085. 'article.copyfrom',
  1086. 'article.catid',
  1087. 'website_category.alias as category_name',
  1088. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1089. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1090. )
  1091. ->orderBy('updated_at', 'desc')
  1092. ->limit($parentImgNum)
  1093. ->get()->all();
  1094. // 查询文字新闻
  1095. }
  1096. if($parentTextNum!=0){
  1097. $textArticles = [];
  1098. $textArticles = Article::where('article.status', 1)
  1099. ->where(function($query) use ($parentCatId) {
  1100. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1101. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1102. })
  1103. ->where(function ($query) use ($website) {
  1104. $query->where(function ($subQuery) use ($website) {
  1105. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1106. ->orWhereNull("ignore_ids");
  1107. });
  1108. })
  1109. ->leftJoin('website_category', function($join) use ($website) {
  1110. $join->on('article.catid', '=', 'website_category.category_id')
  1111. ->where('website_category.website_id', '=', $website['website_id']);
  1112. })
  1113. ->select(
  1114. 'article.id',
  1115. 'article.title',
  1116. 'article.imgurl',
  1117. 'article.author',
  1118. 'article.updated_at',
  1119. 'article.introduce',
  1120. 'article.islink',
  1121. 'article.linkurl',
  1122. 'article.copyfrom',
  1123. 'article.catid',
  1124. 'website_category.alias as category_name',
  1125. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1126. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1127. )
  1128. ->orderBy('updated_at', 'desc')
  1129. ->limit($parentTextNum)
  1130. ->get()->all();
  1131. }
  1132. }
  1133. }
  1134. $resultItem = [
  1135. 'alias' => $parent_alias ?? '',
  1136. 'category_id' => $parentCatId ?? 0,
  1137. 'pinyin' => $parent_pinyin ?? null,
  1138. 'imgnum' => $imgArticles ?? [],
  1139. 'textnum' => $textArticles ?? [],
  1140. ];
  1141. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1142. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1143. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1144. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1145. ->get()->all();
  1146. if (!empty($childCategory)) {
  1147. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1148. // 查询子栏目名称
  1149. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1150. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1151. ->first();
  1152. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1153. $childImgArticles = [];
  1154. $childTextArticles = [];
  1155. $resultItem['child'] = [];
  1156. }else{
  1157. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1158. // 查询子栏目图片新闻
  1159. $childImgArticles = Article::where('catid', $childCatId)
  1160. ->where('status', 1)
  1161. ->where(function ($query) use ($website) {
  1162. $query->where(function ($subQuery) use ($website) {
  1163. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1164. })->orWhereNull("ignore_ids");
  1165. })
  1166. ->select(
  1167. 'article.id',
  1168. 'article.title',
  1169. 'article.imgurl',
  1170. 'article.author',
  1171. 'article.updated_at',
  1172. 'article.introduce',
  1173. 'article.islink',
  1174. 'article.linkurl',
  1175. 'article.copyfrom',
  1176. DB::raw("'$child_pinyin' as pinyin"))
  1177. ->where('imgurl', '!=', '')
  1178. ->orderBy('updated_at', 'desc')
  1179. ->limit($childImgNum)
  1180. ->get()->all();
  1181. // 查询子栏目文字新闻
  1182. $childTextArticles = Article::where('catid', $childCatId)
  1183. ->where('status', 1)
  1184. ->where(function ($query) use ($website) {
  1185. $query->where(function ($subQuery) use ($website) {
  1186. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1187. })->orWhereNull("ignore_ids");
  1188. })
  1189. ->select(
  1190. 'article.id',
  1191. 'article.title',
  1192. 'article.author',
  1193. 'article.updated_at',
  1194. 'article.introduce',
  1195. 'article.islink',
  1196. 'article.linkurl',
  1197. 'article.copyfrom',
  1198. DB::raw("'$child_pinyin' as pinyin"))
  1199. ->orderBy('updated_at', 'desc')
  1200. ->limit($childTextNum)
  1201. ->get()->all();
  1202. $resultItem['child'] = [
  1203. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1204. 'category_id' => $childCatId,
  1205. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1206. 'all_childcat' => $childCategory,
  1207. 'imgnum' => $childImgArticles,
  1208. 'textnum' => $childTextArticles,
  1209. ];
  1210. }
  1211. // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
  1212. }
  1213. }else{
  1214. $resultItem['child'] = [];
  1215. }
  1216. return $resultItem;
  1217. }, $data);
  1218. return Result::success($result);
  1219. // return Result::success($data);
  1220. }
  1221. /**
  1222. * 乡村网-获取特殊新闻模块
  1223. * @param array $data
  1224. * @return array
  1225. */
  1226. public function getWebsiteArticles(array $data): array
  1227. {
  1228. $input['id'] = $data['id'];
  1229. $input['website_id'] = $data['website_id'];
  1230. $data = json_decode($input['id'], true);
  1231. // 使用 array_map 处理每个元素
  1232. $result = array_map(function ($item) use ($input) {
  1233. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1234. $website = [
  1235. 'website_id' => $input['website_id']
  1236. ];
  1237. // 查询栏目名称
  1238. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1239. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1240. if(empty($category)){
  1241. $imgArticles = [];
  1242. $textArticles = [];
  1243. // return Result::error("暂无此栏目",0);
  1244. }else{
  1245. $child_category = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1246. $parent_alias = $category->aLIas_pinyin ? $category->aLIas_pinyin.'/' : null;
  1247. // return Result::success($website);
  1248. // 查询图片新闻
  1249. // 合并查询条件
  1250. $baseQuery = Article::where(function ($query) use ($website) {
  1251. $query->where(function ($subQuery) use ($website) {
  1252. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1253. })->orWhereNull("ignore_ids");
  1254. })
  1255. ->where('website_category.website_id', $website['website_id'])
  1256. ->where('article.status', 1)
  1257. ->leftJoin('website_category', 'website_category.category_id', 'article.catid');
  1258. // 查询文字新闻
  1259. $textArticles = clone $baseQuery;
  1260. $textArticles = $textArticles->whereIn('catid',$child_category)
  1261. ->select(
  1262. 'article.id',
  1263. 'article.title',
  1264. // 'article.imgurl',
  1265. 'article.author',
  1266. 'article.updated_at',
  1267. 'article.introduce',
  1268. 'article.islink',
  1269. 'article.linkurl',
  1270. 'article.copyfrom',
  1271. 'website_category.category_id',
  1272. 'website_category.alias',
  1273. 'website_category.aLIas_pinyin'
  1274. )
  1275. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1276. ->orderBy('article.updated_at', 'desc')
  1277. ->limit($parentTextNum)
  1278. ->get()
  1279. ->all();
  1280. if(empty($textArticles)){
  1281. $textArticles = clone $baseQuery;
  1282. $textArticles = $textArticles->where('catid',$parentCatId)
  1283. ->select(
  1284. 'article.id',
  1285. 'article.title',
  1286. // 'article.imgurl',
  1287. 'article.author',
  1288. 'article.updated_at',
  1289. 'article.introduce',
  1290. 'article.islink',
  1291. 'article.linkurl',
  1292. 'article.copyfrom',
  1293. 'website_category.category_id',
  1294. 'website_category.alias',
  1295. 'website_category.aLIas_pinyin'
  1296. )
  1297. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1298. ->orderBy('article.updated_at', 'desc')
  1299. ->limit($parentTextNum)
  1300. ->get()
  1301. ->all();
  1302. }
  1303. // 查询图片新闻
  1304. $imgArticles = clone $baseQuery;
  1305. $imgArticles = $imgArticles->where('imgurl', '!=', '')->whereIn('catid',$child_category)
  1306. ->select(
  1307. 'article.id',
  1308. 'article.title',
  1309. 'article.imgurl',
  1310. 'article.author',
  1311. 'article.updated_at',
  1312. 'article.introduce',
  1313. 'article.islink',
  1314. 'article.linkurl',
  1315. 'article.copyfrom',
  1316. 'website_category.category_id',
  1317. 'website_category.alias',
  1318. 'website_category.aLIas_pinyin'
  1319. )
  1320. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1321. ->orderBy('article.updated_at', 'desc')
  1322. ->limit($parentImgNum)
  1323. ->get()
  1324. ->all();
  1325. if(empty($imgArticles)){
  1326. $imgArticles = clone $baseQuery;
  1327. $imgArticles = $imgArticles->where('imgurl', '!=', '')->where('catid',$parentCatId)
  1328. ->select(
  1329. 'article.id',
  1330. 'article.title',
  1331. 'article.imgurl',
  1332. 'article.author',
  1333. 'article.updated_at',
  1334. 'article.introduce',
  1335. 'article.islink',
  1336. 'article.linkurl',
  1337. 'article.copyfrom',
  1338. 'website_category.category_id',
  1339. 'website_category.alias',
  1340. 'website_category.aLIas_pinyin'
  1341. )
  1342. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1343. ->orderBy('article.updated_at', 'desc')
  1344. ->limit($parentImgNum)
  1345. ->get()
  1346. ->all();
  1347. }
  1348. }
  1349. $resultItem = [
  1350. 'alias' => $category ? $category->alias : null,
  1351. 'category_id' => $parentCatId,
  1352. 'pinyin' => $pinyin ? $pinyin : null,
  1353. 'imgnum' => $imgArticles ?? [],
  1354. 'textnum' => $textArticles ?? [],
  1355. ];
  1356. return $resultItem;
  1357. }, $data);
  1358. return Result::success($result);
  1359. }
  1360. // 封装处理商品的路由问题
  1361. function processGoods($goods, $data) {
  1362. return $goods->map(function ($good) use ($data) {
  1363. $catid = $good->catid ?? 0;
  1364. $pinyin = '';
  1365. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1366. if (!empty($category->pid) && $category->pid != 0) {
  1367. $level = json_decode($category->category_arr_id);
  1368. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1369. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1370. ->get(['aLIas_pinyin'])
  1371. ->pluck('aLIas_pinyin')
  1372. ->implode('/');
  1373. } else {
  1374. $pinyin = $category->aLIas_pinyin ?? '';
  1375. }
  1376. if(isset($good->city_id) &&!empty($good->city_id)){
  1377. $city = District::where('id', $good->city_id)->first(['name']);
  1378. $good->city_name = $city->name ?? '';
  1379. }
  1380. // 解析imgurl JSON并取第一条数据
  1381. $imgUrls = json_decode($good->imgurl, true);
  1382. $good->imgurl = !empty($imgUrls) ? $imgUrls[0] : null;
  1383. $good->pinyin = $pinyin;
  1384. return $good;
  1385. });
  1386. }
  1387. /**
  1388. * 获取商品模块
  1389. * @param array $data
  1390. * @return array
  1391. * */
  1392. public function getWebsiteshop(array $data): array
  1393. {
  1394. $input['id'] = $data['id'];
  1395. $input['website_id'] = $data['website_id'];
  1396. // return Result::success($input);
  1397. $data = json_decode($input['id'] ?? '', true) ?? [];
  1398. $result = array_map(function ($item) use ($input) {
  1399. // 检查parent元素是否存在且不是undefined
  1400. if (isset($item['level']) && $item['level'] != 'undefined' && $item['level']!= "") {
  1401. list($Levelid, $goodStart,$goodNum) = explode(',', $item['level']);
  1402. $website = $input['website_id'];
  1403. $query = Good::where('good.status', 2)
  1404. ->where('good.website_id', $website);
  1405. switch ($Levelid) {
  1406. case 1:
  1407. case 2:
  1408. case 3:
  1409. $goods = $query->where(function($q) use ($Levelid) {
  1410. $q->whereRaw("JSON_CONTAINS(good.level, '". intval($Levelid). "') = 0")
  1411. ->orWhereRaw("JSON_CONTAINS(good.level, '\"". intval($Levelid). "\"') = 0");
  1412. });
  1413. break;
  1414. case 4:
  1415. $goods = $query;
  1416. break;
  1417. case 5:
  1418. $goods = $query->where('type_id',1);
  1419. break;
  1420. case 6:
  1421. $goods = $query->where('type_id',2);
  1422. break;
  1423. default:
  1424. return [];
  1425. }
  1426. $all_goods = $goods
  1427. ->select('good.id', 'good.name', 'good.imgurl', 'good.description',
  1428. 'good.updated_at', 'good.catid','good.type_id','good.price','good.level',
  1429. 'good.website_id')
  1430. ->orderBy('updated_at','desc')
  1431. ->offset($goodStart)
  1432. ->limit($goodNum)
  1433. ->get();
  1434. $all_goods = $this->processGoods($all_goods, $input);
  1435. }
  1436. return $all_goods;
  1437. }, $data);
  1438. return Result::success($result);
  1439. }
  1440. /**
  1441. * 获取商品分类
  1442. * @param array $data
  1443. * @return array
  1444. * */
  1445. public function getWebsiteshopCat(array $data): array
  1446. {
  1447. $category = WebsiteCategory::where('website_id', $data['website_id'])
  1448. ->whereRaw("JSON_CONTAINS(category_arr_id, '". intval($data['id']). "') = 1")
  1449. ->orWhereRaw("JSON_CONTAINS(category_arr_id, '\"". intval($data['id']). "\"') = 1")
  1450. ->select('category_id', 'alias','aLIas_pinyin','pid','category_arr_id')
  1451. ->orderBy('sort')
  1452. ->get()
  1453. ->map(function ($item) use ($data) {
  1454. $pinyin = [];
  1455. $level = json_decode($item->category_arr_id);
  1456. // 递归获取所有父级栏目的拼音
  1457. if($item->pid!= 0){
  1458. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1459. ->orderByRaw('FIELD(category_id, '.implode(',', $level).')')
  1460. ->get(['aLIas_pinyin'])
  1461. ->pluck('aLIas_pinyin')
  1462. ->implode('/');
  1463. }else{
  1464. $pinyin = $item->aLIas_pinyin;
  1465. }
  1466. $item->aLIas_pinyin = $pinyin;
  1467. return $item;
  1468. });
  1469. $goods = Good::where('website_id', $data['website_id'])
  1470. ->where('status', 2)
  1471. ->select('good.id as good_id', 'name','imgurl','description','updated_at','catid','type_id','website_id')
  1472. ->latest('updated_at')
  1473. ->offset(($data['page']-1)*$data['pageSize'])
  1474. ->limit($data['pageSize'])
  1475. ->get();
  1476. $goods = $this->processGoods($goods, $data);
  1477. if(empty($category)){
  1478. return Result::error("栏目查询失败", 0);
  1479. }
  1480. if(empty($goods)){
  1481. return Result::error("商品查询失败", 0);
  1482. }
  1483. // 调用Tool中的buildMenuTree方法构建菜单树
  1484. $cat_tree = Result::buildMenuTree($category);
  1485. $result = [
  1486. 'category' => $cat_tree,
  1487. 'goods' => $goods,
  1488. ];
  1489. // $resul['goods'] = $goods;
  1490. if(empty($result)){
  1491. return Result::error("查询失败", 0);
  1492. }
  1493. return Result::success($result);
  1494. }
  1495. /*
  1496. * 获取商品列表
  1497. * @param array $data
  1498. * @return array
  1499. * */
  1500. public function getWebsiteshopList(array $data): array
  1501. {
  1502. // return Result::success($data);
  1503. $where = [
  1504. 'status' => 2,
  1505. 'website_id' => $data['website_id'],
  1506. ];
  1507. if((empty($data['catid']) ||!isset($data['catid'])) && (empty($data['keyword']) ||!isset($data['keyword'])) && (empty($data['city_id']) || !isset($data['city_id']))){
  1508. return Result::error("查询失败", 0);
  1509. }
  1510. if ((empty($data['catid']) || !isset($data['catid'])) && (!empty($data['city_id']) || isset($data['city_id']))) {
  1511. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['category_id']);
  1512. $data['catid'] = $category->category_id ?? 0;
  1513. }
  1514. if(isset($data['keyword']) &&!empty($data['keyword'])){
  1515. array_push($where, ['name', 'like', '%'. $data['keyword'].'%']);
  1516. }
  1517. if(isset($data['type_id']) && !empty($data['type_id'])){
  1518. array_push($where, ['type_id', $data['type_id']]);
  1519. }
  1520. $query = Good::where($where)
  1521. ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
  1522. $query->where(function($q) use ($data) {
  1523. $q->WhereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($data['catid']). "') = 1")
  1524. ->orWhereRaw("JSON_CONTAINS(good.cat_arr_id, '\"". intval($data['catid']). "\"') = 1");
  1525. });
  1526. })
  1527. ->when(isset($data['city_id']) && !empty($data['city_id']), function ($query) use ($data) {
  1528. $query->where(function($q) use ($data) {
  1529. $q->WhereRaw("JSON_CONTAINS(good.city_arr_id, '". intval($data['city_id']). "') = 1")
  1530. ->orWhereRaw("JSON_CONTAINS(good.city_arr_id, '\"". intval($data['city_id']). "\"') = 1");
  1531. });
  1532. })
  1533. ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at',
  1534. 'good.catid','good.type_id','good.website_id','good.cat_arr_id','good.created_at','good.city_id')
  1535. ->latest('updated_at');
  1536. // 获取 type_id 为 1 的数据
  1537. $result['type1'] = $this->processGoods(
  1538. $query->clone()
  1539. ->where('type_id', 1)
  1540. ->offset(($data['page'] - 1) * $data['pageSize'])
  1541. ->limit($data['pageSize'])
  1542. ->get(),
  1543. $data
  1544. );
  1545. // 获取 type_id 为 2 的数据
  1546. $result['type2'] = $this->processGoods(
  1547. $query->clone()
  1548. ->where('type_id', 2)
  1549. ->offset(($data['page'] - 1) * $data['pageSize'])
  1550. ->limit($data['pageSize'])
  1551. ->get(),
  1552. $data
  1553. );
  1554. if(empty($result)){
  1555. return Result::error("查询失败", 0);
  1556. }
  1557. return Result::success($result);
  1558. }
  1559. /**
  1560. * 获取商品详情
  1561. * @param array $data
  1562. * @return array
  1563. * */
  1564. public function getWebsiteshopInfo(array $data): array
  1565. {
  1566. $where = [
  1567. 'good.status' => 2,
  1568. 'good.website_id' => $data['website_id'],
  1569. ];
  1570. $goods = Good::where($where)
  1571. ->where('good.id', $data['id'])
  1572. ->leftJoin('website_category', 'website_category.category_id', 'good.catid')
  1573. ->select('good.*','website_category.alias','website_category.category_id')
  1574. ->first();
  1575. if(empty($goods)){
  1576. return Result::error("查询失败", 0);
  1577. }
  1578. $goods->imgurl = json_decode($goods->imgurl, true);
  1579. return Result::success($goods);
  1580. }
  1581. /**
  1582. * 封装处理文章的路由问题
  1583. * */
  1584. function processArticles($articles, $data) {
  1585. if (!is_array($data)) {
  1586. // 可以根据实际情况进行日志记录或抛出异常
  1587. // 这里简单输出错误信息
  1588. echo "Error: \$data is not an array in processArticles. It is of type ". gettype($data). PHP_EOL;
  1589. return $articles;
  1590. }
  1591. return $articles->map(function ($article) use ($data) {
  1592. $catid = $article->cat_arr_id?? '';
  1593. $level = json_decode($catid, true);
  1594. $pinyin = '';
  1595. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1596. if (!empty($category->pid) && $category->pid!= 0) {
  1597. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1598. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1599. ->get(['aLIas_pinyin'])
  1600. ->pluck('aLIas_pinyin')
  1601. ->implode('/');
  1602. } else {
  1603. $pinyin = $category->aLIas_pinyin ?? '';
  1604. }
  1605. $article->pinyin = $pinyin;
  1606. return $article;
  1607. });
  1608. }
  1609. }