NewsService.php 68 KB

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