PcSettingLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\settings\pc;
  20. use app\common\enum\YesNoEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\service\ConfigService;
  23. use app\common\service\FileService;
  24. /**
  25. * PC商城渠道设置
  26. */
  27. class PcSettingLogic extends BaseLogic
  28. {
  29. /**
  30. * @notes 获取PC商城配置
  31. * @author Tab
  32. * @date 2021/11/29 18:04
  33. */
  34. public static function getConfig()
  35. {
  36. $shopName = ConfigService::get('shop', 'name');
  37. $config = [
  38. 'status' => ConfigService::get('pc', 'status', YesNoEnum::YES),
  39. // 渠道关闭后访问页面类型 0-空白页 1-自定义页
  40. 'redirect_type' => ConfigService::get('pc', 'redirect_type', YesNoEnum::NO),
  41. 'redirect_content' => ConfigService::get('pc', 'redirect_content', ''),
  42. 'visit_url' => request()->domain() . '/pc',
  43. 'title' => ConfigService::get('pc', 'title', $shopName),
  44. 'ico' => ConfigService::get('pc', 'ico'),
  45. 'description' => ConfigService::get('pc', 'description', ''),
  46. 'keywords' => ConfigService::get('pc', 'keywords', ''),
  47. 'tools_code' => ConfigService::get('pc', 'tools_code', ''),
  48. ];
  49. $config['ico'] = empty($config['ico']) ? '' : FileService::getFileUrl($config['ico']);
  50. $config['redirect_content'] = html_entity_decode($config['redirect_content']);
  51. $config['tools_code'] = html_entity_decode($config['tools_code']);
  52. return $config;
  53. }
  54. /**
  55. * @notes PC商城设置
  56. * @author Tab
  57. * @date 2021/11/29 18:27
  58. */
  59. public static function setConfig($params)
  60. {
  61. $allowFileds = [
  62. 'status',
  63. 'redirect_type',
  64. 'redirect_content',
  65. 'title',
  66. 'ico',
  67. 'description',
  68. 'keywords',
  69. 'tools_code',
  70. ];
  71. try {
  72. foreach ($allowFileds as $field) {
  73. // // 渠道关闭处理
  74. // if ($field == 'status' && isset($params[$field])) {
  75. // self::redirectUrl($params['redirect_type'], $params['redirect_content'], $params[$field]);
  76. // }
  77. // 把字符转换为 HTML 实体
  78. if ($field == 'redirect_content' || $field == 'tools_code' && isset($params[$field])) {
  79. $params[$field] = htmlentities($params[$field]);
  80. }
  81. // 网站图标
  82. if ($field == 'ico' && isset($params[$field]) ) {
  83. $params[$field] = FileService::setFileUrl($params[$field]);
  84. }
  85. ConfigService::set('pc', $field, $params[$field] ?? '');
  86. }
  87. return true;
  88. } catch (\Exception $e) {
  89. self::$error = $e->getMessage();
  90. return false;
  91. }
  92. }
  93. /**
  94. * @notes 渠道关闭后重定向处理
  95. * @param $redirectType
  96. * @param $redirectContent
  97. * @author Tab
  98. * @date 2021/11/29 18:48
  99. */
  100. public static function redirectUrl($redirectType, $redirectContent, $status)
  101. {
  102. // 没有PC商城直接返回
  103. if (!file_exists('./pc/index.html')) {
  104. return false;
  105. }
  106. // PC商城被修改过先还原
  107. if(file_exists('./pc/index_lock.html')) {
  108. // 存在则原商城入口被修改过,先清除修改后的入口
  109. unlink('./pc/index.html');
  110. // 恢复原入口
  111. rename('./pc/index_lock.html', './pc/index.html');
  112. }
  113. if ($status) {
  114. // 开启状态无需处理
  115. return false;
  116. }
  117. // 显示空白页
  118. if($redirectType == 0) {
  119. // 变更文件名
  120. rename('./pc/index.html', './pc/index_lock.html');
  121. // 创建新空白文件
  122. $newfile = fopen('./pc/index.html', 'w');
  123. // 写入提示语
  124. $content = '<head><meta charset="utf8" /></head>';
  125. $content .= '<p style="margin:0;padding:0;height:100%;display:flex;justify-content:center;align-items:center;color:#cccccc;font-size:30px;">'.trim($redirectContent).'</p>';
  126. fwrite($newfile, $content);
  127. fclose($newfile);
  128. }
  129. // 跳转指定页
  130. if($redirectType == 1) {
  131. // 变更文件名
  132. rename('./pc/index.html', './pc/index_lock.html');
  133. // 创建重定向文件
  134. $newfile = fopen('./pc/index.html', 'w');
  135. if (strpos($redirectContent, 'http') === false && strpos($redirectContent, 'https') === false) {
  136. // 默认带上http协议
  137. $redirectContent = 'http://' . trim($redirectContent);
  138. }
  139. $content = '<script>window.location.href = "' . $redirectContent . '";</script>';
  140. fwrite($newfile, $content);
  141. fclose($newfile);
  142. }
  143. }
  144. }