Config.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\mobileshop\model;
  13. use app\model\BaseModel;
  14. use app\model\system\Config as ConfigModel;
  15. use app\model\web\Config as WebConfigModel;
  16. /**
  17. * 手机版商家端配置
  18. */
  19. class Config extends BaseModel
  20. {
  21. private $path = 'addon/mobileshop/source';
  22. private $not_found_file_error = "未找到源码包,请检查目录文件";
  23. /*************************************************网站部署******************************************/
  24. /**
  25. * 默认部署:无需下载,一键刷新,API接口请求地址为当前域名,编译代码存放到mobileshop文件夹中
  26. * @return array
  27. */
  28. public function downloadCsDefault()
  29. {
  30. try {
  31. $path = $this->path . '/cs_default';
  32. $mshop_path = 'mshop'; // mobileshop端生成目录
  33. $config_path = 'mshop/static/js'; // mobileshop模板文件目录
  34. if (!is_dir($path) || count(scandir($path)) <= 3) {
  35. return $this->error('', $this->not_found_file_error);
  36. }
  37. if (is_dir($mshop_path)) {
  38. // 先将之前的文件删除
  39. if (count(scandir($mshop_path)) > 1) deleteDir($mshop_path);
  40. } else {
  41. // 创建mobileshop目录
  42. mkdir($mshop_path, intval('0777', 8), true);
  43. }
  44. // 将原代码包拷贝到mobileshop目录下
  45. recurseCopy($path, $mshop_path);
  46. $this->copyFile($config_path);
  47. file_put_contents($mshop_path . '/refresh.log', time());
  48. return $this->success();
  49. } catch (\Exception $e) {
  50. return $this->error('', $e->getMessage() . $e->getLine());
  51. }
  52. }
  53. /**
  54. * 独立部署:下载编译代码包后,放到网站根目录下运行
  55. * @param $domain
  56. * @return array
  57. */
  58. public function downloadCsIndep($domain)
  59. {
  60. try {
  61. $path = $this->path . '/cs_indep';
  62. $source_file_path = 'upload/mshop/cs_indep'; // mobileshop端生成目录
  63. $config_path = $source_file_path . '/static/js'; // mobileshop模板文件目录
  64. if (!is_dir($path) || count(scandir($path)) <= 3) {
  65. return $this->error('', $this->not_found_file_error);
  66. }
  67. if (is_dir($source_file_path)) {
  68. // 先将之前的文件删除
  69. if (count(scandir($source_file_path)) > 2) deleteDir($source_file_path);
  70. } else {
  71. // 创建mobileshop目录
  72. mkdir($source_file_path, intval('0777', 8), true);
  73. }
  74. // 将原代码包拷贝到mobileshop目录下
  75. recurseCopy($path, $source_file_path);
  76. $this->copyFile($config_path, $domain);
  77. // 生成压缩包
  78. $file_arr = getFileMap($source_file_path);
  79. if (!empty($file_arr)) {
  80. $zipname = 'mshop_cs_indep_' . date('YmdHi') . '.zip';
  81. $zip = new \ZipArchive();
  82. $res = $zip->open($zipname, \ZipArchive::CREATE);
  83. if ($res === TRUE) {
  84. foreach ($file_arr as $file_path => $file_name) {
  85. if (is_dir($file_path)) {
  86. $file_path = str_replace($source_file_path . '/', '', $file_path);
  87. $zip->addEmptyDir($file_path);
  88. } else {
  89. $zip_path = str_replace($source_file_path . '/', '', $file_path);
  90. $zip->addFile($file_path, $zip_path);
  91. }
  92. }
  93. $zip->close();
  94. header("Content-Type: application/zip");
  95. header("Content-Transfer-Encoding: Binary");
  96. header("Content-Length: " . filesize($zipname));
  97. header("Content-Disposition: attachment; filename=\"" . basename($zipname) . "\"");
  98. readfile($zipname);
  99. @unlink($zipname);
  100. }
  101. }
  102. return $this->success();
  103. } catch (\Exception $e) {
  104. return $this->error('', $e->getMessage() . $e->getLine());
  105. }
  106. }
  107. /**
  108. * 源码下载:下载uni-app代码包,可进行二次开发
  109. * @return array
  110. */
  111. public function downloados()
  112. {
  113. try {
  114. $source_file_path = $this->path . '/os';
  115. if (!is_dir($source_file_path) || count(scandir($source_file_path)) <= 3) {
  116. return $this->error('', $this->not_found_file_error);
  117. }
  118. $file_arr = getFileMap($source_file_path);
  119. if (!empty($file_arr)) {
  120. $zipname = 'mshop_os_' . date('YmdHi') . '.zip';
  121. $zip = new \ZipArchive();
  122. $res = $zip->open($zipname, \ZipArchive::CREATE);
  123. if ($res === TRUE) {
  124. foreach ($file_arr as $file_path => $file_name) {
  125. if (is_dir($file_path)) {
  126. $file_path = str_replace($source_file_path . '/', '', $file_path);
  127. $zip->addEmptyDir($file_path);
  128. } else {
  129. $zip_path = str_replace($source_file_path . '/', '', $file_path);
  130. $zip->addFile($file_path, $zip_path);
  131. }
  132. }
  133. $zip->close();
  134. header("Content-Type: application/zip");
  135. header("Content-Transfer-Encoding: Binary");
  136. header("Content-Length: " . filesize($zipname));
  137. header("Content-Disposition: attachment; filename=\"" . basename($zipname) . "\"");
  138. readfile($zipname);
  139. @unlink($zipname);
  140. }
  141. }
  142. } catch (\Exception $e) {
  143. return $this->error('', $e->getMessage() . $e->getLine());
  144. }
  145. }
  146. /**
  147. * 替换配置信息,API请求域名地址、图片、地图密钥等
  148. * @param $source_path
  149. * @param string $domain
  150. */
  151. private function copyFile($source_path, $domain = __ROOT__)
  152. {
  153. $files = scandir($source_path);
  154. foreach ($files as $path) {
  155. if ($path != '.' && $path != '..') {
  156. $temp_path = $source_path . '/' . $path;
  157. if (file_exists($temp_path)) {
  158. if (preg_match("/(index.)(\w{8})(.js)$/", $temp_path)) {
  159. $content = file_get_contents($temp_path);
  160. $content = $this->paramReplace($content, $domain);
  161. file_put_contents($temp_path, $content);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * 参数替换
  169. * @param $string
  170. * @param string $domain
  171. * @return string|string[]|null
  172. */
  173. private function paramReplace($string, $domain = __ROOT__)
  174. {
  175. $web_config_model = new WebConfigModel();
  176. $web_config = $web_config_model->getMapConfig();
  177. $web_config = $web_config[ 'data' ][ 'value' ];
  178. $patterns = [
  179. '/\{\{\$baseUrl\}\}/',
  180. '/\{\{\$imgDomain\}\}/',
  181. '/\{\{\$h5Domain\}\}/',
  182. '/\{\{\$mpKey\}\}/',
  183. ];
  184. $replacements = [
  185. $domain,
  186. $domain,
  187. $domain . '/mshop',
  188. $web_config[ 'tencent_map_key' ] ?? '',
  189. ];
  190. $string = preg_replace($patterns, $replacements, $string);
  191. return $string;
  192. }
  193. /**
  194. * 设置移动版商家端域名配置
  195. * @param $data
  196. * @param int $site_id
  197. * @param string $app_modle
  198. * @return array
  199. */
  200. public function setMShopDomainName($data, $site_id = 0, $app_modle = 'shop')
  201. {
  202. $config = new ConfigModel();
  203. $res = $config->setConfig($data, '移动版商家端域名配置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_modle ], [ 'config_key', '=', 'MOBILE_SHOP_DOMAIN_NAME' ] ]);
  204. return $res;
  205. }
  206. /**
  207. * 获取移动版商家端域名配置
  208. * @param int $site_id
  209. * @param string $app_module
  210. * @return array
  211. */
  212. public function getMShopDomainName($site_id = 0, $app_module = 'shop')
  213. {
  214. $config = new ConfigModel();
  215. $res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'MOBILE_SHOP_DOMAIN_NAME' ] ]);
  216. if (empty($res[ 'data' ][ 'value' ])) {
  217. $res[ 'data' ][ 'value' ] = [
  218. 'domain_name_mobileshop' => __ROOT__ . '/mshop',
  219. 'deploy_way' => 'default'
  220. ];
  221. } else if ($res[ 'data' ][ 'value' ][ 'domain_name_mobileshop' ] == '') {
  222. $res[ 'data' ][ 'value' ] = [
  223. 'domain_name_mobileshop' => __ROOT__ . '/mshop',
  224. ];
  225. }
  226. return $res;
  227. }
  228. /******************************************************************** 微信小程序配置 start ****************************************************************************/
  229. /**
  230. * 设置微信小程序配置
  231. * @param $data
  232. * @param $is_use
  233. * @return array
  234. */
  235. public function setWeappConfig($data, $is_use, $site_id, $app_module = 'shop')
  236. {
  237. $config = new ConfigModel();
  238. $res = $config->setConfig($data, '商家端微信小程序设置', $is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'MOBILE_SHOP_WEAPP_CONFIG' ] ]);
  239. return $res;
  240. }
  241. /**
  242. * 获取微信小程序配置信息
  243. * @param int $site_id
  244. * @return array
  245. */
  246. public function getWeappConfig($site_id = 0, $app_module = 'shop')
  247. {
  248. $config = new ConfigModel();
  249. $res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'MOBILE_SHOP_WEAPP_CONFIG' ] ]);
  250. return $res;
  251. }
  252. }