NewsService.php 67 KB

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