site_id = $siteId; } /** * Common: 获取自动上传版本信息 * Author: wu-hui * Time: 2024/07/26 11:48 * @return mixed */ public function getAutoReleaseVersion(){ // 配置获取 $config = config('info'); $version = (new ConfigModel())->getWeappVersion($this->site_id)['data']['value']; // 判断:是否为最新版本 $version['is_new_version'] = 0; if(!isset($version['version']) || (isset($version['version']) && $version['version'] != $config['version_no'])){ $version['is_new_version'] = 1; } // 判断:根据是否已经存在上传版本 获取下一个版本的版本号 if(!empty($version['release_version'])){ // 存在 $versionArr = explode('.', $version['release_version']); $versionArr[2]++; $version['release_version_next'] = implode('.', $versionArr); }else{ // 不存在 默认为1.0.0 $version['release_version_next'] = '1.0.0'; } return $version; } /** * Common: 小程序上传 - 公共参数获取 * Author: wu-hui * Time: 2024/07/26 13:44 * @return array * @throws \think\Exception */ private function getParams(){ $path = ROOT_PATH.'/extend'; // 校验是否已安装miniprogram-ci工具 $toolPath = $path . '/miniprogram-ci'; if (!file_exists($toolPath)) throw new \think\Exception('miniprogram-ci工具不存在!'); $uploadJsPath = $toolPath.'/weAppUpload.js'; if (!file_exists($uploadJsPath)) throw new \think\Exception('weAppUpload.js文件不存在!'); $config = (new ConfigModel())->getWeappConfig($this->site_id)['data']['value']; if(empty($config) || empty($config['appid'])) throw new \think\Exception('小程序尚未配置,请先配置您的小程序!'); // 获取版本信息 $version = $this->getAutoReleaseVersion(); // 上传小程序代码 return [ 'appid' => $config['appid'],// 小程序appid 'version' => $version['release_version_next'],// 即将提交的小程序版本 'desc' => '自动上传小程序代码',// 小程序提交备注 'js_path' => $path.'/miniprogram-ci/miniprogram-ci',// 上传js文件路径 'key_path' => $toolPath."/key/private.{$config['appid']}.key",// 小程序自动上传秘钥路径 'project_path' => $toolPath.'/code/',// 项目路径 'qrcodeFormat' => 'image',// 返回二维码文件的格式 "image" 或 "base64", 默认值 "terminal" 供调试用 'qrcodeOutputDest' => $toolPath."/qrcode/{$config['appid']}_preview.jpg",// 二维码文件保存路径 'upload_js_path' => $uploadJsPath, // 'pagePath' => 'pages/index/index',// 预览页面路径 // 'searchQuery' => '', // 预览参数 [注意!]这里的`&`字符在命令行中应写成转义字符`\&` ]; } /** * Common: 小程序上传 - 获取预览二维码 * Author: wu-hui * Time: 2024/07/26 15:44 * @return array|string */ public function getPreviewQrCode($isCreate = false){ set_time_limit(0); try{ // 公共参数获取 $params = $this->getParams(); // 预览图片不存在 生成 if(!file_exists($params['qrcodeOutputDest'])){ // 判断:是否生成预览二维码 if($isCreate){ $params['operate_type'] = 'preview'; $command = 'nohup node '.$params['upload_js_path'].' '.escapeshellarg(json_encode($params)).' >/dev/null 2>&1 &'; exec($command); } return ''; }else{ return img('extend/miniprogram-ci/qrcode/'.$params['appid']."_preview.jpg"); } }catch(\Exception $e){ return $this->error('',$e->getMessage()); } } /** * Common: 小程序上传 - 上传小程序 * Author: wu-hui * Time: 2024/07/26 16:42 * @return array */ public function releaseVersion(){ set_time_limit(0); try{ // 公共参数获取 $params = $this->getParams(); // 小程序包信息修改 $this->replaceInfo($params); // 执行上传命令 $params['operate_type'] = 'upload'; $command = 'nohup node '.$params['upload_js_path'].' '.escapeshellarg(json_encode($params)).' >/dev/null 2>&1 &'; exec($command); // 修改版本信息 $config = config('info'); $versionInfo = $this->getAutoReleaseVersion(); $versionInfo['version'] = $config['version_no']; $versionInfo['release_version'] = $versionInfo['release_version_next']; (new ConfigModel())->setWeappVersion($versionInfo, 1, $this->site_id); return $this->success('', '上传中'); }catch(\Exception $e){ return $this->error('',$e->getMessage()); } } /** * Common: 小程序上传 - 替换指定信息 * Author: wu-hui * Time: 2024/07/29 11:02 * @param $params * @throws \think\Exception */ public function replaceInfo($params){ // 开始替换小程序appid $appidFilePath = $params['project_path'] . 'project.config.json'; if (!file_exists($appidFilePath)) throw new \think\Exception('project.config.json文件不存在!'); $fileContent = file_get_contents($appidFilePath); $pattern = '/wx[a-zA-Z0-9]{13,19}/'; // 匹配wx开头,紧跟13到19位数字和字母的字符串 $fileContent = preg_replace($pattern,$params['appid'],$fileContent);// 执行替换操作 file_put_contents($appidFilePath, $fileContent);// 将替换后的内容写回文件 // 开始替换site_id $siteIdFilePath = $params['project_path'] . 'common/vendor.js'; if (!file_exists($siteIdFilePath)) throw new \think\Exception('vendor.js文件不存在!'); $siteIdContent = file_get_contents($siteIdFilePath); $pattern = '/site_id:\d{1,10}/'; // 匹配wx开头,紧跟13到19位数字和字母的字符串 $replacement = "site_id:".$this->site_id; $siteIdContent = preg_replace($pattern,$replacement,$siteIdContent);// 执行替换操作 file_put_contents($siteIdFilePath, $siteIdContent);// 将替换后的内容写回文件 } /** * Common: 当前小程序自动上传秘钥文件是否存在 * Author: wu-hui * Time: 2024/07/29 11:30 * @return bool * @throws \think\Exception */ public function privateKeyFileIsExist():bool{ // 基本参数获取 $params = $this->getParams(); return file_exists($params['key_path']); } }