NewsService.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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. // 截取到第一个<
  479. $articleData['imgurl'] = $matches[1][0];
  480. var_dump($articleData['imgurl'], '----------imgurl-----------1');
  481. $pos = strpos($articleData['imgurl'], '"');
  482. if ($pos !== false) {
  483. $articleData['imgurl'] = substr($articleData['imgurl'], 0, $pos);
  484. }
  485. //$articleData['imgurl'] = substr($matches[1][0], 0, 240);
  486. //如果有图,设置level=3
  487. if (!in_array(3, $levelArr)) {$levelArr[] = 3;}
  488. $articleData['level'] = json_encode($levelArr);
  489. } else {
  490. if (!in_array(0, $levelArr)) {
  491. $levelArr[] = 0;
  492. }
  493. $articleData['level'] = json_encode($levelArr);
  494. }
  495. }
  496. var_dump($articleData['level'], '----------$articleData[level]----------1');
  497. if ($articleData['keyword'] == '') {
  498. //提取标题+内容中的关键词
  499. $articleData['keyword'] = $data['title'];
  500. // . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
  501. Jieba::init(); // 初始化 jieba-php
  502. Finalseg::init();
  503. $segList = Jieba::cut($articleData['keyword']);
  504. $segList1 = array_slice($segList, 0, 8);
  505. $articleData['keyword'] = implode(',', $segList1);
  506. }
  507. if ($articleData['introduce'] == '') {
  508. //提取内容中的描述
  509. $articleData['introduce'] = substr(str_replace(' ', '', strip_tags($data['content'])), 0, 100);
  510. }
  511. $id = Article::where(['id' => $data['id']])->update($articleData);
  512. $articleDataContent = [
  513. 'content' => $data['content'],
  514. ];
  515. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  516. //处理投票
  517. $id = $data['id'];
  518. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  519. var_dump($suvey_array, 'suvey_array________delete');
  520. //处理投票
  521. if ($is_survey == 1) {
  522. //生成年月日时分秒+8位随机数
  523. $uuid = date('YmdHis') . rand(10000000, 99999999);
  524. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  525. var_dump($suveys_array, '---------------------1');
  526. $suvey_data = [];
  527. if (is_array($suveys_array)) {
  528. foreach ($suveys_array as $key => $value) {
  529. if ($value == '') {
  530. continue;
  531. }
  532. if (is_array($value)) {
  533. $suvey_data[] = [
  534. 'sur_id' => $uuid,
  535. 'art_id' => $id,
  536. 'website_id' => $website_id ?? 2,
  537. 'survey_name' => $survey_name,
  538. 'choice_name' => $value[1],
  539. 'is_other' => 1,
  540. 'other_id' => 0,
  541. 'results' => 0,
  542. ];
  543. } else {
  544. $suvey_data[] = [
  545. 'sur_id' => $uuid,
  546. 'art_id' => $id,
  547. 'website_id' => $website_id ?? 2,
  548. 'survey_name' => $survey_name,
  549. 'choice_name' => $value,
  550. 'is_other' => 0,
  551. 'other_id' => 0,
  552. 'results' => 0,
  553. ];
  554. }
  555. if (empty($suvey_data)) {
  556. throw new \Exception("投票数据为空");
  557. }
  558. }
  559. }
  560. $result = ArticleSurvey::insert($suvey_data);
  561. if (!$result) {
  562. throw new \Exception("投票失败");
  563. }
  564. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  565. if (!$result) {
  566. throw new \Exception("投票失败");
  567. }
  568. } else {
  569. $result = Article::where('id', $id)->update(['survey_id' => '', 'survey_name' => '', 'is_survey' => 0]);
  570. }
  571. Db::commit();
  572. return Result::success([]);
  573. } catch (\Throwable $ex) {
  574. Db::rollBack();
  575. var_dump($ex->getMessage());
  576. return Result::error("更新失败" . $ex->getMessage(), 0);
  577. }
  578. }
  579. /**
  580. * 更新资讯状态
  581. * @param array $data
  582. * @return array
  583. */
  584. public function upArticleStatus(array $data): array
  585. {
  586. $result = Article::where(['id' => $data['id']])->update($data);
  587. if ($result) {
  588. return Result::success();
  589. } else {
  590. return Result::error("更新状态失败", 0);
  591. }
  592. }
  593. /**
  594. * @param array $data
  595. * @return array
  596. */
  597. public function getArticleInfo(array $data): array
  598. {
  599. $where = [
  600. 'article.id' => $data['id'],
  601. // 'article.status' => 1,
  602. ];
  603. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  604. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
  605. $info = $result;
  606. // var_dump($info, 'info');
  607. $info['survey_array'] = $articleSurvey;
  608. if ($result) {
  609. return Result::success($info);
  610. } else {
  611. return Result::error("查询失败", 0);
  612. }
  613. }
  614. /**
  615. * 获取新闻
  616. * @param array $data
  617. * @return array
  618. */
  619. public function getWebsiteArticlett(array $data): array
  620. {
  621. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  622. $result = [];
  623. if ($category) {
  624. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  625. $where = [
  626. 'status' => 1,
  627. ];
  628. var_dump($data, 'data-----------------');
  629. // level=7 根据文章key来匹配文章
  630. if (isset($data['level']) && $data['level'] == 7) {
  631. // 根据文章id获取key
  632. // $data['id'] = 50142;
  633. if (isset($data['id']) && !empty($data['id'])) {
  634. $keyword = Article::where('id', $data['id'])->value('keyword');
  635. $keywordArray = explode(',', $keyword);
  636. $whereL7 = [];
  637. foreach ($keywordArray as $k => $v) {
  638. $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
  639. }
  640. $result = Article::where($whereL7)
  641. ->offset($placeid)
  642. ->limit($data['pageSize'])
  643. ->orderBy('updated_at', 'desc')
  644. ->get();
  645. if (empty($result)) {
  646. return Result::success([]);
  647. }
  648. return Result::success($result);
  649. } else {
  650. return Result::error("参数错误level=7,id不能为空", 0);
  651. }
  652. }
  653. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  654. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  655. var_dump($where, 'where-----------------');
  656. $result = Article::where($where)
  657. ->whereIn("catid", $category)
  658. ->where(function ($query) use ($data) {
  659. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  660. ->orWhereNull("ignore_ids");
  661. })
  662. //$data['level'] == 4 || $data['level'] == 5 查询随机
  663. ->when($data['level'] == 5, function ($query) {
  664. $query->inRandomOrder()
  665. //updated_at最近三十天;
  666. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  667. })
  668. ->when($data['level'] == 4, function ($query) {
  669. $query->orderBy("updated_at", "desc");
  670. })
  671. ->when(!empty($data['level']), function ($query) use ($data) {
  672. if ($data['level'] != 4 && $data['level'] != 5) {
  673. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  674. ->orderBy("updated_at", "desc");
  675. }
  676. })
  677. ->offset($placeid)
  678. ->limit($data['pageSize'])
  679. ->get();
  680. if (empty($result)) {
  681. return Result::error("暂无头条新闻", 0);
  682. }
  683. return Result::success($result);
  684. } else {
  685. return Result::error("本网站下暂无相关栏目", 0);
  686. }
  687. }
  688. /**
  689. * 获取模块新闻
  690. * @param array $data
  691. * @return array
  692. */
  693. public function getWebsiteModelArticles(array $data): array
  694. {
  695. $catid = $data['catid'];
  696. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  697. $category = $category->toArray();
  698. if (!empty($category)) {
  699. $where = [
  700. 'status' => 1,
  701. 'catid' => $catid,
  702. ];
  703. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  704. // 1:文字新闻;2:轮播图;3:图文;
  705. // 级别:0:未分类
  706. // 3:推荐图片
  707. if ($data['level'] == 1) {
  708. $data['level'] = 0;
  709. }
  710. $result = Article::where($where)
  711. ->where(function ($query) use ($data) {
  712. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  713. ->orWhereNull("level")
  714. ->orWhereRaw("level = '[]'");
  715. })
  716. ->where(function ($query) use ($data) {
  717. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  718. ->orWhereNull("ignore_ids");
  719. })
  720. ->orderBy("updated_at", "desc")
  721. ->offset($placeid)
  722. ->limit($data['pagesize'])
  723. ->get();
  724. if (empty($result)) {
  725. return Result::error("此栏目暂无相关新闻", 0);
  726. }
  727. } else {
  728. return Result::error("此网站暂无此栏目", 0);
  729. }
  730. return Result::success($result);
  731. }
  732. /**
  733. *获取新闻列表
  734. * @param array $data
  735. * @return array
  736. */
  737. public function getWebsiteArticleList(array $data): array
  738. {
  739. $where[] = ['status', '=', 1];
  740. if (isset($data['keyword']) && !empty($data['keyword'])) {
  741. array_push($where, ['article.title', 'like', '%' . $data['keyword'] . '%']);
  742. }
  743. if (isset($data['catid']) && !empty($data['catid'])) {
  744. if (is_array($data['catid'])) {
  745. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  746. array_push($where, ['catid', 'in', $data['catid']]);
  747. } else {
  748. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  749. array_push($where, ['catid', '=', $data['catid']]);
  750. }
  751. if (empty($category)) {
  752. return Result::error("此网站暂无此栏目", 0);
  753. }
  754. }
  755. // return Result::success($where);
  756. $rep = Article::where(function ($query) use ($where) {
  757. foreach ($where as $condition) {
  758. if ($condition[1] === 'in') {
  759. $query->whereIn($condition[0], $condition[2]);
  760. } else {
  761. $query->where($condition[0], $condition[1], $condition[2]);
  762. }
  763. }
  764. })
  765. ->where(function ($query) use ($data) {
  766. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  767. ->orWhereNull("ignore_ids");
  768. })
  769. ->orderBy("updated_at", "desc")
  770. ->limit($data['pageSize'])
  771. ->offset(($data['page'] - 1) * $data['pageSize'])
  772. ->get();
  773. $count = Article::where(function ($query) use ($where) {
  774. foreach ($where as $condition) {
  775. if ($condition[1] === 'in') {
  776. $query->whereIn($condition[0], $condition[2]);
  777. } else {
  778. $query->where($condition[0], $condition[1], $condition[2]);
  779. }
  780. }
  781. })->count();
  782. $data = [
  783. 'rows' => $rep->toArray(),
  784. 'count' => $count,
  785. ];
  786. if (empty($rep)) {
  787. return Result::error("没有信息数据");
  788. }
  789. return Result::success($data);
  790. }
  791. /**
  792. * 前端-获取新闻详情
  793. * @param array $data
  794. * @return array
  795. */
  796. public function selectWebsiteArticleInfo(array $data): array
  797. {
  798. $where = [
  799. 'article.id' => $data['id'],
  800. 'article.status' => 1,
  801. ];
  802. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")
  803. ->where(function ($query) use ($data) {
  804. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  805. ->orWhereNull("ignore_ids");
  806. })
  807. ->first();
  808. if (empty($result)) {
  809. return Result::error("暂无此新闻!", 0);
  810. }
  811. $category = WebsiteCategory::leftJoin('website', 'website.id', '=', 'website_category.website_id')
  812. ->select('website_category.*', 'website.website_name', 'website.suffix')
  813. ->where('website_category.website_id', $data['website_id'])
  814. ->where(['website_category.category_id' => $result['catid']])
  815. ->first();
  816. if (empty($category)) {
  817. return Result::error("查询失败", 0);
  818. }
  819. //手动推荐文章
  820. $commend_ids = $result['commend_id'] ? json_decode($result['commend_id']) : [];
  821. $commendArticle = Article::whereIn('id', $commend_ids)
  822. ->where('status', 1)
  823. ->select('id', 'title', 'catid', 'imgurl', 'hits', 'created_at')
  824. ->orderBy('updated_at', 'desc')
  825. ->limit(5)
  826. ->get();
  827. $result['category_id'] = $category['category_id'];
  828. $result['cat_name'] = $category['alias'];
  829. $result['website_name'] = $category['website_name'] ?? "";
  830. $result['suffix'] = $category['suffix'] ?? "";
  831. $result['commendArticle'] = $commendArticle;
  832. return Result::success($result);
  833. }
  834. /**
  835. * 前端-获取网站调查问卷
  836. * @param array $data
  837. * @return array
  838. */
  839. public function getWebsiteSurvey(array $data): array
  840. {
  841. if (isset($data['website_id']) && !empty($data['website_id'])) {
  842. $website = Website::where('id', $data['website_id'])->first();
  843. if (empty($website)) {
  844. return Result::error("暂无此网站", 0);
  845. }
  846. }
  847. if (isset($data['art_id']) && !empty($data['art_id'])) {
  848. $article = Article::where('id', $data['art_id'])->where('status', 1)->first();
  849. if (empty($article)) {
  850. return Result::error("暂无此文章", 0);
  851. }
  852. // return Result::error($data,0);
  853. $where['art_id'] = $data['art_id'];
  854. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  855. } else {
  856. $survey = Article::where(function ($query) {
  857. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  858. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  859. })
  860. ->where('status', 1)
  861. ->where('is_survey', 1)
  862. ->select('survey_id')
  863. ->orderBy('updated_at', 'desc')
  864. ->first();
  865. if (empty($survey)) {
  866. return Result::error("暂无调查问卷", 0);
  867. }
  868. $where['sur_id'] = $survey['survey_id'];
  869. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  870. }
  871. // return Result::success($where);
  872. $result = ArticleSurvey::where($where)
  873. ->where(function ($query) {
  874. $query->where('is_other', 0)
  875. ->orWhere(function ($subQuery) {
  876. $subQuery->where('is_other', 1)
  877. ->where('other_id', 0);
  878. });
  879. })
  880. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  881. ->select('article_survey.*', 'article.survey_type')
  882. ->get()->all();
  883. if (empty($result)) {
  884. return Result::error("此文章暂无调查问卷", 0);
  885. }
  886. return Result::success($result);
  887. }
  888. /**
  889. * 前端-添加网站调查问卷选项
  890. * @param array $data
  891. * @return array
  892. */
  893. public function addWebsiteSurveyOption(array $data): array
  894. {
  895. if (isset($data['website_id']) && !empty($data['website_id'])) {
  896. $website = Website::where('id', $data['website_id'])->first();
  897. if (empty($website)) {
  898. return Result::error("暂无此网站", 0);
  899. }
  900. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  901. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->where('is_other', 1)->where('other_id', 0)->first();
  902. if (empty($survey)) {
  903. return Result::error("此调查问卷不可添加选项", 0);
  904. }
  905. if (isset($data['choice_name']) && !empty($data['choice_name'])) {
  906. $choice = [
  907. 'art_id' => $survey['art_id'],
  908. 'website_id' => $data['website_id'],
  909. 'survey_name' => $survey['survey_name'],
  910. 'choice_name' => $data['choice_name'],
  911. 'sur_id' => $survey['sur_id'],
  912. 'is_other' => 1,
  913. 'other_id' => $survey['id'],
  914. ];
  915. $result = ArticleSurvey::insertGetId($choice);
  916. if (empty($result)) {
  917. return Result::error("添加失败", 0);
  918. }
  919. return Result::success($result);
  920. }
  921. }
  922. return Result::error("添加失败", 0);
  923. }
  924. return Result::error("添加失败", 0);
  925. }
  926. /**
  927. * 前端-调查问卷投票
  928. * @param array $data
  929. * @return array
  930. */
  931. public function addWebsiteSurveyVote(array $data): array
  932. {
  933. // return Result::success($data);
  934. if (isset($data['website_id']) && !empty($data['website_id'])) {
  935. $website = Website::where('id', $data['website_id'])->first();
  936. if (empty($website)) {
  937. return Result::error("暂无此网站", 0);
  938. }
  939. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  940. $is_survey = ArticleSurvey::where('sur_id', $data['sur_id'])->first();
  941. // return Result::success($survey);
  942. if (empty($is_survey)) {
  943. return Result::error("此调查问卷不存在", 0);
  944. }
  945. // return Result::success($survey);
  946. // 调查问卷类型
  947. if (isset($data['choice_id']) && !empty($data['choice_id'])) {
  948. //多选 若是json型则转化成数组类型
  949. if (strpos($data['choice_id'], '[') === 0) {
  950. $data['choice_id'] = json_decode($data['choice_id'], true);
  951. } else {
  952. // 单选 也转换成数组
  953. $data['choice_id'] = [$data['choice_id']];
  954. }
  955. $data['choice_id'] = array_map('intval', $data['choice_id']);
  956. $other = ArticleSurvey::whereIn('id', $data['choice_id'])
  957. ->where('website_id', $data['website_id'])
  958. ->where('is_other', 1)
  959. ->where('other_id', 0)
  960. ->first();
  961. if (!empty($other)) {
  962. return Result::error("请选择已有的选项!", 0);
  963. }
  964. $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
  965. ->where('website_id', $data['website_id'])
  966. ->where('is_other', 1)
  967. ->where('other_id', '!=', 0)
  968. ->first();
  969. // return Result::success($choice['other']);
  970. $choice_id = $data['choice_id'];
  971. if (!empty($choice['other'])) {
  972. // array_push($data['choice_id'],$choice['other']['other_id']);
  973. if (!empty($choice_id)) {
  974. $key = array_search($choice['other']['id'], $choice_id);
  975. if ($key !== false) {
  976. unset($choice_id[$key]);
  977. $choice_id = array_values($choice_id);
  978. }
  979. array_push($choice_id, $choice['other']['other_id']);
  980. } else {
  981. $choice_id[0] = $choice['other']['other_id'];
  982. }
  983. array_push($data['choice_id'], $choice['other']['other_id']);
  984. }
  985. // return Result::success($data);
  986. $choice = ArticleSurvey::whereIn('id', $data['choice_id'])
  987. ->where('website_id', $data['website_id'])
  988. ->increment('results', 1);
  989. if (empty($choice)) {
  990. return Result::error("请选择已有的选项!", 0);
  991. }
  992. $survey['data'] = ArticleSurvey::where('sur_id', $data['sur_id'])
  993. ->where('website_id', $data['website_id'])
  994. ->where('other_id', 0)
  995. ->get();
  996. $survey['choice'] = $choice_id;
  997. return Result::success($survey);
  998. }
  999. return Result::error("参数必填!");
  1000. }
  1001. return Result::error("此调查问卷不存在", 0);
  1002. }
  1003. return Result::error("参数必填!");
  1004. }
  1005. /**
  1006. * 后端-获取网站调查问卷列表
  1007. * @param array $data
  1008. * @return array
  1009. */
  1010. public function getSurveyList(array $data): array
  1011. {
  1012. $where = [];
  1013. if (isset($data['survey_name']) && !empty($data['survey_name'])) {
  1014. array_push($where, ['survey_name', 'like', '%' . $data['survey_name'] . '%']);
  1015. }
  1016. if (isset($data['survey_type']) && $data['survey_type'] != null) {
  1017. array_push($where, ['survey_type', '=', $data['survey_type']]);
  1018. }
  1019. if (isset($data['is_survey']) && $data['is_survey'] != null) {
  1020. array_push($where, ['is_survey', '=', $data['is_survey']]);
  1021. }
  1022. // return Result::success($where);
  1023. if (!empty($where)) {
  1024. $query = Article::where($where)->where(function ($q) {
  1025. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  1026. });
  1027. } else {
  1028. $query = Article::where(function ($q) {
  1029. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  1030. });
  1031. }
  1032. $count = $query->count();
  1033. $survey = $query->orderByDesc('id')
  1034. ->limit($data['pageSize'])
  1035. ->offset(($data['page'] - 1) * $data['pageSize'])
  1036. ->get();
  1037. if (empty($survey->toArray())) {
  1038. return Result::error("暂无调查问卷!", 0);
  1039. }
  1040. $result = [
  1041. 'rows' => $survey,
  1042. 'count' => $count,
  1043. ];
  1044. return Result::success($result);
  1045. }
  1046. /**
  1047. * 后端-获取网站调查问卷详情
  1048. * @param array $data
  1049. * @return array
  1050. */
  1051. public function getSurveyInfo(array $data): array
  1052. {
  1053. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  1054. $where = ['sur_id' => $data['sur_id']];
  1055. $choose = ArticleSurvey::where($where)->where('is_other', 0)
  1056. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  1057. ->select('article_survey.*', 'article.survey_type')
  1058. ->get()->all();
  1059. if (empty($choose)) {
  1060. return Result::error("此调查问卷不存在", 0);
  1061. }
  1062. $resultsArray = array_column($choose, 'results');
  1063. $total = array_sum($resultsArray);
  1064. $other = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  1065. $others = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->get()->all();
  1066. // $total = 0;
  1067. if (!empty($other)) {
  1068. $total = $total + $other['results'];
  1069. $other['choice_name'] = '(其他)';
  1070. if (!empty($others)) {
  1071. $other['hasChildren'] = true;
  1072. // array_push($other,['hasChildren','=',true]);
  1073. $other['children'] = $others;
  1074. $other_choices = [$other->toArray()];
  1075. $mer_choice = array_merge($choose, $other_choices);
  1076. $value_choice = array_values($mer_choice);
  1077. } else {
  1078. // return Result::error('1111');
  1079. $other_choices = [$other->toArray()];
  1080. $other_choices = array_merge($choose, $other_choices);
  1081. $value_choice = array_values($other_choices);
  1082. // return Result::success($result);
  1083. }
  1084. } else {
  1085. $value_choice = $choose;
  1086. }
  1087. $result = [
  1088. 'choose' => $value_choice,
  1089. 'total' => $total,
  1090. ];
  1091. }
  1092. return Result::success($result);
  1093. }
  1094. /**
  1095. * 前端-搜索新闻列表
  1096. * @param array $data
  1097. * @return array
  1098. */
  1099. public function selectWebsiteArticle(array $data): array
  1100. {
  1101. $where = [];
  1102. // 初始化查询构造器
  1103. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  1104. $query = Article::where('status', 1)
  1105. ->whereIn('catid', $category)
  1106. ->where(function ($query) use ($data) {
  1107. $query->where(function ($subQuery) use ($data) {
  1108. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  1109. })->orWhereNull("ignore_ids");
  1110. });
  1111. // return Result::success($all_articles);
  1112. // 检查是否存在 cityid 参数
  1113. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1114. $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
  1115. }
  1116. // 检查是否存在 department_id 参数
  1117. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1118. $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
  1119. }
  1120. // 检查是否存在 keyword 参数
  1121. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1122. $query->where('title', 'like', '%' . $data['keyword'] . '%');
  1123. }
  1124. // 计算总数
  1125. $count = $query->count();
  1126. // 分页查询
  1127. $articles = $query->orderBy("updated_at", "desc")
  1128. ->limit($data['pageSize'])
  1129. ->offset(($data['page'] - 1) * $data['pageSize'])
  1130. ->get()->all();
  1131. if (empty($articles)) {
  1132. return Result::error("没有符合条件的资讯数据");
  1133. }
  1134. $data = [
  1135. 'rows' => $articles,
  1136. 'count' => $count,
  1137. ];
  1138. return Result::success($data);
  1139. }
  1140. /**
  1141. * 模块新闻加强版
  1142. * @param array $data
  1143. * @return array
  1144. */
  1145. public function getWebsiteCatidArticle(array $data): array
  1146. {
  1147. // return Result::success($data);
  1148. $where = [
  1149. // 'category.status' => 1,
  1150. 'website_category.category_id' => $data['catid'],
  1151. 'website_category.website_id' => $data['website_id'],
  1152. // 'article.status' => 1,
  1153. ];
  1154. // $category = WebsiteCategory::where($where);
  1155. if (isset($data['child_catnum']) && !empty($data['child_catnum'])) {
  1156. $child_catnum = $data['child_catnum'] ?? 1;
  1157. $category['child'] = WebsiteCategory::where('pid', $data['catid'])->where('website_id', $data['website_id'])->select('category_id', 'alias')->limit($child_catnum)->get()->toArray();
  1158. $childCategoryIds = array_column($category['child'], 'category_id');
  1159. if (empty($childCategoryIds)) {
  1160. return Result::error("暂无子栏目", 0);
  1161. }
  1162. $imgArticles = [];
  1163. $textArticles = [];
  1164. // return Result::success($childCategoryIds);
  1165. if (isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum'] != 0) {
  1166. // 初始化子分类图片新闻和文字新闻数组
  1167. // 查询所有子级栏目的图文新闻
  1168. $imgArticles = Article::where('catid', $childCategoryIds[0])
  1169. ->where('status', 1)
  1170. ->where('imgurl', '!=', '')
  1171. ->select('*')
  1172. ->orderBy('updated_at', 'desc')
  1173. ->limit($data['child_imgnum'])
  1174. ->get();
  1175. }
  1176. if (isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum'] != 0) {
  1177. // 查询所有子级栏目的文字新闻
  1178. $textArticles = Article::where('catid', $childCategoryIds[0])
  1179. ->where('status', 1)
  1180. // ->where(function ($query) {
  1181. // $query->whereNull('imgurl')
  1182. // ->orWhere('imgurl', '');
  1183. // })
  1184. ->select('*')
  1185. ->orderBy('updated_at', 'desc')
  1186. ->limit($data['child_textnum'])
  1187. ->get();
  1188. }
  1189. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  1190. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  1191. $child['img'] = $imgArticles ? $imgArticles->where('catid', $child['category_id']) : [];
  1192. $child['text'] = $textArticles ? $textArticles->where('catid', $child['category_id']) : [];
  1193. return $child;
  1194. }, $category['child']);
  1195. }
  1196. // }
  1197. if (isset($data['img_num']) && !empty($data['img_num'])) {
  1198. $category['img'] = WebsiteCategory::where($where)
  1199. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1200. ->where('article.status', 1)
  1201. ->where('article.imgurl', '!=', '')
  1202. ->select('article.*', 'website_category.category_id', 'website_category.alias')
  1203. ->orderBy('article.updated_at', 'desc')
  1204. ->limit($data['img_num'])
  1205. ->get();
  1206. }
  1207. if (isset($data['text_num']) && !empty($data['text_num'])) {
  1208. $category['text'] = WebsiteCategory::where($where)
  1209. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1210. ->where('article.status', 1)
  1211. // ->where(function ($query) {
  1212. // $query->whereNull('article.imgurl')
  1213. // ->orWhere('article.imgurl', '');
  1214. // })
  1215. ->select('article.*', 'website_category.category_id', 'website_category.alias')
  1216. ->orderBy('article.updated_at', 'desc')
  1217. ->limit($data['text_num'])
  1218. ->get();
  1219. }
  1220. // $category = $category->get();
  1221. if (empty($category)) {
  1222. return Result::error("查询失败", 0);
  1223. }
  1224. return Result::success($category);
  1225. }
  1226. /**
  1227. * 验证导航名称是否重复
  1228. * @return void
  1229. */
  1230. public function checkCategoryName(array $data): array
  1231. {
  1232. $result = Category::when($data, function ($query) use ($data) {
  1233. if (isset($data['name']) && $data['name']) {
  1234. $query->where("name", $data['name']);
  1235. }
  1236. if (isset($data['id']) && $data['id']) {
  1237. $query->where("id", "!=", $data['id']);
  1238. }
  1239. })->first();
  1240. if ($result) {
  1241. return Result::error("已存在");
  1242. } else {
  1243. return Result::success();
  1244. }
  1245. }
  1246. //20250226 产品列表
  1247. public function getGoodList(array $data): array
  1248. {
  1249. $type_id = isset($data['type_id']) ? $data['type_id'] : '';
  1250. unset($data['type_id']);
  1251. $user_id = isset($data['user_id']) ? $data['user_id'] : '';
  1252. $where = [];
  1253. if ($type_id != '10000') {
  1254. $where = [
  1255. 'good.user_id' => $user_id,
  1256. ];
  1257. }
  1258. //类型
  1259. if (isset($data['type_id']) && $data['type_id']) {
  1260. $where = [
  1261. 'type_id' => $data['type_id'],
  1262. ];
  1263. }
  1264. //名称
  1265. if (isset($data['name']) && $data['name']) {
  1266. $where = [
  1267. 'good.name' => $data['name'],
  1268. ];
  1269. }
  1270. $where1 = [];
  1271. //website_id
  1272. // if (isset($data['website_id']) && $data['website_id']) {
  1273. // $where1 = [
  1274. // 'good.website_id', 'like', '%' . $data['website_id'] . '%',
  1275. // ];
  1276. // }
  1277. // website_name
  1278. if (isset($data['website_name']) && $data['website_name']) {
  1279. $where1[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  1280. }
  1281. // catid
  1282. if (isset($data['category_name']) && $data['category_name']) {
  1283. $where1[] = ['category.name', 'like', '%' . $data['category_name'] . '%'];
  1284. }
  1285. // $result = Good::where($where)
  1286. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  1287. $result = Good::where($where)
  1288. ->when(!empty($where1), function ($query) use ($where1) {
  1289. return $query->where($where1);
  1290. })
  1291. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  1292. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  1293. ->leftJoin('category', 'good.catid', '=', 'category.id')
  1294. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  1295. ->orderBy("id", "desc")
  1296. ->limit($data['page_size'])
  1297. ->offset(($data['page'] - 1) * $data['page_size'])
  1298. ->get();
  1299. $count = Good::where($where)
  1300. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  1301. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  1302. ->leftJoin('category', 'good.catid', '=', 'category.id')
  1303. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  1304. ->orderBy("updated_at", "desc")->count();
  1305. $data = [
  1306. 'rows' => $result->toArray(),
  1307. 'count' => $count,
  1308. ];
  1309. if (empty($result)) {
  1310. return Result::error("此栏目暂无相关产品", 0);
  1311. }
  1312. return Result::success($data);
  1313. }
  1314. public function getGoodInfo(array $data): array
  1315. {
  1316. $result = Good::where('id', $data['id'])->first();
  1317. if (empty($result)) {
  1318. return Result::error("此产品不存在", 0);
  1319. }
  1320. return Result::success($result);
  1321. }
  1322. public function addGood(array $data): array
  1323. {
  1324. // unset($data['city_arr_id']);
  1325. // unset($data['cat_arr_id']);
  1326. $data['city_id'] = end($data['city_arr_id']);
  1327. $data['catid'] = end($data['cat_arr_id']);
  1328. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  1329. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  1330. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  1331. unset($data['imgUrl']);
  1332. $result = Good::insert($data);
  1333. if (empty($result)) {
  1334. return Result::error("添加失败", 0);
  1335. }
  1336. return Result::success($result);
  1337. }
  1338. public function updateGood(array $data): array
  1339. {
  1340. $data['city_id'] = end($data['city_arr_id']);
  1341. $data['catid'] = end($data['cat_arr_id']);
  1342. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  1343. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  1344. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  1345. //设置东八区
  1346. date_default_timezone_set('Asia/Shanghai');
  1347. $data['updated_at'] = date('Y-m-d H:i:s');
  1348. $result = Good::where('id', $data['id'])->update($data);
  1349. if (empty($result)) {
  1350. return Result::error("更新失败", 0);
  1351. }
  1352. return Result::success($result);
  1353. }
  1354. public function delGood(array $data): array
  1355. {
  1356. $result = Good::where('id', $data['id'])->delete();
  1357. if (empty($result)) {
  1358. return Result::error("删除失败", 0);
  1359. }
  1360. return Result::success($result);
  1361. }
  1362. //20250226 产品列表
  1363. //20250306 求职信息
  1364. public function getJobHuntingList(array $data): array
  1365. {
  1366. $where = [];
  1367. if (isset($data['username']) && !empty($data['username'])) {
  1368. $where[] = ['user.user_name', 'like', '%' . $data['username'] . '%'];
  1369. }
  1370. $type_id = isset($data['type_id']) ? $data['type_id'] : '';
  1371. $user_id = isset($data['user_id']) ? $data['user_id'] : '';
  1372. unset($data['type_id']);
  1373. if ($type_id != '10000') {
  1374. $where[] = ['job_hunting.user_id', '=', $user_id];
  1375. }
  1376. $result = JobHunting::where($where)
  1377. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  1378. ->leftJoin('website', 'website.id', '=', 'job_hunting.website_id')
  1379. ->select('job_hunting.*', 'user.nickname as nickname', 'user.user_name as username', 'website.website_name as website_name')
  1380. ->orderBy("id", "desc")
  1381. ->limit($data['page_size'])
  1382. ->offset(($data['page'] - 1) * $data['page_size'])
  1383. ->get();
  1384. if (empty($result)) {
  1385. return Result::error("查询失败", 0);
  1386. }
  1387. $count = JobHunting::where($where)
  1388. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  1389. ->leftJoin('website', 'website.id', '=', 'job_hunting.website_id')
  1390. ->count();
  1391. $data = [
  1392. 'rows' => $result->toArray(),
  1393. 'count' => $count,
  1394. ];
  1395. return Result::success($data);
  1396. }
  1397. public function addJobHunting(array $data): array
  1398. {
  1399. date_default_timezone_set('Asia/Shanghai');
  1400. unset($data['company_name']);
  1401. unset($data['job_industry']);
  1402. unset($data['job_name']);
  1403. unset($data['department']);
  1404. unset($data['job_timeList']);
  1405. unset($data['job_content']);
  1406. $data['created_at'] = date('Y-m-d H:i:s');
  1407. $data['updated_at'] = date('Y-m-d H:i:s');
  1408. var_dump($data, '-----------------test---------');
  1409. $result = JobHunting::create($data);
  1410. if (empty($result)) {
  1411. return Result::error("添加失败", 0);
  1412. }
  1413. return Result::success($result);
  1414. }
  1415. public function delJobHunting(array $data): array
  1416. {
  1417. $result = JobHunting::where('id', $data['id'])->delete();
  1418. if (empty($result)) {
  1419. return Result::error("删除失败", 0);
  1420. }
  1421. return Result::success($result);
  1422. }
  1423. public function updateJobHunting(array $data): array
  1424. {
  1425. //设置东八区
  1426. date_default_timezone_set('Asia/Shanghai');
  1427. unset($data['company_name']);
  1428. unset($data['job_industry']);
  1429. unset($data['job_name']);
  1430. unset($data['department']);
  1431. unset($data['job_timeList']);
  1432. unset($data['job_content']);
  1433. $data['created_at'] = date('Y-m-d H:i:s');
  1434. $data['updated_at'] = date('Y-m-d H:i:s');
  1435. $result = JobHunting::where('id', $data['id'])->update($data);
  1436. if (empty($result)) {
  1437. return Result::error("更新失败", 0);
  1438. }
  1439. return Result::success($result);
  1440. }
  1441. public function getJobHuntingInfo(array $data): array
  1442. {
  1443. $result = JobHunting::where('id', $data['id'])->first();
  1444. if (empty($result)) {
  1445. return Result::error("查询失败", 0);
  1446. }
  1447. return Result::success($result);
  1448. }
  1449. public function getJobHuntingData(array $data): array
  1450. {
  1451. $jobEnum = JobEnum::get();
  1452. $jobIndustry = JobIndustry::get();
  1453. $jobNature = JobNature::get();
  1454. $jobPosition = JobPosition::get();
  1455. $data = [
  1456. 'jobEnum' => $jobEnum,
  1457. 'jobIndustry' => $jobIndustry,
  1458. 'jobNature' => $jobNature,
  1459. 'jobPosition' => $jobPosition,
  1460. ];
  1461. return Result::success($data);
  1462. }
  1463. public function delJobHuntingInfo(array $data): array
  1464. {
  1465. $result = JobHunting::where('id', $data['id'])->delete();
  1466. return Result::success();
  1467. }
  1468. //20250324 通知,公告,消息
  1469. public function delNotice(array $data): array
  1470. {
  1471. return Result::success();
  1472. }
  1473. public function getNoticeInfo(array $data): array
  1474. {
  1475. $result = Notice::where('id', $data['id'])->first();
  1476. if (empty($result)) {
  1477. return Result::error("查询失败", 0);
  1478. }
  1479. return Result::success($result);
  1480. }
  1481. public function addNotice(array $data): array
  1482. {
  1483. $result = Notice::create($data);
  1484. return Result::success($result);
  1485. }
  1486. public function updateNotice(array $data): array
  1487. {
  1488. $result = Notice::where('id', $data['id'])->update($data);
  1489. return Result::success($result);
  1490. }
  1491. public function getNoticeList(array $data): array
  1492. {
  1493. $result = Notice::where('status', 1)->get();
  1494. return Result::success($result);
  1495. }
  1496. public function getNoticeDetail(array $data): array
  1497. {
  1498. $result = Notice::where('id', $data['id'])->first();
  1499. if (empty($result)) {
  1500. return Result::error("查询失败", 0);
  1501. }
  1502. return Result::success($result);
  1503. }
  1504. public function deleteNotice(array $data): array
  1505. {
  1506. $result = Notice::where('id', $data['id'])->delete();
  1507. return Result::success($result);
  1508. }
  1509. public function getMSG(array $data): array
  1510. {
  1511. $type_id = isset($data['type']) ? $data['type'] : 1;
  1512. // '1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)
  1513. $user_id = isset($data['user_id']) ? $data['user_id'] : 0; //用户id
  1514. $result = [];
  1515. if ($type_id == 1) {
  1516. //最近的5篇已审的文章
  1517. $apply_articale = Article::where('status', 1)
  1518. ->orderBy('updated_at', 'desc')
  1519. ->limit(5)->get();
  1520. //获取5条单聊未读聊天消息
  1521. $chat = ChatRecords::where('is_read', 0)
  1522. ->where('user_id', $user_id)
  1523. ->where('talk_type', 1)
  1524. ->orderBy('created_at', 'desc')
  1525. ->limit(5)->get();
  1526. //获取5条未读群聊信息
  1527. $chat_group = ChatRecords::where('is_read', 0)
  1528. ->where('user_id', $user_id)
  1529. ->where('talk_type', 2)
  1530. ->orderBy('created_at', 'desc')
  1531. ->limit(5)->get();
  1532. $count = count($chat) + count($chat_group) + count($apply_articale);
  1533. $result = [
  1534. 'apply_articale' => $apply_articale,
  1535. 'chat' => $chat,
  1536. 'chat_group' => $chat_group,
  1537. 'count' => $count,
  1538. ];
  1539. } elseif ($type_id == 2) {
  1540. //最近的5篇已审的文章
  1541. $apply_articale = Article::where('status', 1)->limit(5)->get();
  1542. //获取5条单聊未读聊天消息
  1543. $chat = ChatRecords::where('is_read', 0)
  1544. ->where('user_id', $user_id)
  1545. ->where('talk_type', 1)
  1546. ->orderBy('created_at', 'desc')
  1547. ->limit(5)->get();
  1548. //获取5条未读群聊信息
  1549. $chat_group = ChatRecords::where('is_read', 0)
  1550. ->where('user_id', $user_id)
  1551. ->where('talk_type', 2)
  1552. ->orderBy('created_at', 'desc')
  1553. ->limit(5)->get();
  1554. $count = count($chat) + count($chat_group) + count($apply_articale);
  1555. $result = [
  1556. 'apply_articale' => $apply_articale,
  1557. 'chat' => $chat,
  1558. 'chat_group' => $chat_group,
  1559. 'count' => $count,
  1560. ];
  1561. } elseif ($type_id == 3) {
  1562. //最近的5篇已审的文章
  1563. $apply_articale = Article::where('status', 1)->limit(5)->get();
  1564. //获取5条单聊未读聊天消息
  1565. $chat = ChatRecords::where('is_read', 0)
  1566. ->where('user_id', $user_id)
  1567. ->where('talk_type', 1)
  1568. ->orderBy('created_at', 'desc')
  1569. ->limit(5)->get();
  1570. //获取5条未读群聊信息
  1571. $chat_group = ChatRecords::where('is_read', 0)
  1572. ->where('user_id', $user_id)
  1573. ->where('talk_type', 2)
  1574. ->orderBy('created_at', 'desc')
  1575. ->limit(5)->get();
  1576. $count = count($chat) + count($chat_group) + count($apply_articale);
  1577. $result = [
  1578. 'apply_articale' => $apply_articale,
  1579. 'chat' => $chat,
  1580. 'chat_group' => $chat_group,
  1581. 'count' => $count,
  1582. ];
  1583. } elseif ($type_id == 4) {
  1584. //最近的5篇已审的文章
  1585. $apply_articale = Article::where('status', 1)->limit(5)->get();
  1586. $count = count($apply_articale);
  1587. $result = [
  1588. 'count' => $count,
  1589. ];
  1590. } elseif ($type_id == 10000) {
  1591. //获取未审核的5篇文章
  1592. $apply_articale = Article::where('status', 0)
  1593. ->orderBy('updated_at', 'desc')
  1594. ->limit(5)->get();
  1595. //获取5条单聊未读聊天消息
  1596. $chat = ChatRecords::where('is_read', 0)
  1597. ->where('user_id', $user_id)
  1598. ->where('talk_type', 1)
  1599. ->orderBy('created_at', 'desc')
  1600. ->limit(5)->get();
  1601. //获取5条未读群聊信息
  1602. $chat_group = ChatRecords::where('is_read', 0)
  1603. ->where('user_id', $user_id)
  1604. ->where('talk_type', 2)
  1605. ->orderBy('created_at', 'desc')
  1606. ->limit(5)->get();
  1607. $count = count($chat) + count($chat_group) + count($apply_articale);
  1608. $result = [
  1609. 'apply_articale' => $apply_articale,
  1610. 'chat' => $chat,
  1611. 'chat_group' => $chat_group,
  1612. 'count' => $count,
  1613. ];
  1614. } elseif ($type_id == 20000) {
  1615. }
  1616. return Result::success($result);
  1617. }
  1618. public function getComplaintList(array $data): array
  1619. {
  1620. $result = Notice::where('status', 1)->get();
  1621. return Result::success($result);
  1622. }
  1623. public function getComplaintInfo(array $data): array
  1624. {
  1625. $result = Notice::where('id', $data['id'])->first();
  1626. if (empty($result)) {
  1627. return Result::error("查询失败", 0);
  1628. }
  1629. return Result::success($result);
  1630. }
  1631. public function addComplaint(array $data): array
  1632. {
  1633. $result = Notice::create($data);
  1634. return Result::success($result);
  1635. }
  1636. public function updateComplaint(array $data): array
  1637. {
  1638. $result = Notice::where('id', $data['id'])->update($data);
  1639. return Result::success($result);
  1640. }
  1641. public function deleteComplaint(array $data): array
  1642. {
  1643. $result = Notice::where('id', $data['id'])->delete();
  1644. return Result::success($result);
  1645. }
  1646. public function getComplainInfo(array $data): array
  1647. {
  1648. $result = Notice::where('id', $data['id'])->first();
  1649. if (empty($result)) {
  1650. return Result::error("查询失败", 0);
  1651. }
  1652. return Result::success($result);
  1653. }
  1654. //20250324 通知,公告,消息
  1655. }