ThemeSetController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2020/6/10
  6. * Time: 14:39
  7. */
  8. namespace app\platform\modules\user\controllers;
  9. use app\common\models\UniAccount;
  10. use app\platform\modules\user\models\OfficialWebsiteContact;
  11. use app\platform\modules\user\models\OfficialWebsiteMultiple;
  12. use app\platform\modules\user\models\OfficialWebsiteTheme;
  13. use Illuminate\Http\Request;
  14. use app\platform\controllers\BaseController;
  15. use Yunshop\Article\models\Article;
  16. use Yunshop\Article\models\Category;
  17. use Yunshop\HelpCenter\models\HelpCenterAddModel;
  18. class ThemeSetController extends BaseController
  19. {
  20. public function index()
  21. {
  22. $set = \Setting::get('official_website.theme_set');
  23. if (!isset($set['is_open'])) {
  24. $set['is_open'] = 1;
  25. $set['is_customize'] = 0;
  26. $set['customize_url'] = '';
  27. \Setting::set('official_website.theme_set',$set);
  28. }
  29. $data = request()->is_open;
  30. $is_customize = request()->is_customize;
  31. $customize_url = request()->customize_url;
  32. if ($data) {
  33. $set['is_open'] = $data;
  34. $set['is_customize'] = $is_customize;
  35. $set['customize_url'] = $customize_url;
  36. \Setting::set('official_website.theme_set',$set);
  37. }
  38. return $this->successJson('',$set);
  39. }
  40. public function createTheme()
  41. {
  42. $data = request()->data;
  43. if (empty($data)) {
  44. return $this->errorJson("数据为空");
  45. }
  46. $validate = $this->validate($this->rules(), $data, $this->message());
  47. if ($validate) {
  48. return $validate;
  49. }
  50. $official = new OfficialWebsiteTheme();
  51. $official->title = $data['title'];
  52. $official->is_default = $data['is_default'];
  53. if ($data['is_default'] == 1) {
  54. OfficialWebsiteTheme::where("is_default",1)->update(["is_default"=>0]);
  55. }
  56. if ($official->save()) {
  57. return $this->successJson('成功');
  58. } else {
  59. return $this->errorJson('失败');
  60. }
  61. }
  62. public function themeList()
  63. {
  64. $list = OfficialWebsiteTheme::orderBy('id','desc')->paginate(10);
  65. return $this->successJson("",$list);
  66. }
  67. public function editTheme()
  68. {
  69. $data = request()->data;
  70. $status = request()->status;
  71. $id = request()->id;
  72. if (empty($id) || empty($status)) {
  73. return $this->errorJson('参数不存在');
  74. }
  75. if (empty($data)) {
  76. $theme = OfficialWebsiteTheme::where("id",$id)->first();
  77. $theme = empty($theme) ? [] : $theme->toArray();
  78. //解压数据
  79. $theme_data = $this->processData($status,$theme);
  80. $theme_data['identification'] = $theme['identification'];
  81. $theme_data['uniAccount'] = UniAccount::select("uniacid","name")->get();
  82. } else {
  83. //保存
  84. $rel = $this->saveData($status,$data,$id);
  85. if ($rel == true) {
  86. return $this->successJson('成功');
  87. } else {
  88. return $this->errorJson('失败');
  89. }
  90. }
  91. return $this->successJson('',$theme_data);
  92. }
  93. public function processData($status,$data)
  94. {
  95. $result = [];
  96. if (empty($data)) {
  97. return $result;
  98. }
  99. if ($status == 1) {
  100. $result = !empty($data['basic']) ? json_decode($data['basic'],true) : [];
  101. if (isset($result['cus_url'])) {
  102. $result['cus_url'] = $result['cus_url'];
  103. }
  104. } elseif ($status == 2) {
  105. $result = !empty($data['top']) ? json_decode($data['top'],true) : [];
  106. if ($result['logo']) {
  107. $result['logo'] = $result['logo'];
  108. }
  109. } elseif ($status == 3) {
  110. if ($data['tail']) {
  111. $result['bottom'] = htmlspecialchars_decode($data['tail']);
  112. }
  113. }
  114. return $result;
  115. }
  116. public function saveData($status,$data,$id=0)
  117. {
  118. if (empty($id)) {
  119. $official = new OfficialWebsiteTheme();
  120. } else {
  121. $official = OfficialWebsiteTheme::where("id",$id)->first();
  122. }
  123. if ($status == 1) {
  124. $data = json_encode($data);
  125. $official->basic = $data;
  126. } elseif ($status == 2) {
  127. $data = json_encode($data);
  128. $official->top = $data;
  129. } elseif ($status == 3) {
  130. $data = htmlspecialchars($data['bottom']);
  131. $official->tail = $data;
  132. } elseif ($status == 4) {
  133. if ($data['is_default'] == 1) {
  134. OfficialWebsiteTheme::where("id",">",0)->update(["is_default"=>0]);
  135. }
  136. $official->is_default = $data['is_default'];
  137. }
  138. if ($official->save()) {
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. public function dentList()
  145. {
  146. $theme = request()->theme_id;
  147. $theme = empty($theme) ? "" : $theme;
  148. $all_multiple = OfficialWebsiteMultiple::where("theme_id",$theme)->select("id","theme_id","name","identification")->get();
  149. $all_multiple = empty($all_multiple) ? [] : $all_multiple->toArray();
  150. return $this->successJson("",$all_multiple);
  151. }
  152. public function multipleList()
  153. {
  154. $data = request()->data;
  155. $theme_id = request()->theme_id;
  156. $theme_id = empty($theme_id) ? 0 : $theme_id;
  157. if (empty($theme_id) || !is_numeric($theme_id)) {
  158. return $this->errorJson("主题ID不能为空且必须为数字");
  159. }
  160. $theme_set = OfficialWebsiteTheme::where("id",$theme_id)->first();
  161. $identification = request()->identification;
  162. $identification = empty($identification) ? "" : $identification;
  163. if (!preg_match("/[a-zA-Z0-9_]{1,15}/",$identification)) {
  164. return $this->errorJson("路径名称只能是字母、数字、下划线且长度不超过15");
  165. }
  166. $multiple = [];
  167. if ($identification) {
  168. $multiple = OfficialWebsiteMultiple::where("theme_id",$theme_id)->where("identification",$identification)->first();
  169. $multiple = empty($multiple) ? [] : $multiple->toArray();
  170. if (isset($multiple['detail'])) {
  171. $multiple['detail'] = htmlspecialchars_decode($multiple['detail']);
  172. }
  173. $all_multiple = OfficialWebsiteMultiple::where("theme_id",$theme_id)->select("id","theme_id","name","identification")->get();
  174. $all_multiple = empty($all_multiple) ? [] : $all_multiple->toArray();
  175. $multiple['all_multiple'] = $all_multiple;
  176. }
  177. if ($data) {
  178. $repeat = OfficialWebsiteMultiple::where("identification",$identification)->where("theme_id",$theme_id)->first();
  179. if ($repeat) {
  180. $official = $repeat;
  181. } else {
  182. $official = new OfficialWebsiteMultiple();
  183. }
  184. $data['url'] = "https://".$_SERVER['HTTP_HOST']."/officialwebsite.php?page_name=".$identification;
  185. $validate = $this->validate($this->rules(), $data, $this->message());
  186. if ($validate) {
  187. return $validate;
  188. }
  189. $article = "";
  190. $help = "";
  191. if ($identification == "resource_page") {
  192. $basic = empty($theme_set) ? [] : json_decode($theme_set['basic'],true);
  193. $cus_uniacid = empty($basic['cus_uniacid']) ? 0 : $basic['cus_uniacid'];
  194. if (app('plugins')->isEnabled('article')) {
  195. $article = Category::where("uniacid",$cus_uniacid)->with(["hasManyArticle"=>function ($query){
  196. $query->select("id","category_id","title","desc","thumb","author","created_at","content")->where('state', 1)->where('type', '!=', 1)->orderBy("id","DESC")->offset(0)->limit(15);
  197. }])->get();
  198. $article = empty($article) ? "" : json_encode($article->toArray());
  199. }
  200. if (app('plugins')->isEnabled('help-center')) {
  201. $help = HelpCenterAddModel::where("uniacid",$cus_uniacid)->select('title', 'content')->orderBy('sort')->offset(0)->limit(25)->get();
  202. $help = empty($help) ? "" : json_encode($help->toArray());
  203. }
  204. }
  205. $official->theme_id = $theme_id;
  206. $official->name = $data['name'];
  207. $official->identification = $identification;
  208. $official->mul_title = $data['mul_title'];
  209. $official->url = $data['url'];
  210. $official->keyword = $data['keyword'];
  211. $official->describe = $data['describe'];
  212. $official->detail = htmlspecialchars($data['detail']);
  213. $official->article = $article;
  214. $official->helper = $help;
  215. if ($official->save()) {
  216. if ($theme_set['identification'] == "common") {
  217. if (!file_exists(base_path("/official/".$theme_set['identification']."/" . $identification . ".html"))) {
  218. $room = '<!DOCTYPE html>
  219. <html>
  220. <head lang="en">
  221. <meta http-equiv="content-Type" content="text/html;charset=UTF-8" />
  222. <meta http-equiv="Access-Control-Allow-Origin" content="*" />
  223. <meta name="keywords" content="<?php echo $keywords ?>">
  224. <meta name="description" content="<?php echo $describe ?>">
  225. <title><?php echo $title ?></title>
  226. <link rel="stylesheet" href="official/css/main.css" />
  227. </head>
  228. <body>
  229. <div id="app">
  230. <div class="nav">
  231. <div class="nav-a">
  232. <div class="nav-logo">
  233. <img src="<?php echo $top["logo"]?>" alt="" />
  234. </div>
  235. <?php foreach($top["navigation"] as $kk=>$vv) {?>
  236. <div class="nav-li <?php if($vv["tool"]==$name){?>nav-selected<?php }?>">
  237. <a href="<?php echo $vv["url"]?>"><?php echo $vv["name"]?></a>
  238. </div>
  239. <?php }?>
  240. <?php if ($top["is_sign"] == 1) {?>
  241. <a href="#" id="jump-url">
  242. <div class="nav-login">立即登录</div>
  243. </a>
  244. <?php }?>
  245. </div>
  246. </div>
  247. <?php echo $content;?>
  248. <?php echo $tail;?>
  249. <div class="all-relation">
  250. <div class="relation-btn" id="relation-btn">
  251. <div class="relation-btn-one" id="callus">
  252. <img src="official/images/lianxikefu.png" alt="">
  253. <div>联系客服</div>
  254. </div>
  255. <div class="relation-btn-one" id="back-top">
  256. <img src="official/images/huidaodingbu.png" alt="">
  257. <div>回到顶部</div>
  258. </div>
  259. </div>
  260. <div class="relation">
  261. <div class="relation-title">在线咨询</div>
  262. <a href="<?php echo $basic[\'cus_link\']?>" title="点击咨询">
  263. <div class="relation-tel">
  264. <div class="relation-tel-img">
  265. <img src="official/images/suspension_qq.png" alt="" />
  266. </div>
  267. <div class="relation-tel-word">在线客服</div>
  268. </div>
  269. </a>
  270. <div class="relation-tel">
  271. <div class="relation-tel-img">
  272. <img src="official/images/suspension_tel.png" alt="" />
  273. </div>
  274. <div class="relation-tel-word"><?php echo $basic["cus_mobile"]?></div>
  275. </div>
  276. <div class="relation-tel relation-tel1">
  277. <div class="relation-tel-img">
  278. <img src="official/images/suspension_wechat.png" alt="" />
  279. </div>
  280. <div class="relation-tel-word">微信二维码</div>
  281. </div>
  282. <div class="qr-img">
  283. <div class="qr-img"><img src="<?php echo $basic["cus_url"]?>" alt="" /></div>
  284. </div>
  285. <img id="close-btn" style="position: absolute;top:5px;right:5px" src="official/images/adsystem_icon_cancle.png" alt="">
  286. </div>
  287. </div>
  288. </div>
  289. <script type="text/javascript">
  290. var url = "";
  291. window.onload = function () {
  292. url = window.location.protocol + "//" + window.location.host + "/admin.html";
  293. if(document.getElementById("jump-url")) {
  294. document.getElementById("jump-url").setAttribute("href", url);
  295. }
  296. document.getElementById("callus").onclick = function(e) {
  297. document.getElementById("relation-btn").style.display="none";
  298. document.getElementsByClassName("all-relation")[0].classList.add("all-relation-hover")
  299. }
  300. document.getElementById("close-btn").onclick = function(e) {
  301. document.getElementById("relation-btn").style.display="inline-block";
  302. document.getElementsByClassName("all-relation")[0].classList.remove("all-relation-hover")
  303. }
  304. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  305. if(scrollTop>0) {
  306. document.getElementById("back-top").style.display = "block"
  307. }
  308. else{
  309. document.getElementById("back-top").style.display = "none"
  310. }
  311. document.getElementById("back-top").onclick = function(e) {
  312. if(document.documentElement.scrollTop) {
  313. document.documentElement.scrollTop = 0;
  314. }
  315. else{
  316. document.body.scrollTop = 0;
  317. }
  318. }
  319. window.onscroll = function() {
  320. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  321. if(scrollTop>0) {
  322. document.getElementById("back-top").style.display = "block"
  323. }
  324. else{
  325. document.getElementById("back-top").style.display = "none"
  326. }
  327. }
  328. };
  329. </script>
  330. </body>
  331. </html>';
  332. file_put_contents(base_path("/official/".$theme_set['identification']."/" . $identification . ".html"), $room);
  333. }
  334. } elseif ($theme_set['identification'] == "website") {
  335. if (!file_exists(base_path("/official/".$theme_set['identification']."/" . $identification . ".html"))) {
  336. $room = '<!DOCTYPE html>
  337. <html>
  338. <head lang="en">
  339. <meta http-equiv="content-Type" content="text/html;charset=UTF-8" />
  340. <meta http-equiv="Access-Control-Allow-Origin" content="*" />
  341. <meta name="keywords" content="<?php echo $keywords ?>">
  342. <meta name="description" content="<?php echo $describe ?>">
  343. <title><?php echo $title ?></title>
  344. <link rel="stylesheet" href="official/website/css/main.css" />
  345. </head>
  346. <body>
  347. <div id="app">
  348. <div class="nav">
  349. <div class="nav-a">
  350. <div class="nav-logo">
  351. <img src="<?php echo $top["logo"]?>" alt="" />
  352. </div>
  353. <?php foreach($top["navigation"] as $kk=>$vv) {?>
  354. <div class="nav-li <?php if($vv["tool"]==$name){?>nav-selected<?php }?>">
  355. <a href="<?php echo $vv["url"]?>"><?php echo $vv["name"]?></a>
  356. </div>
  357. <?php }?>
  358. <?php if ($top["is_sign"] == 1) {?>
  359. <a href="#" id="jump-url">
  360. <div class="nav-login">立即登录</div>
  361. </a>
  362. <?php }?>
  363. </div>
  364. </div>
  365. <?php echo $content;?>
  366. <?php echo $tail;?>
  367. <div class="all-relation">
  368. <div class="relation-btn" id="relation-btn">
  369. <div class="relation-btn-one" id="callus">
  370. <img src="official/website/images/lianxikefu.png" alt="" />
  371. <div>联系客服</div>
  372. </div>
  373. <div class="relation-btn-one" id="back-top">
  374. <img src="official/website/images/huidaodingbu.png" alt="" />
  375. <div>回到顶部</div>
  376. </div>
  377. </div>
  378. <div class="relation">
  379. <div class="relation-title">在线咨询</div>
  380. <a href="<?php echo $basic[\'cus_link\']?>" title="点击咨询">
  381. <div class="relation-tel">
  382. <div class="relation-tel-img">
  383. <img src="official/website/images/suspension_qq.png" alt="" />
  384. </div>
  385. <div class="relation-tel-word">在线客服</div>
  386. </div>
  387. </a>
  388. <div class="relation-tel">
  389. <div class="relation-tel-img">
  390. <img src="official/website/images/suspension_tel.png" alt="" />
  391. </div>
  392. <div class="relation-tel-word"><?php echo $basic[\'cus_mobile\']?></div>
  393. </div>
  394. <div class="relation-tel relation-tel1">
  395. <div class="relation-tel-img">
  396. <img src="official/website/images/suspension_wechat.png" alt="" />
  397. </div>
  398. <div class="relation-tel-word">微信二维码</div>
  399. </div>
  400. <div class="qr-img">
  401. <img src="<?php echo $basic[\'cus_url\']?>" alt="" />
  402. </div>
  403. <img id="close-btn" style="position: absolute; top: -15px; right: -25px" src="official/website/images/adsystem_icon_cancle.png" alt=""/>
  404. </div>
  405. </div>
  406. </div>
  407. <script type="text/javascript">
  408. var url = "";
  409. window.onload = function () {
  410. url = window.location.protocol + "//" + window.location.host + "/admin.html";
  411. if(document.getElementById("jump-url")) {
  412. document.getElementById("jump-url").setAttribute("href", url);
  413. }
  414. document.getElementById("callus").onclick = function(e) {
  415. document.getElementById("relation-btn").style.display="none";
  416. document.getElementsByClassName("all-relation")[0].classList.add("all-relation-hover")
  417. }
  418. document.getElementById("close-btn").onclick = function(e) {
  419. document.getElementById("relation-btn").style.display="inline-block";
  420. document.getElementsByClassName("all-relation")[0].classList.remove("all-relation-hover")
  421. }
  422. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  423. if(scrollTop>0) {
  424. document.getElementById("back-top").style.display = "block"
  425. }
  426. else{
  427. document.getElementById("back-top").style.display = "none"
  428. }
  429. document.getElementById("back-top").onclick = function(e) {
  430. if(document.documentElement.scrollTop) {
  431. document.documentElement.scrollTop = 0;
  432. }
  433. else{
  434. document.body.scrollTop = 0;
  435. }
  436. }
  437. window.onscroll = function() {
  438. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  439. if(scrollTop>0) {
  440. document.getElementById("back-top").style.display = "block"
  441. }
  442. else{
  443. document.getElementById("back-top").style.display = "none"
  444. }
  445. }
  446. };
  447. </script>
  448. </body>
  449. </html>';
  450. file_put_contents(base_path("/official/".$theme_set['identification']."/" . $identification . ".html"), $room);
  451. }
  452. }
  453. return $this->successJson("成功");
  454. } else {
  455. return $this->errorJson("失败");
  456. }
  457. }
  458. return $this->successJson("",$multiple);
  459. }
  460. public function contactList()
  461. {
  462. $list = OfficialWebsiteContact::orderBy("id","desc")->paginate(10);
  463. return $this->successJson("",$list);
  464. }
  465. /**
  466. * 处理表单验证
  467. *
  468. * @param array $rules
  469. * @param \Request|null $request
  470. * @param array $messages
  471. * @param array $customAttributes
  472. * @return \Illuminate\Http\JsonResponse
  473. */
  474. public function validate($rules, $request = null, $messages = [], $customAttributes = [])
  475. {
  476. if (!isset($request)) {
  477. $request = request();
  478. }
  479. $validator = $this->getValidationFactory()->make($request, $rules, $messages, $customAttributes);
  480. if ($validator->fails()) {
  481. return $this->errorJson($validator->errors()->all());
  482. }
  483. }
  484. /**
  485. * 表单验证自定义错误消息
  486. *
  487. * @return array
  488. */
  489. public function message()
  490. {
  491. return [
  492. 'title.required' => '主题名称不能为空',
  493. 'name.required' => '页面名称不能为空',
  494. 'mul_title.required' => '页面标题不能为空',
  495. 'keyword.required' => '关键字不能为空',
  496. 'url.required' => '路径不能为空'
  497. ];
  498. }
  499. /**
  500. * 表单验证规则
  501. *
  502. * @param $user
  503. * @param $data
  504. * @return array
  505. */
  506. public function rules()
  507. {
  508. $rules = [];
  509. if (request()->path() == "admin/user/create_theme") {
  510. $rules = [
  511. 'title' => 'required',
  512. ];
  513. } elseif (request()->path() == "admin/user/multiple_list") {
  514. $rules = [
  515. 'name' => 'required',
  516. 'mul_title' => 'required',
  517. 'keyword' => 'required',
  518. 'url' => 'required'
  519. ];
  520. }
  521. return $rules;
  522. }
  523. }