H5.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\system;
  11. use app\model\BaseModel;
  12. use app\model\web\Config;
  13. class H5 extends BaseModel
  14. {
  15. private $h5_domain = __ROOT__ . '/h5';
  16. public function refresh()
  17. {
  18. try {
  19. $h5_template_path = 'public/h5/default'; // h5模板文件目录
  20. $h5_path = 'h5'; // h5端生成目录
  21. if (!is_dir($h5_template_path) || count(scandir($h5_template_path)) <= 2) {
  22. return $this->error('', '未查找到h5模板');
  23. }
  24. if (is_dir($h5_path)) {
  25. // 先将之前的文件删除
  26. if (count(scandir($h5_path)) > 2) deleteDir($h5_path);
  27. } else {
  28. // 创建H5目录
  29. mkdir($h5_path, intval('0777', 8), true);
  30. }
  31. $this->copyFile($h5_template_path, $h5_path);
  32. file_put_contents($h5_path . '/refresh.log', time());
  33. return $this->success();
  34. } catch ( \Exception $e ) {
  35. return $this->error('', $e->getMessage() . $e->getLine());
  36. }
  37. }
  38. /**
  39. * 独立部署下载
  40. * @return array
  41. */
  42. function downloadH5Separate($domain_name)
  43. {
  44. $this->h5_domain = $domain_name;
  45. $h5_template_path = 'public/h5/separate'; // h5模板文件目录
  46. if (!is_dir($h5_template_path) || count(scandir($h5_template_path)) <= 2) {
  47. return $this->error('', '未查找到h5模板');
  48. }
  49. $source_file_path = 'upload/temp/h5';
  50. if (is_dir($source_file_path)) {
  51. // 先将之前的文件删除
  52. if (count(scandir($source_file_path)) > 2) deleteDir($source_file_path);
  53. } else {
  54. // 创建H5目录
  55. mkdir($source_file_path, intval('0777', 8), true);
  56. }
  57. $this->copyFile($h5_template_path, $source_file_path);
  58. $file_arr = getFileMap($source_file_path);
  59. if (!empty($file_arr)) {
  60. $zipname = 'h5_' . date('Ymd') . '.zip';
  61. $zip = new \ZipArchive();
  62. $res = $zip->open($zipname, \ZipArchive::CREATE);
  63. if ($res === TRUE) {
  64. foreach ($file_arr as $file_path => $file_name) {
  65. if (is_dir($file_path)) {
  66. $file_path = str_replace($source_file_path . '/', '', $file_path);
  67. $zip->addEmptyDir($file_path);
  68. } else {
  69. $zip_path = str_replace($source_file_path . '/', '', $file_path);
  70. $zip->addFile($file_path, $zip_path);
  71. }
  72. }
  73. $zip->close();
  74. header("Content-Type: application/zip");
  75. header("Content-Transfer-Encoding: Binary");
  76. header("Content-Length: " . filesize($zipname));
  77. header("Content-Disposition: attachment; filename=\"" . basename($zipname) . "\"");
  78. readfile($zipname);
  79. @unlink($zipname);
  80. deleteDir($source_file_path);
  81. }
  82. }
  83. }
  84. private function copyFile($source_path, $to_path = '')
  85. {
  86. $files = scandir($source_path);
  87. foreach ($files as $path) {
  88. if ($path != '.' && $path != '..') {
  89. $temp_path = $source_path . '/' . $path;
  90. if (is_dir($temp_path)) {
  91. mkdir($to_path . '/' . $path, intval('0777', 8), true);
  92. $this->copyFile($temp_path, $to_path . '/' . $path);
  93. } else {
  94. if (file_exists($temp_path)) {
  95. if (preg_match("/(index.)(\w{8})(.js)$/", $temp_path)) {
  96. $content = file_get_contents($temp_path);
  97. $content = $this->paramReplace($content);
  98. file_put_contents($to_path . '/' . $path, $content);
  99. } else {
  100. copy($temp_path, $to_path . '/' . $path);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * 参数替换
  109. * @param $site_id
  110. * @param $string
  111. * @return null|string|string[]
  112. */
  113. private function paramReplace($string)
  114. {
  115. $api_model = new Api();
  116. $api_config = $api_model->getApiConfig()['data'];
  117. $web_config_model = new Config();
  118. $web_config = $web_config_model->getMapConfig();
  119. $web_config = $web_config['data']['value'];
  120. $socket_url = (strstr(__ROOT__, 'https://') === false ? str_replace('http', 'ws', __ROOT__) : str_replace('https', 'wss', __ROOT__)) . '/wss';
  121. $patterns = [
  122. '/\{\{\$baseUrl\}\}/',
  123. '/\{\{\$imgDomain\}\}/',
  124. '/\{\{\$h5Domain\}\}/',
  125. '/\{\{\$mpKey\}\}/',
  126. '/\{\{\$apiSecurity\}\}/',
  127. '/\{\{\$publicKey\}\}/',
  128. '/\{\{\$webSocket\}\}/'
  129. ];
  130. $replacements = [
  131. __ROOT__,
  132. __ROOT__,
  133. $this->h5_domain,
  134. $web_config['tencent_map_key'] ?? '',
  135. $api_config['is_use'] ?? 0,
  136. $api_config['value']['public_key'] ?? '',
  137. $socket_url
  138. ];
  139. $string = preg_replace($patterns, $replacements, $string);
  140. return $string;
  141. }
  142. }