PublicRpcService.php 111 KB

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