NewsService.php 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. $result = [];
  283. if ($category) {
  284. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  285. $where = [
  286. 'status' => 1,
  287. ];
  288. var_dump($data, 'data-----------------');
  289. // level=7 根据文章key来匹配文章
  290. if (isset($data['level']) && $data['level'] == 7) {
  291. // 根据文章id获取key
  292. // $data['id'] = 50142;
  293. if (isset($data['id']) && !empty($data['id'])) {
  294. $keyword = Article::where('id', $data['id'])->value('keyword');
  295. $keywordArray = explode(',', $keyword);
  296. $whereL7 = [];
  297. foreach ($keywordArray as $k => $v) {
  298. $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
  299. }
  300. // 原始查询
  301. $result = Article::where($whereL7)
  302. ->offset($placeid)
  303. ->limit($data['pageSize'])
  304. ->orderBy('updated_at', 'desc')
  305. ->get()
  306. ->map(function ($article ) use ($data) {
  307. $catid = $article->catid;
  308. $pinyin = '';
  309. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  310. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  311. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  312. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  313. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  314. }
  315. } else {
  316. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  317. }
  318. $article->pinyin = $pinyin;
  319. return $article;
  320. });
  321. if (empty($result)) {
  322. return Result::success();
  323. }
  324. return Result::success($result);
  325. } else {
  326. return Result::error("参数错误level=7,id不能为空", 0);
  327. }
  328. }
  329. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  330. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  331. var_dump($where, 'where-----------------');
  332. $result = Article::where($where)
  333. ->whereIn("catid", $category)
  334. // ->leftJoin('website_category', 'article.catid', 'website_category.category_id')
  335. // ->where('website_category.website_id', $data['website_id'])
  336. ->where(function ($query) use ($data) {
  337. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  338. ->orWhereNull("ignore_ids");
  339. })
  340. //$data['level'] == 4 || $data['level'] == 5 查询随机
  341. ->when($data['level'] == 5, function ($query) {
  342. $query->inRandomOrder()
  343. //updated_at最近三十天;
  344. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  345. })
  346. ->when($data['level'] == 4, function ($query) {
  347. $query->orderBy("article.updated_at", "desc");
  348. })
  349. ->when(!empty($data['level']), function ($query) use ($data) {
  350. if ($data['level'] != 4 && $data['level'] != 5) {
  351. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  352. ->orderBy("article.updated_at", "desc");
  353. }
  354. })
  355. ->select('article.*')
  356. ->offset($placeid)
  357. ->limit($data['pageSize'])
  358. ->get()
  359. ->map(function ($article ) use ($data) {
  360. $catid = $article->catid;
  361. $pinyin = '';
  362. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  363. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  364. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  365. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  366. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  367. }
  368. } else {
  369. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  370. }
  371. $article->pinyin = $pinyin;
  372. return $article;
  373. });
  374. if (empty($result) || count($result) == 0) {
  375. return Result::error("暂无头条新闻");
  376. }
  377. return Result::success($result);
  378. } else {
  379. return Result::error("本网站下暂无相关栏目", 0);
  380. }
  381. }
  382. /**
  383. * 获取模块新闻
  384. * @param array $data
  385. * @return array
  386. */
  387. public function getWebsiteModelArticles(array $data): array
  388. {
  389. $catid = $data['catid'];
  390. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  391. $category = $category->toArray();
  392. if (!empty($category)) {
  393. $where = [
  394. 'status' => 1,
  395. 'catid' => $catid,
  396. ];
  397. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  398. // 1:文字新闻;2:轮播图;3:图文;
  399. // 级别:0:未分类
  400. // 3:推荐图片
  401. if ($data['level'] == 1) {
  402. $data['level'] = 0;
  403. }
  404. $result = Article::where($where)
  405. ->where(function ($query) use ($data) {
  406. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  407. ->orWhereNull("level")
  408. ->orWhereRaw("level = '[]'");
  409. })
  410. ->where(function ($query) use ($data) {
  411. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  412. ->orWhereNull("ignore_ids");
  413. })
  414. ->orderBy("updated_at", "desc")
  415. ->offset($placeid)
  416. ->limit($data['pagesize'])
  417. ->get();
  418. if (empty($result)) {
  419. return Result::error("此栏目暂无相关新闻", 0);
  420. }
  421. } else {
  422. return Result::error("此网站暂无此栏目", 0);
  423. }
  424. return Result::success($result);
  425. }
  426. /**
  427. *获取新闻列表
  428. * @param array $data
  429. * @return array
  430. */
  431. public function getWebsiteArticleList(array $data): array
  432. {
  433. // return Result::success($data);
  434. $where[] = ['status', '=', 1];
  435. if (isset($data['catid']) && !empty($data['catid'])) {
  436. if (is_array($data['catid'])) {
  437. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  438. array_push($where, ['catid', 'in', $data['catid']]);
  439. } else {
  440. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  441. array_push($where, ['catid', '=', $data['catid']]);
  442. }
  443. if (empty($category)) {
  444. return Result::error("此网站暂无此栏目", 0);
  445. }
  446. }
  447. // return Result::success($where);
  448. $rep = Article::where(function ($query) use ($where) {
  449. foreach ($where as $condition) {
  450. if ($condition[1] === 'in') {
  451. $query->whereIn($condition[0], $condition[2]);
  452. } else {
  453. $query->where($condition[0], $condition[1], $condition[2]);
  454. }
  455. }
  456. })
  457. ->where(function ($query) use ($data) {
  458. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  459. ->orWhereNull("ignore_ids");
  460. })
  461. ->select(
  462. 'article.id',
  463. 'article.title',
  464. 'article.imgurl',
  465. 'article.author',
  466. 'article.updated_at',
  467. 'article.introduce',
  468. 'article.islink',
  469. 'article.linkurl',
  470. 'article.copyfrom',
  471. 'article.catid')
  472. ->orderBy("updated_at", "desc")
  473. ->offset(($data['page'] - 1) * $data['pageSize'])
  474. ->limit($data['pageSize'])
  475. ->get()
  476. ->map(function ($article ) use ($data) {
  477. $catid = $article->catid ?? 0;
  478. $pinyin = '';
  479. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  480. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  481. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  482. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  483. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  484. }
  485. $article->pinyin = $pinyin;
  486. return $article;
  487. });
  488. $count = Article::where(function ($query) use ($where) {
  489. foreach ($where as $condition) {
  490. if ($condition[1] === 'in') {
  491. $query->whereIn($condition[0], $condition[2]);
  492. } else {
  493. $query->where($condition[0], $condition[1], $condition[2]);
  494. }
  495. }
  496. })->count();
  497. $data = [
  498. 'rows' => $rep->toArray(),
  499. 'count' => $count,
  500. ];
  501. if (empty($rep)) {
  502. return Result::error("没有信息数据");
  503. }
  504. return Result::success($data);
  505. }
  506. /**
  507. * 前端-获取新闻详情
  508. * @param array $data
  509. * @return array
  510. */
  511. public function selectWebsiteArticleInfo(array $data): array
  512. {
  513. $where = [
  514. 'article.id'=>$data['id'],
  515. 'article.status'=>1
  516. ];
  517. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
  518. ->where(function ($query) use ($data) {
  519. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  520. ->orWhereNull("ignore_ids");
  521. })
  522. ->first();
  523. if(empty($result)){
  524. return Result::error("暂无此新闻!",0);
  525. }
  526. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  527. if(empty($category)){
  528. return Result::error("查询失败",0);
  529. }
  530. $result['category_id'] = $category['category_id'];
  531. $result['cat_name'] = $category['alias'];
  532. return Result::success($result);
  533. }
  534. /**
  535. * 前端-获取网站调查问卷
  536. * @param array $data
  537. * @return array
  538. */
  539. public function getWebsiteSurvey(array $data): array
  540. {
  541. if(isset($data['website_id']) && !empty($data['website_id'])){
  542. $website = Website::where('id',$data['website_id'])->first();
  543. if(empty($website)){
  544. return Result::error("暂无此网站",0);
  545. }
  546. }
  547. if(isset($data['art_id']) && !empty($data['art_id'])){
  548. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  549. if(empty($article)){
  550. return Result::error("暂无此文章",0);
  551. }
  552. // return Result::error($data,0);
  553. $where['art_id'] = $data['art_id'];
  554. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  555. }else{
  556. $survey = Article::where(function ($query) {
  557. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  558. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  559. })
  560. ->where('status',1)
  561. ->where('is_survey',1)
  562. ->select('survey_id')
  563. ->orderBy('updated_at','desc')
  564. ->first();
  565. if(empty($survey)){
  566. return Result::error("暂无调查问卷",0);
  567. }
  568. $where['sur_id'] = $survey['survey_id'];
  569. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  570. }
  571. // return Result::success($where);
  572. $result = ArticleSurvey::where($where)
  573. ->where(function ($query) {
  574. $query->where('is_other', 0)
  575. ->orWhere(function ($subQuery) {
  576. $subQuery->where('is_other', 1)
  577. ->where('other_id', 0);
  578. });
  579. })
  580. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  581. ->select('article_survey.*', 'article.survey_type')
  582. ->get()->all();
  583. if(empty($result)){
  584. return Result::error("此文章暂无调查问卷",0);
  585. }
  586. return Result::success($result);
  587. }
  588. /**
  589. * 前端-添加网站调查问卷选项
  590. * @param array $data
  591. * @return array
  592. */
  593. public function addWebsiteSurveyOption(array $data): array
  594. {
  595. if(isset($data['website_id']) && !empty($data['website_id'])){
  596. $website = Website::where('id',$data['website_id'])->first();
  597. if(empty($website)){
  598. return Result::error("暂无此网站",0);
  599. }
  600. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  601. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('is_other',1)->where('other_id',0)->first();
  602. if(empty($survey)){
  603. return Result::error("此调查问卷不可添加选项",0);
  604. }
  605. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  606. $choice = [
  607. 'art_id'=>$survey['art_id'],
  608. 'website_id'=>$data['website_id'],
  609. 'survey_name'=>$survey['survey_name'],
  610. 'choice_name'=>$data['choice_name'],
  611. 'sur_id'=>$survey['sur_id'],
  612. 'is_other'=>1,
  613. 'other_id'=>$survey['id'],
  614. ];
  615. $result = ArticleSurvey::insertGetId($choice);
  616. if(empty($result)){
  617. return Result::error("添加失败",0);
  618. }
  619. return Result::success($result);
  620. }
  621. }
  622. return Result::error("添加失败",0);
  623. }
  624. return Result::error("添加失败",0);
  625. }
  626. /**
  627. * 前端-调查问卷投票
  628. * @param array $data
  629. * @return array
  630. */
  631. public function addWebsiteSurveyVote(array $data): array
  632. {
  633. // return Result::success($data);
  634. if(isset($data['website_id']) && !empty($data['website_id'])){
  635. $website = Website::where('id',$data['website_id'])->first();
  636. if(empty($website)){
  637. return Result::error("暂无此网站",0);
  638. }
  639. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  640. $is_survey = ArticleSurvey::where('sur_id',$data['sur_id'])->first();
  641. // return Result::success($survey);
  642. if(empty($is_survey)){
  643. return Result::error("此调查问卷不存在",0);
  644. }
  645. // return Result::success($survey);
  646. // 调查问卷类型
  647. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  648. //多选 若是json型则转化成数组类型
  649. if (strpos($data['choice_id'], '[') === 0) {
  650. $data['choice_id'] = json_decode($data['choice_id'], true);
  651. } else {
  652. // 单选 也转换成数组
  653. $data['choice_id'] = [$data['choice_id']];
  654. }
  655. $data['choice_id'] = array_map('intval', $data['choice_id']);
  656. $other = ArticleSurvey::whereIn('id',$data['choice_id'])
  657. // ->where('website_id',$data['website_id'])
  658. ->where('is_other',1)
  659. ->where('other_id',0)
  660. ->first();
  661. if(!empty($other)){
  662. return Result::error("请选择已有的选项!",0);
  663. }
  664. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  665. // ->where('website_id',$data['website_id'])
  666. ->where('is_other',1)
  667. ->where('other_id','!=',0)
  668. ->first();
  669. // return Result::success($choice['other']);
  670. $choice_id = $data['choice_id'];
  671. if(!empty($choice['other'])){
  672. // array_push($data['choice_id'],$choice['other']['other_id']);
  673. if(!empty($choice_id)){
  674. $key = array_search($choice['other']['id'], $choice_id);
  675. if ($key!== false) {
  676. unset($choice_id[$key]);
  677. $choice_id = array_values($choice_id);
  678. }
  679. array_push($choice_id,$choice['other']['other_id']);
  680. }else{
  681. $choice_id[0] = $choice['other']['other_id'];
  682. }
  683. array_push($data['choice_id'],$choice['other']['other_id']);
  684. }
  685. // return Result::success($data);
  686. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  687. // ->where('website_id',$data['website_id'])
  688. ->increment('results', 1);
  689. if(empty($choice)){
  690. return Result::error("请选择已有的选项!",0);
  691. }
  692. $survey['data'] = ArticleSurvey::where('sur_id',$data['sur_id'])
  693. // ->where('website_id',$data['website_id'])
  694. ->where('other_id', 0)
  695. ->get();
  696. $survey['choice'] = $choice_id;
  697. return Result::success($survey);
  698. }
  699. return Result::error("参数必填!");
  700. }
  701. return Result::error("此调查问卷不存在",0);
  702. }
  703. return Result::error("参数必填!");
  704. }
  705. /**
  706. * 后端-获取网站调查问卷列表
  707. * @param array $data
  708. * @return array
  709. */
  710. public function getSurveyList(array $data): array
  711. {
  712. $where = [];
  713. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  714. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  715. }
  716. if(isset($data['survey_type']) && $data['survey_type'] != null){
  717. array_push($where,['survey_type','=',$data['survey_type']]);
  718. }
  719. if(isset($data['is_survey']) && $data['is_survey'] != null){
  720. array_push($where,['is_survey','=',$data['is_survey']]);
  721. }
  722. // return Result::success($where);
  723. if (!empty($where)) {
  724. $query = Article::where($where)->where(function ($q) {
  725. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  726. });
  727. } else {
  728. $query = Article::where(function ($q) {
  729. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  730. });
  731. }
  732. $count = $query->count();
  733. $survey = $query->orderByDesc('id')
  734. ->limit($data['pageSize'])
  735. ->offset(($data['page']-1)*$data['pageSize'])
  736. ->get();
  737. if(empty($survey->toArray())){
  738. return Result::error("暂无调查问卷!",0);
  739. }
  740. $result = [
  741. 'rows'=>$survey,
  742. 'count'=>$count
  743. ];
  744. return Result::success($result);
  745. }
  746. /**
  747. * 后端-获取网站调查问卷详情
  748. * @param array $data
  749. * @return array
  750. */
  751. public function getSurveyInfo(array $data): array
  752. {
  753. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  754. $where = [ 'sur_id'=>$data['sur_id']];
  755. $choose = ArticleSurvey::where($where)->where('is_other',0)
  756. ->leftJoin('article','article_survey.art_id','article.id')
  757. ->select('article_survey.*','article.survey_type')
  758. ->get()->all();
  759. if(empty($choose)){
  760. return Result::error("此调查问卷不存在",0);
  761. }
  762. $resultsArray = array_column($choose, 'results');
  763. $total = array_sum($resultsArray);
  764. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  765. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  766. // $total = 0;
  767. if(!empty($other)){
  768. $total = $total + $other['results'];
  769. $other['choice_name'] ='(其他)';
  770. if(!empty($others)){
  771. $other['hasChildren'] = true;
  772. // array_push($other,['hasChildren','=',true]);
  773. $other['children'] = $others;
  774. $other_choices = [$other->toArray()];
  775. $mer_choice = array_merge($choose,$other_choices);
  776. $value_choice = array_values($mer_choice);
  777. }
  778. else{
  779. // return Result::error('1111');
  780. $other_choices = [$other->toArray()];
  781. $other_choices = array_merge($choose,$other_choices);
  782. $value_choice = array_values($other_choices);
  783. // return Result::success($result);
  784. }
  785. }else{
  786. $value_choice = $choose;
  787. }
  788. $result = [
  789. 'choose'=>$value_choice,
  790. 'total'=>$total
  791. ];
  792. }
  793. return Result::success($result);
  794. }
  795. /**
  796. * 前端-搜索新闻列表
  797. * @param array $data
  798. * @return array
  799. */
  800. public function selectWebsiteArticle(array $data): array
  801. {
  802. $where = [];
  803. // 初始化查询构造器
  804. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  805. $query = Article::where('status', 1)
  806. ->whereIn('catid', $category)
  807. ->where(function ($query) use ($data) {
  808. $query->where(function ($subQuery) use ($data) {
  809. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  810. })->orWhereNull("ignore_ids");
  811. });
  812. // return Result::success($all_articles);
  813. // 检查是否存在 cityid 参数
  814. if (isset($data['cityid']) && !empty($data['cityid'])) {
  815. $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
  816. }
  817. // 检查是否存在 department_id 参数
  818. if (isset($data['department_id']) && !empty($data['department_id'])) {
  819. $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
  820. }
  821. // 检查是否存在 keyword 参数
  822. if (isset($data['keyword']) && !empty($data['keyword'])) {
  823. $query->where('title', 'like', '%' . $data['keyword'] . '%');
  824. }
  825. // 计算总数
  826. $count = $query->count();
  827. // 分页查询
  828. $articles = $query
  829. ->select(
  830. 'article.id',
  831. 'article.title',
  832. 'article.imgurl',
  833. 'article.author',
  834. 'article.updated_at',
  835. 'article.introduce',
  836. 'article.islink',
  837. 'article.linkurl',
  838. 'article.copyfrom',
  839. 'article.catid',
  840. 'article.department_arr_id',
  841. 'article.city_arr_id',)
  842. ->orderBy("updated_at", "desc")
  843. ->offset(($data['page'] - 1) * $data['pageSize'])
  844. ->limit($data['pageSize'])
  845. ->get()
  846. ->map(function ($article ) use ($data) {
  847. $catid = $article->catid ?? 0;
  848. $pinyin = '';
  849. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  850. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  851. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  852. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  853. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  854. }
  855. $article->pinyin = $pinyin;
  856. return $article;
  857. });
  858. if (empty($articles)) {
  859. return Result::error("没有符合条件的资讯数据");
  860. }
  861. $data = [
  862. 'rows' => $articles,
  863. 'count' => $count,
  864. ];
  865. return Result::success($data);
  866. }
  867. /**
  868. * 模块新闻加强版
  869. * @param array $data
  870. * @return array
  871. */
  872. public function getWebsiteCatidArticle(array $data): array
  873. {
  874. // return Result::success($data);
  875. $where = [
  876. // 'category.status' => 1,
  877. 'website_category.category_id' => $data['catid'],
  878. 'website_category.website_id' => $data['website_id'],
  879. // 'article.status' => 1,
  880. ];
  881. // $category = WebsiteCategory::where($where);
  882. if(isset($data['child_catnum']) && !empty($data['child_catnum'])){
  883. $child_catnum = $data['child_catnum']?? 1;
  884. $category['child'] = WebsiteCategory::where('pid',$data['catid'])->where('website_id',$data['website_id'])->select('category_id','alias')->limit($child_catnum)->get()->toArray();
  885. $childCategoryIds = array_column($category['child'], 'category_id');
  886. if(empty($childCategoryIds)){
  887. return Result::error("暂无子栏目",0);
  888. }
  889. $imgArticles = [];
  890. $textArticles = [];
  891. // return Result::success($childCategoryIds);
  892. if(isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum']!=0){
  893. // 初始化子分类图片新闻和文字新闻数组
  894. // 查询所有子级栏目的图文新闻
  895. $imgArticles = Article::where('catid', $childCategoryIds[0])
  896. ->where(function ($query) use ($data) {
  897. $query->where(function ($subQuery) use ($data) {
  898. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  899. })->orWhereNull("ignore_ids");
  900. })
  901. ->where('status', 1)
  902. ->where('imgurl', '!=', '')
  903. ->select('*')
  904. ->orderBy('updated_at', 'desc')
  905. ->limit($data['child_imgnum'])
  906. ->get();
  907. }
  908. if( isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum']!=0){
  909. // 查询所有子级栏目的文字新闻
  910. $textArticles = Article::where('catid', $childCategoryIds[0])
  911. ->where('status', 1)
  912. ->where(function ($query) use ($data) {
  913. $query->where(function ($subQuery) use ($data) {
  914. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  915. })->orWhereNull("ignore_ids");
  916. })
  917. // ->where(function ($query) {
  918. // $query->whereNull('imgurl')
  919. // ->orWhere('imgurl', '');
  920. // })
  921. ->select('*')
  922. ->orderBy('updated_at', 'desc')
  923. ->limit($data['child_textnum'])
  924. ->get();
  925. }
  926. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  927. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  928. $child['img'] = $imgArticles? $imgArticles->where('catid', $child['category_id']) : [];
  929. $child['text'] = $textArticles? $textArticles->where('catid', $child['category_id']) : [];
  930. return $child;
  931. }, $category['child']);
  932. }
  933. // }
  934. if (isset($data['img_num']) && !empty($data['img_num'])) {
  935. $category['img'] = WebsiteCategory::where($where)
  936. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  937. ->where('article.status', 1)
  938. ->where('article.imgurl', '!=', '')
  939. ->select('article.*','website_category.category_id','website_category.alias')
  940. ->orderBy('article.updated_at', 'desc')
  941. ->limit($data['img_num'])
  942. ->get();
  943. }
  944. if (isset($data['text_num']) && !empty($data['text_num'])) {
  945. $category['text'] = WebsiteCategory::where($where)
  946. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  947. ->where('article.status', 1)
  948. // ->where(function ($query) {
  949. // $query->whereNull('article.imgurl')
  950. // ->orWhere('article.imgurl', '');
  951. // })
  952. ->select('article.*','website_category.category_id','website_category.alias')
  953. ->orderBy('article.updated_at', 'desc')
  954. ->limit($data['text_num'])
  955. ->get();
  956. }
  957. // $category = $category->get();
  958. if(empty($category)){
  959. return Result::error("查询失败", 0);
  960. }
  961. return Result::success($category);
  962. }
  963. /**
  964. * 模块新闻加强plus版
  965. * @param array $data
  966. * @return array
  967. */
  968. public function getWebsiteAllArticle(array $data): array
  969. {
  970. // 修正传入的字符串,将单引号替换为双引号
  971. $input['id'] = $data['id'];
  972. $input['website_id'] = $data['website_id'];
  973. // 将 JSON 字符串转换为 PHP 数组
  974. $data = json_decode($input['id'], true);
  975. // 使用 array_map 处理每个元素
  976. $result = array_map(function ($item) use ($input) {
  977. // 检查parent元素是否存在且不是undefined
  978. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  979. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  980. $website = [
  981. 'website_id' => $input['website_id'],
  982. ];
  983. // 查询栏目名称
  984. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  985. if(empty($category)){
  986. $parent_alias = '';
  987. $parent_pinyin = null;
  988. $imgArticles = [];
  989. $textArticles = [];
  990. }else{
  991. $parent_alias = $category->alias ?? '';
  992. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  993. // 查找子分类ID数组
  994. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  995. array_push($childCategoryIds,$parentCatId);
  996. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  997. if($parentImgNum!=0){
  998. // 查询图片新闻
  999. $imgArticles = Article::where('article.status', 1)
  1000. ->where(function($query) use ($parentCatId) {
  1001. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1002. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1003. })
  1004. ->where(function ($query) use ($website) {
  1005. $query->where(function ($subQuery) use ($website) {
  1006. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1007. ->orWhereNull("ignore_ids");
  1008. });
  1009. })
  1010. ->where('imgurl', '!=', '')
  1011. ->leftJoin('website_category', function($join) use ($website) {
  1012. $join->on('article.catid', '=', 'website_category.category_id')
  1013. ->where('website_category.website_id', '=', $website['website_id']);
  1014. })
  1015. ->select(
  1016. 'article.id',
  1017. 'article.title',
  1018. 'article.imgurl',
  1019. 'article.author',
  1020. 'article.updated_at',
  1021. 'article.introduce',
  1022. 'article.islink',
  1023. 'article.linkurl',
  1024. 'article.copyfrom',
  1025. 'article.catid',
  1026. 'website_category.alias as category_name',
  1027. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1028. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1029. )
  1030. ->orderBy('updated_at', 'desc')
  1031. ->limit($parentImgNum)
  1032. ->get()->all();
  1033. // 查询文字新闻
  1034. }
  1035. if($parentTextNum!=0){
  1036. $textArticles = [];
  1037. $textArticles = Article::where('article.status', 1)
  1038. ->where(function($query) use ($parentCatId) {
  1039. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1040. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1041. })
  1042. ->where(function ($query) use ($website) {
  1043. $query->where(function ($subQuery) use ($website) {
  1044. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1045. ->orWhereNull("ignore_ids");
  1046. });
  1047. })
  1048. ->leftJoin('website_category', function($join) use ($website) {
  1049. $join->on('article.catid', '=', 'website_category.category_id')
  1050. ->where('website_category.website_id', '=', $website['website_id']);
  1051. })
  1052. ->select(
  1053. 'article.id',
  1054. 'article.title',
  1055. 'article.imgurl',
  1056. 'article.author',
  1057. 'article.updated_at',
  1058. 'article.introduce',
  1059. 'article.islink',
  1060. 'article.linkurl',
  1061. 'article.copyfrom',
  1062. 'article.catid',
  1063. 'website_category.alias as category_name',
  1064. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1065. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1066. )
  1067. ->orderBy('updated_at', 'desc')
  1068. ->limit($parentTextNum)
  1069. ->get()->all();
  1070. }
  1071. }
  1072. }
  1073. $resultItem = [
  1074. 'alias' => $parent_alias ?? '',
  1075. 'category_id' => $parentCatId ?? 0,
  1076. 'pinyin' => $parent_pinyin ?? null,
  1077. 'imgnum' => $imgArticles ?? [],
  1078. 'textnum' => $textArticles ?? [],
  1079. ];
  1080. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1081. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1082. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1083. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1084. ->get()->all();
  1085. if (!empty($childCategory)) {
  1086. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1087. // 查询子栏目名称
  1088. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1089. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1090. ->first();
  1091. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1092. $childImgArticles = [];
  1093. $childTextArticles = [];
  1094. $resultItem['child'] = [];
  1095. }else{
  1096. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1097. // 查询子栏目图片新闻
  1098. $childImgArticles = Article::where('catid', $childCatId)
  1099. ->where('status', 1)
  1100. ->where(function ($query) use ($website) {
  1101. $query->where(function ($subQuery) use ($website) {
  1102. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1103. })->orWhereNull("ignore_ids");
  1104. })
  1105. ->select(
  1106. 'article.id',
  1107. 'article.title',
  1108. 'article.imgurl',
  1109. 'article.author',
  1110. 'article.updated_at',
  1111. 'article.introduce',
  1112. 'article.islink',
  1113. 'article.linkurl',
  1114. 'article.copyfrom',
  1115. DB::raw("'$child_pinyin' as pinyin"))
  1116. ->where('imgurl', '!=', '')
  1117. ->orderBy('updated_at', 'desc')
  1118. ->limit($childImgNum)
  1119. ->get()->all();
  1120. // 查询子栏目文字新闻
  1121. $childTextArticles = Article::where('catid', $childCatId)
  1122. ->where('status', 1)
  1123. ->where(function ($query) use ($website) {
  1124. $query->where(function ($subQuery) use ($website) {
  1125. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1126. })->orWhereNull("ignore_ids");
  1127. })
  1128. ->select(
  1129. 'article.id',
  1130. 'article.title',
  1131. 'article.author',
  1132. 'article.updated_at',
  1133. 'article.introduce',
  1134. 'article.islink',
  1135. 'article.linkurl',
  1136. 'article.copyfrom',
  1137. DB::raw("'$child_pinyin' as pinyin"))
  1138. ->orderBy('updated_at', 'desc')
  1139. ->limit($childTextNum)
  1140. ->get()->all();
  1141. $resultItem['child'] = [
  1142. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1143. 'category_id' => $childCatId,
  1144. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1145. 'all_childcat' => $childCategory,
  1146. 'imgnum' => $childImgArticles,
  1147. 'textnum' => $childTextArticles,
  1148. ];
  1149. }
  1150. // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
  1151. }
  1152. }else{
  1153. $resultItem['child'] = [];
  1154. }
  1155. return $resultItem;
  1156. }, $data);
  1157. return Result::success($result);
  1158. // return Result::success($data);
  1159. }
  1160. /**
  1161. * 乡村网-获取特殊新闻模块
  1162. * @param array $data
  1163. * @return array
  1164. */
  1165. public function getWebsiteArticles(array $data): array
  1166. {
  1167. $input['id'] = $data['id'];
  1168. $input['website_id'] = $data['website_id'];
  1169. $data = json_decode($input['id'], true);
  1170. // 使用 array_map 处理每个元素
  1171. $result = array_map(function ($item) use ($input) {
  1172. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1173. $website = [
  1174. 'website_id' => $input['website_id']
  1175. ];
  1176. // 查询栏目名称
  1177. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1178. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1179. if(empty($category)){
  1180. $imgArticles = [];
  1181. $textArticles = [];
  1182. // return Result::error("暂无此栏目",0);
  1183. }else{
  1184. $child_category = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1185. $parent_alias = $category->aLIas_pinyin ? $category->aLIas_pinyin.'/' : null;
  1186. // return Result::success($website);
  1187. // 查询图片新闻
  1188. // 合并查询条件
  1189. $baseQuery = Article::where(function ($query) use ($website) {
  1190. $query->where(function ($subQuery) use ($website) {
  1191. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1192. })->orWhereNull("ignore_ids");
  1193. })
  1194. ->where('website_category.website_id', $website['website_id'])
  1195. ->where('article.status', 1)
  1196. ->leftJoin('website_category', 'website_category.category_id', 'article.catid');
  1197. // 查询文字新闻
  1198. $textArticles = clone $baseQuery;
  1199. $textArticles = $textArticles->whereIn('catid',$child_category)
  1200. ->select(
  1201. 'article.id',
  1202. 'article.title',
  1203. // 'article.imgurl',
  1204. 'article.author',
  1205. 'article.updated_at',
  1206. 'article.introduce',
  1207. 'article.islink',
  1208. 'article.linkurl',
  1209. 'article.copyfrom',
  1210. 'website_category.category_id',
  1211. 'website_category.alias',
  1212. 'website_category.aLIas_pinyin'
  1213. )
  1214. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1215. ->orderBy('article.updated_at', 'desc')
  1216. ->limit($parentTextNum)
  1217. ->get()
  1218. ->all();
  1219. if(empty($textArticles)){
  1220. $textArticles = clone $baseQuery;
  1221. $textArticles = $textArticles->where('catid',$parentCatId)
  1222. ->select(
  1223. 'article.id',
  1224. 'article.title',
  1225. // 'article.imgurl',
  1226. 'article.author',
  1227. 'article.updated_at',
  1228. 'article.introduce',
  1229. 'article.islink',
  1230. 'article.linkurl',
  1231. 'article.copyfrom',
  1232. 'website_category.category_id',
  1233. 'website_category.alias',
  1234. 'website_category.aLIas_pinyin'
  1235. )
  1236. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1237. ->orderBy('article.updated_at', 'desc')
  1238. ->limit($parentTextNum)
  1239. ->get()
  1240. ->all();
  1241. }
  1242. // 查询图片新闻
  1243. $imgArticles = clone $baseQuery;
  1244. $imgArticles = $imgArticles->where('imgurl', '!=', '')->whereIn('catid',$child_category)
  1245. ->select(
  1246. 'article.id',
  1247. 'article.title',
  1248. 'article.imgurl',
  1249. 'article.author',
  1250. 'article.updated_at',
  1251. 'article.introduce',
  1252. 'article.islink',
  1253. 'article.linkurl',
  1254. 'article.copyfrom',
  1255. 'website_category.category_id',
  1256. 'website_category.alias',
  1257. 'website_category.aLIas_pinyin'
  1258. )
  1259. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1260. ->orderBy('article.updated_at', 'desc')
  1261. ->limit($parentImgNum)
  1262. ->get()
  1263. ->all();
  1264. if(empty($imgArticles)){
  1265. $imgArticles = clone $baseQuery;
  1266. $imgArticles = $imgArticles->where('imgurl', '!=', '')->where('catid',$parentCatId)
  1267. ->select(
  1268. 'article.id',
  1269. 'article.title',
  1270. 'article.imgurl',
  1271. 'article.author',
  1272. 'article.updated_at',
  1273. 'article.introduce',
  1274. 'article.islink',
  1275. 'article.linkurl',
  1276. 'article.copyfrom',
  1277. 'website_category.category_id',
  1278. 'website_category.alias',
  1279. 'website_category.aLIas_pinyin'
  1280. )
  1281. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1282. ->orderBy('article.updated_at', 'desc')
  1283. ->limit($parentImgNum)
  1284. ->get()
  1285. ->all();
  1286. }
  1287. }
  1288. $resultItem = [
  1289. 'alias' => $category ? $category->alias : null,
  1290. 'category_id' => $parentCatId,
  1291. 'pinyin' => $pinyin ? $pinyin : null,
  1292. 'imgnum' => $imgArticles ?? [],
  1293. 'textnum' => $textArticles ?? [],
  1294. ];
  1295. return $resultItem;
  1296. }, $data);
  1297. return Result::success($result);
  1298. }
  1299. /**
  1300. *获取头条类新闻模块-合集(暂时用不到)
  1301. * @param array $data
  1302. * @return array
  1303. */
  1304. public function getWebsiteAllArticlett(array $data): array
  1305. {
  1306. $input['id'] = $data['id'];
  1307. $input['website_id'] = $data['website_id'];
  1308. $data = json_decode($input['id'], true);
  1309. // 使用 array_map 处理每个元素
  1310. $result = array_map(function ($item) use ($input) {
  1311. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1312. $website = [
  1313. 'website_id' => $input['website_id']
  1314. ];
  1315. $category = WebsiteCategory::where('website_id', $input['website_id'])->pluck('category_id');
  1316. $category = array_values(array_unique($category->toArray()));
  1317. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  1318. $where = [
  1319. 'status' => 1,
  1320. ];
  1321. return $category;
  1322. }, $data); // 添加第二个参数 $data,确保 array_map 函数有两个参数
  1323. return Result::success($result);
  1324. }
  1325. // 封装处理数据的函数
  1326. function processGoods($goods, $data) {
  1327. return $goods->map(function ($good) use ($data) {
  1328. $catid = $good->catid ?? 0;
  1329. $pinyin = '';
  1330. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1331. if (!empty($category->pid) && $category->pid != 0) {
  1332. $level = json_decode($category->category_arr_id);
  1333. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1334. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1335. ->get(['aLIas_pinyin'])
  1336. ->pluck('aLIas_pinyin')
  1337. ->implode('/');
  1338. } else {
  1339. $pinyin = $category->aLIas_pinyin ?? '';
  1340. }
  1341. if(isset($good->city_id) &&!empty($good->city_id)){
  1342. $city = District::where('id', $good->city_id)->first(['name']);
  1343. $good->city_name = $city->name ?? '';
  1344. }
  1345. // 解析imgurl JSON并取第一条数据
  1346. $imgUrls = json_decode($good->imgurl, true);
  1347. $good->imgurl = !empty($imgUrls) ? $imgUrls[0] : null;
  1348. $good->pinyin = $pinyin;
  1349. return $good;
  1350. });
  1351. }
  1352. /**
  1353. * 获取商品模块
  1354. * @param array $data
  1355. * @return array
  1356. * */
  1357. public function getWebsiteshop(array $data): array
  1358. {
  1359. $input['id'] = $data['id'];
  1360. $input['website_id'] = $data['website_id'];
  1361. // return Result::success($input);
  1362. $data = json_decode($input['id'] ?? '', true) ?? [];
  1363. $result = array_map(function ($item) use ($input) {
  1364. // 检查parent元素是否存在且不是undefined
  1365. if (isset($item['level']) && $item['level'] != 'undefined' && $item['level']!= "") {
  1366. list($Levelid, $goodStart,$goodNum) = explode(',', $item['level']);
  1367. $website = $input['website_id'];
  1368. $query = Good::where('good.status', 2)
  1369. ->where('good.website_id', $website);
  1370. switch ($Levelid) {
  1371. case 1:
  1372. case 2:
  1373. case 3:
  1374. $goods = $query->where(function($q) use ($Levelid) {
  1375. $q->whereRaw("JSON_CONTAINS(good.level, '". intval($Levelid). "') = 0")
  1376. ->orWhereRaw("JSON_CONTAINS(good.level, '\"". intval($Levelid). "\"') = 0");
  1377. });
  1378. break;
  1379. case 4:
  1380. $goods = $query;
  1381. break;
  1382. case 5:
  1383. $goods = $query->where('type_id',1);
  1384. break;
  1385. case 6:
  1386. $goods = $query->where('type_id',2);
  1387. break;
  1388. default:
  1389. return [];
  1390. }
  1391. $all_goods = $goods
  1392. ->select('good.id', 'good.name', 'good.imgurl', 'good.description',
  1393. 'good.updated_at', 'good.catid','good.type_id','good.price','good.level',
  1394. 'good.website_id')
  1395. ->orderBy('updated_at','desc')
  1396. ->offset($goodStart)
  1397. ->limit($goodNum)
  1398. ->get();
  1399. $all_goods = $this->processGoods($all_goods, $input);
  1400. }
  1401. return $all_goods;
  1402. }, $data);
  1403. return Result::success($result);
  1404. }
  1405. /**
  1406. * 获取商品分类
  1407. * @param array $data
  1408. * @return array
  1409. * */
  1410. public function getWebsiteshopCat(array $data): array
  1411. {
  1412. $category = WebsiteCategory::where('website_id', $data['website_id'])
  1413. ->whereRaw("JSON_CONTAINS(category_arr_id, '". intval($data['id']). "') = 1")
  1414. ->orWhereRaw("JSON_CONTAINS(category_arr_id, '\"". intval($data['id']). "\"') = 1")
  1415. ->select('category_id', 'alias','aLIas_pinyin','pid','category_arr_id')
  1416. ->orderBy('sort')
  1417. ->get()
  1418. ->map(function ($item) use ($data) {
  1419. $pinyin = [];
  1420. $level = json_decode($item->category_arr_id);
  1421. // 递归获取所有父级栏目的拼音
  1422. if($item->pid!= 0){
  1423. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1424. ->orderByRaw('FIELD(category_id, '.implode(',', $level).')')
  1425. ->get(['aLIas_pinyin'])
  1426. ->pluck('aLIas_pinyin')
  1427. ->implode('/');
  1428. }else{
  1429. $pinyin = $item->aLIas_pinyin;
  1430. }
  1431. $item->aLIas_pinyin = $pinyin;
  1432. return $item;
  1433. });
  1434. $goods = Good::where('website_id', $data['website_id'])
  1435. ->where('status', 2)
  1436. ->select('good.id as good_id', 'name','imgurl','description','updated_at','catid','type_id','website_id')
  1437. ->latest('updated_at')
  1438. ->offset(($data['page']-1)*$data['pageSize'])
  1439. ->limit($data['pageSize'])
  1440. ->get();
  1441. $goods = $this->processGoods($goods, $data);
  1442. if(empty($category)){
  1443. return Result::error("栏目查询失败", 0);
  1444. }
  1445. if(empty($goods)){
  1446. return Result::error("商品查询失败", 0);
  1447. }
  1448. // 调用Tool中的buildMenuTree方法构建菜单树
  1449. $cat_tree = Result::buildMenuTree($category);
  1450. $result = [
  1451. 'category' => $cat_tree,
  1452. 'goods' => $goods,
  1453. ];
  1454. // $resul['goods'] = $goods;
  1455. if(empty($result)){
  1456. return Result::error("查询失败", 0);
  1457. }
  1458. return Result::success($result);
  1459. }
  1460. /*
  1461. * 获取商品列表
  1462. * @param array $data
  1463. * @return array
  1464. * */
  1465. public function getWebsiteshopList(array $data): array
  1466. {
  1467. // return Result::success($data);
  1468. $where = [
  1469. 'status' => 2,
  1470. 'website_id' => $data['website_id'],
  1471. ];
  1472. if((empty($data['catid']) ||!isset($data['catid'])) && (empty($data['keyword']) ||!isset($data['keyword'])) && (empty($data['city_id']) || !isset($data['city_id']))){
  1473. return Result::error("查询失败", 0);
  1474. }
  1475. if ((empty($data['catid']) || !isset($data['catid'])) && (!empty($data['city_id']) || isset($data['city_id']))) {
  1476. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['category_id']);
  1477. $data['catid'] = $category->category_id ?? 0;
  1478. }
  1479. if(isset($data['keyword']) &&!empty($data['keyword'])){
  1480. array_push($where, ['name', 'like', '%'. $data['keyword'].'%']);
  1481. }
  1482. if(isset($data['type_id']) && !empty($data['type_id'])){
  1483. array_push($where, ['type_id', $data['type_id']]);
  1484. }
  1485. $query = Good::where($where)
  1486. ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
  1487. $query->where(function($q) use ($data) {
  1488. $q->WhereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($data['catid']). "') = 1")
  1489. ->orWhereRaw("JSON_CONTAINS(good.cat_arr_id, '\"". intval($data['catid']). "\"') = 1");
  1490. });
  1491. })
  1492. ->when(isset($data['city_id']) && !empty($data['city_id']), function ($query) use ($data) {
  1493. $query->where(function($q) use ($data) {
  1494. $q->WhereRaw("JSON_CONTAINS(good.city_arr_id, '". intval($data['city_id']). "') = 1")
  1495. ->orWhereRaw("JSON_CONTAINS(good.city_arr_id, '\"". intval($data['city_id']). "\"') = 1");
  1496. });
  1497. })
  1498. ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at',
  1499. 'good.catid','good.type_id','good.website_id','good.cat_arr_id','good.created_at','good.city_id')
  1500. ->latest('updated_at');
  1501. // 获取 type_id 为 1 的数据
  1502. $result['type1'] = $this->processGoods(
  1503. $query->clone()
  1504. ->where('type_id', 1)
  1505. ->offset(($data['page'] - 1) * $data['pageSize'])
  1506. ->limit($data['pageSize'])
  1507. ->get(),
  1508. $data
  1509. );
  1510. // 获取 type_id 为 2 的数据
  1511. $result['type2'] = $this->processGoods(
  1512. $query->clone()
  1513. ->where('type_id', 2)
  1514. ->offset(($data['page'] - 1) * $data['pageSize'])
  1515. ->limit($data['pageSize'])
  1516. ->get(),
  1517. $data
  1518. );
  1519. if(empty($result)){
  1520. return Result::error("查询失败", 0);
  1521. }
  1522. return Result::success($result);
  1523. }
  1524. /**
  1525. * 获取商品详情
  1526. * @param array $data
  1527. * @return array
  1528. * */
  1529. public function getWebsiteshopInfo(array $data): array
  1530. {
  1531. $where = [
  1532. 'good.status' => 2,
  1533. 'good.website_id' => $data['website_id'],
  1534. ];
  1535. $goods = Good::where($where)
  1536. ->where('good.id', $data['id'])
  1537. ->leftJoin('website_category', 'website_category.category_id', 'good.catid')
  1538. ->select('good.*','website_category.alias','website_category.category_id')
  1539. ->first();
  1540. if(empty($goods)){
  1541. return Result::error("查询失败", 0);
  1542. }
  1543. $goods->imgurl = json_decode($goods->imgurl, true);
  1544. return Result::success($goods);
  1545. }
  1546. }