MiniProgramSettingLogic.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\wechat;
  20. use app\common\logic\BaseLogic;
  21. use app\common\model\UploadMnpLog;
  22. use app\common\service\ConfigService;
  23. use app\common\service\FileService;
  24. use think\facade\Db;
  25. /**
  26. * 微信小程序设置逻辑层
  27. * Class MiniProgramSettingLogic
  28. * @package app\adminapi\logic\wechat
  29. */
  30. class MiniProgramSettingLogic extends BaseLogic
  31. {
  32. public static function getConfig()
  33. {
  34. $domainName = $_SERVER['HTTP_HOST'];
  35. $qrCode = ConfigService::get('mini_program', 'qr_code', '');
  36. $qrCode = empty($qrCode) ? $qrCode : FileService::getFileUrl($qrCode);
  37. $config = [
  38. 'name' => ConfigService::get('mini_program', 'name', ''),
  39. 'original_id' => ConfigService::get('mini_program', 'original_id', ''),
  40. 'qr_code' => $qrCode,
  41. 'app_id' => ConfigService::get('mini_program', 'app_id', ''),
  42. 'app_secret' => ConfigService::get('mini_program', 'app_secret', ''),
  43. 'request_domain' => 'https://'.$domainName,
  44. 'socket_domain' => 'wss://'.$domainName,
  45. 'upload_file_domain' => 'https://'.$domainName,
  46. 'download_file_domain' => 'https://'.$domainName,
  47. 'udp_domain' => 'udp://'.$domainName,
  48. 'tcp_domain' => 'tcp://'.$domainName,
  49. 'business_domain' => $domainName,
  50. // 小程序同步发货开关 1开启 0关闭
  51. 'express_send_sync' => ConfigService::get('mini_program', 'express_send_sync', 1),
  52. 'private_key' => ConfigService::get('mini_program', 'private_key', ''),
  53. ];
  54. return $config;
  55. }
  56. /**
  57. * @notes 微信小程序设置
  58. * @param $params
  59. * @author Tab
  60. * @date 2021/7/28 19:01
  61. */
  62. public static function setConfig($params)
  63. {
  64. $qrCode = isset($params['qr_code']) ? FileService::setFileUrl($params['qr_code']) : '';
  65. ConfigService::set('mini_program','name', $params['name'] ?? '');
  66. ConfigService::set('mini_program','original_id',$params['original_id'] ?? '');
  67. ConfigService::set('mini_program','qr_code',$qrCode);
  68. ConfigService::set('mini_program','app_id',$params['app_id']);
  69. ConfigService::set('mini_program','app_secret',$params['app_secret']);
  70. ConfigService::set('mini_program','express_send_sync', $params['express_send_sync'] ?? 1);
  71. if (isset($params['private_key']) && !empty($params['private_key'])) {
  72. $saveDir = '../extend/miniprogram-ci/';
  73. if (!file_exists($saveDir)) {
  74. mkdir($saveDir, 0775, true);
  75. }
  76. //保存文件
  77. $savePath = $saveDir.'private.'.$params['app_id'].'.key';
  78. $f = fopen($savePath, 'w');
  79. fwrite($f, $params['private_key']);
  80. fclose($f);
  81. ConfigService::set('mini_program','private_key',$params['private_key']);
  82. }
  83. }
  84. /**
  85. * @notes 上传小程序代码
  86. * @param $params
  87. * @return bool
  88. * @author ljj
  89. * @date 2025/4/9 下午3:09
  90. */
  91. public function uploadMnp($params)
  92. {
  93. Db::startTrans();
  94. try {
  95. //校验是否已安装miniprogram-ci工具
  96. if (!file_exists('../extend/miniprogram-ci/node_modules/miniprogram-ci')) {
  97. throw new \think\Exception('请先安装miniprogram-ci工具');
  98. }
  99. $baseUrl = 'mp-weixin/common/vendor.js';
  100. //判断是否已存在 vendor_example.js 备份文件,如果不存在则备份
  101. $exampleUrl = 'mp-weixin/common/vendor_example.js';
  102. if(!file_exists($exampleUrl)){
  103. copy($baseUrl, $exampleUrl);
  104. }
  105. //更换小程序域名
  106. $baseUrlData = file_get_contents($exampleUrl);
  107. $domain = request()->domain(true).'/';
  108. $baseUrlData = str_replace("[baseUrl]",$domain,$baseUrlData);
  109. $f = fopen($baseUrl,"w");
  110. fwrite($f,$baseUrlData);
  111. fclose($f);
  112. //插入上传日志
  113. $uploadMnpLog = UploadMnpLog::create([
  114. 'admin_id' => $params['admin_id'],
  115. 'version' => config('project.version'),
  116. ]);
  117. //上传小程序代码
  118. $data = [
  119. 'version' => config('project.version'),
  120. 'desc' => $params['upload_desc'] ?? '',
  121. 'appid' => ConfigService::get('mini_program', 'app_id', ''),
  122. ];
  123. $json_data = json_encode($data);
  124. $command = 'node ../extend/miniprogram-ci/upload.js '.escapeshellarg($json_data).' 2>&1';
  125. $output=null;
  126. $retval = null;
  127. exec($command, $output, $retval);
  128. if ($retval) {
  129. //错误
  130. UploadMnpLog::update([
  131. 'status' => 2,
  132. 'fail_reason' => is_array($output) ? json_encode($output) : $output,
  133. ],['id'=>$uploadMnpLog->id]);
  134. } else {
  135. //成功
  136. UploadMnpLog::update([
  137. 'status' => 1,
  138. ],['id'=>$uploadMnpLog->id]);
  139. }
  140. Db::commit();
  141. return true;
  142. } catch (\Exception $e) {
  143. Db::rollback();
  144. self::$error = $e->getMessage();
  145. return false;
  146. }
  147. }
  148. }