PublicRpcService.php 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314
  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. use App\Model\ComponentType;
  36. use App\Model\Article;
  37. use App\Model\WebsiteCategory;
  38. use App\Model\AdPlace;
  39. use App\Model\WebsiteImg;
  40. use App\Model\SectorComponent;
  41. use App\Model\ComponentImg;
  42. use App\Model\TemplateRule;
  43. use App\Model\SectorType;
  44. #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  45. class PublicRpcService implements PublicRpcServiceInterface
  46. {
  47. #[Inject]
  48. protected Redis $redis;
  49. /**
  50. * @param array $data
  51. * @return array
  52. */
  53. public function getDistrictList(array $data): array
  54. {
  55. $where = [];
  56. if (isset($data['keyWord'])) {
  57. $where = [
  58. ['name', 'like', '%' . $data['keyWord'] . '%']
  59. ];
  60. }
  61. $result = [];
  62. if (isset($data['pageSize'])) {
  63. $rep = District::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get();
  64. $count = District::where($where)->count();
  65. $result = [
  66. 'rows' => $rep,
  67. 'count' => $count
  68. ];
  69. } else {
  70. $result = District::where($data)->orderBy("code", "asc")->get();
  71. }
  72. return $result ? Result::success($result) : Result::error("没有查到数据");
  73. }
  74. /**
  75. * @param array $data
  76. * @return array
  77. */
  78. public function getUserLevelList(array $data): array
  79. {
  80. $where = [];
  81. if (isset($data['keyWord'])) {
  82. $where = [
  83. ['name', 'like', '%' . $data['keyWord'] . '%'],
  84. ];
  85. }
  86. $result = [];
  87. if (isset($data['pageSize'])) {
  88. $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get();
  89. $count = UserLevel::where($where)->count();
  90. $result = [
  91. 'rows' => $rep,
  92. 'count' => $count,
  93. ];
  94. } else {
  95. $result = UserLevel::orderBy("id", "asc")->get();
  96. }
  97. return $result ? Result::success($result) : Result::error("没有查到数据");
  98. }
  99. /**
  100. * 添加用户等级
  101. * @param array $data
  102. * @return array
  103. */
  104. public function addUserLevel(array $data): array
  105. {
  106. LevelUser::insertGetId($data);
  107. return Result::success([]);
  108. }
  109. /**
  110. * 更新等级
  111. * @param array $data
  112. * @return array
  113. */
  114. public function updateUserLevel(array $data): array
  115. {
  116. $result = LevelUser::where(['id' => $data['id']])->update($data);
  117. if ($result) {
  118. return Result::success($result);
  119. }
  120. return Result::error("更新失败");
  121. }
  122. /**
  123. * 删除等级
  124. * @param array $data
  125. * @return array
  126. */
  127. public function delUserLevel(array $data): array
  128. {
  129. $result = LevelUser::where(['id' => $data['id']])->delete();
  130. if ($result) {
  131. return Result::success($result);
  132. }
  133. return Result::error("删除失败");
  134. }
  135. /**
  136. * 查询投诉举报信息
  137. * @param array $data
  138. * @return array
  139. */
  140. public function getLetterOfComplaint(array $data = []): array
  141. {
  142. var_dump("====");
  143. $where = [];
  144. if (isset($data['user_id']) && !empty($data['user_id'])) {
  145. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  146. }
  147. if (isset($data['nature']) && !empty($data['nature'])) {
  148. array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]);
  149. }
  150. if (isset($data['nature_level0']) && !empty($data['nature_level0'])) {
  151. array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]);
  152. }
  153. if (isset($data['status']) && !empty($data['status'])) {
  154. array_push($where, ['letter_of_complaint.status', '=', $data['status']]);
  155. }
  156. $result = [];
  157. if (isset($data['pageSize'])) {
  158. $rep = LetterOfComplaint::where($where)
  159. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  160. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  161. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  162. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  163. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  164. ->select(
  165. "letter_of_complaint.*",
  166. "type_a.type_name as nature_name",
  167. "type_b.type_name as nature_name1",
  168. "type_c.type_name as nature_name0",
  169. "type_d.type_name as status_name",
  170. "type_e.type_name as nature_name3"
  171. )
  172. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get();
  173. $count = LetterOfComplaint::where($where)->count();
  174. if ($rep) {
  175. foreach ($rep as $val) {
  176. if ($val['judgment']) {
  177. $val['judgment'] = json_decode($val['judgment']);
  178. }
  179. if ($val['audio_and_video']) {
  180. $val['audio_and_video'] = json_decode($val['audio_and_video']);
  181. }
  182. if ($val['contract']) {
  183. $val['contract'] = json_decode($val['contract']);
  184. }
  185. if ($val['qualifications']) {
  186. $val['qualifications'] = json_decode($val['qualifications']);
  187. }
  188. }
  189. }
  190. $result = [
  191. 'rows' => $rep,
  192. 'count' => $count,
  193. ];
  194. } else {
  195. $result = LetterOfComplaint::where($where)
  196. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  197. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  198. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  199. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  200. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  201. ->select(
  202. "letter_of_complaint.*",
  203. "type_a.type_name as nature_name",
  204. "type_b.type_name as nature_name1",
  205. "type_c.type_name as nature_name0",
  206. "type_d.type_name as status_name",
  207. "type_e.type_name as nature_name3"
  208. )
  209. ->orderBy("letter_of_complaint.id", "desc")->get();
  210. }
  211. return $result ? Result::success($result) : Result::error("没有查到数据");
  212. }
  213. /**
  214. * 添加投诉举报信息
  215. * @param array $data
  216. * @return array
  217. */
  218. public function addLetterOfComplaint(array $data): array
  219. {
  220. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  221. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  222. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  223. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  224. unset($data['id']);
  225. $result = LetterOfComplaint::insertGetId($data);
  226. if (empty($result)) {
  227. return Result::error("创建失败", 0);
  228. } else {
  229. return Result::success(["id" => $result]);
  230. }
  231. }
  232. /**
  233. * 用户端更新投诉举报
  234. * @param array $data
  235. * @return array
  236. */
  237. public function userUpLetterOfComplaint(array $data): array
  238. {
  239. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  240. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  241. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  242. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  243. $result = LetterOfComplaint::where(['id' => $data['id']])->update($data);
  244. if (empty($result)) {
  245. return Result::error("创建失败", 0);
  246. } else {
  247. return Result::success(["id" => $result]);
  248. }
  249. }
  250. /**
  251. * 管理后台更新投诉举报信息
  252. * @param array $data
  253. * @return array
  254. */
  255. public function upLetterOfComplaint(array $data): array
  256. {
  257. var_dump("admin:", $data);
  258. $where = [
  259. 'id' => $data['id'],
  260. ];
  261. $filtered_array = array_filter($data, function ($value) {
  262. return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value);
  263. });
  264. $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : '';
  265. $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : '';
  266. $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : '';
  267. $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : '';
  268. unset($filtered_array['nature_name']);
  269. unset($filtered_array['type_name']);
  270. unset($filtered_array['nature_level_name']);
  271. unset($filtered_array['status_name']);
  272. unset($filtered_array['is_admin']);
  273. unset($filtered_array['type_level_name']);
  274. $result = LetterOfComplaint::where($where)->update($filtered_array);
  275. if ($result) {
  276. return Result::success($result);
  277. }
  278. return Result::error("更新失败", 0);
  279. }
  280. /**
  281. * 查询投诉举报记录
  282. * @param array $data
  283. * @return array
  284. */
  285. public function getLetterOfComplaintInfo(array $data): array
  286. {
  287. $where = [
  288. 'letter_of_complaint.id' => $data['id'],
  289. ];
  290. if (isset($data['user_id']) && !empty($data['user_id'])) {
  291. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  292. }
  293. $result = LetterOfComplaint::where($where)
  294. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  295. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  296. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  297. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  298. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  299. ->select(
  300. "letter_of_complaint.*",
  301. "type_a.type_name as nature_name",
  302. "type_b.type_name as nature_name1",
  303. "type_c.type_name as nature_name0",
  304. "type_d.type_name as status_name",
  305. "type_e.type_name as nature_name3"
  306. )
  307. ->first();
  308. return Result::success($result);
  309. }
  310. /**
  311. * 删除投诉举报信息
  312. * @param array $data
  313. * @return array
  314. */
  315. public function delLetterOfComplaint(array $data): array
  316. {
  317. $result = LetterOfComplaint::when($data, function ($query) use ($data) {
  318. if (isset($data['id']) && !empty($data['id'])) {
  319. $query->where(['id' => $data['id']]);
  320. }
  321. if (isset($data['user_id']) && !empty($data['user_id'])) {
  322. $query->where(['user_id' => $data['user_id']]);
  323. }
  324. })->delete();
  325. if (empty($result)) {
  326. return Result::error("删除失败", 0);
  327. } else {
  328. return Result::success();
  329. }
  330. }
  331. /**
  332. * 获取举报信息类型
  333. * @param array $data
  334. * @return array
  335. */
  336. public function getLetterType(array $data): array
  337. {
  338. $where = [];
  339. if (isset($data['type'])) {
  340. array_push($where, ['type', '=', $data['type']]);
  341. }
  342. if (isset($data['pid']) && $data['pid'] > 0) {
  343. array_push($where, ['pid', '=', $data['pid']]);
  344. }
  345. $result = LetterType::where($where)->orderBy('sort', 'asc')->get();
  346. return $result ? Result::success($result) : Result::error("没有查到数据");
  347. }
  348. /**
  349. * 更新举报类型
  350. * @param array $data
  351. * @return array
  352. */
  353. public function upLetterType(array $data): array
  354. {
  355. return [];
  356. }
  357. /**
  358. * 添加举报类型
  359. * @param array $data
  360. * @return array
  361. */
  362. public function addLetterType(array $data): array
  363. {
  364. $result = LetterType::insertGetId($data);
  365. if (empty($result)) {
  366. return Result::error("创建失败", 0);
  367. } else {
  368. return Result::success(["id" => $result]);
  369. }
  370. }
  371. /**
  372. * 删除举报类型
  373. * @param array $data
  374. * @return array
  375. */
  376. public function delLetterType(array $data): array
  377. {
  378. $result = LetterType::where('id', $data['id'])->delete();
  379. if (empty($result)) {
  380. return Result::error("删除失败", 0);
  381. } else {
  382. return Result::success();
  383. }
  384. }
  385. /**
  386. * 检测是否已经被接案
  387. * @param array $data
  388. * @return array
  389. */
  390. public function checkMeasure(array $data): array
  391. {
  392. $where = [
  393. 'id' => $data['id'],
  394. ];
  395. $letterOfComplaintInfo = LetterOfComplaint::where($where)->first();
  396. var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']);
  397. //操作人和当前登陆用户id 相等说明是当前人接收的案件
  398. if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) {
  399. return Result::success();
  400. } else {
  401. return Result::error("您不能处理其他人已经接过的案件", 0);
  402. }
  403. }
  404. /**
  405. * 后台获取职能部门
  406. * @param array $data
  407. * @return array
  408. */
  409. public function getZhinengbumenList(array $data): array
  410. {
  411. // 获取分页参数,默认每页 10 条记录
  412. $page = isset($data['page']) ? (int) $data['page'] : 1;
  413. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  414. // 查询数据并分页
  415. $query = Department::query();
  416. // 可以在这里添加更多的查询条件
  417. if (isset($data['search'])) {
  418. $query->where('name', 'like', '%' . $data['search'] . '%');
  419. }
  420. // 执行分页查询
  421. $result = $query->paginate($perPage, ['*'], 'page', $page);
  422. // 返回分页结果
  423. return Result::success([
  424. 'count' => $result->total(),
  425. 'current_page' => $result->currentPage(),
  426. 'last_page' => $result->lastPage(),
  427. 'pagesize' => $result->perPage(),
  428. 'rows' => $result->items(),
  429. ]);
  430. }
  431. /**
  432. * 添加获取职能部门
  433. * @param array $data
  434. * @return array
  435. */
  436. public function addZhinengbumen(array $data): array
  437. {
  438. $result = Department::insertGetId($data);
  439. if (empty($result)) {
  440. return Result::error("创建失败", 0);
  441. } else {
  442. return Result::success(["id" => $result]);
  443. }
  444. }
  445. public function delZhinengbumen(array $data): array
  446. {
  447. $result = Department::where('id', $data['id'])->delete();
  448. if (empty($result)) {
  449. return Result::error("删除失败", 0);
  450. } else {
  451. return Result::success();
  452. }
  453. }
  454. public function getZhinengbumen(array $data): array
  455. {
  456. $result = Department::where('id', $data['id'])
  457. ->orderBy('department', 'desc')
  458. ->first();
  459. return Result::success($result);
  460. }
  461. public function getPidZhinengbumen(array $data): array
  462. {
  463. if (empty($data['pid'])) {
  464. $data['pid'] = 0;
  465. }
  466. $result = Department::where('pid', $data['pid'])->get();
  467. return Result::success($result);
  468. }
  469. public function modZhinengbumen(array $data): array
  470. {
  471. $result = Department::where('id', $data['id'])->update($data);
  472. if (empty($result)) {
  473. return Result::error("修改失败", 0);
  474. } else {
  475. return Result::success();
  476. }
  477. }
  478. /**
  479. * 查询职能列表
  480. * @param array $data
  481. * @return array
  482. */
  483. public function getDepartment(array $data): array
  484. {
  485. $where = [];
  486. if (isset($data['pid'])) {
  487. $where = [
  488. 'pid' => $data['pid'] ?? 0
  489. ];
  490. }
  491. $result = Department::when(!empty($where), function ($query) use ($where) {
  492. $query->where($where);
  493. })->orderBy("sort", "desc")->get();
  494. if (empty($result)) {
  495. return Result::error("查询失败", 0);
  496. } else {
  497. return Result::success($result);
  498. }
  499. }
  500. /**
  501. * 获取所有的buckets
  502. * @param array $data
  503. * @return array
  504. */
  505. public function getBuckets(array $data): array
  506. {
  507. $result = new MinioService();
  508. // 调用服务层的方法获取存储桶列表
  509. $bucketsResponse = $result->listBuckets();
  510. // 直接返回服务层生成的响应
  511. return Result::success($bucketsResponse['data']);
  512. }
  513. /**
  514. * 上传文件
  515. * @param array $data
  516. * @return array
  517. */
  518. public function uploadFile(array $data): array
  519. {
  520. $result = new MinioService();
  521. $rep = $result->uploadFile($data);
  522. if ($rep['code'] == 200) {
  523. return Result::success($rep['data']);
  524. } else {
  525. return Result::error("上传失败!");
  526. }
  527. }
  528. /**
  529. * 黑名单管理
  530. * @param array $data
  531. * @return array
  532. */
  533. public function getBlackWordList(array $data): array
  534. {
  535. $result = BlackWord::when($data, function ($query) use ($data) {
  536. if (isset($data['name']) && $data['name']) {
  537. $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
  538. }
  539. })->orderBy('black_word.id', 'desc')
  540. ->paginate(
  541. intval($data['pageSize']),
  542. [
  543. 'black_word.*',
  544. ],
  545. 'page',
  546. intval($data['page'])
  547. );
  548. $count = $result->total();
  549. $returnData = [
  550. 'rows' => $result->items(),
  551. 'count' => $count
  552. ];
  553. return Result::success($returnData);
  554. }
  555. /**
  556. * 添加黑名单
  557. * @param array $data
  558. * @return array
  559. */
  560. public function addBlackWord(array $data): array
  561. {
  562. Db::beginTransaction();
  563. try {
  564. $info = BlackWord::where(['name' => $data['name']])->first();
  565. if ($info) {
  566. Db::rollBack();
  567. return Result::error("该黑名单已存在", 0);
  568. }
  569. $data['type'] = 10;
  570. $result = BlackWord::insertGetId($data);
  571. $redisKey = 'black_word';
  572. $this->redis->sAdd($redisKey, $data['name']);
  573. Db::commit();
  574. return Result::success(["id" => $result]);
  575. } catch (\Exception $e) {
  576. Db::rollBack();
  577. return Result::error("创建失败" . $e->getMessage(), 0);
  578. }
  579. }
  580. /**
  581. * 删除黑名单
  582. * @param array $data
  583. * @return array
  584. */
  585. public function delBlackWord(array $data): array
  586. {
  587. Db::beginTransaction();
  588. try {
  589. BlackWord::where(['name' => $data['name']])->delete();
  590. $redisKey = 'black_word';
  591. $this->redis->sRem($redisKey, $data['name']);
  592. Db::commit();
  593. return Result::success([]);
  594. } catch (\Exception $e) {
  595. Db::rollBack();
  596. return Result::error("删除失败" . $e->getMessage(), 0);
  597. }
  598. }
  599. /**
  600. * 修改违禁词
  601. * @param array $data
  602. * @return array
  603. */
  604. public function upBlackWord(array $data): array
  605. {
  606. Db::beginTransaction();
  607. try {
  608. $checkInfo = BlackWord::where(['name' => $data['name']])->first();
  609. if ($checkInfo) {
  610. Db::rollBack();
  611. return Result::error("该违禁词已经存在", 0);
  612. }
  613. $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
  614. if ($blackWordInfo) {
  615. //先删除redis
  616. $blackWordInfo = $blackWordInfo->toArray();
  617. $redisKey = 'black_word';
  618. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  619. $this->redis->sAdd($redisKey, $data['name']);
  620. BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
  621. Db::commit();
  622. return Result::success([]);
  623. } else {
  624. Db::rollBack();
  625. return Result::error("系统错误", 0);
  626. }
  627. } catch (\Exception $e) {
  628. Db::rollBack();
  629. return Result::error("修改失败" . $e->getMessage(), 0);
  630. }
  631. }
  632. /**
  633. * 获取风格
  634. * @return void
  635. */
  636. public function getTemplateClassList(array $data): array
  637. {
  638. $where = [];
  639. if (isset($data['name']) && $data['name']) {
  640. array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
  641. }
  642. if (isset($data['keyword']) && $data['keyword']) {
  643. array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']);
  644. }
  645. $template = TemplateClass::when($where, function ($query) use ($where) {
  646. $query->where($where);
  647. });
  648. $count = $template->count();
  649. // $countQuery = clone $template;
  650. $row = $template
  651. ->leftJoin('template', function ($join) {
  652. $join->on('template_class.class_id', '=', 'template.template_class_id');
  653. })
  654. ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count'))
  655. ->groupBy('template_class.id')
  656. ->orderBy('template_class.id', 'desc')
  657. ->offset(($data['page'] - 1) * $data['pageSize'])
  658. ->limit($data['pageSize'])
  659. ->get();
  660. $result = [
  661. 'rows' => $row,
  662. 'count' => $count,
  663. ];
  664. if ($row->isEmpty()) {
  665. return Result::error("暂无风格", 0);
  666. } else {
  667. return Result::success($result);
  668. }
  669. }
  670. /**
  671. * 添加风格
  672. * @param
  673. * @return void
  674. */
  675. public function addTemplateClass(array $data): array
  676. {
  677. $data['keyword'] = json_encode($data['keyword']);
  678. $template_class = TemplateClass::where('name', $data['name'])
  679. ->orWhere('class_id', $data['class_id'])
  680. ->first();
  681. if ($template_class) {
  682. return Result::error("风格名称或者风格编号已存在,不可添加!", 0);
  683. }
  684. $result = TemplateClass::insertGetId($data);
  685. if (empty($result)) {
  686. return Result::error("创建风格失败", 0);
  687. } else {
  688. return Result::success(["id" => $result]);
  689. }
  690. }
  691. /**
  692. * 更新风格
  693. * @param array $data
  694. * @return array
  695. */
  696. public function upTemplateClass(array $data): array
  697. {
  698. $where = [
  699. 'id' => $data['id'],
  700. ];
  701. $template_class = TemplateClass::where($where)->first();
  702. if (empty($template_class)) {
  703. return Result::error("未查询到风格", 0);
  704. }
  705. if ($template_class->type == 1) {
  706. return Result::error("默认风格不能修改", 0);
  707. }
  708. $template = TemplateClass::where('id', '!=', $data['id'])
  709. // ->where(['name' => $data['name']])
  710. // ->orWhere(['class_id' => $data['class_id']])
  711. ->where(function ($query) use ($data) {
  712. $query->where('name', $data['name'])
  713. ->orWhere('class_id', $data['class_id']);
  714. })
  715. ->first();
  716. if ($template) {
  717. return Result::error("风格名称或者风格编号已存在,不可编辑!", 0);
  718. }
  719. $updateData = [
  720. 'name' => $data['name'],
  721. 'keyword' => json_encode($data['keyword']),
  722. 'class_id' => $data['class_id'],
  723. ];
  724. $result = TemplateClass::where($where)->update($updateData);
  725. if (empty($result)) {
  726. return Result::error("更新失败", 0);
  727. } else {
  728. return Result::success($result);
  729. }
  730. }
  731. /**
  732. * 删除风格
  733. * @param array $data
  734. * @return array
  735. */
  736. public function delTemplateClass(array $data): array
  737. {
  738. $where = [
  739. 'id' => $data['id'],
  740. ];
  741. $template_class = TemplateClass::where($where)->first();
  742. if (empty($template_class)) {
  743. return Result::error("未查询到风格", 0);
  744. }
  745. if ($template_class->type == 1) {
  746. return Result::error("默认风格不能删除", 0);
  747. }
  748. $template = Template::where('template_class_id', $template_class['class_id'])->get();
  749. if (!empty($template->all())) {
  750. try {
  751. // 默认风格 1
  752. Db::beginTransaction();
  753. $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]);
  754. if (empty($template)) {
  755. Db::rollBack();
  756. return Result::error("删除失败", 0);
  757. }
  758. $result = TemplateClass::where($where)->delete();
  759. } catch (\Exception $e) {
  760. return Result::error("删除失败" . $e->getMessage(), 0);
  761. }
  762. } else {
  763. $result = TemplateClass::where('id', $data['id'])->delete();
  764. }
  765. if (empty($result)) {
  766. return Result::error("删除失败", 0);
  767. }
  768. return Result::success($result);
  769. }
  770. /**
  771. * 根据风格名称-获取获取所有风格
  772. * @param array $data
  773. * @return array
  774. */
  775. public function getTemplateClass(array $data): array
  776. {
  777. $where = [];
  778. if (!empty($data['template_name'])) {
  779. $where['name'] = $data['template_name'];
  780. }
  781. $result = TemplateClass::where($where)->get();
  782. if (empty($result)) {
  783. return Result::error("暂无风格", 0);
  784. }
  785. return Result::success($result);
  786. }
  787. /**
  788. * 获取皮肤列表
  789. * @param array $data
  790. * @return array
  791. */
  792. public function getTemplateList(array $data): array
  793. {
  794. $where = [];
  795. if (!empty($data['template_class_id'])) {
  796. $where['template_class_id'] = $data['template_class_id'];
  797. }
  798. if (!empty($data['template_name'])) {
  799. array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']);
  800. }
  801. if (!empty($data['template_keyword'])) {
  802. array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']);
  803. }
  804. $result = Template::where($where)
  805. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  806. ->select('template.*', 'template_class.name as template_class_name')
  807. ->orderBy('template.id', 'desc')
  808. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  809. if (empty($result)) {
  810. return Result::error("暂无皮肤", 0);
  811. }
  812. return Result::success($result);
  813. }
  814. public function getTemplateInfo(array $data): array
  815. {
  816. $result = Template::where('template.id', $data['id'])
  817. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  818. ->select('template.*', 'template_class.name as template_class_name')
  819. ->first();
  820. if (empty($result)) {
  821. return Result::error("暂无皮肤", 0);
  822. }
  823. return Result::success($result);
  824. }
  825. /**
  826. * 添加皮肤
  827. * @param array $data
  828. * @return array
  829. */
  830. public function addTemplate(array $data): array
  831. {
  832. var_dump($data);
  833. unset($data['user_id']);
  834. $template = Template::where('template_name', $data['template_name'])
  835. ->orWhere('template_id', $data['template_id'])
  836. ->first();
  837. if ($template) {
  838. return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
  839. }
  840. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  841. $page_type = json_decode($data['page_type'], true);
  842. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  843. $result = Template::insertGetId($data);
  844. if (empty($result)) {
  845. return Result::error("创建失败", 0);
  846. } else {
  847. return Result::success($result);
  848. }
  849. }
  850. /**
  851. * 删除皮肤
  852. * @param array $data
  853. * @return array
  854. */
  855. public function delTemplate(array $data): array
  856. {
  857. $rector = Sector::where('template_id', $data['id'])->first();
  858. if (!empty($rector)) {
  859. return Result::error("此皮肤已被绑定通栏,不可删除", 0);
  860. }
  861. $template = Template::where('id', $data['id'])->first();
  862. if(empty($template)){
  863. return Result::error("此皮肤不存在!", 0);
  864. }
  865. $web_template = WebsiteTemplateInfo::where('template_id', $template['template_id'])->first();
  866. if (!empty($web_template)) {
  867. return Result::error("此皮肤已被绑定网站,不可删除", 0);
  868. }
  869. // if ($template->type == 1) {
  870. // return Result::error("默认皮肤不能删除", 0);
  871. // }
  872. $component_img = ComponentImg::where('template_id',$template['template_id'])->get();
  873. if(!empty($component_img)){
  874. return Result::error('此皮肤已绑定组件预览图,不可删除!');
  875. }
  876. $result = Template::where('id', $data['id'])->delete();
  877. var_dump($result, '-------------------delete');
  878. if ($result) {
  879. return Result::success($result);
  880. } else {
  881. return Result::error("删除失败", 0);
  882. }
  883. }
  884. /**
  885. * 更新皮肤
  886. * @param array $data
  887. * @return array
  888. */
  889. public function updateTemplate(array $data): array
  890. {
  891. unset($data['user_id']);
  892. $template = Template::where('id', $data['id'])
  893. ->first();
  894. if (empty($template)) {
  895. return Result::error("此皮肤不存在!", 0);
  896. }
  897. $template = Template::where('id', '!=', $data['id'])
  898. ->where(function ($query) use ($data) {
  899. $query->where('template_name', $data['template_name'])
  900. ->orWhere('template_id', $data['template_id']);
  901. })
  902. ->first();
  903. if ($template) {
  904. return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
  905. }
  906. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  907. $page_type = json_decode($data['page_type'], true);
  908. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  909. $result = Template::where('id', $data['id'])->update($data);
  910. var_dump($result, '-------------------update');
  911. if (!$result) {
  912. return Result::error("更新失败", 0);
  913. } else {
  914. return Result::success('更新成功');
  915. }
  916. }
  917. /**
  918. * 获取所有尺寸
  919. * @param array $data
  920. * @return array
  921. */
  922. public function getAllSize(array $data): array
  923. {
  924. if(isset($data['id']) && !empty($data['id'])){
  925. $result = Size::where('id', $data['id'])->get()->all();
  926. }else{
  927. $result = Size::get()->all();
  928. }
  929. if(empty($result)){
  930. return Result::error("暂无尺寸", 0);
  931. }
  932. return Result::success($result);
  933. }
  934. /**
  935. * 根据皮肤名称-获取所有皮肤
  936. * @param array $data
  937. * @return array
  938. */
  939. public function getAllTemplate(array $data): array
  940. {
  941. $where = [];
  942. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  943. $where[] = ['template_class_id', $data['template_class_id']];
  944. }
  945. if (isset($data['template_name']) && !empty($data['template_name'])) {
  946. $where[] = ['template_name', 'like', '%' . $data['template_name'] . '%'];
  947. }
  948. $result = Template::where($where)->get()->all();
  949. if (empty($result)) {
  950. return Result::error("暂无皮肤", 0);
  951. }
  952. return Result::success($result);
  953. }
  954. /**
  955. * 获取通栏列表
  956. * @param array $data
  957. * @return array
  958. */
  959. public function getSectorList(array $data): array
  960. {
  961. $where = [];
  962. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  963. $where['template.template_class_id'] = $data['template_class_id'];
  964. }
  965. if (isset($data['template_id']) && !empty($data['template_id'])) {
  966. array_push($where, ['template.template_id', $data['template_id']]);
  967. }
  968. if (isset($data['sector_name']) && !empty($data['sector_name'])) {
  969. array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
  970. }
  971. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  972. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  973. } else if (isset($data['width']) && !empty($data['width'])) {
  974. $size_id = Size::where('width', 'like', '%' . $data['width'] . '%')->pluck('id');
  975. } else if (isset($data['height']) && !empty($data['height'])) {
  976. $size_id = Size::where('height', 'like', '%' . $data['height'] . '%')->pluck('id');
  977. } else {
  978. $size_id = [];
  979. }
  980. if(isset($data['page_type']) && !empty($data['page_type'])){
  981. array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']);
  982. }
  983. $result = Sector::when(!empty($size_id), function ($query) use ($size_id) {
  984. $query->whereIn('sector.size_id', $size_id);
  985. })
  986. ->leftJoin('size', 'size.id', '=', 'sector.size_id')
  987. ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  988. ->leftJoin('template_class', 'template_class.class_id', '=', 'template.template_class_id')
  989. ->leftJoin('sector_place', 'sector_place.sector_type', '=', 'sector.place_type')
  990. ->where($where)
  991. ->where('sector_place.type_id', 1)
  992. ->select('sector.*', 'size.width', 'size.height', 'template_class.name as class_name', 'template_class.class_id', 'template.template_name',
  993. 'sector_place.name as place_name', 'sector_place.sector_img as place_img')
  994. ->orderBy('sector.id', 'desc')
  995. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  996. if(empty($result)){
  997. return Result::error('暂无通栏!');
  998. }
  999. return Result::success($result);
  1000. }
  1001. /**
  1002. * 获取通栏详情
  1003. * @param array $data
  1004. * @return array
  1005. */
  1006. public function getSectorInfo(array $data): array
  1007. {
  1008. // $where = [];
  1009. $where[] = ['sector.id', '=', $data['id']];
  1010. $result = Sector::where($where)
  1011. ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  1012. ->select('sector.*','template.template_name','template.template_id')
  1013. ->orderBy('sector.id', 'desc')
  1014. ->first();
  1015. $result['component'] = SectorComponent::where('sector_component.sector_id', $data['id'])
  1016. ->leftJoin('component', 'component.component_type', '=', 'sector_component.component_id')
  1017. ->select('component.component_type','sector_component.sort_id','component.component_name')
  1018. ->get()->all();
  1019. // $sorts = array_values(array_unique(array_column($components, 'sort_id')));
  1020. // $result['component'] = $components;
  1021. $result['place'] = SectorPlace::where('sector_place.sector_type', $result['place_type'])
  1022. ->where('sector_place.type_id',1)
  1023. ->select('sector_place.sector_img','sector_place.map')
  1024. ->first();
  1025. // $key_num = 0;
  1026. // foreach($result['place'] as $key => $value){
  1027. // if($value['type_id'] == 1){
  1028. // $sector_place['sector_plate'] = $value;
  1029. // }else{
  1030. // $sector_place['component_place'][$key_num] = $value;
  1031. // $key_num++;
  1032. // }
  1033. // }
  1034. // $result['place'] = $sector_place;
  1035. if (empty($result)) {
  1036. return Result::error('此通栏不存在!');
  1037. }
  1038. return Result::success($result);
  1039. }
  1040. /**
  1041. * 获取经纬度信息
  1042. * @return void
  1043. */
  1044. public function getIpInfo(array $data): array
  1045. {
  1046. if (empty($data['ip'])) {
  1047. // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
  1048. return Result::error('IP地址不能为空!');
  1049. }
  1050. $client_ip = $data['ip'];
  1051. try {
  1052. // 直接从容器获取 Guzzle 客户端实例
  1053. /** @var \GuzzleHttp\Client $client */
  1054. $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
  1055. // 使用新的 GeoJS API 地址
  1056. $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
  1057. // 将 timeout 作为请求选项传递
  1058. $response = $client->get($api_url, ['timeout' => 5]);
  1059. if ($response->getStatusCode() !== 200) {
  1060. return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
  1061. }
  1062. $ip_info = json_decode($response->getBody()->getContents(), true);
  1063. // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
  1064. if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
  1065. // GeoJS 返回的经纬度是字符串,转换为浮点数
  1066. $ip_info['latitude'] = floatval($ip_info['latitude']);
  1067. $ip_info['longitude'] = floatval($ip_info['longitude']);
  1068. return Result::success($ip_info);
  1069. }
  1070. // 如果返回的数据不符合预期,提供一个通用错误
  1071. $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
  1072. return Result::error($errorMessage);
  1073. } catch (GuzzleException $e) {
  1074. // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
  1075. return Result::error('请求IP查询接口异常: ' . $e->getMessage());
  1076. } catch (\Throwable $e) {
  1077. return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
  1078. }
  1079. }
  1080. /**
  1081. * 获取天气
  1082. * @param array $data
  1083. * @return array
  1084. */
  1085. public function getWeatherInfo(array $data): array
  1086. {
  1087. $month = $data['month'] ?? date('m');
  1088. $day = $data['day'] ?? date('d');
  1089. // 使用缓存键
  1090. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  1091. // 尝试从缓存获取数据
  1092. $container = \Hyperf\Context\ApplicationContext::getContainer();
  1093. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  1094. if ($cachedData = $cache->get($cacheKey)) {
  1095. return Result::success(unserialize($cachedData));
  1096. }
  1097. $location = $data['latitude'] . ":" . $data['longitude'];
  1098. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=" . \Hyperf\Support\env('WEATHER_KEY') . "&location=" . $location . "&language=zh-Hans&unit=c";
  1099. $ch = curl_init($api_url);
  1100. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1101. $response = curl_exec($ch);
  1102. curl_close($ch);
  1103. // 解析 JSON 响应
  1104. $WeatherInfo = json_decode($response, true);
  1105. // 缓存结果,设置1小时过期
  1106. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  1107. if ($WeatherInfo) {
  1108. return Result::success($WeatherInfo);
  1109. } else {
  1110. return Result::error("获取天气失败", 0);
  1111. }
  1112. }
  1113. /**
  1114. * 获取农历信息
  1115. * @return void
  1116. */
  1117. public function getCalendar(array $data): array
  1118. {
  1119. $calendar = new Calendar();
  1120. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  1121. return Result::success($result);
  1122. }
  1123. /**
  1124. * 添加通栏
  1125. * @param array $data
  1126. * @return array
  1127. */
  1128. public function addSector(array $data): array
  1129. {
  1130. unset($data['user_id']);
  1131. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1132. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1133. $data['page_type'] = json_encode($data['page_type']);
  1134. $template = Template::where('template_id', $data['template_id'])
  1135. ->first();
  1136. if (empty($template)) {
  1137. return Result::error('皮肤不存在!');
  1138. }
  1139. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1140. if(!empty($sector_id)){
  1141. return Result::error('通栏编号已存在!');
  1142. }
  1143. $sector_place = SectorPlace::where('sector_type',$data['place_type'])->get();
  1144. if(empty($sector_place)){
  1145. return Result::error('位置不存在!');
  1146. }
  1147. $sort = json_decode($data['component_code'],true);
  1148. $kw = 0;
  1149. $flattened = [];
  1150. $component = [];
  1151. $sector_component = [];
  1152. foreach ($sort as $key => $subArray) {
  1153. foreach ($subArray as $k => $item) {
  1154. $component[] = $item;
  1155. $sector_component[] = [
  1156. 'sector_id' => $data['sector_id'],
  1157. 'component_id' => $item,
  1158. 'sort_id' => $key + 1,
  1159. 'place_id' => $data['place_type'],
  1160. 'sectorid' => $data['id'],
  1161. ];
  1162. }
  1163. }
  1164. $component_num = count($component);
  1165. $count = count(array_unique($component));
  1166. // return Result::success($sector_component);
  1167. if(count($sort) != $data['component_num'] || $component_num != $count){
  1168. return Result::error('组件关联错误!');
  1169. }
  1170. // 通栏分类:1:资讯类:2:通栏广告类;3:混合类;4:头条类;5:轮播图类;
  1171. $sector_code = [
  1172. $data['sector_id'] => [
  1173. 'sectorName' => $data['sector_name'],
  1174. 'sectorImg' => $data['sector_img'],
  1175. 'sectorHeight' =>$data['pic_height']
  1176. ],
  1177. ];
  1178. // return Result::success($sector_code);
  1179. $data['sector_code'] = json_encode($sector_code);
  1180. Db::beginTransaction();
  1181. try {
  1182. $com_sector = SectorComponent::insert($sector_component);
  1183. if(empty($com_sector)){
  1184. Db::rollBack();
  1185. return Result::error('通栏关联组件失败!');
  1186. }
  1187. // $up_component = Component::whereIn('component_type',$sector_component['component_id'])->update(['status' => 2]);
  1188. // if(empty($up_component)){
  1189. // Db::rollBack();
  1190. // return Result::error('通栏关联组件失败!');
  1191. // }
  1192. $result = Sector::insertGetId($data);
  1193. if (empty($result)) {
  1194. Db::rollBack();
  1195. return Result::error('添加失败');
  1196. }
  1197. Db::commit();
  1198. return Result::success($result);
  1199. } catch (\Exception $e) {
  1200. Db::rollBack();
  1201. return Result::error($e->getMessage());
  1202. }
  1203. }
  1204. /**
  1205. * 删除通栏
  1206. * @param array $data
  1207. * @return array
  1208. */
  1209. public function delSector(array $data): array
  1210. {
  1211. $sector = Sector::where('id', $data['id'])->first();
  1212. if (empty($sector)) {
  1213. return Result::error('通栏不存在!');
  1214. }
  1215. Db::beginTransaction();
  1216. try {
  1217. $component_id = SectorComponent::where('sector_id', $sector['sector_id'])->delete();
  1218. if (empty($component_id)) {
  1219. Db::rollBack();
  1220. return Result::error('解除相关组件关联关系失败!');
  1221. }
  1222. $result = Sector::where('id', $data['id'])->delete();
  1223. if (empty($result)) {
  1224. Db::rollBack();
  1225. return Result::error('通栏删除失败!');
  1226. }
  1227. Db::commit();
  1228. return Result::success('通栏删除成功!');
  1229. } catch (\Exception $e) {
  1230. Db::rollBack();
  1231. return Result::error($e->getMessage());
  1232. }
  1233. }
  1234. /**
  1235. * 修改通栏
  1236. * @param array $data
  1237. * @return array
  1238. */
  1239. public function updateSector(array $data): array
  1240. {
  1241. unset($data['user_id']);
  1242. $sector = Sector::where('id', $data['id'])->first();
  1243. if (empty($sector)) {
  1244. return Result::error('通栏不存在!');
  1245. }
  1246. if($sector['sector_id'] != $data['sector_id']){
  1247. $sector_id = Sector::where('sector_id',$data['sector_id'])->first();
  1248. if(!empty($sector_id)){
  1249. return Result::error('通栏编号已存在!');
  1250. }
  1251. }
  1252. $sector_code = [
  1253. 'sectorName' => strval($data['sector_name']),
  1254. 'sectorId' => intval($data['sector_id']),
  1255. 'sectorType' => intval($data['sector_type']),
  1256. 'sectorPlace' => intval($data['place_type']),
  1257. ];
  1258. // return Result::success($sector_code);
  1259. $data['sector_code'] = json_encode($sector_code);
  1260. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1261. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1262. $data['page_type'] = json_encode($data['page_type']);
  1263. $template = Template::where('template_id', $data['template_id'])
  1264. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1265. ->first();
  1266. if (empty($template)) {
  1267. return Result::error('皮肤不存在!');
  1268. }
  1269. $sort = json_decode($data['component_code'],true);
  1270. $kw = 0;
  1271. $flattened = [];
  1272. $component = [];
  1273. $sector_component = [];
  1274. foreach ($sort as $key => $subArray) {
  1275. foreach ($subArray as $k => $item) {
  1276. $component[] = $item;
  1277. $sector_component[] = [
  1278. 'sector_id' => $data['sector_id'],
  1279. 'sectorid' => $data['id'],
  1280. 'component_id' => $item,
  1281. 'sort_id' => $key + 1,
  1282. 'place_id' => $data['place_type'],
  1283. ];
  1284. }
  1285. }
  1286. // // 批量插入数据到 ComponentSector 表
  1287. // \App\Model\ComponentSector::insert($sector_component);
  1288. $component_num = count($component);
  1289. $count = count(array_unique($component));
  1290. // return Result::success($sector_component);
  1291. if(count($sort) != $data['component_num'] || $component_num != $count){
  1292. return Result::error('组件关联错误!');
  1293. }
  1294. $data['sector_code'] = json_encode($sector_code);
  1295. Db::beginTransaction();
  1296. try {
  1297. $com_sector = SectorComponent::where('sectorid', $data['id'])->pluck('component_id');
  1298. if(empty($com_sector)){
  1299. Db::rollBack();
  1300. return Result::error('通栏解除组件关联失败!');
  1301. }
  1302. // $up_component = Component::whereIn('component_type',$com_sector)->update(['status' => 1]);
  1303. // if(empty($up_component)){
  1304. // Db::rollBack();
  1305. // return Result::error('通栏解除组件关联失败!');
  1306. // }
  1307. $del_SectorComponent = SectorComponent::where('sectorid', $data['id'])->delete();
  1308. if(empty($del_SectorComponent)){
  1309. Db::rollBack();
  1310. return Result::error('通栏解除组件关联失败!');
  1311. }
  1312. $com_sector = SectorComponent::insert($sector_component);
  1313. if(empty($com_sector)){
  1314. Db::rollBack();
  1315. return Result::error('通栏关联组件失败!');
  1316. }
  1317. // $up_component = Component::whereIn('component_id',$sector_component['component_type'])
  1318. // ->update(['status' => 2,]);
  1319. // if(empty($up_component)){
  1320. // Db::rollBack();
  1321. // return Result::error('通栏关联组件失败!');
  1322. // }
  1323. $result = Sector::where('id', $data['id'])->update($data);
  1324. if (empty($result)) {
  1325. Db::rollBack();
  1326. return Result::error('修改失败');
  1327. }
  1328. Db::commit();
  1329. return Result::success($result);
  1330. } catch (\Exception $e) {
  1331. Db::rollBack();
  1332. return Result::error($e->getMessage());
  1333. }
  1334. }
  1335. public function getComponentList(array $data): array
  1336. {
  1337. $where = [];
  1338. if (isset($data['component_name']) && !empty($data['component_name'])) {
  1339. array_push($where, ['component.component_name', 'like', '%' . $data['component_name'] . '%']);
  1340. }
  1341. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  1342. array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']);
  1343. array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']);
  1344. } else if (isset($data['width']) && !empty($data['width'])) {
  1345. array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']);
  1346. } else if (isset($data['height']) && !empty($data['height'])) {
  1347. array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']);
  1348. } else {
  1349. }
  1350. if(isset($data['type_id']) && !empty($data['type_id'])){
  1351. $where['component.type_id'] = $data['type_id'];
  1352. }
  1353. $result = Component::where($where)
  1354. ->leftJoin('component_type', 'component_type.id', '=', 'component.type_id')
  1355. ->leftJoin('component_img', 'component.component_type', '=', 'component_img.component_id')
  1356. ->select(
  1357. 'component.*',
  1358. 'component_type.com_typename',
  1359. DB::raw('COUNT(component_img.id) as img_count')
  1360. )
  1361. ->groupBy('component.id') // 按组件ID分组,确保统计每个组件的图片数量
  1362. ->orderBy('component.updated_at', 'desc')
  1363. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1364. if(empty($result)){
  1365. return Result::error('暂无数据');
  1366. }
  1367. return Result::success($result);
  1368. }
  1369. public function getComponentInfo(array $data): array
  1370. {
  1371. $where = ['component.id'=>$data['id']];
  1372. $result = Component::where($where)
  1373. ->leftJoin('component_type','component_type.id','=','component.type_id')
  1374. ->select( 'component.*','component_type.com_typename')
  1375. ->get();
  1376. if(empty($result)){
  1377. return Result::error('暂无数据');
  1378. }
  1379. return Result::success($result);
  1380. }
  1381. /*
  1382. 新增组件
  1383. */
  1384. public function addComponent(array $data): array
  1385. {
  1386. unset($data['user_id']);
  1387. $component_id = Component::where('component_type',$data['component_type'])->first();
  1388. if(!empty($component_id)){
  1389. return Result::error('组件编号已存在!');
  1390. }
  1391. $component_type = ComponentType::where('id',$data['type_id'])->first();
  1392. if(empty($component_type)){
  1393. return Result::error('组件分类不存在!');
  1394. }
  1395. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;
  1396. // 7:资讯-栏目类组件;8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;
  1397. $data['type_id'] = intval($data['type_id']);
  1398. $add_arr = [
  1399. // 'template_id' => intval($data['template_id']),
  1400. 'component_type' => intval($data['component_type']),
  1401. 'component_name' => $data['component_name'],
  1402. 'component_img' => $data['component_img'],
  1403. 'component_width' => $data['component_width'],
  1404. 'component_height' => $data['component_height'],
  1405. 'type_id' => intval($data['type_id']),
  1406. 'component_keyword' => $data['component_keyword'],
  1407. ];
  1408. $component_head_code = [
  1409. 'component_type' => $add_arr['component_type'],
  1410. 'type_id' => $add_arr['type_id'],
  1411. 'component_name' => $add_arr['component_name'],
  1412. ];
  1413. $add_arr['component_code'] = json_encode($component_head_code,true);
  1414. switch($data['type_id']){
  1415. case 1: //1:资讯-头条组件;
  1416. case 2: //2:资讯-轮播组件;
  1417. case 3: //3:资讯-推荐图类组件;
  1418. case 4: //4:资讯-最新类组件;
  1419. case 5: //5:资讯-推荐类组件;
  1420. case 6: //6:资讯-热点类组件;
  1421. $add_arr['level'] = $data['type_id'];
  1422. $add_arr['img_num'] = intval($data['img_num']);
  1423. $add_arr['text_num'] = intval($data['text_num']);
  1424. $component_data['componentData'] = [
  1425. 'category_id' => '',
  1426. 'level' => $add_arr['level'],
  1427. 'imgSize' => $data['img_num'] ?? '',
  1428. 'textSize' => $data['text_num'] ?? '',
  1429. 'child' => [
  1430. 'id' => '',
  1431. 'imgSize' => '',
  1432. 'textSize' => ''
  1433. ]
  1434. ];
  1435. $add_arr['component_data'] = json_encode($component_data, true);
  1436. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1437. break;
  1438. case 7: //7:资讯-栏目类组件;
  1439. $add_arr['img_num'] = intval($data['img_num']);
  1440. $add_arr['text_num'] = intval($data['text_num']);
  1441. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1442. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1443. $component_data['componentData'] = [
  1444. 'category_id' => '',
  1445. 'level' => '',
  1446. 'imgSize' => $add_arr['img_num'] ?? '',
  1447. 'textSize' => $add_arr['text_num'] ?? '',
  1448. 'child' => [
  1449. 'id' => '',
  1450. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1451. 'textSize' => $add_arr['child_textnum'] ?? '',
  1452. ]
  1453. ];
  1454. $add_arr['component_data'] = json_encode($component_data,true);
  1455. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1456. break;
  1457. case 8: //8:列表类组件;
  1458. $add_arr['pageSize'] = intval($data['pageSize']);
  1459. $component_data['componentData'] = [
  1460. 'category_id' => '',
  1461. 'pageType' => [
  1462. 'page' => 1,
  1463. 'pageSize' => $add_arr['pageSize'] ?? '',
  1464. ]
  1465. ];
  1466. $add_arr['component_data'] = json_encode($component_data,true);
  1467. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1468. break;
  1469. case 9: //9:详情类组件;
  1470. $component_data['componentData'] = [
  1471. 'article_id' => '',
  1472. ];
  1473. $add_arr['component_data'] = json_encode($component_data,true);
  1474. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1475. break;
  1476. case 10: //10:二级导航类组件;
  1477. $add_arr['cate_place'] = 1;
  1478. $add_arr['num'] = intval($data['num']);
  1479. $component_data['componentData'] = [
  1480. 'pid' => '',
  1481. 'placeid' => 1,
  1482. 'num' => $add_arr['num'],
  1483. ];
  1484. $add_arr['component_data'] = json_encode($component_data,true);
  1485. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1486. break;
  1487. case 11: //11:广告类;
  1488. $add_arr['ad_width'] = intval($data['ad_width']);
  1489. $add_arr['ad_height'] = intval($data['ad_height']);
  1490. $add_arr['component_width'] = intval($data['ad_width']);
  1491. $add_arr['component_height'] = intval($data['ad_height']);
  1492. $add_arr['ad_type'] = intval($data['ad_type']);
  1493. // $add_arr['ad_img'] = $data['ad_img'];
  1494. $ad = [
  1495. 'width' => $add_arr['ad_width'],
  1496. 'height' => $add_arr['ad_height'],
  1497. 'name' => '',
  1498. 'price' => '',
  1499. 'introduce' => '',
  1500. 'website_id' => '',
  1501. // 'thumb' => $add_arr['ad_img'],
  1502. 'typeid' => $add_arr['ad_type'],
  1503. 'ad_tag' => '',
  1504. ];
  1505. $add_arr['ad'] = json_encode($ad,true);
  1506. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1507. // $add_arr['adimg_info'] = $data['img_info'];
  1508. // $ad_imginfo = json_decode($data['img_info'],true);
  1509. break;
  1510. case 13: //13:底部导航类;
  1511. $component_data['componentData'] = [
  1512. 'fcat_id' => '',
  1513. ];
  1514. $add_arr['component_data'] = json_encode($component_data,true);
  1515. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1516. break;
  1517. case 14:
  1518. $add_arr['ad_width'] = intval($data['ad_width']);
  1519. $add_arr['ad_height'] = intval($data['ad_height']);
  1520. $add_arr['component_width'] = intval($data['component_width']);
  1521. $add_arr['component_height'] = intval($data['component_height']);
  1522. $add_arr['ad_type'] = intval($data['ad_type']);
  1523. // $add_arr['ad_img'] = $data['ad_img'];
  1524. $add_arr['img_num'] = intval($data['img_num']);
  1525. $add_arr['text_num'] = intval($data['text_num']);
  1526. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1527. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1528. $component_data['componentData'] = [
  1529. 'category_id' => '',
  1530. 'level' => '',
  1531. 'imgSize' => $data['img_num'] ?? '',
  1532. 'textSize' => $data['text_num'] ?? '',
  1533. 'child' => [
  1534. 'id' => '',
  1535. 'imgSize' => $data['child_imgnum'] ?? '',
  1536. 'textSize' => $data['child_textnum'] ?? '',
  1537. ]
  1538. ];
  1539. $ad = [
  1540. 'width' => $add_arr['ad_width'],
  1541. 'height' => $add_arr['ad_height'],
  1542. 'name' => '',
  1543. 'price' => '',
  1544. 'introduce' => '',
  1545. 'website_id' => '',
  1546. // 'thumb' => $add_arr['ad_img'],
  1547. 'typeid' => $add_arr['ad_type'],
  1548. 'ad_tag' => '',
  1549. ];
  1550. $add_arr['ad'] = json_encode($ad,true);
  1551. $add_arr['component_data'] = json_encode($component_data,true);
  1552. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1553. // $add_arr['adimg_info'] = $data['img_info'];
  1554. // $ad_imginfo = json_decode($data['img_info'],true);
  1555. break;
  1556. default:
  1557. break;
  1558. }
  1559. if($data['type_id'] == 11 || $data['type_id'] == 14){
  1560. Db::beginTransaction();
  1561. try{
  1562. // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  1563. // var_dump($website_img);
  1564. // $add_arr['ad_imgid'] = $website_img;
  1565. // if(empty($website_img)){
  1566. // Db::rollBack();
  1567. // return Result::error('广告默认图上传失败!');
  1568. // }
  1569. $result = Component::insertGetId($add_arr);
  1570. if(empty($result)){
  1571. Db::rollBack();
  1572. return Result::error('添加失败!');
  1573. }
  1574. Db::commit();
  1575. }catch(\Exception $e){
  1576. Db::rollback();
  1577. return Result::error('添加失败!');
  1578. }
  1579. }else{
  1580. $result = Component::insertGetId($add_arr);
  1581. if(empty($result)){
  1582. return Result::error('添加组件失败!');
  1583. }
  1584. }
  1585. return Result::success($result);
  1586. }
  1587. public function delComponent(array $data): array
  1588. {
  1589. $component = Component::where('id', $data['id'])->first();
  1590. if(empty($component)){
  1591. return Result::error('组件不存在!');
  1592. }
  1593. $component_id = SectorComponent::where('component_id', $component['component_type'])->first();
  1594. if (!empty($component_id)) {
  1595. return Result::error('请先解除相关组件关联关系!');
  1596. }
  1597. Db::beginTransaction();
  1598. try{
  1599. // if($component['type_id'] == 11 || $component['type_id'] == 14){
  1600. // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
  1601. // if(!empty($ad_img)){
  1602. // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
  1603. // if(empty($ad_img)){
  1604. // Db::rollBack();
  1605. // return Result::error('删除广告默认图失败!');
  1606. // }
  1607. // }
  1608. // }
  1609. $component_img = ComponentImg::where('component_id',$component['component_type'])->first();
  1610. if(!empty($component_img)){
  1611. $del_img = ComponentImg::where('component_id',$component['component_type'])->delete();
  1612. if(empty($del_img)){
  1613. Db::rollBack();
  1614. return Result::error('删除组件预览图失败!');
  1615. }
  1616. }
  1617. $result = Component::where('id',$data['id'])->delete();
  1618. if(empty($result)){
  1619. Db::rollBack();
  1620. return Result::error('删除失败!');
  1621. }
  1622. Db::commit();
  1623. return Result::success($result);
  1624. }catch(\Exception $e){
  1625. Db::rollback();
  1626. return Result::error('删除失败!');
  1627. }
  1628. }
  1629. public function updateComponent(array $data): array
  1630. {
  1631. unset($data['user_id']);
  1632. $id = $data['id'];
  1633. unset($data['id']);
  1634. $component_id = Component::where('id','!=',$id)->where('component_type',$data['component_type'])->first();
  1635. if(!empty($component_id) || $component_id != null){
  1636. return Result::error('组件编号已存在!');
  1637. }
  1638. $component_type = ComponentType::where('id',$data['type_id'])->first();
  1639. if(empty($component_type)){
  1640. return Result::error('组件分类不存在!');
  1641. }
  1642. $component = Component::where('id',$id)->first();
  1643. if(empty($component)){
  1644. return Result::error('组件不存在!');
  1645. }
  1646. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;
  1647. // 7:资讯-栏目类组件;8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;
  1648. $data['type_id'] = intval($data['type_id']);
  1649. $add_arr = [
  1650. // 'template_id' => intval($data['template_id']),
  1651. 'component_type' => intval($data['component_type']),
  1652. 'component_name' => $data['component_name'],
  1653. 'component_img' => $data['component_img'],
  1654. 'component_width' => $data['component_width'],
  1655. 'component_height' => $data['component_height'],
  1656. 'type_id' => intval($data['type_id']),
  1657. 'component_keyword' => $data['component_keyword'],
  1658. ];
  1659. $component_head_code = [
  1660. 'component_type' => $add_arr['component_type'],
  1661. 'type_id' => $add_arr['type_id'],
  1662. 'component_name' => $add_arr['component_name'],
  1663. ];
  1664. $add_arr['component_code'] = json_encode($component_head_code,true);
  1665. switch($data['type_id']){
  1666. case 1: //1:资讯-头条组件;
  1667. case 2: //2:资讯-轮播组件;
  1668. case 3: //3:资讯-推荐图类组件;
  1669. case 4: //4:资讯-最新类组件;
  1670. case 5: //5:资讯-推荐类组件;
  1671. case 6: //6:资讯-热点类组件;
  1672. $add_arr['level'] = $data['type_id'];
  1673. $add_arr['img_num'] = intval($data['img_num']);
  1674. $add_arr['text_num'] = intval($data['text_num']);
  1675. $component_data['componentData'] = [
  1676. 'category_id' => '',
  1677. 'level' => $add_arr['level'],
  1678. 'imgSize' => $data['img_num'] ?? '',
  1679. 'textSize' => $data['text_num'] ?? '',
  1680. 'child' => [
  1681. 'id' => '',
  1682. 'imgSize' => '',
  1683. 'textSize' => ''
  1684. ]
  1685. ];
  1686. $add_arr['component_data'] = json_encode($component_data, true);
  1687. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1688. break;
  1689. case 7: //7:资讯-栏目类组件;
  1690. $add_arr['img_num'] = intval($data['img_num']);
  1691. $add_arr['text_num'] = intval($data['text_num']);
  1692. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1693. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1694. $component_data['componentData'] = [
  1695. 'category_id' => '',
  1696. 'level' => '',
  1697. 'imgSize' => $add_arr['img_num'] ?? '',
  1698. 'textSize' => $add_arr['text_num'] ?? '',
  1699. 'child' => [
  1700. 'id' => '',
  1701. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1702. 'textSize' => $add_arr['child_textnum'] ?? '',
  1703. ]
  1704. ];
  1705. $add_arr['component_data'] = json_encode($component_data,true);
  1706. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1707. break;
  1708. case 8: //8:列表类组件;
  1709. $add_arr['pageSize'] = intval($data['pageSize']);
  1710. $component_data['componentData'] = [
  1711. 'category_id' => '',
  1712. 'pageType' => [
  1713. 'page' => 1,
  1714. 'pageSize' => $add_arr['pageSize'] ?? '',
  1715. ]
  1716. ];
  1717. $add_arr['component_data'] = json_encode($component_data,true);
  1718. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1719. break;
  1720. case 9: //9:详情类组件;
  1721. $component_data['componentData'] = [
  1722. 'article_id' => '',
  1723. ];
  1724. $add_arr['component_data'] = json_encode($component_data,true);
  1725. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1726. break;
  1727. case 10: //10:二级导航类组件;
  1728. $add_arr['cate_place'] = 1;
  1729. $add_arr['num'] = intval($data['num']);
  1730. $component_data['componentData'] = [
  1731. 'pid' => '',
  1732. 'placeid' => 1,
  1733. 'num' => $add_arr['num'],
  1734. ];
  1735. $add_arr['component_data'] = json_encode($component_data,true);
  1736. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1737. break;
  1738. case 11: //11:广告类;
  1739. $add_arr['ad_width'] = intval($data['ad_width']);
  1740. $add_arr['ad_height'] = intval($data['ad_height']);
  1741. $add_arr['component_width'] = intval($data['ad_width']);
  1742. $add_arr['component_height'] = intval($data['ad_height']);
  1743. $add_arr['ad_type'] = intval($data['ad_type']);
  1744. // $add_arr['ad_img'] = $data['ad_img'];
  1745. $ad = [
  1746. 'width' => $add_arr['ad_width'],
  1747. 'height' => $add_arr['ad_height'],
  1748. 'name' => '',
  1749. 'price' => '',
  1750. 'introduce' => '',
  1751. 'website_id' => '',
  1752. // 'thumb' => $add_arr['ad_img'],
  1753. 'typeid' => $add_arr['ad_type'],
  1754. 'ad_tag' => '',
  1755. ];
  1756. $add_arr['ad'] = json_encode($ad,true);
  1757. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1758. // $add_arr['adimg_info'] = $data['img_info'];
  1759. // $ad_imginfo = json_decode($data['img_info'],true);
  1760. break;
  1761. case 13: //13:底部导航类;
  1762. $component_data['componentData'] = [
  1763. 'fcat_id' => '',
  1764. ];
  1765. $add_arr['component_data'] = json_encode($component_data,true);
  1766. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1767. break;
  1768. case 14:
  1769. $add_arr['ad_width'] = intval($data['ad_width']);
  1770. $add_arr['ad_height'] = intval($data['ad_height']);
  1771. $add_arr['component_width'] = intval($data['component_width']);
  1772. $add_arr['component_height'] = intval($data['component_height']);
  1773. $add_arr['ad_type'] = intval($data['ad_type']);
  1774. // $add_arr['ad_img'] = $data['ad_img'];
  1775. $add_arr['img_num'] = intval($data['img_num']);
  1776. $add_arr['text_num'] = intval($data['text_num']);
  1777. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1778. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1779. $component_data['componentData'] = [
  1780. 'category_id' => '',
  1781. 'level' => '',
  1782. 'imgSize' => $data['img_num'] ?? '',
  1783. 'textSize' => $data['text_num'] ?? '',
  1784. 'child' => [
  1785. 'id' => '',
  1786. 'imgSize' => $data['child_imgnum'] ?? '',
  1787. 'textSize' => $data['child_textnum'] ?? '',
  1788. ]
  1789. ];
  1790. $ad = [
  1791. 'width' => $add_arr['ad_width'],
  1792. 'height' => $add_arr['ad_height'],
  1793. 'name' => '',
  1794. 'price' => '',
  1795. 'introduce' => '',
  1796. 'website_id' => '',
  1797. // 'thumb' => $add_arr['ad_img'],
  1798. 'typeid' => $add_arr['ad_type'],
  1799. 'ad_tag' => '',
  1800. ];
  1801. $add_arr['ad'] = json_encode($ad,true);
  1802. $add_arr['component_data'] = json_encode($component_data,true);
  1803. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1804. // $add_arr['adimg_info'] = $data['img_info'];
  1805. // $ad_imginfo = json_decode($data['img_info'],true);
  1806. break;
  1807. default:
  1808. break;
  1809. }
  1810. // var_dump("ad_imginfo:",$ad_imginfo);
  1811. $cll_column = [
  1812. 'img_num' => null,
  1813. 'text_num' => null,
  1814. 'child_imgnum' => null,
  1815. 'child_textnum' => null,
  1816. 'category_id' => null,
  1817. 'child_id' => null,
  1818. 'num' => null,
  1819. 'level' => null,
  1820. 'fcatid' => null,
  1821. 'article_id' => null,
  1822. 'cate_place' => null,
  1823. 'pageSize' => null,
  1824. // 'ad_img' => null,
  1825. 'ad_width' => null,
  1826. 'ad_height' => null,
  1827. 'ad' => '',
  1828. 'ad_type' => null,
  1829. // 'ad_imgid' => null,
  1830. 'adimg_info' => '[]',
  1831. ];
  1832. Db::beginTransaction();
  1833. try{
  1834. // return Result::success($add_arr);
  1835. // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] != 11 && $data['type_id'] != 14)){
  1836. // // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
  1837. // if(empty($website_img)){
  1838. // Db::rollBack();
  1839. // return Result::error('广告默认图删除失败!');
  1840. // }
  1841. // var_dump("删除",$website_img);
  1842. // }
  1843. // if(($data['type_id'] == 11 || $data['type_id'] == 14) && ($component['type_id'] != 11 && $component['type_id'] != 14)){
  1844. // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  1845. // $add_arr['ad_imgid'] = $website_img;
  1846. // if(empty($website_img)){
  1847. // Db::rollBack();
  1848. // return Result::error('广告默认图上传失败!');
  1849. // }
  1850. // var_dump("上传",$website_img);
  1851. // }
  1852. // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] == 11 || $data['type_id'] == 14)){
  1853. // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
  1854. // if(empty($website_img)){
  1855. // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  1856. // $add_arr['ad_imgid'] = $website_img;
  1857. // }else{
  1858. // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->update($ad_imginfo);
  1859. // $add_arr['ad_imgid'] = $component['ad_imgid'];
  1860. // }
  1861. // if(empty($website_img)){
  1862. // Db::rollBack();
  1863. // return Result::error('广告默认图修改失败!');
  1864. // }
  1865. // }
  1866. $add_arr = array_merge($cll_column,$add_arr);
  1867. $result = Component::where('id',$id)->update($add_arr);
  1868. if(empty($result)){
  1869. Db::rollBack();
  1870. return Result::error('修改失败!');
  1871. }
  1872. Db::commit();
  1873. }catch(\Exception $e){
  1874. Db::rollBack();
  1875. return Result::error('修改失败!');
  1876. }
  1877. return Result::success($result);
  1878. }
  1879. public function getWebsiteTemplateInfo(array $data)
  1880. {
  1881. $where = [];
  1882. if (isset($data['id'])) {
  1883. $where[] = ['id', '=', $data['id']];
  1884. }
  1885. $result = WebsiteTemplateInfo::where($where)
  1886. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  1887. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  1888. ->select('website_template_info.*', 'website.website_name')
  1889. ->get();
  1890. if ($result) {
  1891. return Result::success($result);
  1892. } else {
  1893. return Result::error('暂无数据');
  1894. }
  1895. }
  1896. /**
  1897. * 自助建站----1.获取页面类型回显
  1898. * @param array $data
  1899. * @return array
  1900. */
  1901. public function getWebPageType(array $data): array
  1902. {
  1903. // 1:图片 2:文字 3:底部
  1904. $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get();
  1905. if (empty($friend_link->toArray())) {
  1906. return Result::error('暂无友情链接数据!');
  1907. }
  1908. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  1909. if (empty($footer_category->toArray())) {
  1910. return Result::error('暂无底部导航数据!');
  1911. }
  1912. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  1913. if (empty($page_type)) {
  1914. $page_type = [];
  1915. } else {
  1916. $page_type = json_decode($page_type, true);
  1917. }
  1918. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1919. if (isset($page_type) && in_array(5, $page_type)) {
  1920. $page_type = 1;
  1921. } else {
  1922. $page_type = 0;
  1923. }
  1924. $result = [
  1925. 'friend_link' => $friend_link,
  1926. 'footer_category' => $footer_category,
  1927. 'page_type' => $page_type,
  1928. ];
  1929. return Result::success($result);
  1930. }
  1931. /**
  1932. * 自助建站----2.添加页面类型
  1933. * @param array $data
  1934. * @return array
  1935. */
  1936. public function addWebPageType(array $data): array
  1937. {
  1938. // return Result::success($data);
  1939. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  1940. if ($data['is_search'] == 1) {
  1941. $page_type = json_encode([1,2,3,4,5,6,7]);
  1942. } else {
  1943. $page_type = json_encode([1,2,3,4,6,7]);
  1944. }
  1945. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  1946. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1947. $website_template = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  1948. Db::beginTransaction();
  1949. try{
  1950. if (empty($website_template_info)) {
  1951. $result['template_info'] = WebsiteTemplateInfo::create([
  1952. 'website_id' => $data['website_id'],
  1953. 'page_type' => $page_type,
  1954. 'user_id' => $data['user_id']
  1955. ])->id;
  1956. } else {
  1957. if ($website_template_info['status'] == 2) {
  1958. Db::rollBack();
  1959. return Result::error('网站已应用,不可再次修改!');
  1960. }
  1961. $result['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type, 'action_id' => 1, 'user_id' => $data['user_id']]);
  1962. }
  1963. if (!$result['template_info']) {
  1964. Db::rollBack();
  1965. return Result::error('网站所选页面添加失败');
  1966. }
  1967. if(empty($website_template)){
  1968. $result['template'] = WebsiteTemplate::insertGetId([
  1969. 'website_id' => $data['website_id'],
  1970. 'page_type' => $page_type,
  1971. 'user_id' => $data['user_id'],
  1972. ]);
  1973. }else{
  1974. $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])->update([
  1975. 'page_type' => $page_type,
  1976. ]);
  1977. }
  1978. if(!$result['template']){
  1979. Db::rollBack();
  1980. return Result::error('添加网站预制模版信息失败!');
  1981. }
  1982. Db::commit();
  1983. return Result::success($result);
  1984. }catch(\Exception $e){
  1985. Db::rollBack();
  1986. return Result::error('添加失败');
  1987. }
  1988. }
  1989. /**
  1990. * 自助建站---流程---3.选择皮肤列表
  1991. * @param array $data
  1992. * @return array
  1993. */
  1994. public function getWebsiteTemplateList(array $data): array
  1995. {
  1996. $where = [];
  1997. if (isset($data['template_class_id']) && $data['template_class_id']) {
  1998. $where[] = ['template_class_id', '=', $data['template_class_id']];
  1999. }
  2000. // 0:未构建 1:未应用 2:已应用 status
  2001. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2002. if (empty($templste_info)) {
  2003. return Result::error('请先填写网站基础信息!');
  2004. }
  2005. if ($templste_info['status'] == 2) {
  2006. return Result::error('网站已应用,不可再次修改!');
  2007. }
  2008. if (isset($data['keyword']) && !empty($data['keyword'])) {
  2009. if (is_array($data['keyword'])) {
  2010. // 拼接多个模糊查询条件
  2011. $query = Template::query();
  2012. foreach ($data['keyword'] as $kw) {
  2013. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  2014. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  2015. }
  2016. } else {
  2017. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  2018. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  2019. }
  2020. } else {
  2021. $query = Template::where($where);
  2022. }
  2023. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  2024. // return Result::success($template_id);
  2025. if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) {
  2026. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  2027. } else {
  2028. $template_page = [];
  2029. }
  2030. if (!empty($template_id['template_id'])) {
  2031. $result['template_id'] = $templste_info['template_id'];
  2032. }
  2033. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  2034. $template_page_str = json_encode($template_page);
  2035. $result['template'] = $query->where($where)
  2036. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  2037. ->orderBy('updated_at', 'desc')
  2038. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2039. if (empty($result['template'])) {
  2040. $result['template'] = Template::orderBy('updated_at', 'desc')
  2041. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2042. }
  2043. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  2044. if ($template) {
  2045. $result['template_id'] = $template;
  2046. } else {
  2047. $result['template_id'] = 0;
  2048. }
  2049. return Result::success($result);
  2050. }
  2051. /**
  2052. * 自助建站---流程---4.选择皮肤
  2053. * @param array $data
  2054. * @return array
  2055. */
  2056. public function chooseWebsiteTemplate(array $data): array
  2057. {
  2058. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2059. if (empty($website_template_info)) {
  2060. return Result::error('请先填写网站基础信息!');
  2061. }
  2062. // 0:未构建 1:未应用 2:已应用 status
  2063. if ($website_template_info['status'] == 2) {
  2064. return Result::error('网站已应用,不可再次修改!');
  2065. }
  2066. $template = Template::where('template_id', $data['template_id'])->first();
  2067. if (empty($template)) {
  2068. return Result::error('未查询到皮肤!');
  2069. }
  2070. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  2071. // 分别更新 WebsiteTemplateInfo 和 WebsiteTemplate 表
  2072. Db::beginTransaction();
  2073. try {
  2074. $reuslt['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])
  2075. ->update(['template_id' => $data['template_id'],'action_id' => 2,'user_id'=> $data['user_id']]);
  2076. if(empty($reuslt['template_info'])){
  2077. Db::rollBack();
  2078. return Result::error('选择皮肤失败!');
  2079. }
  2080. $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])
  2081. ->update(['template_id' => $data['template_id'],'user_id'=> $data['user_id']]);
  2082. if(empty($result['template'])){
  2083. Db::rollBack();
  2084. return Result::error('网站模版选择皮肤失败!');
  2085. }
  2086. Db::commit();
  2087. return Result::success($result);
  2088. } catch (\Exception $e) {
  2089. Db::rollBack();
  2090. return Result::error('选择皮肤失败!');
  2091. }
  2092. }
  2093. /**
  2094. * 自助建站---流程---5.保存模版
  2095. * @param array $data
  2096. * @return array
  2097. */
  2098. public function saveWebsiteTemplate(array $data): array
  2099. {
  2100. // return Result::success($data);
  2101. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2102. if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
  2103. return Result::error('请先填写网站基础信息!');
  2104. }
  2105. // 0:未构建 1:未应用 2:已应用 status
  2106. if ($website_template_info['status'] == 2) {
  2107. return Result::error('网站已应用,不可再次保存!');
  2108. }
  2109. if ($website_template_info['action_id'] != 2) {
  2110. return Result::error('请完成之前的步骤!');
  2111. }
  2112. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  2113. // var_dump($data['template_data']);
  2114. try {
  2115. Db::beginTransaction();
  2116. if (empty($template_info)) {
  2117. Db::rollback();
  2118. return Result::error('该网站不存在已保存的模版!');
  2119. } else {
  2120. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  2121. [
  2122. 'template_data' => $data['template_data'],
  2123. 'user_id' => $data['user_id'],
  2124. 'canvas_data' => $data['canvas_data'],
  2125. ]
  2126. );
  2127. }
  2128. if (empty($template)) {
  2129. Db::rollback();
  2130. return Result::error('保存失败!');
  2131. } else {
  2132. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  2133. if (empty($tempalte_info)) {
  2134. Db::rollback();
  2135. return Result::error('网站搭建状态保存失败!');
  2136. }
  2137. Db::commit();
  2138. }
  2139. } catch (\Exception $e) {
  2140. return Result::error('模版数据格式错误!');
  2141. }
  2142. return Result::success($template);
  2143. }
  2144. /**
  2145. * 自助建站---流程---6.返显保存的模版
  2146. * @param array $data
  2147. * @return array
  2148. */
  2149. public function getWebsiteTemplate(array $data): array
  2150. {
  2151. $web = Website::where('id', $data['website_id'])->first();
  2152. if (empty($web)) {
  2153. return Result::error('未查询到网站信息!');
  2154. }
  2155. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  2156. ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
  2157. if (empty($template)) {
  2158. return Result::error('未查询到模版!');
  2159. }
  2160. $template['page_type'] = json_decode($template['page_type'],true);
  2161. return Result::success($template);
  2162. }
  2163. /**
  2164. * 通栏版式管理-获取通栏版式列表
  2165. */
  2166. public function getSectorPlaceList(array $data): array
  2167. {
  2168. $where['sector_place.type_id'] = $data['type_id'];
  2169. if(isset($data['component_num']) && !empty($data['component_num'])){
  2170. $where['sector_place.component_num'] = $data['component_num'];
  2171. }
  2172. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  2173. $where['sector_place.sector_type'] = $data['sector_type'];
  2174. }
  2175. // if(isset($data['component_type']) && !empty($data['component_type'])){
  2176. // $where['sector_place.component_type'] = $data['component_type'];
  2177. // }
  2178. if(isset($data['name']) && !empty($data['name'])){
  2179. array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']);
  2180. }
  2181. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
  2182. array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
  2183. array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
  2184. } else if( isset($data['width']) && !empty($data['width'])){
  2185. array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
  2186. }else if( isset($data['height']) && !empty($data['height'])){
  2187. array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
  2188. }else{
  2189. $size_id = [];
  2190. }
  2191. $sector_place = SectorPlace::where($where)
  2192. // ->when(!empty($size_id), function ($query) use ($size_id) {
  2193. // $query->whereIn('sector_place.size_id', $size_id);
  2194. // })
  2195. // ->leftJoin('size','sector_place.size_id','=','size.id')
  2196. ->select('sector_place.*');
  2197. $result['count'] = $sector_place->count();
  2198. $result['row'] =$sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2199. if(empty($result['count'])){
  2200. return Result::error("暂无相关版式数据!");
  2201. }
  2202. return Result::success($result);
  2203. }
  2204. /**
  2205. * 通栏版式管理-添加通栏版式
  2206. */
  2207. public function addSectorPlace(array $data): array
  2208. {
  2209. // 1:通栏;2:组件;
  2210. if($data['type_id'] == 1){
  2211. $sector_place = SectorPlace::where('sector_type', $data['type'])->first();
  2212. if(!empty($sector_place)){
  2213. return Result::error('通栏版式类别编号已存在!');
  2214. }
  2215. $data = [
  2216. 'name' => $data['name'],
  2217. 'component_num' => $data['component_num'],
  2218. 'size_id' => $data['size_id'],
  2219. 'sort_id' => 0,
  2220. 'sector_img' => $data['sector_img'],
  2221. 'sector_type' => $data['type'],
  2222. 'type_id' => $data['type_id'],
  2223. 'status' => 0,
  2224. ];
  2225. }else{
  2226. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first();
  2227. if(empty($sector_type)){
  2228. return Result::error('通栏版式类别不存在!');
  2229. }
  2230. // $com_query = SectorPlace::where('type_id',2);
  2231. // ->query(function($query) use ($data){
  2232. // $query->where('component_type', $data['type'])
  2233. // ->Subquery(function($subQuery) use ($data) {
  2234. // $subQuery->where('sector_type', $data['sector_type'])
  2235. // ->where('sort_id', $data['sort']);
  2236. // });
  2237. // }) //若是组件版式编号或者组件版式位置重复用此语句筛选
  2238. // ->where('sector_type',$data['sector_type']) //若是组件版式编号在同一个通栏版式下不重复就行,则启用此语句
  2239. $component = SectorPlace::where('type_id',2)->where('component_type',$data['type'])->first();
  2240. if(!empty($component)){
  2241. return Result::error('组件版式类别编号已存在!');
  2242. }
  2243. // $com_query = clone $com_query;
  2244. $com_num = SectorPlace::where('type_id',2)->where('sector_type',$data['sector_type'])->count('sort_id');
  2245. // return Result::success($com_num);
  2246. if($sector_type['component_num'] = $com_num){
  2247. $sector = SectorPlace::where('sector_type',$data['sector_type'])->where('type_id',1)->update(['status'=>1]);
  2248. if(empty($sector)){
  2249. return Result::error('通栏版式状态修改失败!');
  2250. }
  2251. }
  2252. $data = [
  2253. 'name' => $data['name'],
  2254. 'component_num' => 0,
  2255. 'size_id' => $data['size_id'],
  2256. 'sort_id' => $data['sort_id'],
  2257. 'sector_img' => $data['sector_img'],
  2258. 'component_type' => $data['type'],
  2259. 'type_id' => $data['type_id'],
  2260. 'sector_type' => $data['sector_type'],
  2261. ];
  2262. }
  2263. $result = SectorPlace::insertGetId($data);
  2264. if(empty($result)){
  2265. return Result::error('添加失败!');
  2266. }
  2267. return Result::success('添加成功!',$result);
  2268. }
  2269. /**
  2270. * 通栏版式管理-修改通栏版式
  2271. */
  2272. public function upSectorPlace(array $data): array
  2273. {
  2274. $id = $data['id'];
  2275. // 1:通栏;2:组件;
  2276. if($data['type_id'] == 1){
  2277. $sector_place = SectorPlace::where('id','!=',$id)->where('sector_type', $data['type'])->where('type_id',1)->first();
  2278. if(!empty($sector_place)){
  2279. return Result::error('通栏版式类别编号已存在!');
  2280. }
  2281. $data = [
  2282. 'name' => $data['name'],
  2283. 'component_num' => $data['component_num'],
  2284. 'size_id' => $data['size_id'],
  2285. 'sort_id' => 0,
  2286. 'sector_img' => $data['sector_img'],
  2287. 'sector_type' => $data['type'],
  2288. 'type_id' => $data['type_id'],
  2289. ];
  2290. }else{
  2291. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first();
  2292. if(empty($sector_type)){
  2293. return Result::error('通栏版式类别不存在!');
  2294. }
  2295. $component = SectorPlace::where('id','!=',$id)
  2296. ->where('component_type', $data['type'])
  2297. ->where('type_id',2)
  2298. ->first();
  2299. if(!empty($component)){
  2300. return Result::error('组件版式类别编号已存在!');
  2301. }
  2302. $data = [
  2303. 'name' => $data['name'],
  2304. 'component_num' => 0,
  2305. 'size_id' => $data['size_id'],
  2306. 'sort_id' => $data['sort_id'],
  2307. 'sector_img' => $data['sector_img'],
  2308. 'component_type' => $data['type'],
  2309. 'type_id' => $data['type_id'],
  2310. 'sector_type' => $data['sector_type'],
  2311. ];
  2312. }
  2313. $result = SectorPlace::where('id',$id)->update($data);
  2314. if(empty($result)){
  2315. return Result::error('修改失败!');
  2316. }
  2317. return Result::success('修改成功!',$result);
  2318. }
  2319. /**
  2320. * 通栏版式管理-删除通栏版式
  2321. */
  2322. public function delSectorPlace(array $data): array
  2323. {
  2324. $sector_place = SectorPlace::where('id',$data['id'])->first();
  2325. if(empty($sector_place)){
  2326. return Result::error('通栏版式不存在!');
  2327. }
  2328. if($sector_place['type_id'] == 1){
  2329. $sector = Sector::where('place_type', $sector_place['sector_type'])->first();
  2330. $component = SectorPlace::where('sector_type', $sector_place['sector_type'])
  2331. ->where('type_id',2)
  2332. ->first();
  2333. if(!empty($sector) || !empty($component)){
  2334. return Result::error('通栏版式分类已应用,暂不可删除!');
  2335. }
  2336. }else{
  2337. $sector = Component::where('place_type', $sector_place['component_type'])->first();
  2338. if(!empty($sector)){
  2339. return Result::error('组件版式分类已应用,暂不可删除!');
  2340. }
  2341. $component_num = SectorPlace::where('sector_type', $sector_place['sector_type'])
  2342. ->where('type_id',1)->update(['status'=> 0]);
  2343. if(empty($component_num)){
  2344. return Result::error('通栏版式状态修改失败!');
  2345. }
  2346. }
  2347. $result = SectorPlace::where('id',$data['id'])->delete();
  2348. if(empty($result)){
  2349. return Result::error('删除失败!');
  2350. }
  2351. return Result::success('删除成功!');
  2352. }
  2353. /**
  2354. * 自助建站-通栏版式管理-获取通栏版式详情
  2355. */
  2356. public function getSectorPlaceInfo(array $data): array
  2357. {
  2358. $result = SectorPlace::where('sector_place.id',$data['id'])
  2359. ->leftJoin('size','sector_place.size_id','=','size.id')
  2360. ->select('sector_place.*','size.width','size.height')
  2361. ->first();
  2362. if(empty($result)){
  2363. return Result::error('通栏版式不存在!');
  2364. }
  2365. return Result::success($result);
  2366. }
  2367. /**
  2368. * 自助建站-通栏版式管理-获取通栏版式列表
  2369. */
  2370. public function getSectorPlaceSort(array $data): array
  2371. {
  2372. $sector = SectorPlace::where('sector_type',$data['sector_type'])->first();
  2373. if(empty($sector)){
  2374. return Result::error('通栏版式不存在!');
  2375. }
  2376. $component = SectorPlace::where('type_id',2)->where('sector_type',$sector['sector_type'])->pluck('sort_id')->toArray();
  2377. if(count($component) < $sector['component_num']){
  2378. // 根据 $sector['component_num'] 的值生成对应元素数量的数组
  2379. if (isset($sector['component_num']) && is_numeric($sector['component_num'])) {
  2380. $num = intval($sector['component_num']);
  2381. if ($num > 0 && $num <= 10) {
  2382. // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
  2383. $sector_arr = range(1, $num);
  2384. } else {
  2385. $sector_arr = [];
  2386. }
  2387. } else {
  2388. $sector_arr = [];
  2389. }
  2390. // 获取 $sector_arr 中不在 $component 里的元素
  2391. $sort = array_diff($sector_arr, $component);
  2392. // return Result::success($sort);
  2393. }
  2394. if(!empty($sort)){
  2395. $sort = array_values(array_map(function($value) {
  2396. return ['id' => $value];
  2397. }, $sort));
  2398. }else{
  2399. return Result::error("组件版式数量已达上限!");
  2400. }
  2401. return Result::success($sort);
  2402. }
  2403. /**
  2404. * 获取所有通栏版式
  2405. */
  2406. public function getAllSectorPlace(array $data): array
  2407. {
  2408. $where['type_id'] = $data['type_id'];
  2409. // if($data['type_id'] == 1){
  2410. // $where['status'] = 1;
  2411. // }
  2412. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  2413. $where['sector_type'] = $data['sector_type'];
  2414. }
  2415. if(isset($data['component_num']) && !empty($data['component_num'])){
  2416. $where['component_num'] = $data['component_num'];
  2417. }
  2418. $sector_place = SectorPlace::where($where)
  2419. ->when($data['type_id'] == 2, function($query) use($data){
  2420. $query->orderBy('sort_id','asc');
  2421. })
  2422. ->get()->all();
  2423. if($data['type_id'] == 2){
  2424. foreach($sector_place as $key => $value){
  2425. $sector_place[$key]['component'] = Component::where('type_id',$value['type'])
  2426. ->where('component_width',$value['width'])
  2427. ->where('component_height',$value['height'])
  2428. ->get()->all();
  2429. }
  2430. }
  2431. if(empty($sector_place)){
  2432. return Result::error('通栏版式不存在!');
  2433. }
  2434. return Result::success($sector_place);
  2435. }
  2436. /**
  2437. * 组件管理-获取组件类型
  2438. */
  2439. public function getComponentType(array $data): array
  2440. {
  2441. $where = [];
  2442. if(isset($data['id']) && !empty($data['id'])){
  2443. $where['id'] = $data['id'];
  2444. }
  2445. $component_type = ComponentType::where($where)->get()->all();
  2446. if(empty($component_type)){
  2447. return Result::error('组件类型不存在!');
  2448. }
  2449. if(count($component_type) == 1){
  2450. $all_code = json_decode($component_type[0]['com_code'] ?? '',true);
  2451. $component_type[0]['all_code'] = $all_code['listType'] ?? [];
  2452. $result = $component_type[0];
  2453. }else{
  2454. $result = $component_type;
  2455. }
  2456. if(empty($component_type)){
  2457. return Result::error('组件类型不存在!');
  2458. }
  2459. return Result::success($component_type);
  2460. }
  2461. /**
  2462. * 组件管理-获取所有组件
  2463. */
  2464. public function getAllComponent(array $data): array
  2465. {
  2466. $where = [];
  2467. if((isset($data['sector_id']) && !empty($data['sector_id'])) || (isset($data['sort_id']) && !empty($data['sort_id']))){
  2468. $sector['sector_component.sector_id'] = $data['sector_id'];
  2469. $sector['sector_component.sort_id'] = $data['sort_id'];
  2470. $where = SectorComponent::where($sector)->pluck('component_id')->toArray();
  2471. $component = Component::whereIn('component_type',$where)->get()->all();
  2472. }else{
  2473. if(isset($data['type_id']) && !empty($data['type_id'])){
  2474. $where['component.type_id'] = $data['type_id'];
  2475. }
  2476. if(isset($data['width']) && !empty($data['width'])){
  2477. $where['component.component_width'] = $data['width'];
  2478. }
  2479. if(isset($data['height']) && !empty($data['height'])){
  2480. $where['component.component_height'] = $data['height'];
  2481. }
  2482. $component = Component::where($where)
  2483. // ->leftJoin('size','component.size_id','=','size.id')
  2484. // ->select('component.*','size.width','size.height')
  2485. ->get()->all();
  2486. }
  2487. if(empty($component)){
  2488. return Result::error('组件不存在!');
  2489. }
  2490. return Result::success($component);
  2491. }
  2492. /**
  2493. * 自助建站-流程管理-获取所有通栏
  2494. */
  2495. public function getAllSector(array $data): array
  2496. {
  2497. $where = [];
  2498. if(isset($data['template_id']) && !empty($data['template_id'])){
  2499. $where['template_id'] = $data['template_id'];
  2500. }
  2501. if(isset($data['page_type']) && !empty($data['page_type'])){
  2502. array_push($where,['page_type', 'like', '%'.$data['page_type'].'%']);
  2503. }
  2504. $sector = Sector::where($where)
  2505. // ->leftJoin('size','sector.size_id','=','size.id')
  2506. ->select('sector.*')
  2507. ->get()->all();
  2508. if(empty($sector)){
  2509. return Result::error('通栏不存在!');
  2510. }
  2511. return Result::success($sector);
  2512. }
  2513. /**
  2514. * 自助建站-组件管理-获取组件预览图列表
  2515. */
  2516. public function getComponentImgList(array $data): array
  2517. {
  2518. $where['component_img.component_id'] = $data['component_id'];
  2519. if(isset($data['template_id']) && !empty($data['template_id'])){
  2520. $where['component_img.template_id'] = $data['template_id'];
  2521. }
  2522. $component_img = ComponentImg::where($where)
  2523. ->leftJoin('template','component_img.template_id','=','template.template_id')
  2524. ->leftJoin('component','component_img.component_id','=','component.component_type')
  2525. ->select('component_img.*','template.template_name','component.component_name')
  2526. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2527. if($component_img->isEmpty()){
  2528. return Result::error('组件预览图不存在!');
  2529. }
  2530. $component_img = [
  2531. 'data' => $component_img->items(),
  2532. 'total' => $component_img->total(),
  2533. ];
  2534. return Result::success($component_img);
  2535. }
  2536. /**
  2537. * 自助建站-组件管理-添加组件预览图
  2538. */
  2539. public function addComponentImg(array $data): array
  2540. {
  2541. unset($data['user_id']);
  2542. $component = Component::where('component_type',$data['component_id'])->first();
  2543. if(empty($component)){
  2544. return Result::error('组件不存在!');
  2545. }
  2546. if($component['type_id'] == 11){
  2547. $img = ComponentImg::where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all();
  2548. if(!empty($img)){
  2549. return Result::error('该组件已存在此皮肤的预览图!');
  2550. }
  2551. }
  2552. $component = ComponentImg::where('component_id',$data['component_id'])->where('img_id',$data['img_id'])->first();
  2553. if(!empty($component)){
  2554. return Result::error('组件预览图编号已存在!');
  2555. }
  2556. $component_img = ComponentImg::insertGetId($data);
  2557. if(empty($component_img)){
  2558. return Result::error('添加失败!');
  2559. }
  2560. return Result::success($component_img);
  2561. }
  2562. /**
  2563. * 自助建站-组件管理-删除组件预览图
  2564. */
  2565. public function delComponentImg(array $data): array
  2566. {
  2567. $component_img = ComponentImg::where('id',$data['id'])->delete();
  2568. if(empty($component_img)){
  2569. return Result::error('删除失败!');
  2570. }
  2571. return Result::success($component_img);
  2572. }
  2573. /**
  2574. * 自助建站-组件管理-更新组件预览图
  2575. */
  2576. public function updateComponentImg(array $data): array
  2577. {
  2578. $component = Component::where('component_type',$data['component_id'])->first();
  2579. if(empty($component)){
  2580. return Result::error('组件不存在!');
  2581. }
  2582. if($component['type_id'] == 11){
  2583. $img = ComponentImg::where('id','!=',$data['id'])->where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all();
  2584. if(!empty($img)){
  2585. return Result::error('该组件已存在此皮肤的预览图!');
  2586. }
  2587. }
  2588. unset($data['user_id']);
  2589. // unset($data['updated_at']);
  2590. $id = $data['id'];
  2591. unset($data['id']);
  2592. // return Result::success($data);
  2593. $component_img = ComponentImg::where('id',$id)->update($data);
  2594. if(empty($component_img)){
  2595. return Result::error('更新失败!');
  2596. }
  2597. return Result::success($component_img);
  2598. }
  2599. /**
  2600. * 自助建站-获取通栏类型
  2601. */
  2602. public function getSectorType(array $data): array
  2603. {
  2604. $sector = SectorType::get()->all();
  2605. if(empty($sector)){
  2606. return Result::error('通栏类型不存在!');
  2607. }
  2608. return Result::success($sector);
  2609. }
  2610. /**
  2611. * 自助建站-随机获取模板
  2612. */
  2613. public function randomWebTemplate(array $data): array
  2614. {
  2615. $web = Website::where('id',$data['website_id'])->first();
  2616. if(empty($web)){
  2617. return Result::error('此网站不存在!');
  2618. }
  2619. $template = Template:: where('template_id',$data['template_id'])->first();
  2620. if(empty($template)){
  2621. return Result::error('此皮肤不存在!');
  2622. }
  2623. $rule = TemplateRule::first();
  2624. switch ($data['page']) {
  2625. case 1:
  2626. $index_rule = json_decode($rule['index_rule']);
  2627. break;
  2628. case 2:
  2629. $index_rule = json_decode($rule['class_rule']);
  2630. break;
  2631. case 3:
  2632. $index_rule = json_decode($rule['list_rule']);
  2633. break;
  2634. case 4:
  2635. $index_rule = json_decode($rule['info_rule']);
  2636. break;
  2637. case 5:
  2638. $index_rule = json_decode($rule['search_rule']);
  2639. break;
  2640. case 6:
  2641. $index_rule = json_decode($rule['footerlist_rule']);
  2642. break;
  2643. case 7:
  2644. $index_rule = json_decode($rule['footerinfo_rule']);
  2645. break;
  2646. default:
  2647. break;
  2648. }
  2649. // return Result::success($index_rule);
  2650. $tempalte['index'] = $this->randomPage($data,$data['page'],$index_rule);
  2651. // $tempalte['class'] = $this->randomPage($data,2,$class_rule);
  2652. // $tempalte['list'] = $this->randomPage($data,3);
  2653. // $tempalte['article'] = $this->randomPage($data,4);
  2654. // $tempalte['search'] = $this->randomPage($data,5);
  2655. // $tempalte['aloneList'] = $this->randomPage($data,6);
  2656. // $tempalte['aloneArticle'] = $this->randomPage($data,7);
  2657. // $index = $this->randomPage($data);
  2658. return Result::success($tempalte);
  2659. }
  2660. public function randomPage($data,$page,$rule){
  2661. // return $rule;
  2662. $sectors = Sector::where('sector.template_id',$data['template_id'])
  2663. ->where('sector.page_type','like','%'.$page.'%')
  2664. ->select('sector.sector_name', 'sector.sector_id','sector.pic_height','sector.sector_img',
  2665. 'sector.sector_type','sector.sector_code','sector.id','sector.sector_width')
  2666. ->orderBy('id')
  2667. ->get()->all();
  2668. if(empty($sectors)){
  2669. return Result::error('通栏不存在!');
  2670. }
  2671. // return $sectors;
  2672. // 获取每个类别的通栏数量
  2673. $type_num = array_count_values(array_column($sectors,'sector_type'));
  2674. // 此页面通栏的所有类别
  2675. $all_sector_types = array_keys($type_num);
  2676. //获取每个通栏类别在其中的位置 -------暂时不考虑先,应该是必需通栏类型或者非必需通栏类型,每个类型应该取一个,其余取可重复的通栏
  2677. $sector_type_keys = array_column($sectors,'sector_type');
  2678. // 将数组处理为二维数组,值相同的合并为同一个数组,保持原有键值
  2679. $sector_types = array_reduce(array_keys($sector_type_keys), function ($result, $key) use ($sector_type_keys) {
  2680. $value = $sector_type_keys[$key];
  2681. if (!isset($result[$value])) {
  2682. $result[$value] = [];
  2683. }
  2684. $result[$value][$key] = $key;
  2685. $result[$value] = array_values($result[$value]);
  2686. return $result;
  2687. }, []);
  2688. // return $sector_types;
  2689. $must_type = $rule->must_type;
  2690. $sector_num = $rule->max_num;
  2691. // 获取必需通栏类别与查询到的通栏类别的交集数量;
  2692. $is_must = count(array_intersect($must_type, $all_sector_types));
  2693. // $is_must交集数量若是等于必需通栏类别的数量相等则不缺必需通栏
  2694. if( $is_must != count($must_type)){
  2695. return '此页面缺少必要通栏!';
  2696. }
  2697. // var_dump("sector_types",$all_sector_types);
  2698. // var_dump("must_type",$must_type);
  2699. // return $must_type;
  2700. // 非必须通栏的随机处理
  2701. $not_must_type = array_diff($all_sector_types,$must_type);
  2702. $total = count($not_must_type);
  2703. $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度
  2704. $and_type = $rule->and_type ?? [];
  2705. // return $not_must_type;
  2706. if ($randomCount === 0) {
  2707. $random_sector = [];
  2708. $rand_count = 0;
  2709. } else {
  2710. // 随机获取指定数量的键名(保留原键)
  2711. $randomKeys = array_rand($not_must_type, $randomCount);
  2712. // 处理单元素情况(array_rand返回字符串,需转为数组)
  2713. if (!is_array($randomKeys)) {
  2714. $randomKeys = [$randomKeys];
  2715. }
  2716. // 通栏关联性规则暂时无法实现---------------
  2717. // 假设 $oneDimensionalArray 是一维数组,$randomKeys 是随机数组
  2718. // 判断 $oneDimensionalArray 是否为一维数组
  2719. // if (isset($and_type) && is_array($and_type) && !array_is_list(array_filter($and_type, 'is_array'))) {
  2720. // 判断是否存在有关联通栏的交集
  2721. // if (isset($and_type) && !empty($and_type) && count(array_intersect($and_type, $randomKeys)) > 0) {
  2722. // // var_dump("11111",$and_type);
  2723. // // var_dump("22222",$randomKeys);
  2724. // // 将一维数组中的值都放到随机数组中
  2725. // $randomKeys = array_merge($randomKeys, $and_type);
  2726. // }
  2727. // var_dump(count(array_intersect($and_type, $randomKeys)));
  2728. // }
  2729. // -----------------------------
  2730. // 根据随机键名提取元素(保留原键值关系)
  2731. // $randomKeys = array_intersect($sector_type_keys,$randomKeys);
  2732. // var_dump("randomKeys",$randomKeys);
  2733. $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
  2734. $rand_count = count($random_sector);
  2735. }
  2736. // var_dump("randomKeys",$randomKeys);
  2737. // var_dump("random_sector",$random_sector);
  2738. var_dump("rand_count",$rand_count);
  2739. // return $random_sector;
  2740. // 必需通栏
  2741. $must_type = array_intersect($all_sector_types,$must_type);
  2742. // return $must_type;
  2743. // 此代码用于从 $sectors 数组中筛选出必需的通栏数据。
  2744. // 1. array_keys($must_type) 获取 $must_type 数组的所有键名
  2745. // 2. array_flip() 将这些键名作为值,生成一个新数组
  2746. // 3. array_intersect_key() 比较 $sectors 和上述新数组的键名,返回键名相同的元素
  2747. // 4. array_values() 重新索引返回的数组,使其键名从 0 开始连续递增
  2748. // 最终得到的 $must_sector 数组包含了 $sectors 中对应 $must_type 键名的所有必需通栏数据
  2749. $must_sector = array_values(array_intersect_key($sectors, array_flip(array_keys($must_type))));
  2750. $must_count = count($must_sector);
  2751. // var_dump("must_sector",$must_sector);
  2752. // var_dump("must_count",$must_count);
  2753. // return $must_sector;
  2754. // 计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
  2755. $repeat_num = $sector_num-$rand_count-$must_count;
  2756. // 组合起来通栏
  2757. $sector_zuhe1 = array_merge($must_sector,$random_sector);
  2758. $sector_ids = array_column($sector_zuhe1, 'id');
  2759. if($repeat_num > 0){
  2760. $rule_repeat = (array)$rule->type_max;
  2761. $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type'));
  2762. // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
  2763. foreach($rule_repeat as $key => $value){
  2764. if(in_array($key,array_keys($sector_zuhe1_type))){
  2765. $rule_repeat[$key] = $value-1;
  2766. }
  2767. }
  2768. var_dump("repeat_num",$repeat_num);
  2769. // return array_intersect($sector_zuhe1,$must_sector);
  2770. if(count(array_intersect($sector_zuhe1,$must_sector)) != count($must_sector)){
  2771. return '此页面缺少必要通栏!';
  2772. }
  2773. // 剩余通栏进行重复处理
  2774. // $rule_repeat = (array)$rule->type_max;
  2775. // 取到可以重复的通栏类别
  2776. $rule_repaet_type = array_keys($rule_repeat);
  2777. $repeat_type = array_intersect_key($sector_types, array_flip($rule_repaet_type));
  2778. // 获取可以随机重复的通栏
  2779. $repaet_key = 0;
  2780. $num = 0;
  2781. $type = [];
  2782. // return $rule_repaet_type;
  2783. // 防止无限循环,设置最大迭代次数
  2784. $max_iterations = 20;
  2785. $iteration_count = 0;
  2786. // return [
  2787. // 'repaet_key' => $repaet_key,
  2788. // 'repeat_num' => $repeat_num,
  2789. // 'rule_repeat' => $rule_repeat,
  2790. // 'repeat_type' => $repeat_type,
  2791. // 'must_type' => $must_type,
  2792. // ];
  2793. while($repaet_key < $repeat_num && $iteration_count < $max_iterations){
  2794. $num++;
  2795. foreach($repeat_type as $key => $value){
  2796. if(!isset($type[$key])){
  2797. $type[$key] = 0;
  2798. }else{
  2799. $type[$key]++;
  2800. }
  2801. // $type代表每个类型的通栏数量,每个类型的通栏的数量应该≤规则限制的数量
  2802. if($repaet_key < $repeat_num && $type[$key] <= $rule_repeat[$key]){
  2803. // $max_repaert_num = $rule_repeat[$key];
  2804. // if($max_repaert_num < $num ){
  2805. // // $arr_key = $num+$key;
  2806. if (count($value) > 1) {
  2807. // 若 可以随机的通栏类型存在多个通栏,随机取其一
  2808. $repeat_sector_key[$repaet_key] = $value[array_rand($value)];
  2809. } else {
  2810. $repeat_sector_key[$repaet_key] = $value[0];
  2811. }
  2812. $type[$repaet_key] = $repeat_sector_key[$repaet_key];
  2813. $repaet_key++;
  2814. // }
  2815. }
  2816. // else{
  2817. // break;
  2818. // }
  2819. }
  2820. // var_dump("num", $type[$key]);
  2821. // var_dump("repeat_sector_key", $repeat_sector_key);
  2822. $iteration_count++;
  2823. }
  2824. // return $repeat_sector_key;
  2825. // var_dump("num", $num);
  2826. // var_dump("repeat_sector_key", $repeat_sector_key);
  2827. // 随机的重复通栏键值转换成真正的通栏
  2828. $repeat_sector = [];
  2829. if(isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)){
  2830. foreach ($repeat_sector_key as $key => $value) {
  2831. if (isset($sectors[$value])) {
  2832. $repeat_sector[$key] = $sectors[$value];
  2833. }
  2834. }
  2835. }
  2836. $repeat_count = count($repeat_sector);
  2837. var_dump("repeat_count",$repeat_count);
  2838. // var_dump("repeat_sector", $repeat_sector);
  2839. // return ($data['page'] == '2' || $data['page'] == 2);
  2840. $sector = array_merge($repeat_sector,$sector_zuhe1);
  2841. }else{
  2842. $sector = $sector_zuhe1;
  2843. }
  2844. // var_dump("sector_ids",$sector_ids);
  2845. // return [
  2846. // 'random_sector'=>$random_sector,
  2847. // 'must_sector'=>$must_sector,
  2848. // 'sector_ids'=>$sector_ids,
  2849. // ];
  2850. // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
  2851. // var_dump("sector",$sector);
  2852. // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
  2853. $sector_specal_sort = array_column($sector,'sector_type');
  2854. if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort) || in_array(5,$sector_specal_sort)){
  2855. $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
  2856. $pic_key = array_search(7,$sector_specal_sort) ?? null;
  2857. $cat_key = array_search(5,$sector_specal_sort) ?? null;
  2858. $toutiao = $sector[$toutiao_key];
  2859. $pic = $sector[$pic_key];
  2860. $cat = $sector[$cat_key];
  2861. $sort_1 = $sector[0];
  2862. $sort_2 = $sector[1];
  2863. $sort_3 = $sector[2];
  2864. // return $sector_specal_sort;
  2865. // return [
  2866. // 'sector_ids' => array_column($sector_zuhe1,'id'),
  2867. // // 'repeat_sector' => $repeat_sector,
  2868. // 'sector_id' => array_column($sector,'id'),
  2869. // 'array_intersect' => count(array_intersect($sector,$must_sector)),
  2870. // 'old' => [ $toutiao_key,$pic_key,$cat_key],
  2871. // 'new' => [ $sort_1,$sort_2,$sort_3]
  2872. // ];
  2873. if($data['page'] == 1 || $data['page'] == '1'){
  2874. if($toutiao_key){
  2875. $sector[0] = $toutiao;
  2876. $sector[$toutiao_key] = $sort_1;
  2877. $sector[1] = $pic;
  2878. $sector[$pic_key] = $sort_2;
  2879. }
  2880. if($cat_key){
  2881. $sector[0] = $pic;
  2882. $sector[$pic_key] = $sort_1;
  2883. }
  2884. }
  2885. if($data['page'] == '2' || $data['page'] == 2 || in_array(5,$sector_specal_sort)){
  2886. $sector[0] = $cat;
  2887. $sector[$cat_key] = $sort_1;
  2888. if($toutiao_key && $pic_key){
  2889. $sector[1] = $toutiao;
  2890. $sector[$toutiao_key] = $sort_2;
  2891. $sector[2] = $pic;
  2892. $sector[$pic_key] = $sort_3;
  2893. }
  2894. if($pic_key){
  2895. $sector[1] = $pic;
  2896. $sector[$pic_key] = $sort_2;
  2897. }
  2898. }
  2899. }
  2900. $sector_id = array_column($sector,'id');
  2901. // if(count(array_intersect($sector,$must_sector)) != count($must_sector)){
  2902. // return '此页面缺少必要通栏!';
  2903. // }
  2904. $data['page'] = $page;
  2905. $component[] = $this->randomComponent($sector_id,$data,$sector);
  2906. var_dump("sector_id",$sector_id);
  2907. return ['sector_id'=>$sector_id,
  2908. // 'sector'=>$sector,
  2909. 'component'=>$component,
  2910. // 'sector'=>$sector,
  2911. ];
  2912. }
  2913. public function randomComponent($sector_id,$data,$sector){
  2914. $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id)
  2915. ->leftJoin('component','component.component_type','sector_component.component_id')
  2916. ->leftJoin('component_img','component_img.component_id','component.component_type')
  2917. ->where('component_img.template_id','=',$data['template_id'])
  2918. ->select('component.*','component_img.img_name','component_img.img_id','component_img.img_url',
  2919. 'sector_component.sort_id','sector_component.sectorid')
  2920. ->orderBy('sector_component.sort_id')
  2921. ->orderBy('component.component_type')
  2922. ->get();
  2923. // 按sectorid和sort_id分组数据
  2924. $groupedResults = [];
  2925. $sort_ids = [];
  2926. $sector_ids = [];
  2927. $component_list = [];
  2928. $component_ids = [];
  2929. $img_ids = [];
  2930. $sort_key = 0;
  2931. // return $rawResults;
  2932. // $groupedResults重构数组,将相同位置的组件合并,相同组件的样式合并(已经包括所有选择的组件及组件样式)
  2933. // $sort_component = array_column($rawResults,'sort_id');
  2934. foreach ($rawResults as $key => $item) {
  2935. $sectorId = $item->sectorid;
  2936. $sortId = $item->sort_id;
  2937. $componentId = $item->component_type;
  2938. $component_data = $item->component_data;
  2939. $component_column = $item->component_column;
  2940. $component_type = $item->type_id;
  2941. $component_ad = $item->ad;
  2942. $imgId = $item->img_id;
  2943. if (!isset($groupedResults[$sectorId])) {
  2944. $groupedResults[$sectorId] = [];
  2945. }
  2946. $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : [];
  2947. $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : [];
  2948. $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : [];
  2949. $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : [];
  2950. // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
  2951. if (!isset($componentData['componentData']) || !is_array($componentData['componentData']) ) {
  2952. $componentData['componentData'] = [];
  2953. }
  2954. $componentData['componentData'] += $component_listtype;
  2955. $component_code = $componentData;
  2956. $component_list[$key] = $component_code;
  2957. // $component_list[$key]['listType'] = $component_listtype;
  2958. // }
  2959. $sort_id = intval($sortId-1);
  2960. // if(!isset($groupedResults[$sectorId][$sort_id])) {
  2961. // // $groupedResults[$sectorId][$sortId] = [];
  2962. // $com_key = 0;
  2963. // $groupedResults[$sectorId][$sort_id][$com_key] = [
  2964. // 'component_type' => $item->component_type,
  2965. // 'type_id' => $component_type,
  2966. // 'sort' => $sortId,
  2967. // 'width' => $item->component_width,
  2968. // 'height' => $item->component_height,
  2969. // 'images' => [
  2970. // 'img_name' => $item->img_name,
  2971. // 'img_id' => $item->img_id,
  2972. // 'img_url' => $item->img_url,
  2973. // 'component_type' => $component_type,
  2974. // ]
  2975. // ];
  2976. // // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数)
  2977. // }else{
  2978. // if((isset($component_ids) && in_array($componentId,$component_ids))){
  2979. // if(isset($img_arr)){
  2980. // $img_arr++;
  2981. // }else{
  2982. // $img_arr = 0;
  2983. // if(isset($com_key)){
  2984. // $com_key++;
  2985. // }
  2986. // }
  2987. // // $com_id_key = array_search($componentId,$component_ids);
  2988. // $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_arr] = [
  2989. // 'img_name' => $item->img_name,
  2990. // 'img_id' => $item->img_id,
  2991. // 'img_url' => $item->img_url,
  2992. // 'component_type' => $component_type,
  2993. // ];
  2994. // }else{
  2995. // $img_arr = 0;
  2996. // $com_key = $com_key ?? 0;
  2997. // $groupedResults[$sectorId][$sort_id][$com_key] = [
  2998. // 'component_type' => $item->component_type,
  2999. // 'type_id' => $component_type,
  3000. // 'sort' => $sortId,
  3001. // 'width' => $item->component_width,
  3002. // 'height' => $item->component_height,
  3003. // 'images' =>
  3004. // [
  3005. // $img_arr =>
  3006. // [
  3007. // 'img_name' => $item->img_name,
  3008. // 'img_id' => $item->img_id,
  3009. // 'img_url' => $item->img_url,
  3010. // 'component_type' => $component_type,
  3011. // ]
  3012. // ]
  3013. // ];
  3014. // }
  3015. // }
  3016. // $component = $groupedResults[$sectorId][$sort_id];
  3017. // if(count($component) > 1){
  3018. // $com_key = 0;
  3019. // }
  3020. // if(!in_array($sectorId,$sector_ids)){
  3021. // $sort_key = 0;
  3022. // $sector_ids[$sectorId][$sort_key] = $sectorId;
  3023. // }else{
  3024. // $sort_key++;
  3025. // $sector_ids[$sectorId][$sort_key] = $sectorId;
  3026. // }
  3027. if(!isset($com_key) || !in_array($sectorId,$sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])){
  3028. $com_key = 0;
  3029. // if(!in_array($componentId,$component_ids)){
  3030. // $img_key = 0;
  3031. // }
  3032. $img_key = 0;
  3033. }else if(in_array($sortId,$sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId,$component_ids)){
  3034. $com_key++;
  3035. }
  3036. if(in_array($componentId,$component_ids) && !in_array($imgId,$img_ids[$componentId])){
  3037. $img_key++;
  3038. }else{
  3039. $img_key=0;
  3040. }
  3041. if(!in_array($componentId,$component_ids)){
  3042. $groupedResults[$sectorId][$sort_id][$com_key] = [
  3043. 'component_type' => $item->component_type,
  3044. 'type_id' => $component_type,
  3045. 'sort' => $sortId,
  3046. 'width' => $item->component_width,
  3047. 'height' => $item->component_height,
  3048. ];
  3049. }
  3050. $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_key] = [
  3051. 'img_name' => $item->img_name,
  3052. 'img_id' => $imgId,
  3053. 'img_url' => $item->img_url,
  3054. 'component_type' => $componentId,
  3055. ];
  3056. // $component_type[$key] = $groupedResults[$sectorId][$sort_id]['component_type'];
  3057. // $sort_com = $groupedResults[$sectorId][$sort_id]['component_type'];
  3058. // if(count($sort_com) == 1){
  3059. // $com_key = 0;
  3060. // }
  3061. // // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类;
  3062. // // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类;
  3063. if (is_scalar($sectorId) && is_scalar($sort_id)) {
  3064. if(!in_array($component_type,[10,12,16])){
  3065. if($component_type == 11 || $component_type == 14){
  3066. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad'] = $component_ad;
  3067. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id'];
  3068. }
  3069. if($component_type != 11){
  3070. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
  3071. }
  3072. }else{
  3073. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = [];
  3074. }
  3075. } else {
  3076. // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
  3077. trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
  3078. }
  3079. // // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
  3080. $component_ids[$key] = $componentId;
  3081. if (!isset($sort_ids[$sectorId])) {
  3082. $sort_ids[$sectorId] = [];
  3083. }
  3084. if (!in_array($sortId, $sort_ids[$sectorId])) {
  3085. $sort_ids[$sectorId][] = $sortId;
  3086. }
  3087. if (!isset($img_ids[$componentId])) {
  3088. $img_ids[$componentId] = [];
  3089. }
  3090. if (!in_array($imgId, $img_ids[$componentId])) {
  3091. $img_ids[$componentId][] = $imgId;
  3092. }
  3093. $sector_ids[$key] = $sectorId;
  3094. // $img_ids[$componentId] = $imgId;
  3095. }
  3096. // return $groupedResults;
  3097. $result = [];
  3098. // return $sectors;
  3099. $y_num = 0;
  3100. foreach ($sector as $key => $value) {
  3101. // array_push($value['sort_id'],$key);
  3102. // $sector_id_ = $value['sector_id'];
  3103. $sectorid = $value['id'];
  3104. $template_data[$key] = [
  3105. 'sectorName' => $value['sector_id'],
  3106. 'sort' => $key+1,
  3107. ];
  3108. $date = date('mdHisu' ); // 获取时分秒微秒,转换为纯数字格式
  3109. // $date = 11010630021;
  3110. if(substr($date,0,1) == 0){
  3111. $date = substr($date, 0, 10); // 截取前9位
  3112. }else{
  3113. $date = substr($date, 0, 9); // 截取前9位
  3114. }
  3115. $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数
  3116. $y_num = $y_num + $value['pic_height'];
  3117. $sector_width = $value['sector_width']/100;
  3118. $canvas_data[$key] = [
  3119. 'i' => $random_num,
  3120. 'x' => 0,
  3121. 'y' => $y_num,
  3122. 'w' => $sector_width,
  3123. 'h' => $value['pic_height'],
  3124. 'type' => $value['sector_id'],
  3125. 'content' => [
  3126. 'sectorName' => $value['sector_id'],
  3127. ]
  3128. ];
  3129. // $sector_key[$key] = array_keys($value );
  3130. $sector_component = [];
  3131. if(isset($groupedResults[$sectorid]) && !empty($groupedResults[$sectorid]) && is_array($groupedResults[$sectorid])){
  3132. foreach ($groupedResults[$sectorid] as $sortId => $components) {
  3133. // 通栏某一位置的随机组件
  3134. if(count($components) > 1){
  3135. $componentIds = count($components);
  3136. $selectedComponentId = rand(0, $componentIds - 1);
  3137. }else{
  3138. $selectedComponentId = 0;
  3139. }
  3140. // var_dump($components[$selectedComponentId]);
  3141. // $Component = $components[$selectedComponentId];
  3142. $Component['component_type'] = $components[$selectedComponentId]['component_type'] ?? 0;
  3143. $Component['sort'] = $components[$selectedComponentId]['sort'] ?? 1;
  3144. $Component['type_id'] = $components[$selectedComponentId]['type_id'] ?? 0;
  3145. // 通栏某一位置的随机组件样式图
  3146. // 检查 $Component['images'] 是否存在且为数组
  3147. if (isset($components[$selectedComponentId]['images']) && is_array($components[$selectedComponentId]['images'])) {
  3148. $images = count($components[$selectedComponentId]['images']);
  3149. $selectedImage = 0;
  3150. if ($images > 1) {
  3151. $selectedImage = rand(0, $images - 1);
  3152. }
  3153. if (isset($components[$selectedComponentId]['images'][$selectedImage])) {
  3154. $Component['component_style'] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage]->img_id : $components[$selectedComponentId]['images'][$selectedImage]['img_id'];
  3155. }
  3156. }
  3157. $image_info[$key] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage] : $components[$selectedComponentId]['images'][$selectedImage];
  3158. // var_dump($images);
  3159. $Component['componentData'] = $components[$selectedComponentId]['componentData'] ?? [];
  3160. if($data['page'] == 1 &&(isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'],[7,14,15])) ){
  3161. $Component['componentData']['name'] = '请选择栏目';
  3162. }
  3163. if($data['page'] == 2 && (isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'],[7,14,15]))){
  3164. $Component['componentData']['name'] = '自动生成';
  3165. }
  3166. if(isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 11){
  3167. $ad = $components[$selectedComponentId]['ad'];
  3168. $ad['thumb'] = $image_info[$key]['img_url'];
  3169. $template_data[$key]['ad'] = $ad;
  3170. $canvas_adinfo[$key] = $ad;
  3171. $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
  3172. // // 确保 $ad 和 $canvas_adinfo[$key] 元素数量一致,直接组合
  3173. // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3174. $Component['componentData'] = [];
  3175. }
  3176. if(isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 14){
  3177. $ad = $components[$selectedComponentId]['ad'];
  3178. // $ad['thumb'] = $image_info[$key]['img_url'];
  3179. $template_data[$key]['ad'] = $ad;
  3180. $canvas_adinfo[$key] = $ad;
  3181. $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
  3182. // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3183. // // $Component['componentData'] = [];
  3184. }
  3185. // var_dump('---------------------------',$components[$selectedComponentId]);
  3186. $sector_component[$sortId] = $Component;
  3187. // $canvas_component[$sortId] = $Component;
  3188. // $sector_component[$sortId] = $selectedComponentId;
  3189. }
  3190. // var_dump("image",$image);
  3191. }
  3192. $template_data[$key]['componentList'] = $sector_component;
  3193. $canvas_data[$key]['content']['componentList'] = $sector_component;
  3194. // $canvas_data[$key]['ad'] = $key;
  3195. $canvas_data[$key]['dataSort'] = $key;
  3196. $canvas_data[$key]['moved'] = false;
  3197. // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
  3198. }
  3199. // return $canvas_data;
  3200. return [
  3201. 'template_data' => $template_data,
  3202. 'canvas_data' => $canvas_data,
  3203. ];
  3204. }
  3205. }