NewsService.php 67 KB

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