NewsService.php 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857
  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\District;
  10. use App\Model\Good;
  11. use App\Model\JobCompany;
  12. use App\Model\Website;
  13. use App\Model\JobRecruiting;
  14. use App\Model\JobIndustry;
  15. use App\Model\JobPosition;
  16. use App\Model\Notice;
  17. use App\Model\JobNature;
  18. use App\Model\JobEnum;
  19. use App\Model\User;
  20. use App\Model\UserRole;
  21. use App\Model\News;
  22. use App\Model\UserInfo;
  23. use App\Model\WebsiteGroup;;
  24. use Hyperf\DbConnection\Db;
  25. use Hyperf\RpcServer\Annotation\RpcService;
  26. use App\Tools\Result;
  27. use Directory;
  28. use Ramsey\Uuid\Uuid;
  29. use Hyperf\Utils\Random;
  30. use Fukuball\Jieba\Jieba;
  31. use Fukuball\Jieba\Finalseg;
  32. use Hyperf\Utils\Collection;
  33. use function Hyperf\Support\retry;
  34. use App\Tools\PublicData;
  35. use App\Model\ChatGroups;
  36. use App\Model\ChatGroupsMember;
  37. use App\Model\ChatRecords;
  38. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  39. class NewsService implements NewsServiceInterface
  40. {
  41. /**
  42. * 获取导航池列表
  43. * @param array $data
  44. * @return array
  45. */
  46. public function getCategoryList(array $data): array
  47. {
  48. $search = $data['name'] ?? '';
  49. $page = (int)($data['page'] ?? 1);
  50. $perPage = (int)($data['pageSize'] ?? 10);
  51. // 1. 查出所有匹配的分类id
  52. if ($search) {
  53. $matchedIds = Category::where('name', 'like', "%{$search}%")->pluck('id')->toArray();
  54. if (empty($matchedIds)) {
  55. return Result::success(['rows' => [], 'total' => 0]);
  56. }
  57. // 2. 递归查所有父级,直到一级分类
  58. $idsStr = implode(',', $matchedIds);
  59. $sql = "
  60. WITH RECURSIVE parents AS (
  61. SELECT * FROM category WHERE id IN ($idsStr)
  62. UNION ALL
  63. SELECT c.* FROM category c
  64. INNER JOIN parents p ON c.id = p.pid
  65. )
  66. SELECT * FROM parents
  67. ";
  68. $parents = Db::select($sql);
  69. $parentIds = [];
  70. foreach ($parents as $row) {
  71. if ($row->pid == 0) {
  72. $parentIds[] = $row->id;
  73. }
  74. }
  75. $parentIds = array_unique($parentIds);
  76. } else {
  77. // 没有搜索,一级分类就是根
  78. $parentIds = Category::where('pid', 0)->pluck('id')->toArray();
  79. }
  80. // 3. 一级分类分页
  81. $total = count($parentIds);
  82. $parentIds = array_slice($parentIds, ($page - 1) * $perPage, $perPage);
  83. if (empty($parentIds)) {
  84. return Result::success(['rows' => [], 'total' => $total]);
  85. }
  86. // 4. 递归查所有子孙
  87. $idsStr = implode(',', $parentIds);
  88. $sql = "
  89. WITH RECURSIVE subcategories AS (
  90. SELECT * FROM category WHERE id IN ($idsStr)
  91. UNION ALL
  92. SELECT c.* FROM category c
  93. INNER JOIN subcategories s ON c.pid = s.id
  94. )
  95. SELECT * FROM subcategories ORDER BY updated_at DESC
  96. ";
  97. $allCategories = Db::select($sql);
  98. // 5. 组装树结构
  99. $categoryMap = [];
  100. foreach ($allCategories as $cat) {
  101. $cat = (array)$cat;
  102. $cat['children'] = [];
  103. $categoryMap[$cat['id']] = $cat;
  104. }
  105. foreach ($categoryMap as $id => &$cat) {
  106. if ($cat['pid'] && isset($categoryMap[$cat['pid']])) {
  107. $categoryMap[$cat['pid']]['children'][] = &$cat;
  108. }
  109. }
  110. unset($cat);
  111. $categoryTree = [];
  112. foreach ($parentIds as $id) {
  113. if (isset($categoryMap[$id])) {
  114. $categoryTree[] = $categoryMap[$id];
  115. }
  116. }
  117. return Result::success([
  118. 'rows' => $categoryTree,
  119. 'total' => $total,
  120. ]);
  121. }
  122. private function findMatchedCategories($search)
  123. {
  124. if ($search) {
  125. return Category::where('name', 'like', "%{$search}%")
  126. ->orderBy('updated_at', 'desc')
  127. ->get();
  128. }
  129. return Category::orderBy('updated_at', 'desc') // 按 updated_at 字段倒序排序
  130. ->get();
  131. }
  132. private function findAllParents($categories)
  133. {
  134. $allCategories = [];
  135. foreach ($categories as $category) {
  136. $parentId = $category->pid;
  137. while ($parentId > 0) {
  138. $parent = Category::find($parentId);
  139. if ($parent) {
  140. $allCategories[$parent->id] = $parent;
  141. $parentId = $parent->pid;
  142. } else {
  143. break;
  144. }
  145. }
  146. $allCategories[$category->id] = $category;
  147. }
  148. return array_values($allCategories);
  149. }
  150. private function buildCategoryTree($category, $allRequiredCategories)
  151. {
  152. $categoryData = $category->toArray();
  153. $children = [];
  154. foreach ($allRequiredCategories as $c) {
  155. if ($c->pid === $category->id) {
  156. $children[] = $this->buildCategoryTree($c, $allRequiredCategories);
  157. }
  158. }
  159. if (!empty($children)) {
  160. $categoryData['children'] = $children;
  161. }
  162. return $categoryData;
  163. }
  164. public function myCategoryList(array $data): array
  165. {
  166. $sszq = $data['sszq'] ?? '';
  167. unset($data['sszq']);
  168. //1,2,3 根据这些webid,。从website_category表中取出对应的分类id,然后从category表中取出分类信息
  169. $catorytids = WebsiteCategory::whereIn('website_id', explode(',', $sszq))->get()->pluck('category_id')->toArray();
  170. $where[] = [
  171. 'pid',
  172. '=',
  173. $data['pid'],
  174. ];
  175. if (isset($data['name'])) {
  176. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  177. }
  178. var_dump($where);
  179. //根据分类id,从category表中取出分类信息
  180. $result = Category::where($where)
  181. ->whereIn('category.id', $catorytids)
  182. ->select('category.*', 'category.id as category_id')->get();
  183. if (empty($result)) {
  184. return Result::error("没有栏目数据");
  185. }
  186. return Result::success($result);
  187. }
  188. /**
  189. * @param array $data
  190. * @return array
  191. */
  192. public function categoryList(array $data): array
  193. {
  194. $where[] = [
  195. 'pid',
  196. '=',
  197. $data['pid'],
  198. ];
  199. if (isset($data['name'])) {
  200. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  201. }
  202. var_dump($where);
  203. $result = Category::where($where)->select('category.*', 'category.id as category_id')->get();
  204. if (empty($result)) {
  205. return Result::error("没有栏目数据");
  206. }
  207. return Result::success($result);
  208. }
  209. /**
  210. * @param array $data
  211. * @return array
  212. */
  213. public function addCategory(array $data): array
  214. {
  215. if (isset($data['id'])) {
  216. unset($data['id']);
  217. }
  218. $id = Category::insertGetId($data);
  219. if (empty($id)) {
  220. return Result::error("添加失败");
  221. }
  222. return Result::success(['id' => $id]);
  223. }
  224. /**
  225. * @param array $data
  226. * @return array
  227. */
  228. public function delCategory(array $data): array
  229. {
  230. Db::beginTransaction();
  231. try {
  232. $categoryList = Category::where(['pid' => $data['id']])->get();
  233. if ($categoryList->toArray()) {
  234. Db::rollBack();
  235. return Result::error("分类下面有子分类不能删除");
  236. }
  237. $articleList = Article::where(['catid' => $data['id']])->get();
  238. if ($articleList->toArray()) {
  239. Db::rollBack();
  240. return Result::error("分类下面有资讯不能删除");
  241. }
  242. $result = Category::where($data)->delete();
  243. WebsiteCategory::where(['category_id' => $data['id']])->delete();
  244. if (!$result) {
  245. return Result::error("删除失败");
  246. }
  247. return Result::success($result);
  248. Db::commit();
  249. } catch (\Exception $e) {
  250. Db::rollBack();
  251. return Result::error("删除失败");
  252. }
  253. }
  254. /**
  255. * @param array $data
  256. * @return array
  257. */
  258. public function updateCategory(array $data): array
  259. {
  260. $where = [
  261. 'id' => $data['id'],
  262. ];
  263. $result = Category::where($where)->update($data);
  264. if ($result) {
  265. return Result::success($result);
  266. } else {
  267. return Result::error("更新失败");
  268. }
  269. }
  270. /**
  271. * 获取导航池信息
  272. * @param array $data
  273. * @return array
  274. */
  275. public function getCategoryInfo(array $data): array
  276. {
  277. $where = [
  278. 'id' => $data['id'],
  279. ];
  280. $result = Category::where($where)->first();
  281. if ($result) {
  282. return Result::success($result);
  283. } else {
  284. return Result::error("更新失败");
  285. }
  286. }
  287. /**
  288. * @param array $data
  289. * @return array
  290. */
  291. public function getArticleList(array $data): array
  292. {
  293. //判断是否是管理员'1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)'
  294. $type_id = $data['type_id'];
  295. unset($data['type_id']);
  296. $user_id = $data['user_id'];
  297. unset($data['user_id']);
  298. $where = [];
  299. $status1 = [];
  300. if (isset($data['id']) && $data['id']) {
  301. array_push($where, ['article.id', '=', $data['id']]);
  302. }
  303. if (isset($data['title']) && $data['title']) {
  304. array_push($where, ['article.title', 'like', '%' . $data['title'] . '%']);
  305. }
  306. if (isset($data['category_name']) && $data['category_name']) {
  307. array_push($where, ['category.name', 'like', '%' . $data['category_name'] . '%']);
  308. }
  309. if (isset($data['author']) && $data['author']) {
  310. array_push($where, ['article.author', '=', $data['author']]);
  311. }
  312. if (isset($data['islink']) && $data['islink'] !== "") {
  313. array_push($where, ['article.islink', '=', $data['islink']]);
  314. }
  315. if (isset($data['status']) && $data['status'] !== "") {
  316. array_push($where, ['article.status', '=', $data['status']]);
  317. }
  318. if (isset($data['status1'])) {
  319. $status1 = json_decode(($data['status1']));
  320. }
  321. //不是管理员展示个人数据;
  322. if ($type_id != 10000) {
  323. $where[] = ['article.admin_user_id', '=', $user_id];
  324. }
  325. $rep = Article::where($where)
  326. ->whereNotIn('article.status', [404])
  327. ->when($status1, function ($query) use ($status1) {
  328. if (isset($status1) && $status1) {
  329. $query->whereIn('article.status', $status1);
  330. }
  331. })
  332. ->leftJoin('category', 'article.catid', 'category.id')
  333. ->leftJoin('website_category', function ($join) {
  334. $join->on('article.web_site_id', '=', 'website_category.website_id')
  335. ->on('article.catid', '=', 'website_category.category_id');
  336. })
  337. ->select("article.*", "category.name as category_name", "website_category.alias")
  338. ->orderBy("article.updated_at", "desc")
  339. ->limit($data['pageSize'])
  340. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  341. $count = Article::where($where)->whereNotIn('article.status', [404])
  342. ->when($status1, function ($query) use ($status1) {
  343. if (isset($status1) && $status1) {
  344. $query->whereIn('article.status', $status1);
  345. }
  346. })
  347. ->leftJoin('category', 'article.catid', 'category.id')->count();
  348. $data = [
  349. 'rows' => $rep->toArray(),
  350. 'count' => $count,
  351. ];
  352. if (empty($rep)) {
  353. return Result::error("没有信息数据");
  354. }
  355. return Result::success($data);
  356. }
  357. /**
  358. * @param array $data
  359. * @return array
  360. */
  361. public function addArticle(array $data): array
  362. {
  363. var_dump($data, '----------12-----------1');
  364. unset($data['user_type']);
  365. unset($data['nav_add_pool_id']);
  366. // $data['cat_arr_id'] = is_string($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  367. Db::beginTransaction();
  368. try {
  369. //处理投票
  370. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  371. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  372. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  373. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  374. unset($data['is_survey']);
  375. unset($data['survey_name']);
  376. unset($data['suvey_array']);
  377. // unset($data['website_id']);
  378. // unset($data['web_site_id']);
  379. // $data['web_site_id'] = is_array($data['web_site_id']) ? json_encode($data['web_site_id']) : ($data['web_site_id']);
  380. if ($data['hits'] == '') {
  381. $data['hits'] = 0;
  382. }
  383. if ($data['is_original'] == '') {
  384. $data['is_original'] = 0;
  385. }
  386. if ($data['status'] == '') {
  387. $data['status'] = 0;
  388. }
  389. $articleData = $data;
  390. unset($articleData['content']);
  391. //自动处理缩略图、关键字、描述
  392. if ($articleData['imgurl'] == '') {
  393. //如果没有图,设置level=0
  394. $levelArr = json_decode($articleData['level'], true);
  395. var_dump($levelArr, '----------levelArr-----------1');
  396. //content中提取图片第一个图,正则提取
  397. $reg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  398. preg_match_all($reg, $data['content'], $matches);
  399. if (isset($matches[1][0])) {
  400. //截取varchar240
  401. $articleData['imgurl'] = substr($matches[1][0], 0, 240);
  402. //如果有图,设置level=3
  403. if (!in_array(3, $levelArr)) {
  404. $levelArr[] = 3;
  405. }
  406. $articleData['level'] = json_encode($levelArr);
  407. } else {
  408. if (!in_array(0, $levelArr)) {
  409. $levelArr[] = 0;
  410. }
  411. $articleData['level'] = json_encode($levelArr);
  412. }
  413. }
  414. var_dump($articleData['level'], '----------$articleData[level]----------1');
  415. if ($articleData['keyword'] == '') {
  416. //提取标题+内容中的关键词
  417. $articleData['keyword'] = $data['title'];
  418. // . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
  419. Jieba::init(); // 初始化 jieba-php
  420. Finalseg::init();
  421. $segList = Jieba::cut($articleData['keyword']);
  422. $segList1 = array_slice($segList, 0, 8);
  423. $articleData['keyword'] = implode(',', $segList1);
  424. }
  425. if ($articleData['introduce'] == '') {
  426. //提取内容中的描述
  427. $articleData['introduce'] = substr(str_replace(' ', '', strip_tags($data['content'])), 0, 100);
  428. }
  429. $id = Article::insertGetId($articleData);
  430. $articleDataContent = [
  431. 'article_id' => $id,
  432. 'content' => $data['content'],
  433. ];
  434. ArticleData::insertGetId($articleDataContent);
  435. //处理投票
  436. if ($is_survey == 1) {
  437. //生成年月日时分秒+8位随机数
  438. $uuid = date('YmdHis') . rand(10000000, 99999999);
  439. var_dump($suvey_array, 'suvey_array________');
  440. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  441. var_dump($suveys_array, '---------------------1');
  442. var_dump($suvey_array, '---------------------2');
  443. $suvey_data = [];
  444. foreach ($suveys_array as $key => $value) {
  445. if ($value == '') {
  446. continue;
  447. }
  448. if (is_array($value)) {
  449. $suvey_data[] = [
  450. 'sur_id' => $uuid,
  451. 'art_id' => $id,
  452. 'website_id' => $website_id ?? 2,
  453. 'survey_name' => $survey_name,
  454. 'choice_name' => $value[1],
  455. 'is_other' => 1,
  456. 'other_id' => 0,
  457. 'results' => 0,
  458. ];
  459. } else {
  460. $suvey_data[] = [
  461. 'sur_id' => $uuid,
  462. 'art_id' => $id,
  463. 'website_id' => $website_id ?? 2,
  464. 'survey_name' => $survey_name,
  465. 'choice_name' => $value,
  466. 'is_other' => 0,
  467. 'other_id' => 0,
  468. 'results' => 0,
  469. ];
  470. }
  471. if (empty($suvey_data)) {
  472. throw new \Exception("投票数据为空");
  473. }
  474. }
  475. $result = ArticleSurvey::insert($suvey_data);
  476. if (!$result) {
  477. throw new \Exception("投票失败,ArticleSurvey插入失败");
  478. }
  479. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  480. if (!$result) {
  481. throw new \Exception("投票失败,更新主表失败");
  482. }
  483. }
  484. Db::commit();
  485. return Result::success(['id' => $id]);
  486. } catch (\Throwable $ex) {
  487. Db::rollBack();
  488. var_dump($ex->getMessage());
  489. return Result::error("创建失败", 0);
  490. }
  491. }
  492. /**
  493. * @param array $data
  494. * @return array
  495. */
  496. public function delArticle(array $data): array
  497. {
  498. $result = Article::where($data)->delete();
  499. //survey投票删除
  500. articleSurvey::where(['art_id' => $data['id']])->delete();
  501. if (!$result) {
  502. return Result::error("删除失败");
  503. }
  504. return Result::success($result);
  505. }
  506. /**
  507. * @param array $data
  508. * @return array
  509. */
  510. public function updateArticle(array $data): array
  511. {
  512. var_dump($data, '----------12-----------1');
  513. Db::beginTransaction();
  514. unset($data['user_type']);
  515. // unset($data['web_site_id']);
  516. unset($data['nav_add_pool_id']);
  517. try {
  518. //处理投票
  519. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  520. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  521. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  522. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  523. unset($data['is_survey']);
  524. unset($data['survey_name']);
  525. unset($data['suvey_array']);
  526. unset($data['website_id']);
  527. $data['web_site_id'] = is_array($data['web_site_id']) ? json_encode($data['web_site_id']) : ($data['web_site_id']);
  528. if ($data['hits'] == '') {
  529. $data['hits'] = 0;
  530. }
  531. if ($data['is_original'] == '') {
  532. $data['is_original'] = 0;
  533. }
  534. if ($data['status'] == '') {
  535. $data['status'] = 0;
  536. }
  537. $data['cat_arr_id'] = is_array($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : $data['cat_arr_id'];
  538. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  539. $articleData = $data;
  540. unset($articleData['content']);
  541. unset($articleData['status_name']);
  542. unset($articleData['name']);
  543. unset($articleData['content']);
  544. unset($articleData['pid_arr']);
  545. unset($articleData['pid']);
  546. //自动处理缩略图、关键字、描述
  547. if ($articleData['imgurl'] == '') {
  548. //如果没有图,设置level=0
  549. $levelArr = json_decode($articleData['level'], true);
  550. var_dump($levelArr, '----------levelArr-----------1');
  551. //content中提取图片第一个图,正则提取
  552. $reg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  553. preg_match_all($reg, $data['content'], $matches);
  554. if (isset($matches[1][0])) {
  555. //截取varchar240
  556. // 截取到第一个<
  557. $articleData['imgurl'] = $matches[1][0];
  558. var_dump($articleData['imgurl'], '----------imgurl-----------1');
  559. $pos = strpos($articleData['imgurl'], '"');
  560. if ($pos !== false) {
  561. $articleData['imgurl'] = substr($articleData['imgurl'], 0, $pos);
  562. }
  563. //$articleData['imgurl'] = substr($matches[1][0], 0, 240);
  564. //如果有图,设置level=3
  565. if (!in_array(3, $levelArr)) {
  566. $levelArr[] = 3;
  567. }
  568. $articleData['level'] = json_encode($levelArr);
  569. } else {
  570. if (!in_array(0, $levelArr)) {
  571. $levelArr[] = 0;
  572. }
  573. $articleData['level'] = json_encode($levelArr);
  574. }
  575. }
  576. var_dump($articleData['level'], '----------$articleData[level]----------1');
  577. if ($articleData['keyword'] == '') {
  578. //提取标题+内容中的关键词
  579. $articleData['keyword'] = $data['title'];
  580. // . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
  581. Jieba::init(); // 初始化 jieba-php
  582. Finalseg::init();
  583. $segList = Jieba::cut($articleData['keyword']);
  584. $segList1 = array_slice($segList, 0, 8);
  585. $articleData['keyword'] = implode(',', $segList1);
  586. }
  587. if ($articleData['introduce'] == '') {
  588. //提取内容中的描述
  589. $articleData['introduce'] = substr(str_replace(' ', '', strip_tags($data['content'])), 0, 100);
  590. }
  591. $id = Article::where(['id' => $data['id']])->update($articleData);
  592. $articleDataContent = [
  593. 'content' => $data['content'],
  594. ];
  595. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  596. //处理投票
  597. $id = $data['id'];
  598. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  599. var_dump($suvey_array, 'suvey_array________delete');
  600. //处理投票
  601. if ($is_survey == 1) {
  602. //生成年月日时分秒+8位随机数
  603. $uuid = date('YmdHis') . rand(10000000, 99999999);
  604. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  605. var_dump($suveys_array, '---------------------1');
  606. $suvey_data = [];
  607. if (is_array($suveys_array)) {
  608. foreach ($suveys_array as $key => $value) {
  609. if ($value == '') {
  610. continue;
  611. }
  612. if (is_array($value)) {
  613. $suvey_data[] = [
  614. 'sur_id' => $uuid,
  615. 'art_id' => $id,
  616. 'website_id' => $website_id ?? 2,
  617. 'survey_name' => $survey_name,
  618. 'choice_name' => $value[1],
  619. 'is_other' => 1,
  620. 'other_id' => 0,
  621. 'results' => 0,
  622. ];
  623. } else {
  624. $suvey_data[] = [
  625. 'sur_id' => $uuid,
  626. 'art_id' => $id,
  627. 'website_id' => $website_id ?? 2,
  628. 'survey_name' => $survey_name,
  629. 'choice_name' => $value,
  630. 'is_other' => 0,
  631. 'other_id' => 0,
  632. 'results' => 0,
  633. ];
  634. }
  635. if (empty($suvey_data)) {
  636. throw new \Exception("投票数据为空");
  637. }
  638. }
  639. }
  640. $result = ArticleSurvey::insert($suvey_data);
  641. if (!$result) {
  642. throw new \Exception("投票失败");
  643. }
  644. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  645. if (!$result) {
  646. throw new \Exception("投票失败");
  647. }
  648. } else {
  649. $result = Article::where('id', $id)->update(['survey_id' => '', 'survey_name' => '', 'is_survey' => 0]);
  650. }
  651. Db::commit();
  652. return Result::success([]);
  653. } catch (\Throwable $ex) {
  654. Db::rollBack();
  655. var_dump($ex->getMessage());
  656. return Result::error("更新失败" . $ex->getMessage(), 0);
  657. }
  658. }
  659. /**
  660. * 更新资讯状态
  661. * @param array $data
  662. * @return array
  663. */
  664. public function upArticleStatus(array $data): array
  665. {
  666. $result = Article::where(['id' => $data['id']])->update($data);
  667. if ($result) {
  668. return Result::success();
  669. } else {
  670. return Result::error("更新状态失败", 0);
  671. }
  672. }
  673. /**
  674. * @param array $data
  675. * @return array
  676. */
  677. public function getArticleInfo(array $data): array
  678. {
  679. $where = [
  680. 'article.id' => $data['id'],
  681. // 'article.status' => 1,
  682. ];
  683. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  684. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
  685. $info = $result;
  686. // var_dump($info, 'info');
  687. $info['survey_array'] = $articleSurvey;
  688. if ($result) {
  689. return Result::success($info);
  690. } else {
  691. return Result::error("查询失败", 0);
  692. }
  693. }
  694. /**
  695. * 获取新闻
  696. * @param array $data
  697. * @return array
  698. */
  699. public function getWebsiteArticlett(array $data): array
  700. {
  701. // return Result::success($data['website_id']);
  702. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  703. $category = array_values(array_unique($category->toArray()));
  704. $result = [];
  705. if ($category) {
  706. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  707. $where = [
  708. 'status' => 1,
  709. ];
  710. var_dump($data, 'data-----------------');
  711. // level=7 根据文章key来匹配文章
  712. if (isset($data['level']) && $data['level'] == 7) {
  713. // 根据文章id获取key
  714. // $data['id'] = 50142;
  715. if (isset($data['id']) && !empty($data['id'])) {
  716. $keyword = Article::where('id', $data['id'])->value('keyword');
  717. $keywordArray = explode(',', $keyword);
  718. $whereL7 = [];
  719. foreach ($keywordArray as $k => $v) {
  720. $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
  721. }
  722. // 原始查询
  723. $result = Article::where($whereL7)
  724. ->offset($placeid)
  725. ->limit($data['pageSize'])
  726. ->orderBy('updated_at', 'desc')
  727. ->get()
  728. ->map(function ($article) use ($data) {
  729. $catid = $article->catid;
  730. $pinyin = '';
  731. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  732. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  733. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  734. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  735. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  736. }
  737. } else {
  738. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  739. }
  740. $article->pinyin = $pinyin;
  741. return $article;
  742. });
  743. if (empty($result)) {
  744. return Result::success([]);
  745. }
  746. return Result::success($result);
  747. } else {
  748. return Result::error("参数错误level=7,id不能为空", 0);
  749. }
  750. }
  751. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  752. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  753. var_dump($where, 'where-----------------');
  754. $result = Article::where($where)
  755. ->whereIn("catid", $category)
  756. // ->leftJoin('website_category', 'article.catid', 'website_category.category_id')
  757. // ->where('website_category.website_id', $data['website_id'])
  758. ->where(function ($query) use ($data) {
  759. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  760. ->orWhereNull("ignore_ids");
  761. })
  762. //$data['level'] == 4 || $data['level'] == 5 查询随机
  763. ->when($data['level'] == 5, function ($query) {
  764. $query->inRandomOrder()
  765. //updated_at最近三十天;
  766. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  767. })
  768. ->when($data['level'] == 4, function ($query) {
  769. $query->orderBy("article.updated_at", "desc");
  770. })
  771. ->when(!empty($data['level']), function ($query) use ($data) {
  772. if ($data['level'] != 4 && $data['level'] != 5) {
  773. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  774. ->orderBy("article.updated_at", "desc");
  775. }
  776. })
  777. ->select('article.*')
  778. ->offset($placeid)
  779. ->limit($data['pageSize'])
  780. ->get()
  781. ->map(function ($article) use ($data) {
  782. $catid = $article->catid;
  783. $pinyin = '';
  784. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  785. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  786. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  787. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  788. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  789. }
  790. } else {
  791. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  792. }
  793. $article->pinyin = $pinyin;
  794. return $article;
  795. });
  796. if (empty($result)) {
  797. return Result::error("暂无头条新闻", 0);
  798. }
  799. return Result::success($result);
  800. } else {
  801. return Result::error("本网站下暂无相关栏目", 0);
  802. }
  803. }
  804. /**
  805. * 获取模块新闻
  806. * @param array $data
  807. * @return array
  808. */
  809. public function getWebsiteModelArticles(array $data): array
  810. {
  811. $catid = $data['catid'];
  812. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  813. $category = $category->toArray();
  814. if (!empty($category)) {
  815. $where = [
  816. 'status' => 1,
  817. 'catid' => $catid,
  818. ];
  819. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  820. // 1:文字新闻;2:轮播图;3:图文;
  821. // 级别:0:未分类
  822. // 3:推荐图片
  823. if ($data['level'] == 1) {
  824. $data['level'] = 0;
  825. }
  826. $result = Article::where($where)
  827. ->where(function ($query) use ($data) {
  828. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  829. ->orWhereNull("level")
  830. ->orWhereRaw("level = '[]'");
  831. })
  832. ->where(function ($query) use ($data) {
  833. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  834. ->orWhereNull("ignore_ids");
  835. })
  836. ->orderBy("updated_at", "desc")
  837. ->offset($placeid)
  838. ->limit($data['pagesize'])
  839. ->get();
  840. if (empty($result)) {
  841. return Result::error("此栏目暂无相关新闻", 0);
  842. }
  843. } else {
  844. return Result::error("此网站暂无此栏目", 0);
  845. }
  846. return Result::success($result);
  847. }
  848. /**
  849. *获取新闻列表
  850. * @param array $data
  851. * @return array
  852. */
  853. public function getWebsiteArticleList(array $data): array
  854. {
  855. // return Result::success($data);
  856. $where[] = ['status', '=', 1];
  857. if (isset($data['catid']) && !empty($data['catid'])) {
  858. if (is_array($data['catid'])) {
  859. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  860. array_push($where, ['catid', 'in', $data['catid']]);
  861. } else {
  862. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  863. array_push($where, ['catid', '=', $data['catid']]);
  864. }
  865. if (empty($category)) {
  866. return Result::error("此网站暂无此栏目", 0);
  867. }
  868. }
  869. // return Result::success($where);
  870. $rep = Article::where(function ($query) use ($where) {
  871. foreach ($where as $condition) {
  872. if ($condition[1] === 'in') {
  873. $query->whereIn($condition[0], $condition[2]);
  874. } else {
  875. $query->where($condition[0], $condition[1], $condition[2]);
  876. }
  877. }
  878. })
  879. ->where(function ($query) use ($data) {
  880. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  881. ->orWhereNull("ignore_ids");
  882. })
  883. ->select(
  884. 'article.id',
  885. 'article.title',
  886. 'article.imgurl',
  887. 'article.author',
  888. 'article.updated_at',
  889. 'article.introduce',
  890. 'article.islink',
  891. 'article.linkurl',
  892. 'article.copyfrom',
  893. 'article.catid'
  894. )
  895. ->orderBy("updated_at", "desc")
  896. ->limit($data['pageSize'])
  897. ->offset(($data['page'] - 1) * $data['pageSize'])
  898. ->get()
  899. ->map(function ($article) use ($data) {
  900. $catid = $article->catid ?? 0;
  901. $pinyin = '';
  902. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  903. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  904. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  905. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  906. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin . '/' . $category->aLIas_pinyin : null;
  907. }
  908. $article->pinyin = $pinyin;
  909. return $article;
  910. });
  911. $count = Article::where(function ($query) use ($where) {
  912. foreach ($where as $condition) {
  913. if ($condition[1] === 'in') {
  914. $query->whereIn($condition[0], $condition[2]);
  915. } else {
  916. $query->where($condition[0], $condition[1], $condition[2]);
  917. }
  918. }
  919. })->count();
  920. $data = [
  921. 'rows' => $rep->toArray(),
  922. 'count' => $count,
  923. ];
  924. if (empty($rep)) {
  925. return Result::error("没有信息数据");
  926. }
  927. return Result::success($data);
  928. }
  929. /**
  930. * 前端-获取新闻详情
  931. * @param array $data
  932. * @return array
  933. */
  934. public function selectWebsiteArticleInfo(array $data): array
  935. {
  936. $where = [
  937. 'article.id' => $data['id'],
  938. 'article.status' => 1,
  939. ];
  940. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")
  941. ->where(function ($query) use ($data) {
  942. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  943. ->orWhereNull("ignore_ids");
  944. })
  945. ->first();
  946. if (empty($result)) {
  947. return Result::error("暂无此新闻!", 0);
  948. }
  949. $category = WebsiteCategory::leftJoin('website', 'website.id', '=', 'website_category.website_id')
  950. ->select('website_category.*', 'website.website_name', 'website.suffix')
  951. ->where('website_category.website_id', $data['website_id'])
  952. ->where(['website_category.category_id' => $result['catid']])
  953. ->first();
  954. if (empty($category)) {
  955. return Result::error("查询失败", 0);
  956. }
  957. //手动推荐文章
  958. $commend_ids = $result['commend_id'] ? json_decode($result['commend_id']) : [];
  959. $commendArticle = Article::whereIn('id', $commend_ids)
  960. ->where('status', 1)
  961. ->select('id', 'title', 'catid', 'imgurl', 'hits', 'created_at')
  962. ->orderBy('updated_at', 'desc')
  963. ->limit(5)
  964. ->get();
  965. $result['category_id'] = $category['category_id'];
  966. $result['cat_name'] = $category['alias'];
  967. $result['website_name'] = $category['website_name'] ?? "";
  968. $result['suffix'] = $category['suffix'] ?? "";
  969. $result['commendArticle'] = $commendArticle;
  970. return Result::success($result);
  971. }
  972. /**
  973. * 前端-获取网站调查问卷
  974. * @param array $data
  975. * @return array
  976. */
  977. public function getWebsiteSurvey(array $data): array
  978. {
  979. if (isset($data['website_id']) && !empty($data['website_id'])) {
  980. $website = Website::where('id', $data['website_id'])->first();
  981. if (empty($website)) {
  982. return Result::error("暂无此网站", 0);
  983. }
  984. }
  985. if (isset($data['art_id']) && !empty($data['art_id'])) {
  986. $article = Article::where('id', $data['art_id'])->where('status', 1)->first();
  987. if (empty($article)) {
  988. return Result::error("暂无此文章", 0);
  989. }
  990. // return Result::error($data,0);
  991. $where['art_id'] = $data['art_id'];
  992. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  993. } else {
  994. $survey = Article::where(function ($query) {
  995. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  996. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  997. })
  998. ->where('status', 1)
  999. ->where('is_survey', 1)
  1000. ->select('survey_id')
  1001. ->orderBy('updated_at', 'desc')
  1002. ->first();
  1003. if (empty($survey)) {
  1004. return Result::error("暂无调查问卷", 0);
  1005. }
  1006. $where['sur_id'] = $survey['survey_id'];
  1007. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  1008. }
  1009. // return Result::success($where);
  1010. $result = ArticleSurvey::where($where)
  1011. ->where(function ($query) {
  1012. $query->where('is_other', 0)
  1013. ->orWhere(function ($subQuery) {
  1014. $subQuery->where('is_other', 1)
  1015. ->where('other_id', 0);
  1016. });
  1017. })
  1018. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  1019. ->select('article_survey.*', 'article.survey_type')
  1020. ->get()->all();
  1021. if (empty($result)) {
  1022. return Result::error("此文章暂无调查问卷", 0);
  1023. }
  1024. return Result::success($result);
  1025. }
  1026. /**
  1027. * 前端-添加网站调查问卷选项
  1028. * @param array $data
  1029. * @return array
  1030. */
  1031. public function addWebsiteSurveyOption(array $data): array
  1032. {
  1033. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1034. $website = Website::where('id', $data['website_id'])->first();
  1035. if (empty($website)) {
  1036. return Result::error("暂无此网站", 0);
  1037. }
  1038. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  1039. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->where('is_other', 1)->where('other_id', 0)->first();
  1040. if (empty($survey)) {
  1041. return Result::error("此调查问卷不可添加选项", 0);
  1042. }
  1043. if (isset($data['choice_name']) && !empty($data['choice_name'])) {
  1044. $choice = [
  1045. 'art_id' => $survey['art_id'],
  1046. 'website_id' => $data['website_id'],
  1047. 'survey_name' => $survey['survey_name'],
  1048. 'choice_name' => $data['choice_name'],
  1049. 'sur_id' => $survey['sur_id'],
  1050. 'is_other' => 1,
  1051. 'other_id' => $survey['id'],
  1052. ];
  1053. $result = ArticleSurvey::insertGetId($choice);
  1054. if (empty($result)) {
  1055. return Result::error("添加失败", 0);
  1056. }
  1057. return Result::success($result);
  1058. }
  1059. }
  1060. return Result::error("添加失败", 0);
  1061. }
  1062. return Result::error("添加失败", 0);
  1063. }
  1064. /**
  1065. * 前端-调查问卷投票
  1066. * @param array $data
  1067. * @return array
  1068. */
  1069. public function addWebsiteSurveyVote(array $data): array
  1070. {
  1071. // return Result::success($data);
  1072. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1073. $website = Website::where('id', $data['website_id'])->first();
  1074. if (empty($website)) {
  1075. return Result::error("暂无此网站", 0);
  1076. }
  1077. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  1078. $is_survey = ArticleSurvey::where('sur_id', $data['sur_id'])->first();
  1079. // return Result::success($survey);
  1080. if (empty($is_survey)) {
  1081. return Result::error("此调查问卷不存在", 0);
  1082. }
  1083. // return Result::success($survey);
  1084. // 调查问卷类型
  1085. if (isset($data['choice_id']) && !empty($data['choice_id'])) {
  1086. //多选 若是json型则转化成数组类型
  1087. if (strpos($data['choice_id'], '[') === 0) {
  1088. $data['choice_id'] = json_decode($data['choice_id'], true);
  1089. } else {
  1090. // 单选 也转换成数组
  1091. $data['choice_id'] = [$data['choice_id']];
  1092. }
  1093. $data['choice_id'] = array_map('intval', $data['choice_id']);
  1094. $other = ArticleSurvey::whereIn('id', $data['choice_id'])
  1095. ->where('website_id', $data['website_id'])
  1096. ->where('is_other', 1)
  1097. ->where('other_id', 0)
  1098. ->first();
  1099. if (!empty($other)) {
  1100. return Result::error("请选择已有的选项!", 0);
  1101. }
  1102. $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
  1103. ->where('website_id', $data['website_id'])
  1104. ->where('is_other', 1)
  1105. ->where('other_id', '!=', 0)
  1106. ->first();
  1107. // return Result::success($choice['other']);
  1108. $choice_id = $data['choice_id'];
  1109. if (!empty($choice['other'])) {
  1110. // array_push($data['choice_id'],$choice['other']['other_id']);
  1111. if (!empty($choice_id)) {
  1112. $key = array_search($choice['other']['id'], $choice_id);
  1113. if ($key !== false) {
  1114. unset($choice_id[$key]);
  1115. $choice_id = array_values($choice_id);
  1116. }
  1117. array_push($choice_id, $choice['other']['other_id']);
  1118. } else {
  1119. $choice_id[0] = $choice['other']['other_id'];
  1120. }
  1121. array_push($data['choice_id'], $choice['other']['other_id']);
  1122. }
  1123. // return Result::success($data);
  1124. $choice = ArticleSurvey::whereIn('id', $data['choice_id'])
  1125. ->where('website_id', $data['website_id'])
  1126. ->increment('results', 1);
  1127. if (empty($choice)) {
  1128. return Result::error("请选择已有的选项!", 0);
  1129. }
  1130. $survey['data'] = ArticleSurvey::where('sur_id', $data['sur_id'])
  1131. ->where('website_id', $data['website_id'])
  1132. ->where('other_id', 0)
  1133. ->get();
  1134. $survey['choice'] = $choice_id;
  1135. return Result::success($survey);
  1136. }
  1137. return Result::error("参数必填!");
  1138. }
  1139. return Result::error("此调查问卷不存在", 0);
  1140. }
  1141. return Result::error("参数必填!");
  1142. }
  1143. /**
  1144. * 后端-获取网站调查问卷列表
  1145. * @param array $data
  1146. * @return array
  1147. */
  1148. public function getSurveyList(array $data): array
  1149. {
  1150. $where = [];
  1151. if (isset($data['survey_name']) && !empty($data['survey_name'])) {
  1152. array_push($where, ['survey_name', 'like', '%' . $data['survey_name'] . '%']);
  1153. }
  1154. if (isset($data['survey_type']) && $data['survey_type'] != null) {
  1155. array_push($where, ['survey_type', '=', $data['survey_type']]);
  1156. }
  1157. if (isset($data['is_survey']) && $data['is_survey'] != null) {
  1158. array_push($where, ['is_survey', '=', $data['is_survey']]);
  1159. }
  1160. // return Result::success($where);
  1161. if (!empty($where)) {
  1162. $query = Article::where($where)->where(function ($q) {
  1163. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  1164. });
  1165. } else {
  1166. $query = Article::where(function ($q) {
  1167. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  1168. });
  1169. }
  1170. $count = $query->count();
  1171. $survey = $query->orderByDesc('id')
  1172. ->limit($data['pageSize'])
  1173. ->offset(($data['page'] - 1) * $data['pageSize'])
  1174. ->get();
  1175. if (empty($survey->toArray())) {
  1176. return Result::error("暂无调查问卷!", 0);
  1177. }
  1178. $result = [
  1179. 'rows' => $survey,
  1180. 'count' => $count,
  1181. ];
  1182. return Result::success($result);
  1183. }
  1184. /**
  1185. * 后端-获取网站调查问卷详情
  1186. * @param array $data
  1187. * @return array
  1188. */
  1189. public function getSurveyInfo(array $data): array
  1190. {
  1191. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  1192. $where = ['sur_id' => $data['sur_id']];
  1193. $choose = ArticleSurvey::where($where)->where('is_other', 0)
  1194. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  1195. ->select('article_survey.*', 'article.survey_type')
  1196. ->get()->all();
  1197. if (empty($choose)) {
  1198. return Result::error("此调查问卷不存在", 0);
  1199. }
  1200. $resultsArray = array_column($choose, 'results');
  1201. $total = array_sum($resultsArray);
  1202. $other = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  1203. $others = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->get()->all();
  1204. // $total = 0;
  1205. if (!empty($other)) {
  1206. $total = $total + $other['results'];
  1207. $other['choice_name'] = '(其他)';
  1208. if (!empty($others)) {
  1209. $other['hasChildren'] = true;
  1210. // array_push($other,['hasChildren','=',true]);
  1211. $other['children'] = $others;
  1212. $other_choices = [$other->toArray()];
  1213. $mer_choice = array_merge($choose, $other_choices);
  1214. $value_choice = array_values($mer_choice);
  1215. } else {
  1216. // return Result::error('1111');
  1217. $other_choices = [$other->toArray()];
  1218. $other_choices = array_merge($choose, $other_choices);
  1219. $value_choice = array_values($other_choices);
  1220. // return Result::success($result);
  1221. }
  1222. } else {
  1223. $value_choice = $choose;
  1224. }
  1225. $result = [
  1226. 'choose' => $value_choice,
  1227. 'total' => $total,
  1228. ];
  1229. }
  1230. return Result::success($result);
  1231. }
  1232. /**
  1233. * 前端-搜索新闻列表
  1234. * @param array $data
  1235. * @return array
  1236. */
  1237. public function selectWebsiteArticle(array $data): array
  1238. {
  1239. $where = [];
  1240. // 初始化查询构造器
  1241. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  1242. $query = Article::where('status', 1)
  1243. ->whereIn('catid', $category)
  1244. ->where(function ($query) use ($data) {
  1245. $query->where(function ($subQuery) use ($data) {
  1246. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  1247. })->orWhereNull("ignore_ids");
  1248. });
  1249. // return Result::success($all_articles);
  1250. // 检查是否存在 cityid 参数
  1251. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1252. $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
  1253. }
  1254. // 检查是否存在 department_id 参数
  1255. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1256. $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
  1257. }
  1258. // 检查是否存在 keyword 参数
  1259. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1260. $query->where('title', 'like', '%' . $data['keyword'] . '%');
  1261. }
  1262. // 计算总数
  1263. $count = $query->count();
  1264. // 分页查询
  1265. $articles = $query
  1266. ->select(
  1267. 'article.id',
  1268. 'article.title',
  1269. 'article.imgurl',
  1270. 'article.author',
  1271. 'article.updated_at',
  1272. 'article.introduce',
  1273. 'article.islink',
  1274. 'article.linkurl',
  1275. 'article.copyfrom',
  1276. 'article.catid',
  1277. 'article.department_arr_id',
  1278. 'article.city_arr_id',
  1279. )
  1280. ->orderBy("updated_at", "desc")
  1281. ->offset(($data['page'] - 1) * $data['pageSize'])
  1282. ->limit($data['pageSize'])
  1283. ->get()
  1284. ->map(function ($article) use ($data) {
  1285. $catid = $article->catid ?? 0;
  1286. $pinyin = '';
  1287. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  1288. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1289. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  1290. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  1291. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin . '/' . $category->aLIas_pinyin : null;
  1292. }
  1293. $article->pinyin = $pinyin;
  1294. return $article;
  1295. });
  1296. if (empty($articles)) {
  1297. return Result::error("没有符合条件的资讯数据");
  1298. }
  1299. $data = [
  1300. 'rows' => $articles,
  1301. 'count' => $count,
  1302. ];
  1303. return Result::success($data);
  1304. }
  1305. /**
  1306. * 模块新闻加强版
  1307. * @param array $data
  1308. * @return array
  1309. */
  1310. public function getWebsiteCatidArticle(array $data): array
  1311. {
  1312. // return Result::success($data);
  1313. $where = [
  1314. // 'category.status' => 1,
  1315. 'website_category.category_id' => $data['catid'],
  1316. 'website_category.website_id' => $data['website_id'],
  1317. // 'article.status' => 1,
  1318. ];
  1319. // $category = WebsiteCategory::where($where);
  1320. if (isset($data['child_catnum']) && !empty($data['child_catnum'])) {
  1321. $child_catnum = $data['child_catnum'] ?? 1;
  1322. $category['child'] = WebsiteCategory::where('pid', $data['catid'])->where('website_id', $data['website_id'])->select('category_id', 'alias')->limit($child_catnum)->get()->toArray();
  1323. $childCategoryIds = array_column($category['child'], 'category_id');
  1324. if (empty($childCategoryIds)) {
  1325. return Result::error("暂无子栏目", 0);
  1326. }
  1327. $imgArticles = [];
  1328. $textArticles = [];
  1329. // return Result::success($childCategoryIds);
  1330. if (isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum'] != 0) {
  1331. // 初始化子分类图片新闻和文字新闻数组
  1332. // 查询所有子级栏目的图文新闻
  1333. $imgArticles = Article::where('catid', $childCategoryIds[0])
  1334. ->where(function ($query) use ($data) {
  1335. $query->where(function ($subQuery) use ($data) {
  1336. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  1337. })->orWhereNull("ignore_ids");
  1338. })
  1339. ->where('status', 1)
  1340. ->where('imgurl', '!=', '')
  1341. ->select('*')
  1342. ->orderBy('updated_at', 'desc')
  1343. ->limit($data['child_imgnum'])
  1344. ->get();
  1345. }
  1346. if (isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum'] != 0) {
  1347. // 查询所有子级栏目的文字新闻
  1348. $textArticles = Article::where('catid', $childCategoryIds[0])
  1349. ->where('status', 1)
  1350. ->where(function ($query) use ($data) {
  1351. $query->where(function ($subQuery) use ($data) {
  1352. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  1353. })->orWhereNull("ignore_ids");
  1354. })
  1355. // ->where(function ($query) {
  1356. // $query->whereNull('imgurl')
  1357. // ->orWhere('imgurl', '');
  1358. // })
  1359. ->select('*')
  1360. ->orderBy('updated_at', 'desc')
  1361. ->limit($data['child_textnum'])
  1362. ->get();
  1363. }
  1364. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  1365. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  1366. $child['img'] = $imgArticles ? $imgArticles->where('catid', $child['category_id']) : [];
  1367. $child['text'] = $textArticles ? $textArticles->where('catid', $child['category_id']) : [];
  1368. return $child;
  1369. }, $category['child']);
  1370. }
  1371. // }
  1372. if (isset($data['img_num']) && !empty($data['img_num'])) {
  1373. $category['img'] = WebsiteCategory::where($where)
  1374. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1375. ->where('article.status', 1)
  1376. ->where('article.imgurl', '!=', '')
  1377. ->select('article.*', 'website_category.category_id', 'website_category.alias')
  1378. ->orderBy('article.updated_at', 'desc')
  1379. ->limit($data['img_num'])
  1380. ->get();
  1381. }
  1382. if (isset($data['text_num']) && !empty($data['text_num'])) {
  1383. $category['text'] = WebsiteCategory::where($where)
  1384. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1385. ->where('article.status', 1)
  1386. // ->where(function ($query) {
  1387. // $query->whereNull('article.imgurl')
  1388. // ->orWhere('article.imgurl', '');
  1389. // })
  1390. ->select('article.*', 'website_category.category_id', 'website_category.alias')
  1391. ->orderBy('article.updated_at', 'desc')
  1392. ->limit($data['text_num'])
  1393. ->get();
  1394. }
  1395. // $category = $category->get();
  1396. if (empty($category)) {
  1397. return Result::error("查询失败", 0);
  1398. }
  1399. return Result::success($category);
  1400. }
  1401. /**
  1402. * 模块新闻加强plus版
  1403. * @param array $data
  1404. * @return array
  1405. */
  1406. public function getWebsiteAllArticle(array $data): array
  1407. {
  1408. // 修正传入的字符串,将单引号替换为双引号
  1409. $input['id'] = $data['id'];
  1410. $input['website_id'] = $data['website_id'];
  1411. // 将 JSON 字符串转换为 PHP 数组
  1412. $data = json_decode($input['id'], true);
  1413. // 使用 array_map 处理每个元素
  1414. $result = array_map(function ($item) use ($input) {
  1415. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1416. $website = [
  1417. 'website_id' => $input['website_id'],
  1418. ];
  1419. // 查询栏目名称
  1420. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id', 'aLIas_pinyin']);
  1421. if (empty($category)) {
  1422. $imgArticles = [];
  1423. $textArticles = [];
  1424. // return Result::error("暂无此栏目",0);
  1425. } else {
  1426. $parent_alias = $category->alias ?? '';
  1427. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1428. // 查询图片新闻
  1429. $imgArticles = Article::where('catid', $parentCatId)
  1430. ->where('status', 1)
  1431. ->where(function ($query) use ($website) {
  1432. $query->where(function ($subQuery) use ($website) {
  1433. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1434. })->orWhereNull("ignore_ids");
  1435. })
  1436. ->where('imgurl', '!=', '')
  1437. ->select(
  1438. 'article.id',
  1439. 'article.title',
  1440. 'article.imgurl',
  1441. 'article.author',
  1442. 'article.updated_at',
  1443. 'article.introduce',
  1444. 'article.islink',
  1445. 'article.linkurl',
  1446. 'article.copyfrom',
  1447. DB::raw("'$parent_pinyin' as pinyin") // 添加 pinyin 字段
  1448. )
  1449. ->orderBy('updated_at', 'desc')
  1450. ->limit($parentImgNum)
  1451. ->get()->all();
  1452. // 查询文字新闻
  1453. $textArticles = Article::where('catid', $parentCatId)
  1454. ->where('status', 1)
  1455. ->where(function ($query) use ($website) {
  1456. $query->where(function ($subQuery) use ($website) {
  1457. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1458. })->orWhereNull("ignore_ids");
  1459. })
  1460. ->select(
  1461. 'article.id',
  1462. 'article.title',
  1463. 'article.author',
  1464. 'article.updated_at',
  1465. 'article.introduce',
  1466. 'article.islink',
  1467. 'article.linkurl',
  1468. 'article.copyfrom',
  1469. DB::raw("'$parent_pinyin' as pinyin")
  1470. )
  1471. ->orderBy('updated_at', 'desc')
  1472. ->limit($parentTextNum)
  1473. ->get()->all();
  1474. }
  1475. $resultItem = [
  1476. 'alias' => $parent_alias,
  1477. 'category_id' => $parentCatId,
  1478. 'pinyin' => $parent_pinyin,
  1479. 'imgnum' => $imgArticles,
  1480. 'textnum' => $textArticles,
  1481. ];
  1482. if (!empty($item['child']) && $item['child'] != "") {
  1483. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin . '/' : '';
  1484. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1485. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1486. ->get()->all();
  1487. if ($childCategory) {
  1488. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1489. // 查询子栏目名称
  1490. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1491. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1492. ->first();
  1493. $child_pinyin = $childCategoryInfo->aLIas_pinyin ? $childCategoryInfo->aLIas_pinyin : null;
  1494. if (empty($childCategoryInfo)) {
  1495. $childImgArticles = [];
  1496. $childTextArticles = [];
  1497. } else {
  1498. // 查询子栏目图片新闻
  1499. $childImgArticles = Article::where('catid', $childCatId)
  1500. ->where('status', 1)
  1501. ->where(function ($query) use ($website) {
  1502. $query->where(function ($subQuery) use ($website) {
  1503. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1504. })->orWhereNull("ignore_ids");
  1505. })
  1506. ->select(
  1507. 'article.id',
  1508. 'article.title',
  1509. 'article.imgurl',
  1510. 'article.author',
  1511. 'article.updated_at',
  1512. 'article.introduce',
  1513. 'article.islink',
  1514. 'article.linkurl',
  1515. 'article.copyfrom',
  1516. DB::raw("'$child_pinyin' as pinyin")
  1517. )
  1518. ->where('imgurl', '!=', '')
  1519. ->orderBy('updated_at', 'desc')
  1520. ->limit($childImgNum)
  1521. ->get()->all();
  1522. // 查询子栏目文字新闻
  1523. $childTextArticles = Article::where('catid', $childCatId)
  1524. ->where('status', 1)
  1525. ->where(function ($query) use ($website) {
  1526. $query->where(function ($subQuery) use ($website) {
  1527. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1528. })->orWhereNull("ignore_ids");
  1529. })
  1530. ->select(
  1531. 'article.id',
  1532. 'article.title',
  1533. 'article.author',
  1534. 'article.updated_at',
  1535. 'article.introduce',
  1536. 'article.islink',
  1537. 'article.linkurl',
  1538. 'article.copyfrom',
  1539. DB::raw("'$child_pinyin' as pinyin")
  1540. )
  1541. ->orderBy('updated_at', 'desc')
  1542. ->limit($childTextNum)
  1543. ->get()->all();
  1544. }
  1545. $resultItem['child'] = [
  1546. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1547. 'category_id' => $childCatId,
  1548. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1549. 'all_childcat' => $childCategory,
  1550. 'imgnum' => $childImgArticles,
  1551. 'textnum' => $childTextArticles,
  1552. ];
  1553. // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
  1554. }
  1555. }
  1556. return $resultItem;
  1557. }, $data);
  1558. return Result::success($result);
  1559. // return Result::success($data);
  1560. }
  1561. /**
  1562. * 乡村网-获取特殊新闻模块
  1563. * @param array $data
  1564. * @return array
  1565. */
  1566. public function getWebsiteArticles(array $data): array
  1567. {
  1568. $input['id'] = $data['id'];
  1569. $input['website_id'] = $data['website_id'];
  1570. $data = json_decode($input['id'], true);
  1571. // 使用 array_map 处理每个元素
  1572. $result = array_map(function ($item) use ($input) {
  1573. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1574. $website = [
  1575. 'website_id' => $input['website_id']
  1576. ];
  1577. // 查询栏目名称
  1578. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id', 'aLIas_pinyin']);
  1579. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1580. if (empty($category)) {
  1581. $imgArticles = [];
  1582. $textArticles = [];
  1583. // return Result::error("暂无此栏目",0);
  1584. } else {
  1585. $parent_alias = $category->aLIas_pinyin ? $category->aLIas_pinyin . '/' : null;
  1586. // return Result::success($website);
  1587. // 查询图片新闻
  1588. // 合并查询条件
  1589. $baseQuery = Article::where(function ($query) use ($website) {
  1590. $query->where(function ($subQuery) use ($website) {
  1591. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1592. })->orWhereNull("ignore_ids");
  1593. })
  1594. ->whereRaw("JSON_CONTAINS(category_arr_id, '" . intval($parentCatId) . "')")
  1595. ->leftJoin('website_category', 'website_category.category_id', 'article.catid')
  1596. ->where('website_category.website_id', $website['website_id'])
  1597. ->where('article.status', 1);
  1598. // 查询文字新闻
  1599. $textArticles = clone $baseQuery;
  1600. $textArticles = $textArticles
  1601. ->select(
  1602. 'article.id',
  1603. 'article.title',
  1604. // 'article.imgurl',
  1605. 'article.author',
  1606. 'article.updated_at',
  1607. 'article.introduce',
  1608. 'article.islink',
  1609. 'article.linkurl',
  1610. 'article.copyfrom',
  1611. 'website_category.category_id',
  1612. 'website_category.alias',
  1613. 'website_category.aLIas_pinyin'
  1614. )
  1615. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1616. ->orderBy('article.updated_at', 'desc')
  1617. ->limit($parentTextNum)
  1618. ->get()
  1619. ->all();
  1620. // 查询图片新闻
  1621. $imgArticles = clone $baseQuery;
  1622. $imgArticles = $imgArticles
  1623. ->select(
  1624. 'article.id',
  1625. 'article.title',
  1626. 'article.imgurl',
  1627. 'article.author',
  1628. 'article.updated_at',
  1629. 'article.introduce',
  1630. 'article.islink',
  1631. 'article.linkurl',
  1632. 'article.copyfrom',
  1633. 'website_category.category_id',
  1634. 'website_category.alias',
  1635. 'website_category.aLIas_pinyin'
  1636. )
  1637. ->selectRaw("CONCAT(?, aLIas_pinyin ) as aLIas_pinyin", [$parent_alias])
  1638. ->orderBy('article.updated_at', 'desc')
  1639. ->where('imgurl', '!=', '')
  1640. ->limit($parentImgNum)
  1641. ->get()
  1642. ->all();
  1643. }
  1644. $resultItem = [
  1645. 'alias' => $category ? $category->alias : null,
  1646. 'category_id' => $parentCatId,
  1647. 'pinyin' => $pinyin ? $pinyin : null,
  1648. 'imgnum' => $imgArticles ?? [],
  1649. 'textnum' => $textArticles ?? [],
  1650. ];
  1651. return $resultItem;
  1652. }, $data);
  1653. return Result::success($result);
  1654. }
  1655. /**
  1656. *获取头条类新闻模块-合集
  1657. * @param array $data
  1658. * @return array
  1659. */
  1660. public function getWebsiteAllArticlett(array $data): array
  1661. {
  1662. $input['id'] = $data['id'];
  1663. $input['website_id'] = $data['website_id'];
  1664. $data = json_decode($input['id'], true);
  1665. // 使用 array_map 处理每个元素
  1666. $result = array_map(function ($item) use ($input) {
  1667. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1668. $website = [
  1669. 'website_id' => $input['website_id']
  1670. ];
  1671. $category = WebsiteCategory::where('website_id', $input['website_id'])->pluck('category_id');
  1672. $category = array_values(array_unique($category->toArray()));
  1673. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  1674. $where = [
  1675. 'status' => 1,
  1676. ];
  1677. return $category;
  1678. }, $data); // 添加第二个参数 $data,确保 array_map 函数有两个参数
  1679. return Result::success($result);
  1680. }
  1681. /**
  1682. * 验证导航名称是否重复
  1683. * @return void
  1684. */
  1685. public function checkCategoryName(array $data): array
  1686. {
  1687. $result = Category::when($data, function ($query) use ($data) {
  1688. if (isset($data['name']) && $data['name']) {
  1689. $query->where("name", $data['name']);
  1690. }
  1691. if (isset($data['id']) && $data['id']) {
  1692. $query->where("id", "!=", $data['id']);
  1693. }
  1694. })->first();
  1695. if ($result) {
  1696. return Result::error("已存在");
  1697. } else {
  1698. return Result::success();
  1699. }
  1700. }
  1701. //20250226 产品列表
  1702. public function getGoodList(array $data): array
  1703. {
  1704. $type_id = isset($data['type_id']) ? $data['type_id'] : '';
  1705. unset($data['type_id']);
  1706. $user_id = isset($data['user_id']) ? $data['user_id'] : '';
  1707. $where = [];
  1708. if ($type_id != '10000') {
  1709. $where = [
  1710. 'good.user_id' => $user_id,
  1711. ];
  1712. }
  1713. //类型
  1714. if (isset($data['type_id']) && $data['type_id']) {
  1715. $where = [
  1716. 'type_id' => $data['type_id'],
  1717. ];
  1718. }
  1719. //名称
  1720. if (isset($data['name']) && $data['name']) {
  1721. $where = [
  1722. 'good.name' => $data['name'],
  1723. ];
  1724. }
  1725. $where1 = [];
  1726. //website_id
  1727. // if (isset($data['website_id']) && $data['website_id']) {
  1728. // $where1 = [
  1729. // 'good.website_id', 'like', '%' . $data['website_id'] . '%',
  1730. // ];
  1731. // }
  1732. // website_name
  1733. if (isset($data['website_name']) && $data['website_name']) {
  1734. $where1[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  1735. }
  1736. // catid
  1737. if (isset($data['category_name']) && $data['category_name']) {
  1738. $where1[] = ['category.name', 'like', '%' . $data['category_name'] . '%'];
  1739. }
  1740. // $result = Good::where($where)
  1741. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  1742. $result = Good::where($where)
  1743. ->when(!empty($where1), function ($query) use ($where1) {
  1744. return $query->where($where1);
  1745. })
  1746. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  1747. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  1748. ->leftJoin('category', 'good.catid', '=', 'category.id')
  1749. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  1750. ->orderBy("id", "desc")
  1751. ->limit($data['page_size'])
  1752. ->offset(($data['page'] - 1) * $data['page_size'])
  1753. ->get();
  1754. $count = Good::where($where)
  1755. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  1756. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  1757. ->leftJoin('category', 'good.catid', '=', 'category.id')
  1758. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  1759. ->orderBy("updated_at", "desc")->count();
  1760. $data = [
  1761. 'rows' => $result->toArray(),
  1762. 'count' => $count,
  1763. ];
  1764. if (empty($result)) {
  1765. return Result::error("此栏目暂无相关产品", 0);
  1766. }
  1767. return Result::success($data);
  1768. }
  1769. public function getGoodInfo(array $data): array
  1770. {
  1771. $result = Good::where('id', $data['id'])->first();
  1772. if (empty($result)) {
  1773. return Result::error("此产品不存在", 0);
  1774. }
  1775. return Result::success($result);
  1776. }
  1777. public function addGood(array $data): array
  1778. {
  1779. // unset($data['city_arr_id']);
  1780. // unset($data['cat_arr_id']);
  1781. $data['city_id'] = end($data['city_arr_id']);
  1782. $data['catid'] = end($data['cat_arr_id']);
  1783. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  1784. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  1785. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  1786. unset($data['imgUrl']);
  1787. $result = Good::insert($data);
  1788. if (empty($result)) {
  1789. return Result::error("添加失败", 0);
  1790. }
  1791. return Result::success($result);
  1792. }
  1793. public function updateGood(array $data): array
  1794. {
  1795. $data['city_id'] = end($data['city_arr_id']);
  1796. $data['catid'] = end($data['cat_arr_id']);
  1797. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  1798. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  1799. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  1800. //设置东八区
  1801. date_default_timezone_set('Asia/Shanghai');
  1802. $data['updated_at'] = date('Y-m-d H:i:s');
  1803. $result = Good::where('id', $data['id'])->update($data);
  1804. if (empty($result)) {
  1805. return Result::error("更新失败", 0);
  1806. }
  1807. return Result::success($result);
  1808. }
  1809. public function delGood(array $data): array
  1810. {
  1811. $result = Good::where('id', $data['id'])->delete();
  1812. if (empty($result)) {
  1813. return Result::error("删除失败", 0);
  1814. }
  1815. return Result::success($result);
  1816. }
  1817. //20250226 产品列表
  1818. //20250306 求职信息
  1819. public function getJobHuntingList(array $data): array
  1820. {
  1821. $where = [];
  1822. if (isset($data['username']) && !empty($data['username'])) {
  1823. $where[] = ['user.user_name', 'like', '%' . $data['username'] . '%'];
  1824. }
  1825. $type_id = isset($data['type_id']) ? $data['type_id'] : '';
  1826. $user_id = isset($data['user_id']) ? $data['user_id'] : '';
  1827. unset($data['type_id']);
  1828. if ($type_id != '10000') {
  1829. $where[] = ['job_hunting.user_id', '=', $user_id];
  1830. }
  1831. $result = JobHunting::where($where)
  1832. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  1833. ->leftJoin('website', 'website.id', '=', 'job_hunting.website_id')
  1834. ->select('job_hunting.*', 'user.nickname as nickname', 'user.user_name as username', 'website.website_name as website_name')
  1835. ->orderBy("id", "desc")
  1836. ->limit($data['page_size'])
  1837. ->offset(($data['page'] - 1) * $data['page_size'])
  1838. ->get();
  1839. if (empty($result)) {
  1840. return Result::error("查询失败", 0);
  1841. }
  1842. $count = JobHunting::where($where)
  1843. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  1844. ->leftJoin('website', 'website.id', '=', 'job_hunting.website_id')
  1845. ->count();
  1846. $data = [
  1847. 'rows' => $result->toArray(),
  1848. 'count' => $count,
  1849. ];
  1850. return Result::success($data);
  1851. }
  1852. public function addJobHunting(array $data): array
  1853. {
  1854. date_default_timezone_set('Asia/Shanghai');
  1855. unset($data['company_name']);
  1856. unset($data['job_industry']);
  1857. unset($data['job_name']);
  1858. unset($data['department']);
  1859. unset($data['job_timeList']);
  1860. unset($data['job_content']);
  1861. $data['created_at'] = date('Y-m-d H:i:s');
  1862. $data['updated_at'] = date('Y-m-d H:i:s');
  1863. var_dump($data, '-----------------test---------');
  1864. $result = JobHunting::create($data);
  1865. if (empty($result)) {
  1866. return Result::error("添加失败", 0);
  1867. }
  1868. return Result::success($result);
  1869. }
  1870. public function delJobHunting(array $data): array
  1871. {
  1872. $result = JobHunting::where('id', $data['id'])->delete();
  1873. if (empty($result)) {
  1874. return Result::error("删除失败", 0);
  1875. }
  1876. return Result::success($result);
  1877. }
  1878. public function updateJobHunting(array $data): array
  1879. {
  1880. //设置东八区
  1881. date_default_timezone_set('Asia/Shanghai');
  1882. unset($data['company_name']);
  1883. unset($data['job_industry']);
  1884. unset($data['job_name']);
  1885. unset($data['department']);
  1886. unset($data['job_timeList']);
  1887. unset($data['job_content']);
  1888. $data['created_at'] = date('Y-m-d H:i:s');
  1889. $data['updated_at'] = date('Y-m-d H:i:s');
  1890. $result = JobHunting::where('id', $data['id'])->update($data);
  1891. if (empty($result)) {
  1892. return Result::error("更新失败", 0);
  1893. }
  1894. return Result::success($result);
  1895. }
  1896. public function getJobHuntingInfo(array $data): array
  1897. {
  1898. $result = JobHunting::where('id', $data['id'])->first();
  1899. if (empty($result)) {
  1900. return Result::error("查询失败", 0);
  1901. }
  1902. return Result::success($result);
  1903. }
  1904. public function getJobHuntingData(array $data): array
  1905. {
  1906. $jobEnum = JobEnum::get();
  1907. $jobIndustry = JobIndustry::get();
  1908. $jobNature = JobNature::get();
  1909. $jobPosition = JobPosition::get();
  1910. $data = [
  1911. 'jobEnum' => $jobEnum,
  1912. 'jobIndustry' => $jobIndustry,
  1913. 'jobNature' => $jobNature,
  1914. 'jobPosition' => $jobPosition,
  1915. ];
  1916. return Result::success($data);
  1917. }
  1918. public function delJobHuntingInfo(array $data): array
  1919. {
  1920. $result = JobHunting::where('id', $data['id'])->delete();
  1921. return Result::success();
  1922. }
  1923. //20250324 通知,公告,消息
  1924. public function delNotice(array $data): array
  1925. {
  1926. return Result::success();
  1927. }
  1928. public function getNoticeInfo(array $data): array
  1929. {
  1930. $result = Notice::where('id', $data['id'])->first();
  1931. if (empty($result)) {
  1932. return Result::error("查询失败", 0);
  1933. }
  1934. return Result::success($result);
  1935. }
  1936. public function addNotice(array $data): array
  1937. {
  1938. date_default_timezone_set('Asia/Shanghai');
  1939. $data['created_at'] = date('Y-m-d H:i:s');
  1940. $data['updated_at'] = date('Y-m-d H:i:s');
  1941. $user_id = UserInfo::
  1942. //city_id不是null
  1943. whereNotNull('city_id')
  1944. ->whereNotNull('department_id')
  1945. ->where(function ($query) use ($data) {
  1946. $query->where(function ($subQuery) use ($data) {
  1947. $subQuery->whereRaw("JSON_VALID(city_arr_id) AND JSON_CONTAINS(city_arr_id, '" . intval($data['city_id']) . "') = 1");
  1948. });
  1949. })
  1950. ->where(function ($query) use ($data) {
  1951. $query->where(function ($subQuery) use ($data) {
  1952. $subQuery->whereRaw("JSON_VALID(department_arr_id) AND JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "') = 1");
  1953. });
  1954. })
  1955. ->pluck('user_id')->toArray();
  1956. $user_id = array_unique($user_id);
  1957. $chat_ids = $user_id;
  1958. $user_id = json_encode($user_id);
  1959. $data['re_user_ids'] = $user_id;
  1960. $result = Notice::insertGetId($data);
  1961. if ($result && $data['is_group'] == 1) {
  1962. //chat_group
  1963. $group_id = PublicData::uuid();
  1964. $groupData = [
  1965. 'id' => $group_id,
  1966. 'creator_id' => $data['user_id'],
  1967. 'group_name' => $data['group_name'] ?? '',
  1968. 'profile' => $data['profile'] ?? 0,
  1969. ];
  1970. $groupResult = ChatGroups::insertGetId($groupData);
  1971. var_dump($groupResult, '-----------------groupid-------');
  1972. $groupMemberData = [
  1973. 'id' => PublicData::uuid(),
  1974. 'user_id' => $data['user_id'],
  1975. 'group_id' => $group_id,
  1976. 'leader' => 2,
  1977. ];
  1978. $groupMemberResult = ChatGroupsMember::insertGetId($groupMemberData);
  1979. //$chat_ids 去除掉$data['user_id']
  1980. $chat_ids = array_diff($chat_ids, [$data['user_id']]);
  1981. //插入群成员表
  1982. $groupMemberDataUser = [];
  1983. foreach ($chat_ids as $key => $value) {
  1984. $groupMemberDataUser[] = [
  1985. 'id' => PublicData::uuid(),
  1986. 'user_id' => $value,
  1987. 'group_id' => $group_id,
  1988. 'leader' => 1,
  1989. ];
  1990. }
  1991. ChatGroupsMember::insert($groupMemberDataUser);
  1992. //更新result的 group_id
  1993. $data['group_id'] = $group_id;
  1994. Notice::where(['id' => $result])->update($data);
  1995. }
  1996. if (empty($result)) {
  1997. return Result::error("添加失败", 0);
  1998. }
  1999. return Result::success($result);
  2000. }
  2001. public function updateNotice(array $data): array
  2002. {
  2003. date_default_timezone_set('Asia/Shanghai');
  2004. //根据city_id 和department_id 查询出对应的user_id,放到 re_user_ids
  2005. $user_id = UserInfo::
  2006. //city_id不是null
  2007. whereNotNull('city_id')
  2008. ->whereNotNull('department_id')
  2009. ->where(function ($query) use ($data) {
  2010. $query->where(function ($subQuery) use ($data) {
  2011. $subQuery->whereRaw("JSON_VALID(city_arr_id) AND JSON_CONTAINS(city_arr_id, '" . intval($data['city_id']) . "') = 1");
  2012. });
  2013. })
  2014. ->where(function ($query) use ($data) {
  2015. $query->where(function ($subQuery) use ($data) {
  2016. $subQuery->whereRaw("JSON_VALID(department_arr_id) AND JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "') = 1");
  2017. });
  2018. })
  2019. ->pluck('user_id')->toArray();
  2020. $user_id = array_unique($user_id);
  2021. $user_id = json_encode($user_id);
  2022. $data['re_user_ids'] = $user_id;
  2023. $data['updated_at'] = date('Y-m-d H:i:s');
  2024. $result = Notice::where('id', $data['id'])->update($data);
  2025. return Result::success($result);
  2026. }
  2027. public function getNoticeList(array $data): array
  2028. {
  2029. $where = [];
  2030. //title
  2031. if (isset($data['title']) && !empty($data['title'])) {
  2032. $where[] = ['title', 'like', '%' . $data['title'] . '%'];
  2033. }
  2034. //level
  2035. if (isset($data['level']) && !empty($data['level'])) {
  2036. $where[] = ['level', '=', $data['level']];
  2037. }
  2038. var_dump($data['type_id'], '-----------------test---------');
  2039. if ($data['type_id'] == 10000) {
  2040. $result = Notice::where('status', 1)
  2041. ->where($where)
  2042. ->orderBy('updated_at', 'desc')
  2043. ->limit($data['page_size'])
  2044. ->offset(($data['page'] - 1) * $data['page_size'])
  2045. ->get();
  2046. } else {
  2047. $result = Notice::where('status', 1)
  2048. ->where($where)
  2049. ->where('user_id', $data['user_id'])
  2050. ->orWhere(function ($query) use ($data) {
  2051. $query->where('re_user_ids', 'like', '%' . $data['user_id'] . '%');
  2052. })
  2053. ->orderBy('updated_at', 'desc')
  2054. ->limit($data['page_size'])
  2055. ->offset(($data['page'] - 1) * $data['page_size'])
  2056. ->get();
  2057. }
  2058. if (empty($result)) {
  2059. return Result::error("查询失败", 0);
  2060. }
  2061. $count = Notice::where('status', 1)->count();
  2062. $data = [
  2063. 'rows' => $result,
  2064. 'count' => $count,
  2065. ];
  2066. return Result::success($data);
  2067. }
  2068. public function getNoticeDetail(array $data): array
  2069. {
  2070. $result = Notice::where('id', $data['id'])->first();
  2071. if (empty($result)) {
  2072. return Result::error("查询失败", 0);
  2073. }
  2074. return Result::success($result);
  2075. }
  2076. public function deleteNotice(array $data): array
  2077. {
  2078. $notice = Notice::where('id', $data['id'])->first();
  2079. if (empty($notice)) {
  2080. return Result::error("删除失败", 0);
  2081. }
  2082. $chat_group_id = $notice->group_id;
  2083. if ($chat_group_id) {
  2084. //删除群聊
  2085. ChatGroups::where('id', $chat_group_id)->delete();
  2086. //删除群成员
  2087. ChatGroupsMember::where('group_id', $chat_group_id)->delete();
  2088. //删除群聊记录
  2089. ChatRecords::where('receiver_id', $chat_group_id)->delete();
  2090. }
  2091. $result = Notice::where('id', $data['id'])->delete();
  2092. return Result::success($result);
  2093. }
  2094. public function getMSG(array $data): array
  2095. {
  2096. $type_id = isset($data['type_id']) ? $data['type_id'] : 1;
  2097. // '1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)
  2098. $user_id = isset($data['user_id']) ? $data['user_id'] : 0; //用户id
  2099. $result = [];
  2100. if ($type_id == 1) {
  2101. //最近的5篇已审的文章
  2102. $apply_articale = Article::where('status', 1)
  2103. ->where('admin_user_id', $user_id)
  2104. ->orderBy('updated_at', 'desc')
  2105. ->limit(5)->get();
  2106. //获取5条单聊未读聊天消息
  2107. $chat = ChatRecords::where('is_read', 0)
  2108. ->where('user_id', $user_id)
  2109. ->where('talk_type', 1)
  2110. ->orderBy('created_at', 'desc')
  2111. ->limit(5)->get();
  2112. //获取5条未读群聊信息
  2113. $chat_group = ChatRecords::where('is_read', 0)
  2114. ->where('user_id', $user_id)
  2115. ->where('talk_type', 2)
  2116. ->orderBy('created_at', 'desc')
  2117. ->limit(5)->get();
  2118. $count = count($chat) + count($chat_group) + count($apply_articale);
  2119. $result = [
  2120. 'apply_articale' => $apply_articale,
  2121. 'chat' => $chat,
  2122. 'chat_group' => $chat_group,
  2123. 'count' => $count,
  2124. ];
  2125. } elseif ($type_id == 2) {
  2126. //最近的5篇已审的文章
  2127. $apply_articale = Article::where('status', 1)
  2128. ->where('admin_user_id', $user_id)
  2129. ->limit(5)->get();
  2130. //获取5条单聊未读聊天消息
  2131. $chat = ChatRecords::where('is_read', 0)
  2132. ->where('user_id', $user_id)
  2133. ->where('talk_type', 1)
  2134. ->orderBy('created_at', 'desc')
  2135. ->limit(5)->get();
  2136. //获取5条未读群聊信息
  2137. $chat_group = ChatRecords::where('is_read', 0)
  2138. ->where('user_id', $user_id)
  2139. ->where('talk_type', 2)
  2140. ->orderBy('created_at', 'desc')
  2141. ->limit(5)->get();
  2142. $count = count($chat) + count($chat_group) + count($apply_articale);
  2143. $result = [
  2144. 'apply_articale' => $apply_articale,
  2145. 'chat' => $chat,
  2146. 'chat_group' => $chat_group,
  2147. 'count' => $count,
  2148. ];
  2149. } elseif ($type_id == 3) {
  2150. //最近的5篇已审的文章
  2151. $apply_articale = Article::where('status', 1)
  2152. ->where('admin_user_id', $user_id)
  2153. ->limit(5)->get();
  2154. //获取5条单聊未读聊天消息
  2155. $chat = ChatRecords::where('is_read', 0)
  2156. ->where('user_id', $user_id)
  2157. ->where('talk_type', 1)
  2158. ->orderBy('created_at', 'desc')
  2159. ->limit(5)->get();
  2160. //获取5条未读群聊信息
  2161. $chat_group = ChatRecords::where('is_read', 0)
  2162. ->where('user_id', $user_id)
  2163. ->where('talk_type', 2)
  2164. ->orderBy('created_at', 'desc')
  2165. ->limit(5)->get();
  2166. $count = count($chat) + count($chat_group) + count($apply_articale);
  2167. $result = [
  2168. 'apply_articale' => $apply_articale,
  2169. 'chat' => $chat,
  2170. 'chat_group' => $chat_group,
  2171. 'count' => $count,
  2172. ];
  2173. } elseif ($type_id == 4) {
  2174. //最近的5篇已审的文章
  2175. $apply_articale = Article::where('status', 1)
  2176. ->where('admin_user_id', $user_id)
  2177. ->limit(5)->get();
  2178. $count = count($apply_articale);
  2179. $result = [
  2180. 'apply_articale' => $apply_articale,
  2181. 'count' => $count,
  2182. ];
  2183. } elseif ($type_id == 10000) {
  2184. //获取未审核的5篇文章
  2185. $apply_articale = Article::where('status', 0)
  2186. ->orderBy('updated_at', 'desc')
  2187. ->limit(5)->get();
  2188. //获取5条单聊未读聊天消息
  2189. $chat = ChatRecords::where('is_read', 0)
  2190. ->where('user_id', $user_id)
  2191. ->where('talk_type', 1)
  2192. ->orderBy('created_at', 'desc')
  2193. ->limit(5)->get();
  2194. //获取5条未读群聊信息
  2195. $chat_group = ChatRecords::where('is_read', 0)
  2196. ->where('user_id', $user_id)
  2197. ->where('talk_type', 2)
  2198. ->orderBy('created_at', 'desc')
  2199. ->limit(5)->get();
  2200. $count = count($chat) + count($chat_group) + count($apply_articale);
  2201. $result = [
  2202. 'apply_articale' => $apply_articale,
  2203. 'chat' => $chat,
  2204. 'chat_group' => $chat_group,
  2205. 'count' => $count,
  2206. ];
  2207. } elseif ($type_id == 20000) {
  2208. }
  2209. var_dump($type_id, '-----------------test---------');
  2210. return Result::success($result);
  2211. }
  2212. public function getComplaintList(array $data): array
  2213. {
  2214. $result = Notice::where('status', 1)->get();
  2215. return Result::success($result);
  2216. }
  2217. public function getComplaintInfo(array $data): array
  2218. {
  2219. $result = Notice::where('id', $data['id'])->first();
  2220. if (empty($result)) {
  2221. return Result::error("查询失败", 0);
  2222. }
  2223. return Result::success($result);
  2224. }
  2225. public function addComplaint(array $data): array
  2226. {
  2227. $result = Notice::create($data);
  2228. return Result::success($result);
  2229. }
  2230. public function updateComplaint(array $data): array
  2231. {
  2232. $result = Notice::where('id', $data['id'])->update($data);
  2233. return Result::success($result);
  2234. }
  2235. public function deleteComplaint(array $data): array
  2236. {
  2237. $result = Notice::where('id', $data['id'])->delete();
  2238. return Result::success($result);
  2239. }
  2240. public function getComplainInfo(array $data): array
  2241. {
  2242. $result = Notice::where('id', $data['id'])->first();
  2243. if (empty($result)) {
  2244. return Result::error("查询失败", 0);
  2245. }
  2246. return Result::success($result);
  2247. }
  2248. //20250324 通知,公告,消息
  2249. // 20250306 -------招聘--------fr
  2250. /*
  2251. * 招聘列表
  2252. * */
  2253. public function getJobRecruitingList(array $data): array
  2254. {
  2255. $where = [];
  2256. if (isset($data['keyword']) && !empty($data['keyword'])) {
  2257. array_push($where, ['job_recruiting.title', 'like', '%' . $data['keyword'] . '%']);
  2258. }
  2259. $user = User::where('id', $data['user_id'])->first();
  2260. if (empty($user)) {
  2261. return Result::error("用户不存在", 0);
  2262. }
  2263. // 3:企业会员
  2264. if ($user['type_id'] == 3) {
  2265. array_push($where, ['job_recruiting.user_id', $data['user_id']]);
  2266. array_push($where, ['job_recruiting.website_id', $data['website_id']]);
  2267. }
  2268. // 如果 $where 为空,则不添加 where 条件
  2269. $result['rows'] = JobRecruiting::when(!empty($where), function ($query) use ($where) {
  2270. return $query->where($where);
  2271. })
  2272. ->leftJoin('website', 'job_recruiting.website_id', '=', 'website.id')
  2273. ->leftJoin('user', 'job_recruiting.user_id', '=', 'user.id')
  2274. ->select('job_recruiting.*', 'website.website_name as website_name', 'user.user_name as user_name')
  2275. ->orderBy("updated_at", "desc")
  2276. ->offset(($data['page'] - 1) * $data['page_size'])
  2277. ->limit($data['page_size'])
  2278. ->get()
  2279. ->all();
  2280. $result['count'] = JobRecruiting::when(!empty($where), function ($query) use ($where) {
  2281. return $query->where($where);
  2282. })
  2283. ->count();
  2284. if (empty($result)) {
  2285. return Result::error("暂无招聘信息", 0);
  2286. }
  2287. return Result::success($result);
  2288. }
  2289. /*
  2290. * 招聘信息添加
  2291. * */
  2292. public function addJobRecruiting(array $data): array
  2293. {
  2294. // return Result::success($data);
  2295. $user = User::where('user.id', $data['user_id'])
  2296. ->where('user.status', 1)
  2297. ->leftJoin('user_info', 'user_info.user_id', 'user.id')
  2298. ->select(
  2299. 'user.type_id',
  2300. 'user.mobile',
  2301. 'user.email',
  2302. 'user_info.business_name',
  2303. 'user_info.hy_id',
  2304. 'user_info.company_nature',
  2305. 'user_info.company_size',
  2306. 'user_info.introduction',
  2307. 'user_info.real_name',
  2308. 'user_info.company_url',
  2309. 'user_info.address_arr_id',
  2310. 'user_info.address'
  2311. )
  2312. ->first();
  2313. if (empty($user) || $user['type_id'] != $data['user_type']) {
  2314. return Result::error("用户不存在", 0);
  2315. }
  2316. $web = Website::where('id', $data['website_id'])->first();
  2317. if (empty($web)) {
  2318. return Result::error("网站不存在", 0);
  2319. }
  2320. // return Result::success($user);
  2321. $data['action_id'] = $data['user_id'];
  2322. $data['user_type'] = $user['type_id'];
  2323. $data['cat_arr_id'] = array_values(array_unique($data['cat_arr_id']));
  2324. $data['city_arr_id'] = array_values(array_unique($data['city_arr_id']));
  2325. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode(array_map('intval', $data['cat_arr_id'])) : '';
  2326. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode(array_map('intval', $data['city_arr_id'])) : '';
  2327. // 公司地址 管理员必填
  2328. $data['address_arr_id'] = array_values(array_unique($data['address_arr_id']));
  2329. $data['address_arr_id'] = isset($data['address_arr_id']) ? json_encode(array_map('intval', $data['address_arr_id'])) : '';
  2330. // 管理员-企业相关信息
  2331. $company = [
  2332. // 'user_id' => $data['user_id']?? null,
  2333. 'business_name' => $data['business_name'] ?? null,
  2334. 'company_hy_id' => $data['company_hy_id'] ?? null,
  2335. 'company_size' => $data['company_size'] ?? null,
  2336. 'company_nature' => $data['company_nature'] ?? null,
  2337. 'introduction' => $data['introduction'] ?? null,
  2338. 'real_name' => $data['real_name'] ?? null,
  2339. 'mobile' => $data['mobile'] ?? null,
  2340. 'company_url' => $data['company_url'] ?? null,
  2341. 'address_arr_id' => $data['address_arr_id'] ?? null,
  2342. 'address' => $data['address'] ?? null,
  2343. 'email' => $data['email'] ?? null,
  2344. ];
  2345. //去掉相关企业信息
  2346. $job = array_diff_key($data, array_flip(array_keys($company)));
  2347. Db::beginTransaction();
  2348. try {
  2349. // 先添加职位相关信息
  2350. $jobId = JobRecruiting::insertGetId($job);
  2351. if (empty($jobId)) {
  2352. Db::rollBack();
  2353. return Result::error("添加失败");
  2354. }
  2355. // 添加公司信息
  2356. $company['user_id'] = $data['user_id'] ?? null;
  2357. $company['job_id'] = $jobId;
  2358. $company['user_type'] = $user['type_id'] ?? null;
  2359. $company['website_id'] = $data['website_id'] ?? null;
  2360. if ($user['type_id'] == 10000) {
  2361. // 管理员添加企业信息
  2362. // return Result::success($company);
  2363. $companyId = JobCompany::insertGetId($company);
  2364. if (empty($companyId)) {
  2365. Db::rollBack();
  2366. return Result::error("添加失败");
  2367. }
  2368. } else {
  2369. // 企业会员添加企业信息
  2370. $company = [
  2371. 'user_id' => $data['user_id'] ?? null,
  2372. 'business_name' => $user['business_name'] ?? null,
  2373. 'company_hy_id' => $user['hy_id'] ?? null,
  2374. 'company_size' => $user['company_size'] ?? null,
  2375. 'company_nature' => $user['company_nature'] ?? null,
  2376. 'introduction' => $user['introduction'] ?? null,
  2377. 'real_name' => $user['real_name'] ?? null,
  2378. 'mobile' => $user['mobile'] ?? null,
  2379. 'company_url' => $user['company_url'] ?? null,
  2380. 'address_arr_id' => $user['address_arr_id'] ?? null,
  2381. 'address' => $user['address'] ?? null,
  2382. 'email' => $user['email'] ?? null,
  2383. 'website_id' => $data['website_id'] ?? null,
  2384. 'user_type' => $user['type_id'] ?? null,
  2385. 'job_id' => $jobId,
  2386. ];
  2387. $companyId = JobCompany::insertGetId($company);
  2388. if (empty($companyId)) {
  2389. Db::rollBack();
  2390. return Result::error("添加失败");
  2391. }
  2392. // return Result::success($company);
  2393. }
  2394. Db::commit();
  2395. } catch (\Exception $e) {
  2396. Db::rollBack();
  2397. return Result::error($e->getMessage(), 0);
  2398. }
  2399. $result = [
  2400. 'job_id' => $jobId,
  2401. 'company_id' => $companyId,
  2402. ];
  2403. if (empty($result)) {
  2404. return Result::error("添加失败", 0);
  2405. }
  2406. return Result::success($result);
  2407. }
  2408. /*
  2409. * 获取招聘信息详情
  2410. * */
  2411. public function getJobRecruitingInfo(array $data): array
  2412. {
  2413. $result = JobRecruiting::where('job_recruiting.id', $data['id'])
  2414. ->leftJoin('website', 'job_recruiting.website_id', '=', 'website.id')
  2415. ->leftJoin('job_company', 'job_recruiting.id', '=', 'job_company.job_id')
  2416. ->select(
  2417. 'job_recruiting.*',
  2418. 'website.website_name as website_name',
  2419. 'job_company.business_name',
  2420. 'job_company.company_hy_id',
  2421. 'job_company.company_size',
  2422. 'job_company.company_nature',
  2423. 'job_company.introduction',
  2424. 'job_company.real_name',
  2425. 'job_company.mobile',
  2426. 'job_company.company_url',
  2427. 'job_company.address_arr_id',
  2428. 'job_company.address',
  2429. 'job_company.email'
  2430. )
  2431. ->first();
  2432. if (empty($result)) {
  2433. return Result::error("招聘信息不存在", 0);
  2434. }
  2435. // return Result::success($job);
  2436. return Result::success($result);
  2437. }
  2438. /*
  2439. * 修改招聘信息
  2440. * */
  2441. public function upJobRecruiting(array $data): array
  2442. {
  2443. $job = JobRecruiting::where('job_recruiting.id', $data['id'])->first();
  2444. // return Result::success($job);
  2445. if (empty($job)) {
  2446. return Result::error("招聘信息不存在", 0);
  2447. }
  2448. $user = User::where('id', $data['user_id'])->first();
  2449. // return Result::success($user);
  2450. if (empty($user) || $user['type_id'] != $data['user_type']) {
  2451. return Result::error("用户不存在", 0);
  2452. }
  2453. if ($user['type_id'] == 3 && $job['user_id'] != $user['id']) {
  2454. return Result::error("用户暂无权限修改此招聘信息!", 0);
  2455. }
  2456. $data['cat_arr_id'] = array_values(array_unique($data['cat_arr_id']));
  2457. $data['city_arr_id'] = array_values(array_unique($data['city_arr_id']));
  2458. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode(array_map('intval', $data['cat_arr_id'])) : '';
  2459. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode(array_map('intval', $data['city_arr_id'])) : '';
  2460. // 公司地址 管理员必填
  2461. $data['address_arr_id'] = array_values(array_unique($data['address_arr_id']));
  2462. $data['address_arr_id'] = isset($data['address_arr_id']) ? json_encode(array_map('intval', $data['address_arr_id'])) : '';
  2463. // 管理员-企业相关信息
  2464. $company = [
  2465. 'business_name' => $data['business_name'] ?? null,
  2466. 'company_hy_id' => $data['hy_id'] ?? null,
  2467. 'company_size' => $data['company_size'] ?? null,
  2468. 'company_nature' => $data['company_nature'] ?? null,
  2469. 'introduction' => $data['introduction'] ?? null,
  2470. 'real_name' => $data['real_name'] ?? null,
  2471. 'mobile' => $data['mobile'] ?? null,
  2472. 'company_url' => $data['company_url'] ?? null,
  2473. 'address_arr_id' => $data['address_arr_id'] ?? null,
  2474. 'address' => $data['address'] ?? null,
  2475. 'email' => $data['email'] ?? null,
  2476. ];
  2477. //去掉相关企业信息
  2478. $data = array_diff_key($data, array_flip(array_keys($company)));
  2479. $jobId = $data['id'];
  2480. $web = $data['website_id'];
  2481. $data['action_id'] = $data['user_id'];
  2482. unset($data['user_id']);
  2483. unset($data['user_type']);
  2484. unset($data['id']);
  2485. unset($data['website_id']);
  2486. // return Result::success($data);
  2487. Db::beginTransaction();
  2488. try {
  2489. // 管理员修改招聘信息
  2490. if ($user['type_id'] == 10000) {
  2491. $data['website_id'] = $web;
  2492. $company['website_id'] = $data['website_id'];
  2493. }
  2494. // Db::rollBack();
  2495. // return Result::success($company);
  2496. $result['job'] = JobRecruiting::where('id', $jobId)->update($data);
  2497. if (empty($result['job'])) {
  2498. Db::rollBack();
  2499. return Result::error("修改招聘信息失败");
  2500. }
  2501. // 管理员修改企业相关信息
  2502. $result['company'] = JobCompany::where('job_id', $jobId)->update($company);
  2503. if (empty($result['company'])) {
  2504. Db::rollBack();
  2505. return Result::error("修改企业相关信息失败");
  2506. }
  2507. Db::commit();
  2508. // return Result::success($result);
  2509. } catch (\Exception $e) {
  2510. Db::rollBack();
  2511. return Result::error($e->getMessage(), 0);
  2512. }
  2513. return Result::success($result);
  2514. }
  2515. /*
  2516. * 招聘信息删除
  2517. * */
  2518. public function delJobRecruiting(array $data): array
  2519. {
  2520. $user = User::where('id', $data['user_id'])->first();
  2521. if (empty($user)) {
  2522. return Result::error("用户不存在", 0);
  2523. }
  2524. $job = JobRecruiting::where('id', $data['id'])->first();
  2525. if (empty($job)) {
  2526. return Result::error("招聘信息不存在", 0);
  2527. }
  2528. if ($user['type_id'] == 3 && $job['user_id'] != $user['id']) {
  2529. return Result::error("用户暂无权限修改此招聘信息!", 0);
  2530. }
  2531. Db::beginTransaction();
  2532. try {
  2533. $result['job'] = JobRecruiting::where('id', $data['id'])->delete();
  2534. if (empty($result['job'])) {
  2535. Db::rollBack();
  2536. return Result::error("删除招聘信息失败");
  2537. }
  2538. $result['company'] = JobCompany::where('job_id', $data['id'])->delete();
  2539. if (empty($result['company'])) {
  2540. Db::rollBack();
  2541. return Result::error("删除企业相关信息失败");
  2542. }
  2543. Db::commit();
  2544. } catch (\Exception $e) {
  2545. Db::rollBack();
  2546. return Result::error($e->getMessage(), 0);
  2547. }
  2548. return Result::success($result);
  2549. }
  2550. /*
  2551. * 获取公司信息
  2552. * */
  2553. public function getJobCompany(array $data): array
  2554. {
  2555. $user = User::where('user.id', $data['user_id'])
  2556. ->leftJoin('user_info', 'user_info.user_id', 'user.id')
  2557. ->select('user.user_name','user.mobile','user.email','user.type_id','user_info.*')
  2558. ->first();
  2559. // return Result::success($user);
  2560. if (empty($user)) {
  2561. return Result::error("用户不存在", 0);
  2562. }
  2563. if ($user['type_id'] == 3) {
  2564. $result = [
  2565. // 'id' => 0,
  2566. 'user_id' => $data['user_id'],
  2567. 'website_id' => $data['website_id'],
  2568. 'business_name' => $user['business_name'], // 企业名称
  2569. 'company_hy_id' => $user['company_hy_id'], // 企业所属行业
  2570. 'company_nature' => $user['company_nature'], // 公司性质
  2571. 'company_size' => $user['company_size'], // 公司规模
  2572. 'introduction' => $user['introduction'], // 公司简介
  2573. 'real_name' => $user['real_name'], // 企业联系人
  2574. 'mobile' => $user['mobile'], // 企业联系电话
  2575. 'company_url' => $user['company_url'], // 企业网址
  2576. 'address_arr_id' => $user['address_arr_id'], // 企业网址
  2577. 'address' => $user['address'], // 企业地址
  2578. 'email' => $user['email'], // 企业邮箱
  2579. ];
  2580. } else {
  2581. return Result::error("用户类型错误", 0);
  2582. }
  2583. if (empty($result)) {
  2584. return Result::error("公司信息不存在", 0);
  2585. }
  2586. return Result::success($result);
  2587. }
  2588. /*
  2589. * 修改公司信息
  2590. * */
  2591. public function upJobCompany(array $data): array
  2592. {
  2593. // return Result::success($data);
  2594. $user = User::where('user.id', $data['user_id'])
  2595. ->where('user.status',1)
  2596. ->select('user.user_name','user.type_id')
  2597. ->first();
  2598. if(empty($user)){
  2599. return Result::error("用户不存在", 0);
  2600. }
  2601. $data['address_arr_id'] = isset($data['address_arr_id']) ? json_encode(array_map('intval', $data['address_arr_id'])) : '';
  2602. $company = [
  2603. 'business_name' => $data['business_name'], // 企业名称
  2604. 'company_hy_id' => $data['company_hy_id'], // 企业所属行业
  2605. 'company_nature' => $data['company_nature'], // 公司性质
  2606. 'company_size' => $data['company_size'], // 公司规模
  2607. 'introduction' => $data['introduction'], // 公司简介
  2608. // 'real_name' => $data['real_name'], // 企业联系人
  2609. 'company_url' => $data['company_url'], // 企业网址
  2610. 'address_arr_id' => $data['address_arr_id'], // 企业地址
  2611. 'address' => $data['address'], // 企业详细地址
  2612. ];
  2613. if($user['type_id'] == 3){
  2614. Db::beginTransaction();
  2615. try {
  2616. $result['userinfo'] = UserInfo::where('user_id', $data['user_id'])->update([
  2617. 'business_name' => $data['business_name'], // 企业名称
  2618. 'company_hy_id' => $data['company_hy_id'], // 企业所属行业
  2619. 'company_nature' => $data['company_nature'], // 公司性质
  2620. 'company_size' => $data['company_size'], // 公司规模
  2621. 'introduction' => $data['introduction'], // 公司简介
  2622. // 'real_name' => $data['real_name'], // 企业联系人
  2623. 'company_url' => $data['company_url'], // 企业网址
  2624. 'address_arr_id' => $data['address_arr_id'], // 企业地址
  2625. 'address' => $data['address'], // 企业详细地址
  2626. ]);
  2627. $result['job_company'] = JobCompany::where('user_id', $data['user_id'])->update($company);
  2628. Db::commit();
  2629. } catch (\Exception $e) {
  2630. Db::rollBack();
  2631. return Result::error($e->getMessage(), 0);
  2632. }
  2633. }else{
  2634. return Result::error("用户类型错误", 0);
  2635. }
  2636. if (empty($result)) {
  2637. return Result::error("修改失败", 0);
  2638. }
  2639. return Result::success($result);
  2640. }
  2641. /*
  2642. * 获取省-市
  2643. * */
  2644. public function getJobRecruitingArea(array $data): array
  2645. {
  2646. if (isset($data['province_id']) && $data['province_id'] != null) {
  2647. $result = District::where('pid', $data['pid'])->get()->all();
  2648. } else {
  2649. $result = District::where('level', 1)->get()->all();
  2650. }
  2651. if (empty($result)) {
  2652. return Result::error("暂无此省市", 0);
  2653. }
  2654. return Result::success($result);
  2655. }
  2656. /*
  2657. * 获取行业分类
  2658. * */
  2659. public function getIndustry(array $data): array
  2660. {
  2661. $result = JobIndustry::get()->all();
  2662. if (empty($result)) {
  2663. return Result::error("暂无行业分类", 0);
  2664. }
  2665. return Result::success($result);
  2666. }
  2667. /*
  2668. * 获取职位
  2669. * */
  2670. public function getPositionList(array $data): array
  2671. {
  2672. if (isset($data['zwpid']) && $data['zwpid'] != null) {
  2673. $result = JobPosition::where('zwpid', $data['zwpid'])->get()->all();
  2674. } else {
  2675. $result = JobPosition::where('zwpid', 0)->get()->all();
  2676. }
  2677. if (empty($result)) {
  2678. return Result::error("暂无此职位", 0);
  2679. }
  2680. return Result::success($result);
  2681. }
  2682. /*
  2683. * 获取工作性质-菜单
  2684. * */
  2685. public function getJobNature(array $data): array
  2686. {
  2687. $result = JobEnum::where('egroup', 'nature')->get()->all();
  2688. if (empty($result)) {
  2689. return Result::error("暂无工作性质", 0);
  2690. }
  2691. return Result::success($result);
  2692. }
  2693. /*
  2694. * 获取工作经验-菜单
  2695. * */
  2696. public function getExperience(array $data): array
  2697. {
  2698. $result = JobEnum::where('egroup', 'years')->get()->all();
  2699. if (empty($result)) {
  2700. return Result::error("暂无工作经验", 0);
  2701. }
  2702. return Result::success($result);
  2703. }
  2704. /*
  2705. * 获取学历-菜单
  2706. * */
  2707. public function getEducation(array $data): array
  2708. {
  2709. $result = JobEnum::where('egroup', 'education')->get()->all();
  2710. if (empty($result)) {
  2711. return Result::error("暂无学历", 0);
  2712. }
  2713. return Result::success($result);
  2714. }
  2715. /*
  2716. * 获取薪资-菜单
  2717. * */
  2718. public function getSalary(array $data): array
  2719. {
  2720. $result = JobEnum::where('egroup', 'income')->get()->all();
  2721. if (empty($result)) {
  2722. return Result::error("暂无薪资", 0);
  2723. }
  2724. return Result::success($result);
  2725. }
  2726. /*
  2727. * 获取语言-菜单
  2728. * */
  2729. public function getLanguage(array $data): array
  2730. {
  2731. $result = JobEnum::where('egroup', 'language')->get()->all();
  2732. if (empty($result)) {
  2733. return Result::error("暂无语言", 0);
  2734. }
  2735. return Result::success($result);
  2736. }
  2737. /*
  2738. * 获取掌握程度-菜单
  2739. * */
  2740. public function getLevel(array $data): array
  2741. {
  2742. $result = JobEnum::where('egroup', 'languagetype')->get()->all();
  2743. if (empty($result)) {
  2744. return Result::error("暂无工作性质", 0);
  2745. }
  2746. return Result::success($result);
  2747. }
  2748. // 公司信息
  2749. /*
  2750. * 获取公司性质-菜单
  2751. * */
  2752. public function getCompanyNature(array $data): array
  2753. {
  2754. $result = JobNature::get()->all();
  2755. if (empty($result)) {
  2756. return Result::error("暂无公司性质", 0);
  2757. }
  2758. return Result::success($result);
  2759. }
  2760. /*
  2761. * 获取公司规模-菜单
  2762. * */
  2763. public function getCompanySize(array $data): array
  2764. {
  2765. $result = JobEnum::where('egroup', 'cosize')->get()->all();
  2766. if (empty($result)) {
  2767. return Result::error("暂无公司规模", 0);
  2768. }
  2769. return Result::success($result);
  2770. }
  2771. // 20250306 招聘
  2772. }