PublicRpcService.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. <?php
  2. namespace App\JsonRpc;
  3. use _PHPStan_62c6a0a8b\OndraM\CiDetector\Env;
  4. use App\Model\BlackWord;
  5. use App\Model\Department;
  6. use App\Model\District;
  7. use App\Model\LetterOfComplaint;
  8. use App\Model\LetterType;
  9. use App\Model\LevelUser;
  10. use App\Model\UserLevel;
  11. use App\Tools\Result;
  12. use Hyperf\DbConnection\Db;
  13. use Hyperf\Di\Annotation\Inject;
  14. use Hyperf\RpcServer\Annotation\RpcService;
  15. use App\Service\MinioService;
  16. use Hyperf\Redis\Redis;
  17. use Overtrue\ChineseCalendar\Calendar;
  18. use App\Model\TemplateClass;
  19. use App\Model\Template;
  20. use App\Model\WebsiteTemplate;
  21. use App\Model\WebsiteTemplateInfo;
  22. use App\Model\Sector;
  23. use App\Model\Component;
  24. use App\Model\Link;
  25. use App\Model\FooterCategory;
  26. use App\Model\Size;
  27. use function Hyperf\Support\retry;
  28. use Hyperf\Paginator\Paginator;
  29. use App\Model\User;
  30. use App\Model\Website;
  31. use App\Constants\ErrorCode;
  32. use GuzzleHttp\Client;
  33. use GuzzleHttp\Exception\GuzzleException;
  34. use App\Model\SectorPlace;
  35. #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  36. class PublicRpcService implements PublicRpcServiceInterface
  37. {
  38. #[Inject]
  39. protected Redis $redis;
  40. /**
  41. * @param array $data
  42. * @return array
  43. */
  44. public function getDistrictList(array $data): array
  45. {
  46. $where = [];
  47. if (isset($data['keyWord'])) {
  48. $where = [
  49. ['name', 'like', '%' . $data['keyWord'] . '%']
  50. ];
  51. }
  52. $result = [];
  53. if (isset($data['pageSize'])) {
  54. $rep = District::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get();
  55. $count = District::where($where)->count();
  56. $result = [
  57. 'rows' => $rep,
  58. 'count' => $count
  59. ];
  60. } else {
  61. $result = District::where($data)->orderBy("code", "asc")->get();
  62. }
  63. return $result ? Result::success($result) : Result::error("没有查到数据");
  64. }
  65. /**
  66. * @param array $data
  67. * @return array
  68. */
  69. public function getUserLevelList(array $data): array
  70. {
  71. $where = [];
  72. if (isset($data['keyWord'])) {
  73. $where = [
  74. ['name', 'like', '%' . $data['keyWord'] . '%'],
  75. ];
  76. }
  77. $result = [];
  78. if (isset($data['pageSize'])) {
  79. $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get();
  80. $count = UserLevel::where($where)->count();
  81. $result = [
  82. 'rows' => $rep,
  83. 'count' => $count,
  84. ];
  85. } else {
  86. $result = UserLevel::orderBy("id", "asc")->get();
  87. }
  88. return $result ? Result::success($result) : Result::error("没有查到数据");
  89. }
  90. /**
  91. * 添加用户等级
  92. * @param array $data
  93. * @return array
  94. */
  95. public function addUserLevel(array $data): array
  96. {
  97. LevelUser::insertGetId($data);
  98. return Result::success([]);
  99. }
  100. /**
  101. * 更新等级
  102. * @param array $data
  103. * @return array
  104. */
  105. public function updateUserLevel(array $data): array
  106. {
  107. $result = LevelUser::where(['id' => $data['id']])->update($data);
  108. if ($result) {
  109. return Result::success($result);
  110. }
  111. return Result::error("更新失败");
  112. }
  113. /**
  114. * 删除等级
  115. * @param array $data
  116. * @return array
  117. */
  118. public function delUserLevel(array $data): array
  119. {
  120. $result = LevelUser::where(['id' => $data['id']])->delete();
  121. if ($result) {
  122. return Result::success($result);
  123. }
  124. return Result::error("删除失败");
  125. }
  126. /**
  127. * 查询投诉举报信息
  128. * @param array $data
  129. * @return array
  130. */
  131. public function getLetterOfComplaint(array $data = []): array
  132. {
  133. var_dump("====");
  134. $where = [];
  135. if (isset($data['user_id']) && !empty($data['user_id'])) {
  136. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  137. }
  138. if (isset($data['nature']) && !empty($data['nature'])) {
  139. array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]);
  140. }
  141. if (isset($data['nature_level0']) && !empty($data['nature_level0'])) {
  142. array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]);
  143. }
  144. if (isset($data['status']) && !empty($data['status'])) {
  145. array_push($where, ['letter_of_complaint.status', '=', $data['status']]);
  146. }
  147. $result = [];
  148. if (isset($data['pageSize'])) {
  149. $rep = LetterOfComplaint::where($where)
  150. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  151. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  152. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  153. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  154. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  155. ->select(
  156. "letter_of_complaint.*",
  157. "type_a.type_name as nature_name",
  158. "type_b.type_name as nature_name1",
  159. "type_c.type_name as nature_name0",
  160. "type_d.type_name as status_name",
  161. "type_e.type_name as nature_name3"
  162. )
  163. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get();
  164. $count = LetterOfComplaint::where($where)->count();
  165. if ($rep) {
  166. foreach ($rep as $val) {
  167. if ($val['judgment']) {
  168. $val['judgment'] = json_decode($val['judgment']);
  169. }
  170. if ($val['audio_and_video']) {
  171. $val['audio_and_video'] = json_decode($val['audio_and_video']);
  172. }
  173. if ($val['contract']) {
  174. $val['contract'] = json_decode($val['contract']);
  175. }
  176. if ($val['qualifications']) {
  177. $val['qualifications'] = json_decode($val['qualifications']);
  178. }
  179. }
  180. }
  181. $result = [
  182. 'rows' => $rep,
  183. 'count' => $count,
  184. ];
  185. } else {
  186. $result = LetterOfComplaint::where($where)
  187. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  188. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  189. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  190. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  191. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  192. ->select(
  193. "letter_of_complaint.*",
  194. "type_a.type_name as nature_name",
  195. "type_b.type_name as nature_name1",
  196. "type_c.type_name as nature_name0",
  197. "type_d.type_name as status_name",
  198. "type_e.type_name as nature_name3"
  199. )
  200. ->orderBy("letter_of_complaint.id", "desc")->get();
  201. }
  202. return $result ? Result::success($result) : Result::error("没有查到数据");
  203. }
  204. /**
  205. * 添加投诉举报信息
  206. * @param array $data
  207. * @return array
  208. */
  209. public function addLetterOfComplaint(array $data): array
  210. {
  211. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  212. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  213. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  214. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  215. unset($data['id']);
  216. $result = LetterOfComplaint::insertGetId($data);
  217. if (empty($result)) {
  218. return Result::error("创建失败", 0);
  219. } else {
  220. return Result::success(["id" => $result]);
  221. }
  222. }
  223. /**
  224. * 用户端更新投诉举报
  225. * @param array $data
  226. * @return array
  227. */
  228. public function userUpLetterOfComplaint(array $data): array
  229. {
  230. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  231. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  232. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  233. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  234. $result = LetterOfComplaint::where(['id' => $data['id']])->update($data);
  235. if (empty($result)) {
  236. return Result::error("创建失败", 0);
  237. } else {
  238. return Result::success(["id" => $result]);
  239. }
  240. }
  241. /**
  242. * 管理后台更新投诉举报信息
  243. * @param array $data
  244. * @return array
  245. */
  246. public function upLetterOfComplaint(array $data): array
  247. {
  248. var_dump("admin:", $data);
  249. $where = [
  250. 'id' => $data['id'],
  251. ];
  252. $filtered_array = array_filter($data, function ($value) {
  253. return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value);
  254. });
  255. $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : '';
  256. $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : '';
  257. $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : '';
  258. $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : '';
  259. unset($filtered_array['nature_name']);
  260. unset($filtered_array['type_name']);
  261. unset($filtered_array['nature_level_name']);
  262. unset($filtered_array['status_name']);
  263. unset($filtered_array['is_admin']);
  264. unset($filtered_array['type_level_name']);
  265. $result = LetterOfComplaint::where($where)->update($filtered_array);
  266. if ($result) {
  267. return Result::success($result);
  268. }
  269. return Result::error("更新失败", 0);
  270. }
  271. /**
  272. * 查询投诉举报记录
  273. * @param array $data
  274. * @return array
  275. */
  276. public function getLetterOfComplaintInfo(array $data): array
  277. {
  278. $where = [
  279. 'letter_of_complaint.id' => $data['id'],
  280. ];
  281. if (isset($data['user_id']) && !empty($data['user_id'])) {
  282. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  283. }
  284. $result = LetterOfComplaint::where($where)
  285. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  286. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  287. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  288. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  289. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  290. ->select(
  291. "letter_of_complaint.*",
  292. "type_a.type_name as nature_name",
  293. "type_b.type_name as nature_name1",
  294. "type_c.type_name as nature_name0",
  295. "type_d.type_name as status_name",
  296. "type_e.type_name as nature_name3"
  297. )
  298. ->first();
  299. return Result::success($result);
  300. }
  301. /**
  302. * 删除投诉举报信息
  303. * @param array $data
  304. * @return array
  305. */
  306. public function delLetterOfComplaint(array $data): array
  307. {
  308. $result = LetterOfComplaint::when($data, function ($query) use ($data) {
  309. if (isset($data['id']) && !empty($data['id'])) {
  310. $query->where(['id' => $data['id']]);
  311. }
  312. if (isset($data['user_id']) && !empty($data['user_id'])) {
  313. $query->where(['user_id' => $data['user_id']]);
  314. }
  315. })->delete();
  316. if (empty($result)) {
  317. return Result::error("删除失败", 0);
  318. } else {
  319. return Result::success();
  320. }
  321. }
  322. /**
  323. * 获取举报信息类型
  324. * @param array $data
  325. * @return array
  326. */
  327. public function getLetterType(array $data): array
  328. {
  329. $where = [];
  330. if (isset($data['type'])) {
  331. array_push($where, ['type', '=', $data['type']]);
  332. }
  333. if (isset($data['pid']) && $data['pid'] > 0) {
  334. array_push($where, ['pid', '=', $data['pid']]);
  335. }
  336. $result = LetterType::where($where)->orderBy('sort', 'asc')->get();
  337. return $result ? Result::success($result) : Result::error("没有查到数据");
  338. }
  339. /**
  340. * 更新举报类型
  341. * @param array $data
  342. * @return array
  343. */
  344. public function upLetterType(array $data): array
  345. {
  346. return [];
  347. }
  348. /**
  349. * 添加举报类型
  350. * @param array $data
  351. * @return array
  352. */
  353. public function addLetterType(array $data): array
  354. {
  355. $result = LetterType::insertGetId($data);
  356. if (empty($result)) {
  357. return Result::error("创建失败", 0);
  358. } else {
  359. return Result::success(["id" => $result]);
  360. }
  361. }
  362. /**
  363. * 删除举报类型
  364. * @param array $data
  365. * @return array
  366. */
  367. public function delLetterType(array $data): array
  368. {
  369. $result = LetterType::where('id', $data['id'])->delete();
  370. if (empty($result)) {
  371. return Result::error("删除失败", 0);
  372. } else {
  373. return Result::success();
  374. }
  375. }
  376. /**
  377. * 检测是否已经被接案
  378. * @param array $data
  379. * @return array
  380. */
  381. public function checkMeasure(array $data): array
  382. {
  383. $where = [
  384. 'id' => $data['id'],
  385. ];
  386. $letterOfComplaintInfo = LetterOfComplaint::where($where)->first();
  387. var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']);
  388. //操作人和当前登陆用户id 相等说明是当前人接收的案件
  389. if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) {
  390. return Result::success();
  391. } else {
  392. return Result::error("您不能处理其他人已经接过的案件", 0);
  393. }
  394. }
  395. /**
  396. * 后台获取职能部门
  397. * @param array $data
  398. * @return array
  399. */
  400. public function getZhinengbumenList(array $data): array
  401. {
  402. // 获取分页参数,默认每页 10 条记录
  403. $page = isset($data['page']) ? (int) $data['page'] : 1;
  404. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  405. // 查询数据并分页
  406. $query = Department::query();
  407. // 可以在这里添加更多的查询条件
  408. if (isset($data['search'])) {
  409. $query->where('name', 'like', '%' . $data['search'] . '%');
  410. }
  411. // 执行分页查询
  412. $result = $query->paginate($perPage, ['*'], 'page', $page);
  413. // 返回分页结果
  414. return Result::success([
  415. 'count' => $result->total(),
  416. 'current_page' => $result->currentPage(),
  417. 'last_page' => $result->lastPage(),
  418. 'pagesize' => $result->perPage(),
  419. 'rows' => $result->items(),
  420. ]);
  421. }
  422. /**
  423. * 添加获取职能部门
  424. * @param array $data
  425. * @return array
  426. */
  427. public function addZhinengbumen(array $data): array
  428. {
  429. $result = Department::insertGetId($data);
  430. if (empty($result)) {
  431. return Result::error("创建失败", 0);
  432. } else {
  433. return Result::success(["id" => $result]);
  434. }
  435. }
  436. public function delZhinengbumen(array $data): array
  437. {
  438. $result = Department::where('id', $data['id'])->delete();
  439. if (empty($result)) {
  440. return Result::error("删除失败", 0);
  441. } else {
  442. return Result::success();
  443. }
  444. }
  445. public function getZhinengbumen(array $data): array
  446. {
  447. $result = Department::where('id', $data['id'])
  448. ->orderBy('department', 'desc')
  449. ->first();
  450. return Result::success($result);
  451. }
  452. public function getPidZhinengbumen(array $data): array
  453. {
  454. if (empty($data['pid'])) {
  455. $data['pid'] = 0;
  456. }
  457. $result = Department::where('pid', $data['pid'])->get();
  458. return Result::success($result);
  459. }
  460. public function modZhinengbumen(array $data): array
  461. {
  462. $result = Department::where('id', $data['id'])->update($data);
  463. if (empty($result)) {
  464. return Result::error("修改失败", 0);
  465. } else {
  466. return Result::success();
  467. }
  468. }
  469. /**
  470. * 查询职能列表
  471. * @param array $data
  472. * @return array
  473. */
  474. public function getDepartment(array $data): array
  475. {
  476. $where = [];
  477. if (isset($data['pid'])) {
  478. $where = [
  479. 'pid' => $data['pid'] ?? 0
  480. ];
  481. }
  482. $result = Department::when(!empty($where), function ($query) use ($where) {
  483. $query->where($where);
  484. })->orderBy("sort", "desc")->get();
  485. if (empty($result)) {
  486. return Result::error("查询失败", 0);
  487. } else {
  488. return Result::success($result);
  489. }
  490. }
  491. /**
  492. * 获取所有的buckets
  493. * @param array $data
  494. * @return array
  495. */
  496. public function getBuckets(array $data): array
  497. {
  498. $result = new MinioService();
  499. // 调用服务层的方法获取存储桶列表
  500. $bucketsResponse = $result->listBuckets();
  501. // 直接返回服务层生成的响应
  502. return Result::success($bucketsResponse['data']);
  503. }
  504. /**
  505. * 上传文件
  506. * @param array $data
  507. * @return array
  508. */
  509. public function uploadFile(array $data): array
  510. {
  511. $result = new MinioService();
  512. $rep = $result->uploadFile($data);
  513. if ($rep['code'] == 200) {
  514. return Result::success($rep['data']);
  515. } else {
  516. return Result::error("上传失败!");
  517. }
  518. }
  519. /**
  520. * 黑名单管理
  521. * @param array $data
  522. * @return array
  523. */
  524. public function getBlackWordList(array $data): array
  525. {
  526. $result = BlackWord::when($data, function ($query) use ($data) {
  527. if (isset($data['name']) && $data['name']) {
  528. $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
  529. }
  530. })->orderBy('black_word.id', 'desc')
  531. ->paginate(
  532. intval($data['pageSize']),
  533. [
  534. 'black_word.*',
  535. ],
  536. 'page',
  537. intval($data['page'])
  538. );
  539. $count = $result->total();
  540. $returnData = [
  541. 'rows' => $result->items(),
  542. 'count' => $count
  543. ];
  544. return Result::success($returnData);
  545. }
  546. /**
  547. * 添加黑名单
  548. * @param array $data
  549. * @return array
  550. */
  551. public function addBlackWord(array $data): array
  552. {
  553. Db::beginTransaction();
  554. try {
  555. $info = BlackWord::where(['name' => $data['name']])->first();
  556. if ($info) {
  557. Db::rollBack();
  558. return Result::error("该黑名单已存在", 0);
  559. }
  560. $data['type'] = 10;
  561. $result = BlackWord::insertGetId($data);
  562. $redisKey = 'black_word';
  563. $this->redis->sAdd($redisKey, $data['name']);
  564. Db::commit();
  565. return Result::success(["id" => $result]);
  566. } catch (\Exception $e) {
  567. Db::rollBack();
  568. return Result::error("创建失败" . $e->getMessage(), 0);
  569. }
  570. }
  571. /**
  572. * 删除黑名单
  573. * @param array $data
  574. * @return array
  575. */
  576. public function delBlackWord(array $data): array
  577. {
  578. Db::beginTransaction();
  579. try {
  580. BlackWord::where(['name' => $data['name']])->delete();
  581. $redisKey = 'black_word';
  582. $this->redis->sRem($redisKey, $data['name']);
  583. Db::commit();
  584. return Result::success([]);
  585. } catch (\Exception $e) {
  586. Db::rollBack();
  587. return Result::error("删除失败" . $e->getMessage(), 0);
  588. }
  589. }
  590. /**
  591. * 修改违禁词
  592. * @param array $data
  593. * @return array
  594. */
  595. public function upBlackWord(array $data): array
  596. {
  597. Db::beginTransaction();
  598. try {
  599. $checkInfo = BlackWord::where(['name' => $data['name']])->first();
  600. if ($checkInfo) {
  601. Db::rollBack();
  602. return Result::error("该违禁词已经存在", 0);
  603. }
  604. $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
  605. if ($blackWordInfo) {
  606. //先删除redis
  607. $blackWordInfo = $blackWordInfo->toArray();
  608. $redisKey = 'black_word';
  609. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  610. $this->redis->sAdd($redisKey, $data['name']);
  611. BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
  612. Db::commit();
  613. return Result::success([]);
  614. } else {
  615. Db::rollBack();
  616. return Result::error("系统错误", 0);
  617. }
  618. } catch (\Exception $e) {
  619. Db::rollBack();
  620. return Result::error("修改失败" . $e->getMessage(), 0);
  621. }
  622. }
  623. /**
  624. * 获取风格
  625. * @return void
  626. */
  627. public function getTemplateClassList(array $data): array
  628. {
  629. $where = [];
  630. if (isset($data['name']) && $data['name']) {
  631. array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
  632. }
  633. if (isset($data['keyword']) && $data['keyword']) {
  634. array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']);
  635. }
  636. $template = TemplateClass::when($where, function ($query) use ($where) {
  637. $query->where($where);
  638. });
  639. $count = $template->count();
  640. // $countQuery = clone $template;
  641. $row = $template
  642. ->leftJoin('template', function ($join) {
  643. $join->on('template_class.class_id', '=', 'template.template_class_id');
  644. })
  645. ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count'))
  646. ->groupBy('template_class.id')
  647. ->orderBy('template_class.id', 'desc')
  648. ->offset(($data['page'] - 1) * $data['pageSize'])
  649. ->limit($data['pageSize'])
  650. ->get();
  651. $result = [
  652. 'rows' => $row,
  653. 'count' => $count,
  654. ];
  655. if ($row->isEmpty()) {
  656. return Result::error("暂无风格", 0);
  657. } else {
  658. return Result::success($result);
  659. }
  660. }
  661. /**
  662. * 添加风格
  663. * @param
  664. * @return void
  665. */
  666. public function addTemplateClass(array $data): array
  667. {
  668. $data['keyword'] = json_encode($data['keyword']);
  669. $template_class = TemplateClass::where('name', $data['name'])
  670. ->orWhere('class_id', $data['class_id'])
  671. ->first();
  672. if ($template_class) {
  673. return Result::error("风格名称或者风格编号已存在,不可添加!", 0);
  674. }
  675. $result = TemplateClass::insertGetId($data);
  676. if (empty($result)) {
  677. return Result::error("创建风格失败", 0);
  678. } else {
  679. return Result::success(["id" => $result]);
  680. }
  681. }
  682. /**
  683. * 更新风格
  684. * @param array $data
  685. * @return array
  686. */
  687. public function upTemplateClass(array $data): array
  688. {
  689. $where = [
  690. 'id' => $data['id'],
  691. ];
  692. $template_class = TemplateClass::where($where)->first();
  693. if (empty($template_class)) {
  694. return Result::error("未查询到风格", 0);
  695. }
  696. if ($template_class->type == 1) {
  697. return Result::error("默认风格不能修改", 0);
  698. }
  699. $template = TemplateClass::where('id', '!=', $data['id'])
  700. // ->where(['name' => $data['name']])
  701. // ->orWhere(['class_id' => $data['class_id']])
  702. ->where(function ($query) use ($data) {
  703. $query->where('name', $data['name'])
  704. ->orWhere('class_id', $data['class_id']);
  705. })
  706. ->first();
  707. if ($template) {
  708. return Result::error("风格名称或者风格编号已存在,不可编辑!", 0);
  709. }
  710. $updateData = [
  711. 'name' => $data['name'],
  712. 'keyword' => json_encode($data['keyword']),
  713. 'class_id' => $data['class_id'],
  714. ];
  715. $result = TemplateClass::where($where)->update($updateData);
  716. if (empty($result)) {
  717. return Result::error("更新失败", 0);
  718. } else {
  719. return Result::success($result);
  720. }
  721. }
  722. /**
  723. * 删除风格
  724. * @param array $data
  725. * @return array
  726. */
  727. public function delTemplateClass(array $data): array
  728. {
  729. $where = [
  730. 'id' => $data['id'],
  731. ];
  732. $template_class = TemplateClass::where($where)->first();
  733. if (empty($template_class)) {
  734. return Result::error("未查询到风格", 0);
  735. }
  736. if ($template_class->type == 1) {
  737. return Result::error("默认风格不能删除", 0);
  738. }
  739. $template = Template::where('template_class_id', $template_class['class_id'])->get();
  740. if (!empty($template->all())) {
  741. try {
  742. // 默认风格 1
  743. Db::beginTransaction();
  744. $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]);
  745. if (empty($template)) {
  746. Db::rollBack();
  747. return Result::error("删除失败", 0);
  748. }
  749. $result = TemplateClass::where($where)->delete();
  750. } catch (\Exception $e) {
  751. return Result::error("删除失败" . $e->getMessage(), 0);
  752. }
  753. } else {
  754. $result = TemplateClass::where('id', $data['id'])->delete();
  755. }
  756. if (empty($result)) {
  757. return Result::error("删除失败", 0);
  758. }
  759. return Result::success($result);
  760. }
  761. /**
  762. * 根据风格名称-获取获取所有风格
  763. * @param array $data
  764. * @return array
  765. */
  766. public function getTemplateClass(array $data): array
  767. {
  768. $where = [];
  769. if (!empty($data['template_name'])) {
  770. $where['name'] = $data['template_name'];
  771. }
  772. $result = TemplateClass::where($where)->get();
  773. if (empty($result)) {
  774. return Result::error("暂无风格", 0);
  775. }
  776. return Result::success($result);
  777. }
  778. /**
  779. * 获取皮肤列表
  780. * @param array $data
  781. * @return array
  782. */
  783. public function getTemplateList(array $data): array
  784. {
  785. $where = [];
  786. if (!empty($data['template_class_id'])) {
  787. $where['template_class_id'] = $data['template_class_id'];
  788. }
  789. if (!empty($data['template_name'])) {
  790. array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']);
  791. }
  792. if (!empty($data['template_keyword'])) {
  793. array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']);
  794. }
  795. $result = Template::where($where)
  796. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  797. ->select('template.*', 'template_class.name as template_class_name')
  798. ->orderBy('template.id', 'desc')
  799. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  800. if (empty($result)) {
  801. return Result::error("暂无皮肤", 0);
  802. }
  803. return Result::success($result);
  804. }
  805. public function getTemplateInfo(array $data): array
  806. {
  807. $result = Template::where('template.id', $data['id'])
  808. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  809. ->select('template.*', 'template_class.name as template_class_name')
  810. ->first();
  811. if (empty($result)) {
  812. return Result::error("暂无皮肤", 0);
  813. }
  814. return Result::success($result);
  815. }
  816. /**
  817. * 添加皮肤
  818. * @param array $data
  819. * @return array
  820. */
  821. public function addTemplate(array $data): array
  822. {
  823. var_dump($data);
  824. unset($data['user_id']);
  825. $template = Template::where('template_name', $data['template_name'])
  826. ->orWhere('template_id', $data['template_id'])
  827. ->first();
  828. if ($template) {
  829. return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
  830. }
  831. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  832. $page_type = json_decode($data['page_type'], true);
  833. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  834. $result = Template::insertGetId($data);
  835. if (empty($result)) {
  836. return Result::error("创建失败", 0);
  837. } else {
  838. return Result::success($result);
  839. }
  840. }
  841. /**
  842. * 删除皮肤
  843. * @param array $data
  844. * @return array
  845. */
  846. public function delTemplate(array $data): array
  847. {
  848. $rector = Sector::where('template_id', $data['id'])->first();
  849. if (!empty($rector)) {
  850. return Result::error("此皮肤已被绑定通栏,不可删除", 0);
  851. }
  852. $template = WebsiteTemplateInfo::where('template_id', $data['id'])->first();
  853. if (empty($template)) {
  854. return Result::error("此皮肤已被绑定网站,不可删除", 0);
  855. }
  856. if ($template->type == 1) {
  857. return Result::error("默认皮肤不能删除", 0);
  858. }
  859. $result = Template::where('id', $data['id'])->delete();
  860. var_dump($result, '-------------------delete');
  861. if ($result) {
  862. return Result::success($result);
  863. } else {
  864. return Result::error("删除失败", 0);
  865. }
  866. }
  867. /**
  868. * 更新皮肤
  869. * @param array $data
  870. * @return array
  871. */
  872. public function updateTemplate(array $data): array
  873. {
  874. unset($data['user_id']);
  875. $template = Template::where('id', $data['id'])
  876. ->first();
  877. if (empty($template)) {
  878. return Result::error("此皮肤不存在!", 0);
  879. }
  880. $template = Template::where('id', '!=', $data['id'])
  881. ->where(function ($query) use ($data) {
  882. $query->where('template_name', $data['template_name'])
  883. ->orWhere('template_id', $data['template_id']);
  884. })
  885. ->first();
  886. if ($template) {
  887. return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
  888. }
  889. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  890. $page_type = json_decode($data['page_type'], true);
  891. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  892. $result = Template::where('id', $data['id'])->update($data);
  893. var_dump($result, '-------------------update');
  894. if (!$result) {
  895. return Result::error("更新失败", 0);
  896. } else {
  897. return Result::success('更新成功');
  898. }
  899. }
  900. /**
  901. * 获取所有尺寸
  902. * @param array $data
  903. * @return array
  904. */
  905. public function getAllSize(array $data): array
  906. {
  907. if(isset($data['id']) && !empty($data['id'])){
  908. $result = Size::where('id', $data['id'])->get()->all();
  909. }else{
  910. $result = Size::get()->all();
  911. }
  912. if(empty($result)){
  913. return Result::error("暂无尺寸", 0);
  914. }
  915. return Result::success($result);
  916. }
  917. /**
  918. * 根据皮肤名称-获取所有皮肤
  919. * @param array $data
  920. * @return array
  921. */
  922. public function getAllTemplate(array $data): array
  923. {
  924. $where = [];
  925. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  926. $where[] = ['template_class_id', $data['template_class_id']];
  927. }
  928. if (isset($data['template_name']) && !empty($data['template_name'])) {
  929. $where[] = ['template_name', 'like', '%' . $data['template_name'] . '%'];
  930. }
  931. $result = Template::where($where)->get()->all();
  932. if (empty($result)) {
  933. return Result::error("暂无皮肤", 0);
  934. }
  935. return Result::success($result);
  936. }
  937. /**
  938. * 获取通栏列表
  939. * @param array $data
  940. * @return array
  941. */
  942. public function getSectorList(array $data): array
  943. {
  944. $where = [];
  945. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  946. $where['template.template_class_id'] = $data['template_class_id'];
  947. }
  948. if (isset($data['template_id']) && !empty($data['template_id'])) {
  949. array_push($where, ['template.template_id', $data['template_id']]);
  950. }
  951. if (isset($data['sector_name']) && !empty($data['sector_name'])) {
  952. array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
  953. }
  954. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  955. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  956. } else if (isset($data['width']) && !empty($data['width'])) {
  957. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->pluck('id');
  958. } else if (isset($data['height']) && !empty($data['height'])) {
  959. $size_id = Size::where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  960. } else {
  961. $size_id = [];
  962. }
  963. if(isset($data['page_type']) && !empty($data['page_type'])){
  964. array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']);
  965. }
  966. $result = Sector::when(!empty($size_id), function ($query) use ($size_id) {
  967. $query->whereIn('sector.size_id', $size_id);
  968. })
  969. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  970. ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  971. ->leftJoin('template_class', 'template_class.class_id', '=', 'template.template_class_id')
  972. ->leftJoin('sector_place', 'sector_place.sector_type', '=', 'sector.place_type')
  973. ->where($where)
  974. ->where('sector_place.type_id', 1)
  975. ->select('sector.*', 'size.width', 'size.height', 'template_class.name as class_name', 'template_class.class_id', 'template.template_name',
  976. 'sector_place.name as place_name', 'sector_place.sector_img as place_img')
  977. ->orderBy('sector.id', 'desc')
  978. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  979. if(empty($result)){
  980. return Result::error('暂无通栏!');
  981. }
  982. return Result::success($result);
  983. }
  984. /**
  985. * 获取通栏详情
  986. * @param array $data
  987. * @return array
  988. */
  989. public function getSectorInfo(array $data): array
  990. {
  991. $where = [];
  992. $where[] = ['sector.id', '=', $data['id']];
  993. $result = Sector::where($where)
  994. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  995. ->select('sector.*', 'size.width', 'size.height')
  996. ->orderBy('sector.id', 'desc')
  997. ->first();
  998. if (empty($result)) {
  999. return Result::error('此通栏不存在!');
  1000. }
  1001. return Result::success($result);
  1002. }
  1003. /**
  1004. * 获取经纬度信息
  1005. * @return void
  1006. */
  1007. public function getIpInfo(array $data): array
  1008. {
  1009. if (empty($data['ip'])) {
  1010. // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
  1011. return Result::error('IP地址不能为空!');
  1012. }
  1013. $client_ip = $data['ip'];
  1014. try {
  1015. // 直接从容器获取 Guzzle 客户端实例
  1016. /** @var \GuzzleHttp\Client $client */
  1017. $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
  1018. // 使用新的 GeoJS API 地址
  1019. $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
  1020. // 将 timeout 作为请求选项传递
  1021. $response = $client->get($api_url, ['timeout' => 5]);
  1022. if ($response->getStatusCode() !== 200) {
  1023. return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
  1024. }
  1025. $ip_info = json_decode($response->getBody()->getContents(), true);
  1026. // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
  1027. if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
  1028. // GeoJS 返回的经纬度是字符串,转换为浮点数
  1029. $ip_info['latitude'] = floatval($ip_info['latitude']);
  1030. $ip_info['longitude'] = floatval($ip_info['longitude']);
  1031. return Result::success($ip_info);
  1032. }
  1033. // 如果返回的数据不符合预期,提供一个通用错误
  1034. $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
  1035. return Result::error($errorMessage);
  1036. } catch (GuzzleException $e) {
  1037. // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
  1038. return Result::error('请求IP查询接口异常: ' . $e->getMessage());
  1039. } catch (\Throwable $e) {
  1040. return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
  1041. }
  1042. }
  1043. /**
  1044. * 获取天气
  1045. * @param array $data
  1046. * @return array
  1047. */
  1048. public function getWeatherInfo(array $data): array
  1049. {
  1050. $month = $data['month'] ?? date('m');
  1051. $day = $data['day'] ?? date('d');
  1052. // 使用缓存键
  1053. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  1054. // 尝试从缓存获取数据
  1055. $container = \Hyperf\Context\ApplicationContext::getContainer();
  1056. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  1057. if ($cachedData = $cache->get($cacheKey)) {
  1058. return Result::success(unserialize($cachedData));
  1059. }
  1060. $location = $data['latitude'] . ":" . $data['longitude'];
  1061. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=" . \Hyperf\Support\env('WEATHER_KEY') . "&location=" . $location . "&language=zh-Hans&unit=c";
  1062. $ch = curl_init($api_url);
  1063. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1064. $response = curl_exec($ch);
  1065. curl_close($ch);
  1066. // 解析 JSON 响应
  1067. $WeatherInfo = json_decode($response, true);
  1068. // 缓存结果,设置1小时过期
  1069. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  1070. if ($WeatherInfo) {
  1071. return Result::success($WeatherInfo);
  1072. } else {
  1073. return Result::error("获取天气失败", 0);
  1074. }
  1075. }
  1076. /**
  1077. * 获取农历信息
  1078. * @return void
  1079. */
  1080. public function getCalendar(array $data): array
  1081. {
  1082. $calendar = new Calendar();
  1083. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  1084. return Result::success($result);
  1085. }
  1086. /**
  1087. * 添加通栏
  1088. * @param array $data
  1089. * @return array
  1090. */
  1091. public function addSector(array $data): array
  1092. {
  1093. unset($data['user_id']);
  1094. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1095. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1096. $data['page_type'] = json_encode($data['page_type']);
  1097. $template = Template::where('template_id', $data['template_id'])
  1098. ->first();
  1099. if (empty($template)) {
  1100. return Result::error('皮肤不存在!');
  1101. }
  1102. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1103. if(!empty($sector_id)){
  1104. return Result::error('通栏编号已存在!');
  1105. }
  1106. $data['status'] = 0;
  1107. // 通栏分类:1:资讯类:2:通栏广告类;3:混合类;4:头条类;5:轮播图类;
  1108. $sector_code = [
  1109. 'sectorName' => $data['sector_name'],
  1110. 'sectorId' => $data['sector_id'],
  1111. 'sectorType' => $data['sector_type'],
  1112. 'sectorPlace' => $data['place_type'],
  1113. ];
  1114. // return Result::success($sector_code);
  1115. $data['sector_code'] = json_encode($sector_code);
  1116. $result = Sector::insertGetId($data);
  1117. if (empty($result)) {
  1118. return Result::error('添加失败');
  1119. }
  1120. return Result::success($result);
  1121. }
  1122. /**
  1123. * 删除通栏
  1124. * @param array $data
  1125. * @return array
  1126. */
  1127. public function delSector(array $data): array
  1128. {
  1129. $component_id = Component::where('sector_id', $data['id'])->pluck('id')->toArray();
  1130. if (!empty($component_id)) {
  1131. return Result::error('请先删除相关组件!');
  1132. }
  1133. $result = Sector::where('id', $data['id'])->delete();
  1134. if ($result == 1) {
  1135. return Result::success('删除成功');
  1136. } else {
  1137. return Result::error('删除失败');
  1138. }
  1139. }
  1140. /**
  1141. * 修改通栏
  1142. * @param array $data
  1143. * @return array
  1144. */
  1145. public function updateSector(array $data): array
  1146. {
  1147. unset($data['user_id']);
  1148. $sector = Sector::where('id', $data['id'])->first();
  1149. if (empty($sector)) {
  1150. return Result::error('通栏不存在!');
  1151. }
  1152. if($sector['sector_id'] != $data['sector_id']){
  1153. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1154. if(!empty($sector_id)){
  1155. return Result::error('通栏编号已存在!');
  1156. }
  1157. }
  1158. $sector_code = [
  1159. 'sectorName' => strval($data['sector_name']),
  1160. 'sectorId' => intval($data['sector_id']),
  1161. 'sectorType' => intval($data['sector_type']),
  1162. 'sectorPlace' => intval($data['place_type']),
  1163. ];
  1164. // return Result::success($sector_code);
  1165. $data['sector_code'] = json_encode($sector_code);
  1166. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1167. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1168. $data['page_type'] = json_encode($data['page_type']);
  1169. $template = Template::where('template_id', $data['template_id'])
  1170. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1171. ->first();
  1172. if (empty($template)) {
  1173. return Result::error('皮肤不存在!');
  1174. }
  1175. $result = Sector::where('id', $data['id'])->update($data);
  1176. if ($result == 1) {
  1177. return Result::success('修改成功');
  1178. } else {
  1179. return Result::error('修改失败');
  1180. }
  1181. }
  1182. public function getComponentList(array $data): array
  1183. {
  1184. $where = [];
  1185. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  1186. $where['template_class.class_id'] = $data['template_class_id'];
  1187. }
  1188. if (isset($data['component_name']) && !empty($data['component_name'])) {
  1189. array_push($where, ['component.component_name', 'like', '%' . $data['component_name'] . '%']);
  1190. }
  1191. if (isset($data['sector_id']) && !empty($data['sector_id'])) {
  1192. $where['component.sector_id'] = $data['sector_id'];
  1193. };
  1194. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  1195. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  1196. } else if (isset($data['width']) && !empty($data['width'])) {
  1197. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->pluck('id');
  1198. } else if (isset($data['height']) && !empty($data['height'])) {
  1199. $size_id = Size::where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  1200. } else {
  1201. $size_id = [];
  1202. }
  1203. if(isset($data['page_type']) && !empty($data['page_type'])){
  1204. array_push($where, ['component.page_type', 'like', '%' . $data['page_type'] . '%']);
  1205. }
  1206. if(isset($data['type_id']) && !empty($data['type_id'])){
  1207. $where['component.type_id'] = $data['type_id'];
  1208. }
  1209. $result = Component::when(!empty($size_id), function ($query) use ($size_id) {
  1210. $query->whereIn('component.size_id', $size_id);
  1211. })
  1212. ->where($where)
  1213. ->leftJoin('template', 'template.template_id', '=', 'component.template_id')
  1214. ->leftJoin('template_class', 'template.template_class_id', '=', 'template_class.class_id') // 添加与 template_class 的关联
  1215. ->leftJoin('sector', 'sector.sector_id', '=', 'component.sector_id')
  1216. ->leftJoin('size','size.id','=','component.size_id')
  1217. ->leftJoin('component_type','component_type.id','=','component.type_id')
  1218. ->select( 'component.*', 'sector.sector_id','sector.sector_name','template.template_id',
  1219. 'template.template_name','template_class.class_id', 'template_class.name as class_name',
  1220. 'size.width','size.height','component_type.com_typename')
  1221. ->orderBy('sector.updated_at', 'desc')
  1222. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1223. if(empty($result)){
  1224. return Result::error('暂无数据');
  1225. }
  1226. return Result::success($result);
  1227. }
  1228. public function getComponentInfo(array $data): array
  1229. {
  1230. $where = [];
  1231. $result = Component::where($where)
  1232. ->leftJoin('template', 'template.id', '=', 'component.template_id')
  1233. ->leftJoin('template_class', 'template_class.id', '=', 'template.template_class_id') // 添加这一行
  1234. ->leftJoin('sector', 'sector.id', '=', 'component.sector_id')
  1235. ->select('template_class.name as template_class_name', 'template.template_name as template_name', 'sector.sector_name as sector_name', 'component.*')
  1236. ->get();
  1237. return Result::success($result);
  1238. }
  1239. public function addComponent(array $data): array
  1240. {
  1241. unset($data['user_id']);
  1242. $sector = Sector::where('sector_id',$data['sector_id'])->first();
  1243. if(empty($sector)){
  1244. return Result::error('通栏不存在!');
  1245. }
  1246. $component_id = Component::where('component_type',$data['component_type'])->first();
  1247. if(!empty($component_id)){
  1248. return Result::error('组件编号已存在!');
  1249. }
  1250. $component_place = SectorPlace::where('sector_type',$sector['place_type'])
  1251. ->where('type_id',2)->where('sort_id',$data['sort_id'])->first();
  1252. // return Result::success($component_place);
  1253. // 组件分类:2:资讯-轮播组件;3:资讯-推荐类组件;4:资讯-热点类组件;5:广告类组件;
  1254. // 6:列表类组件;7:详情类组件;8:底部导航类组件;9:栏目资讯类组件;10:最新资讯类;11:导航栏类;',
  1255. if(is_array($data['page_type']) && !empty($data['page_type'])){
  1256. $page_type = array_values(array_map('intval',$data['page_type']));
  1257. $data['page_type'] = json_encode($page_type,true);
  1258. }
  1259. $data['type_id'] = intval($data['type_id']);
  1260. $add_arr = [
  1261. 'template_id' => $data['template_id'],
  1262. 'sector_id' => $data['sector_id'],
  1263. 'page_type' => $data['page_type'],
  1264. 'component_type' => $data['component_type'],
  1265. 'component_name' => $data['component_name'],
  1266. 'component_img' => $data['component_img'],
  1267. 'place_type' => $component_place['component_type'],
  1268. 'size_id' => $component_place['size_id'],
  1269. 'type_id' => $data['type_id'],
  1270. 'sort_id' => $data['sort_id'],
  1271. ];
  1272. switch($data['type_id']){
  1273. case 1: //1:资讯-头条组件;
  1274. case 2: //2:资讯-轮播组件;
  1275. case 3: //3:资讯-推荐类组件;
  1276. case 4: //4:资讯-热点类组件;
  1277. case 10: //10:最新资讯类;
  1278. $add_arr['level'] = $data['level'];
  1279. $add_arr['img_num'] = $data['img_num'];
  1280. $add_arr['text_num'] = $data['text_num'];
  1281. break;
  1282. case 5:
  1283. $add_arr['img_num'] = $data['img_num'];
  1284. $add_arr['text_num'] = $data['text_num'];
  1285. break;
  1286. case 7: //7:列表类组件;
  1287. $add_arr['pageSize'] = $data['pageSize'];
  1288. break;
  1289. case 11: //11:导航栏类;
  1290. $add_arr['cate_place'] = 0;
  1291. $add_arr['pageSize'] = $data['pageSize'];
  1292. break;
  1293. default:
  1294. break;
  1295. }
  1296. return Result::success($add_arr);
  1297. // $result = Component::insertGetId($data);
  1298. if ($result) {
  1299. return Result::success($result);
  1300. } else {
  1301. return Result::error('添加失败');
  1302. }
  1303. }
  1304. public function delComponent(array $data): array
  1305. {
  1306. $result = Component::where('id', $data['id'])->delete();
  1307. return Result::success($result);
  1308. }
  1309. public function updateComponent(array $data): array
  1310. {
  1311. $result = Component::where('id', $data['id'])->update($data);
  1312. return Result::success($result);
  1313. }
  1314. public function getWebsiteTemplateInfo(array $data)
  1315. {
  1316. $where = [];
  1317. if (isset($data['id'])) {
  1318. $where[] = ['id', '=', $data['id']];
  1319. }
  1320. $result = WebsiteTemplateInfo::where($where)
  1321. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  1322. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  1323. ->select('website_template_info.*', 'website.website_name')
  1324. ->get();
  1325. if ($result) {
  1326. return Result::success($result);
  1327. } else {
  1328. return Result::error('暂无数据');
  1329. }
  1330. }
  1331. /**
  1332. * 自助建站----1.获取页面类型回显
  1333. * @param array $data
  1334. * @return array
  1335. */
  1336. public function getWebPageType(array $data): array
  1337. {
  1338. // 1:图片 2:文字 3:底部
  1339. $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get();
  1340. if (empty($friend_link->toArray())) {
  1341. return Result::error('暂无友情链接数据!');
  1342. }
  1343. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  1344. if (empty($footer_category->toArray())) {
  1345. return Result::error('暂无底部导航数据!');
  1346. }
  1347. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  1348. if (empty($page_type)) {
  1349. $page_type = [];
  1350. } else {
  1351. $page_type = json_decode($page_type, true);
  1352. }
  1353. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1354. if (isset($page_type) && in_array(5, $page_type)) {
  1355. $page_type = 1;
  1356. } else {
  1357. $page_type = 0;
  1358. }
  1359. $result = [
  1360. 'friend_link' => $friend_link,
  1361. 'footer_category' => $footer_category,
  1362. 'page_type' => $page_type,
  1363. ];
  1364. return Result::success($result);
  1365. }
  1366. /**
  1367. * 自助建站----2.添加页面类型
  1368. * @param array $data
  1369. * @return array
  1370. */
  1371. public function addWebPageType(array $data): array
  1372. {
  1373. // return Result::success($data);
  1374. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  1375. if ($data['is_search'] == 1) {
  1376. $page_type = json_encode([1, 2, 3, 4, 5, 6, 7]);
  1377. } else {
  1378. $page_type = json_encode([1, 2, 3, 4, 6, 7]);
  1379. }
  1380. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1381. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1382. // return Result::success($website_template_info);
  1383. // return Result::success(empty($website_template_info));
  1384. if (empty($website_template_info)) {
  1385. $result = WebsiteTemplateInfo::insertGetId(['website_id' => $data['website_id'], 'page_type' => $page_type, 'user_id' => $data['user_id']]);
  1386. } else {
  1387. if ($website_template_info['status'] == 2) {
  1388. return Result::error('网站已应用,不可再次修改!');
  1389. }
  1390. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type], ['action_id' => 1, 'user_id' => $data['user_id']]);
  1391. }
  1392. if ($result) {
  1393. return Result::success($result);
  1394. } else {
  1395. return Result::error('添加失败');
  1396. }
  1397. }
  1398. /**
  1399. * 自助建站---流程---3.选择皮肤列表
  1400. * @param array $data
  1401. * @return array
  1402. */
  1403. public function getWebsiteTemplateList(array $data): array
  1404. {
  1405. $where = [];
  1406. if (isset($data['template_class_id']) && $data['template_class_id']) {
  1407. $where[] = ['template_class_id', '=', $data['template_class_id']];
  1408. }
  1409. // 0:未构建 1:未应用 2:已应用 status
  1410. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1411. if (empty($templste_info)) {
  1412. return Result::error('请先填写网站基础信息!');
  1413. }
  1414. if ($templste_info['status'] == 2) {
  1415. return Result::error('网站已应用,不可再次修改!');
  1416. }
  1417. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1418. if (is_array($data['keyword'])) {
  1419. // 拼接多个模糊查询条件
  1420. $query = Template::query();
  1421. foreach ($data['keyword'] as $kw) {
  1422. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  1423. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  1424. }
  1425. } else {
  1426. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  1427. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  1428. }
  1429. } else {
  1430. $query = Template::where($where);
  1431. }
  1432. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  1433. // return Result::success($template_id);
  1434. if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) {
  1435. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  1436. } else {
  1437. $template_page = [];
  1438. }
  1439. if (!empty($template_id['template_id'])) {
  1440. $result['template_id'] = $templste_info['template_id'];
  1441. }
  1442. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  1443. $template_page_str = json_encode($template_page);
  1444. $result['template'] = $query->where($where)
  1445. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  1446. ->orderBy('updated_at', 'desc')
  1447. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1448. if (empty($result['template'])) {
  1449. $result['template'] = Template::orderBy('updated_at', 'desc')
  1450. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1451. }
  1452. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  1453. if ($template) {
  1454. $result['template_id'] = $template;
  1455. } else {
  1456. $result['template_id'] = 0;
  1457. }
  1458. return Result::success($result);
  1459. }
  1460. /**
  1461. * 自助建站---流程---4.选择皮肤
  1462. * @param array $data
  1463. * @return array
  1464. */
  1465. public function chooseWebsiteTemplate(array $data): array
  1466. {
  1467. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1468. if (empty($website_template_info)) {
  1469. return Result::error('请先填写网站基础信息!');
  1470. }
  1471. // 0:未构建 1:未应用 2:已应用 status
  1472. if ($website_template_info['status'] == 2) {
  1473. return Result::error('网站已应用,不可再次修改!');
  1474. }
  1475. $template = Template::where('template_id', $data['template_id'])->first();
  1476. if (empty($template)) {
  1477. return Result::error('未查询到皮肤!');
  1478. }
  1479. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1480. $web_template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1481. if ($web_template) {
  1482. return Result::success($web_template);
  1483. } else {
  1484. return Result::error('选择皮肤失败!');
  1485. }
  1486. }
  1487. /**
  1488. * 自助建站---流程---5.保存模版
  1489. * @param array $data
  1490. * @return array
  1491. */
  1492. public function saveWebsiteTemplate(array $data): array
  1493. {
  1494. // return Result::success($data);
  1495. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1496. if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
  1497. return Result::error('请先填写网站基础信息!');
  1498. }
  1499. // 0:未构建 1:未应用 2:已应用 status
  1500. if ($website_template_info['status'] == 2) {
  1501. return Result::error('网站已应用,不可再次保存!');
  1502. }
  1503. if ($website_template_info['action_id'] != 2) {
  1504. return Result::error('请完成之前的步骤!');
  1505. }
  1506. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  1507. // var_dump($data['template_data']);
  1508. try {
  1509. Db::beginTransaction();
  1510. if (empty($template_info)) {
  1511. $template = WebsiteTemplate::insertGetId([
  1512. 'website_id' => $data['website_id'],
  1513. 'template_data' => $data['template_data'],
  1514. 'user_id' => $data['user_id'],
  1515. 'canvas_data' => $data['canvas_data'],
  1516. 'page_type' => $website_template_info['page_type'],
  1517. 'template_id' => $website_template_info['template_id'],
  1518. ]);
  1519. } else {
  1520. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  1521. [
  1522. 'template_data' => $data['template_data'],
  1523. 'user_id' => $data['user_id'],
  1524. 'canvas_data' => $data['canvas_data'],
  1525. 'page_type' => $website_template_info['page_type'],
  1526. 'template_id' => $website_template_info['template_id'],
  1527. ]
  1528. );
  1529. }
  1530. if (empty($template)) {
  1531. Db::rollback();
  1532. return Result::error('保存失败!');
  1533. } else {
  1534. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  1535. if (empty($tempalte_info)) {
  1536. Db::rollback();
  1537. return Result::error('保存失败!');
  1538. }
  1539. Db::commit();
  1540. }
  1541. } catch (\Exception $e) {
  1542. return Result::error('模版数据格式错误!');
  1543. }
  1544. return Result::success($template);
  1545. }
  1546. /**
  1547. * 自助建站---流程---6.返显保存的模版
  1548. * @param array $data
  1549. * @return array
  1550. */
  1551. public function getWebsiteTemplate(array $data): array
  1552. {
  1553. $web = Website::where('id', $data['website_id'])->first();
  1554. if (empty($web)) {
  1555. return Result::error('未查询到网站信息!');
  1556. }
  1557. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  1558. ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
  1559. if (empty($template)) {
  1560. return Result::error('未查询到模版!');
  1561. }
  1562. return Result::success($template);
  1563. }
  1564. /**
  1565. * 通栏版式管理-获取通栏版式列表
  1566. */
  1567. public function getSectorPlaceList(array $data): array
  1568. {
  1569. $where['sector_place.type_id'] = $data['type_id'];
  1570. if(isset($data['component_num']) && !empty($data['component_num'])){
  1571. $where['sector_place.component_num'] = $data['component_num'];
  1572. }
  1573. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  1574. $where['sector_place.sector_type'] = $data['sector_type'];
  1575. }
  1576. // if(isset($data['component_type']) && !empty($data['component_type'])){
  1577. // $where['sector_place.component_type'] = $data['component_type'];
  1578. // }
  1579. if(isset($data['name']) && !empty($data['name'])){
  1580. array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']);
  1581. }
  1582. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
  1583. $size_id = Size::where('width','like','%' .$data['width'].'%')->where('height','like','%' .$data['height'].'%')->pluck('id');
  1584. } else if( isset($data['width']) && !empty($data['width'])){
  1585. $size_id = Size::where('width','like','%' .$data['width'].'%')->pluck('id');
  1586. }else if( isset($data['height']) && !empty($data['height'])){
  1587. $size_id = Size::where('height','like','%' .$data['height'].'%')->pluck('id');
  1588. }else{
  1589. $size_id = [];
  1590. }
  1591. $sector_place = SectorPlace::where($where)
  1592. ->when(!empty($size_id), function ($query) use ($size_id) {
  1593. $query->whereIn('sector_place.size_id', $size_id);
  1594. })
  1595. ->leftJoin('size','sector_place.size_id','=','size.id')
  1596. ->select('sector_place.*','size.width','size.height');
  1597. $result['count'] = $sector_place->count();
  1598. $result['row'] =$sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1599. if(empty($result['count'])){
  1600. return Result::error("暂无相关版式数据!");
  1601. }
  1602. return Result::success($result);
  1603. }
  1604. /**
  1605. * 通栏版式管理-添加通栏版式
  1606. */
  1607. public function addSectorPlace(array $data): array
  1608. {
  1609. // 1:通栏;2:组件;
  1610. if($data['type_id'] == 1){
  1611. $sector_place = SectorPlace::where('sector_type', $data['type'])->first();
  1612. if(!empty($sector_place)){
  1613. return Result::error('通栏版式类别编号已存在!');
  1614. }
  1615. $data = [
  1616. 'name' => $data['name'],
  1617. 'component_num' => $data['component_num'],
  1618. 'size_id' => $data['size_id'],
  1619. 'sort_id' => 0,
  1620. 'sector_img' => $data['sector_img'],
  1621. 'sector_type' => $data['type'],
  1622. 'type_id' => $data['type_id'],
  1623. 'status' => 0,
  1624. ];
  1625. }else{
  1626. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first();
  1627. if(empty($sector_type)){
  1628. return Result::error('通栏版式类别不存在!');
  1629. }
  1630. // $com_query = SectorPlace::where('type_id',2);
  1631. // ->query(function($query) use ($data){
  1632. // $query->where('component_type', $data['type'])
  1633. // ->Subquery(function($subQuery) use ($data) {
  1634. // $subQuery->where('sector_type', $data['sector_type'])
  1635. // ->where('sort_id', $data['sort']);
  1636. // });
  1637. // }) //若是组件版式编号或者组件版式位置重复用此语句筛选
  1638. // ->where('sector_type',$data['sector_type']) //若是组件版式编号在同一个通栏版式下不重复就行,则启用此语句
  1639. $component = SectorPlace::where('type_id',2)->where('component_type',$data['type'])->first();
  1640. if(!empty($component)){
  1641. return Result::error('组件版式类别编号已存在!');
  1642. }
  1643. // $com_query = clone $com_query;
  1644. $com_num = SectorPlace::where('type_id',2)->where('sector_type',$data['sector_type'])->count('sort_id');
  1645. // return Result::success($com_num);
  1646. if($sector_type['component_num'] = $com_num){
  1647. $sector = SectorPlace::where('sector_type',$data['sector_type'])->where('type_id',1)->update(['status'=>1]);
  1648. if(empty($sector)){
  1649. return Result::error('通栏版式状态修改失败!');
  1650. }
  1651. }
  1652. $data = [
  1653. 'name' => $data['name'],
  1654. 'component_num' => 0,
  1655. 'size_id' => $data['size_id'],
  1656. 'sort_id' => $data['sort_id'],
  1657. 'sector_img' => $data['sector_img'],
  1658. 'component_type' => $data['type'],
  1659. 'type_id' => $data['type_id'],
  1660. 'sector_type' => $data['sector_type'],
  1661. ];
  1662. }
  1663. $result = SectorPlace::insertGetId($data);
  1664. if(empty($result)){
  1665. return Result::error('添加失败!');
  1666. }
  1667. return Result::success('添加成功!',$result);
  1668. }
  1669. /**
  1670. * 通栏版式管理-修改通栏版式
  1671. */
  1672. public function upSectorPlace(array $data): array
  1673. {
  1674. $id = $data['id'];
  1675. // 1:通栏;2:组件;
  1676. if($data['type_id'] == 1){
  1677. $sector_place = SectorPlace::where('id','!=',$id)->where('sector_type', $data['type'])->where('type_id',1)->first();
  1678. if(!empty($sector_place)){
  1679. return Result::error('通栏版式类别编号已存在!');
  1680. }
  1681. $data = [
  1682. 'name' => $data['name'],
  1683. 'component_num' => $data['component_num'],
  1684. 'size_id' => $data['size_id'],
  1685. 'sort_id' => 0,
  1686. 'sector_img' => $data['sector_img'],
  1687. 'sector_type' => $data['type'],
  1688. 'type_id' => $data['type_id'],
  1689. ];
  1690. }else{
  1691. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first();
  1692. if(empty($sector_type)){
  1693. return Result::error('通栏版式类别不存在!');
  1694. }
  1695. $component = SectorPlace::where('id','!=',$id)
  1696. ->where('component_type', $data['type'])
  1697. ->where('type_id',2)
  1698. ->first();
  1699. if(!empty($component)){
  1700. return Result::error('组件版式类别编号已存在!');
  1701. }
  1702. $data = [
  1703. 'name' => $data['name'],
  1704. 'component_num' => 0,
  1705. 'size_id' => $data['size_id'],
  1706. 'sort_id' => $data['sort_id'],
  1707. 'sector_img' => $data['sector_img'],
  1708. 'component_type' => $data['type'],
  1709. 'type_id' => $data['type_id'],
  1710. 'sector_type' => $data['sector_type'],
  1711. ];
  1712. }
  1713. $result = SectorPlace::where('id',$id)->update($data);
  1714. if(empty($result)){
  1715. return Result::error('修改失败!');
  1716. }
  1717. return Result::success('修改成功!',$result);
  1718. }
  1719. /**
  1720. * 通栏版式管理-删除通栏版式
  1721. */
  1722. public function delSectorPlace(array $data): array
  1723. {
  1724. $sector_place = SectorPlace::where('id',$data['id'])->first();
  1725. if(empty($sector_place)){
  1726. return Result::error('通栏版式不存在!');
  1727. }
  1728. if($sector_place['type_id'] == 1){
  1729. $sector = Sector::where('place_type', $sector_place['sector_type'])->first();
  1730. $component = SectorPlace::where('sector_type', $sector_place['sector_type'])
  1731. ->where('type_id',2)
  1732. ->first();
  1733. if(!empty($sector) || !empty($component)){
  1734. return Result::error('通栏版式分类已应用,暂不可删除!');
  1735. }
  1736. }else{
  1737. $sector = Component::where('place_type', $sector_place['component_type'])->first();
  1738. if(!empty($sector)){
  1739. return Result::error('组件版式分类已应用,暂不可删除!');
  1740. }
  1741. $component_num = SectorPlace::where('sector_type', $sector_place['sector_type'])
  1742. ->where('type_id',1)->update(['status'=> 0]);
  1743. if(empty($component_num)){
  1744. return Result::error('通栏版式状态修改失败!');
  1745. }
  1746. }
  1747. $result = SectorPlace::where('id',$data['id'])->delete();
  1748. if(empty($result)){
  1749. return Result::error('删除失败!');
  1750. }
  1751. return Result::success('删除成功!');
  1752. }
  1753. /**
  1754. * 自助建站-通栏版式管理-获取通栏版式详情
  1755. */
  1756. public function getSectorPlaceInfo(array $data): array
  1757. {
  1758. $result = SectorPlace::where('sector_place.id',$data['id'])
  1759. ->leftJoin('size','sector_place.size_id','=','size.id')
  1760. ->select('sector_place.*','size.width','size.height')
  1761. ->first();
  1762. if(empty($result)){
  1763. return Result::error('通栏版式不存在!');
  1764. }
  1765. return Result::success($result);
  1766. }
  1767. /**
  1768. * 自助建站-通栏版式管理-获取通栏版式列表
  1769. */
  1770. public function getSectorPlaceSort(array $data): array
  1771. {
  1772. $sector = SectorPlace::where('sector_type',$data['sector_type'])->first();
  1773. if(empty($sector)){
  1774. return Result::error('通栏版式不存在!');
  1775. }
  1776. $component = SectorPlace::where('type_id',2)->where('sector_type',$sector['sector_type'])->pluck('sort_id')->toArray();
  1777. if(count($component) < $sector['component_num']){
  1778. // 根据 $sector['component_num'] 的值生成对应元素数量的数组
  1779. if (isset($sector['component_num']) && is_numeric($sector['component_num'])) {
  1780. $num = intval($sector['component_num']);
  1781. if ($num > 0 && $num <= 10) {
  1782. // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
  1783. $sector_arr = range(1, $num);
  1784. } else {
  1785. $sector_arr = [];
  1786. }
  1787. } else {
  1788. $sector_arr = [];
  1789. }
  1790. // 获取 $sector_arr 中不在 $component 里的元素
  1791. $sort = array_diff($sector_arr, $component);
  1792. // return Result::success($sort);
  1793. }
  1794. if(!empty($sort)){
  1795. $sort = array_values(array_map(function($value) {
  1796. return ['id' => $value];
  1797. }, $sort));
  1798. }else{
  1799. return Result::error("组件版式数量已达上限!");
  1800. }
  1801. return Result::success($sort);
  1802. }
  1803. /**
  1804. * 获取所有通栏版式
  1805. */
  1806. public function getAllSectorPlace(array $data): array
  1807. {
  1808. $where['type_id'] = $data['type_id'];
  1809. if($data['type_id'] == 1){
  1810. $where['status'] = 1;
  1811. }
  1812. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  1813. $where['sector_type'] = $data['sector_type'];
  1814. }
  1815. if(isset($data['component_num']) && !empty($data['component_num'])){
  1816. $where['component_num'] = $data['component_num'];
  1817. }
  1818. $sector_place = SectorPlace::where($where)
  1819. ->leftJoin('size','sector_place.size_id','=','size.id')
  1820. ->select('sector_place.*','size.width','size.height')
  1821. ->get()->all();
  1822. if(empty($sector_place)){
  1823. return Result::error('通栏版式不存在!');
  1824. }
  1825. return Result::success($sector_place);
  1826. }
  1827. }