PublicRpcService.php 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265
  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['article_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['aloneList_rule']);
  2642. break;
  2643. case 7:
  2644. $index_rule = json_decode($rule['aloneArticle_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 $is_must;
  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. $must_sector = array_values(array_intersect_key($sectors, array_flip(array_keys($must_type))));
  2744. $must_count = count($must_sector);
  2745. // var_dump("must_sector",$must_sector);
  2746. var_dump("must_count",$must_count);
  2747. // return $must_sector;
  2748. // 计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
  2749. $repeat_num = $sector_num-$rand_count-$must_count;
  2750. // 组合起来通栏
  2751. $sector_zuhe1 = array_merge($must_sector,$random_sector);
  2752. // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
  2753. $rule_repeat = (array)$rule->type_max;
  2754. $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type'));
  2755. // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
  2756. foreach($rule_repeat as $key => $value){
  2757. if(in_array($key,array_keys($sector_zuhe1_type))){
  2758. $rule_repeat[$key] = $value-1;
  2759. }
  2760. }
  2761. var_dump("repeat_num",$repeat_num);
  2762. // return $sector_zuhe1;
  2763. // 剩余通栏进行重复处理
  2764. // $rule_repeat = (array)$rule->type_max;
  2765. // 取到可以重复的通栏类别
  2766. $rule_repaet_type = array_keys($rule_repeat);
  2767. $repeat_type = array_intersect_key($sector_types, array_flip($rule_repaet_type));
  2768. // 获取可以随机重复的通栏
  2769. $repaet_key = 0;
  2770. $num = 0;
  2771. $type = [];
  2772. // 防止无限循环,设置最大迭代次数
  2773. $max_iterations = 1000;
  2774. $iteration_count = 0;
  2775. // return $rule_repeat;
  2776. while($repaet_key < $repeat_num && $iteration_count < $max_iterations){
  2777. $num++;
  2778. foreach($repeat_type as $key => $value){
  2779. $type[$key] = $num;
  2780. // $type代表每个类型的通栏数量,每个类型的通栏的数量应该≤规则限制的数量
  2781. if($repaet_key < $repeat_num && $type[$key] <= $rule_repeat[$key]-1){
  2782. // $max_repaert_num = $rule_repeat[$key];
  2783. // if($max_repaert_num < $num ){
  2784. // $arr_key = $num+$key;
  2785. if (count($value) > 0) {
  2786. // 若 可以随机的通栏类型存在多个通栏,随机取其一
  2787. $repeat_sector_key[$repaet_key] = $value[array_rand($value)];
  2788. } else {
  2789. $repeat_sector_key[$repaet_key] = $value[0];
  2790. }
  2791. // $type[$repaet_key] = $repeat_sector_key[$repaet_key];
  2792. $repaet_key++;
  2793. // }
  2794. }else{
  2795. break;
  2796. }
  2797. }
  2798. $iteration_count++;
  2799. }
  2800. // var_dump("num", $num);
  2801. // var_dump("repeat_sector_key", $repeat_sector_key);
  2802. // 随机的重复通栏键值转换成真正的通栏
  2803. $repeat_sector = [];
  2804. foreach ($repeat_sector_key as $key => $value) {
  2805. if (isset($sectors[$value])) {
  2806. $repeat_sector[$key] = $sectors[$value];
  2807. }
  2808. }
  2809. $repeat_count = count($repeat_sector);
  2810. var_dump("repeat_count",$repeat_count);
  2811. // var_dump("repeat_sector", $repeat_sector);
  2812. $sector = array_merge($repeat_sector,$sector_zuhe1);
  2813. // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
  2814. $sector_specal_sort = array_column($sector,'sector_type');
  2815. if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort)){
  2816. $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
  2817. $pic_key = array_search(7,$sector_specal_sort) ?? null;
  2818. $toutiao = $sector[$toutiao_key];
  2819. $pic = $sector[$pic_key];
  2820. $soty_1 = $sector[0];
  2821. $sort_2 = $sector[1];
  2822. if(!empty($toutiao_key)){
  2823. $sector[0] = $toutiao;
  2824. $sector[$toutiao_key] = $soty_1;
  2825. $sector[1] = $pic;
  2826. $sector[$pic_key] = $sort_2;
  2827. }else{
  2828. $sector[0] = $pic;
  2829. $sector[$pic_key] = $sort_2;
  2830. }
  2831. }
  2832. $sector_id = array_column($sector,'id');
  2833. $data['page'] = $page;
  2834. $component[] = $this->randomComponent($sector_id,$data,$sector);
  2835. var_dump("sector_id",$sector_id);
  2836. return ['sector_id'=>$sector_id,
  2837. // 'sector'=>$sector,
  2838. 'component'=>$component,
  2839. // 'sector'=>$sector,
  2840. ];
  2841. }
  2842. public function randomComponent($sector_id,$data,$sector){
  2843. $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id)
  2844. ->leftJoin('component','component.component_type','sector_component.component_id')
  2845. ->leftJoin('component_img','component_img.component_id','component.component_type')
  2846. ->where('component_img.template_id','=',$data['template_id'])
  2847. ->select('component.*','component_img.img_name','component_img.img_id','component_img.img_url',
  2848. 'sector_component.sort_id','sector_component.sectorid')
  2849. ->orderBy('sector_component.sort_id')
  2850. ->orderBy('component.component_type')
  2851. ->get();
  2852. // 按sectorid和sort_id分组数据
  2853. $groupedResults = [];
  2854. $sort_ids = [];
  2855. $sector_ids = [];
  2856. $component_list = [];
  2857. $component_ids = [];
  2858. $img_ids = [];
  2859. $sort_key = 0;
  2860. // return $rawResults;
  2861. // $groupedResults重构数组,将相同位置的组件合并,相同组件的样式合并(已经包括所有选择的组件及组件样式)
  2862. // $sort_component = array_column($rawResults,'sort_id');
  2863. foreach ($rawResults as $key => $item) {
  2864. $sectorId = $item->sectorid;
  2865. $sortId = $item->sort_id;
  2866. $componentId = $item->component_type;
  2867. $component_data = $item->component_data;
  2868. $component_column = $item->component_column;
  2869. $component_type = $item->type_id;
  2870. $component_ad = $item->ad;
  2871. $imgId = $item->img_id;
  2872. if (!isset($groupedResults[$sectorId])) {
  2873. $groupedResults[$sectorId] = [];
  2874. }
  2875. $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : [];
  2876. $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : [];
  2877. $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : [];
  2878. $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : [];
  2879. // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
  2880. if (!isset($componentData['componentData']) || !is_array($componentData['componentData']) ) {
  2881. $componentData['componentData'] = [];
  2882. }
  2883. $componentData['componentData'] += $component_listtype;
  2884. $component_code = $componentData;
  2885. $component_list[$key] = $component_code;
  2886. // $component_list[$key]['listType'] = $component_listtype;
  2887. // }
  2888. $sort_id = intval($sortId-1);
  2889. // if(!isset($groupedResults[$sectorId][$sort_id])) {
  2890. // // $groupedResults[$sectorId][$sortId] = [];
  2891. // $com_key = 0;
  2892. // $groupedResults[$sectorId][$sort_id][$com_key] = [
  2893. // 'component_type' => $item->component_type,
  2894. // 'type_id' => $component_type,
  2895. // 'sort' => $sortId,
  2896. // 'width' => $item->component_width,
  2897. // 'height' => $item->component_height,
  2898. // 'images' => [
  2899. // 'img_name' => $item->img_name,
  2900. // 'img_id' => $item->img_id,
  2901. // 'img_url' => $item->img_url,
  2902. // 'component_type' => $component_type,
  2903. // ]
  2904. // ];
  2905. // // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数)
  2906. // }else{
  2907. // if((isset($component_ids) && in_array($componentId,$component_ids))){
  2908. // if(isset($img_arr)){
  2909. // $img_arr++;
  2910. // }else{
  2911. // $img_arr = 0;
  2912. // if(isset($com_key)){
  2913. // $com_key++;
  2914. // }
  2915. // }
  2916. // // $com_id_key = array_search($componentId,$component_ids);
  2917. // $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_arr] = [
  2918. // 'img_name' => $item->img_name,
  2919. // 'img_id' => $item->img_id,
  2920. // 'img_url' => $item->img_url,
  2921. // 'component_type' => $component_type,
  2922. // ];
  2923. // }else{
  2924. // $img_arr = 0;
  2925. // $com_key = $com_key ?? 0;
  2926. // $groupedResults[$sectorId][$sort_id][$com_key] = [
  2927. // 'component_type' => $item->component_type,
  2928. // 'type_id' => $component_type,
  2929. // 'sort' => $sortId,
  2930. // 'width' => $item->component_width,
  2931. // 'height' => $item->component_height,
  2932. // 'images' =>
  2933. // [
  2934. // $img_arr =>
  2935. // [
  2936. // 'img_name' => $item->img_name,
  2937. // 'img_id' => $item->img_id,
  2938. // 'img_url' => $item->img_url,
  2939. // 'component_type' => $component_type,
  2940. // ]
  2941. // ]
  2942. // ];
  2943. // }
  2944. // }
  2945. // $component = $groupedResults[$sectorId][$sort_id];
  2946. // if(count($component) > 1){
  2947. // $com_key = 0;
  2948. // }
  2949. // if(!in_array($sectorId,$sector_ids)){
  2950. // $sort_key = 0;
  2951. // $sector_ids[$sectorId][$sort_key] = $sectorId;
  2952. // }else{
  2953. // $sort_key++;
  2954. // $sector_ids[$sectorId][$sort_key] = $sectorId;
  2955. // }
  2956. if(!isset($com_key) || !in_array($sectorId,$sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])){
  2957. $com_key = 0;
  2958. // if(!in_array($componentId,$component_ids)){
  2959. // $img_key = 0;
  2960. // }
  2961. $img_key = 0;
  2962. }else if(in_array($sortId,$sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId,$component_ids)){
  2963. $com_key++;
  2964. }
  2965. if(in_array($componentId,$component_ids) && !in_array($imgId,$img_ids[$componentId])){
  2966. $img_key++;
  2967. }else{
  2968. $img_key=0;
  2969. }
  2970. if(!in_array($componentId,$component_ids)){
  2971. $groupedResults[$sectorId][$sort_id][$com_key] = [
  2972. 'component_type' => $item->component_type,
  2973. 'type_id' => $component_type,
  2974. 'sort' => $sortId,
  2975. 'width' => $item->component_width,
  2976. 'height' => $item->component_height,
  2977. ];
  2978. }
  2979. $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_key] = [
  2980. 'img_name' => $item->img_name,
  2981. 'img_id' => $imgId,
  2982. 'img_url' => $item->img_url,
  2983. 'component_type' => $componentId,
  2984. ];
  2985. // $component_type[$key] = $groupedResults[$sectorId][$sort_id]['component_type'];
  2986. // $sort_com = $groupedResults[$sectorId][$sort_id]['component_type'];
  2987. // if(count($sort_com) == 1){
  2988. // $com_key = 0;
  2989. // }
  2990. // // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类;
  2991. // // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类;
  2992. if (is_scalar($sectorId) && is_scalar($sort_id)) {
  2993. if(!in_array($component_type,[10,12,16])){
  2994. if($component_type == 15){
  2995. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad'] = $component_ad;
  2996. }else{
  2997. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
  2998. }
  2999. }else{
  3000. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = [];
  3001. }
  3002. } else {
  3003. // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
  3004. trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
  3005. }
  3006. // // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
  3007. $component_ids[$key] = $componentId;
  3008. if (!isset($sort_ids[$sectorId])) {
  3009. $sort_ids[$sectorId] = [];
  3010. }
  3011. if (!in_array($sortId, $sort_ids[$sectorId])) {
  3012. $sort_ids[$sectorId][] = $sortId;
  3013. }
  3014. if (!isset($img_ids[$componentId])) {
  3015. $img_ids[$componentId] = [];
  3016. }
  3017. if (!in_array($imgId, $img_ids[$componentId])) {
  3018. $img_ids[$componentId][] = $imgId;
  3019. }
  3020. $sector_ids[$key] = $sectorId;
  3021. // $img_ids[$componentId] = $imgId;
  3022. }
  3023. // return $groupedResults;
  3024. $result = [];
  3025. // return $sectors;
  3026. $y_num = 0;
  3027. foreach ($sector as $key => $value) {
  3028. // array_push($value['sort_id'],$key);
  3029. // $sector_id_ = $value['sector_id'];
  3030. $sectorid = $value['id'];
  3031. $template_data[$key] = [
  3032. 'sectorName' => $value['sector_id'],
  3033. 'sort' => $key+1,
  3034. ];
  3035. $date = date('mdHisu' ); // 获取时分秒微秒,转换为纯数字格式
  3036. // $date = 11010630021;
  3037. if(substr($date,0,1) == 0){
  3038. $date = substr($date, 0, 10); // 截取前9位
  3039. }else{
  3040. $date = substr($date, 0, 9); // 截取前9位
  3041. }
  3042. $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数
  3043. $y_num = $y_num + $value['pic_height'];
  3044. $sector_width = $value['sector_width']/100;
  3045. $canvas_data[$key] = [
  3046. 'i' => $random_num,
  3047. 'x' => 0,
  3048. 'y' => $y_num,
  3049. 'w' => $sector_width,
  3050. 'h' => $value['pic_height'],
  3051. 'type' => $value['sector_id'],
  3052. 'content' => [
  3053. 'sectorName' => $value['sector_id'],
  3054. ]
  3055. ];
  3056. // $sector_key[$key] = array_keys($value );
  3057. $sector_component = [];
  3058. if(isset($groupedResults[$sectorid]) && !empty($groupedResults[$sectorid]) && is_array($groupedResults[$sectorid])){
  3059. foreach ($groupedResults[$sectorid] as $sortId => $components) {
  3060. // 通栏某一位置的随机组件
  3061. if(count($components) > 1){
  3062. $componentIds = count($components);
  3063. $selectedComponentId = rand(0, $componentIds - 1);
  3064. }else{
  3065. $selectedComponentId = 0;
  3066. }
  3067. // var_dump($components[$selectedComponentId]);
  3068. // $Component = $components[$selectedComponentId];
  3069. $Component['component_type'] = $components[$selectedComponentId]['component_type'] ?? 0;
  3070. $Component['sort'] = $components[$selectedComponentId]['sort'] ?? 1;
  3071. $Component['type_id'] = $components[$selectedComponentId]['type_id'] ?? 0;
  3072. // 通栏某一位置的随机组件样式图
  3073. // 检查 $Component['images'] 是否存在且为数组
  3074. if (isset($components[$selectedComponentId]['images']) && is_array($components[$selectedComponentId]['images'])) {
  3075. $images = count($components[$selectedComponentId]['images']);
  3076. $selectedImage = 0;
  3077. if ($images > 1) {
  3078. $selectedImage = rand(0, $images - 1);
  3079. }
  3080. if (isset($components[$selectedComponentId]['images'][$selectedImage])) {
  3081. $Component['component_style'] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage]->img_id : $components[$selectedComponentId]['images'][$selectedImage]['img_id'];
  3082. }
  3083. }
  3084. $image_info[$key] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage] : $components[$selectedComponentId]['images'][$selectedImage];
  3085. // var_dump($images);
  3086. $Component['componentData'] = $components[$selectedComponentId]['componentData'];
  3087. if($data['page'] == 1 &&(isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'],[7,14,15])) ){
  3088. $Component['componentData']['name'] = '请选择栏目';
  3089. }
  3090. if($data['page'] == 2 && (isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'],[7,14,15]))){
  3091. $Component['componentData']['name'] = '自动生成';
  3092. }
  3093. if(isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 11){
  3094. $ad = $components[$selectedComponentId]['componentData']['ad'];
  3095. $template_data[$key]['ad'] = $ad;
  3096. $values = [
  3097. $components[$selectedComponentId]['width'],
  3098. $components[$selectedComponentId]['height'],
  3099. '',
  3100. '',
  3101. '',
  3102. $data['website_id'],
  3103. $image_info[$key]['img_url'],
  3104. 2,
  3105. 0
  3106. ];
  3107. $canvas_adinfo[$key] = array_combine(array_slice($ad, 0, count($values)), $values);
  3108. $canvas_data[$key]['ad'] = $canvas_adinfo[$key];
  3109. // 确保 $ad 和 $canvas_adinfo[$key] 元素数量一致,直接组合
  3110. // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3111. $Component['componentData'] = [];
  3112. }
  3113. if(isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 15){
  3114. $ad = $components[$selectedComponentId]['ad'];
  3115. $template_data[$key]['ad'] = $ad;
  3116. $values = [
  3117. $components[$selectedComponentId]['width'],
  3118. $components[$selectedComponentId]['height'],
  3119. '',
  3120. '',
  3121. '',
  3122. $data['website_id'],
  3123. $image_info[$key]['img_url'],
  3124. 2,
  3125. 0
  3126. ];
  3127. $canvas_adinfo[$key] = array_combine(array_slice($ad, 0, count($values)), $values);
  3128. $canvas_data[$key]['ad'] = $canvas_adinfo[$key];
  3129. // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3130. $Component['componentData'] = [];
  3131. }
  3132. // var_dump('---------------------------',$components[$selectedComponentId]);
  3133. $sector_component[$sortId] = $Component;
  3134. // $canvas_component[$sortId] = $Component;
  3135. // $sector_component[$sortId] = $selectedComponentId;
  3136. }
  3137. // var_dump("image",$image);
  3138. }
  3139. $template_data[$key]['componentList'] = $sector_component;
  3140. $canvas_data[$key]['content']['componentList'] = $sector_component;
  3141. // $canvas_data[$key]['ad'] = $key;
  3142. $canvas_data[$key]['dataSort'] = $key;
  3143. $canvas_data[$key]['moved'] = false;
  3144. // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
  3145. }
  3146. // return $canvas_data;
  3147. return [
  3148. 'template_data' => $template_data,
  3149. 'canvas_data' => $canvas_data,
  3150. ];
  3151. }
  3152. }