Просмотр исходного кода

小程序后台下载编译源码

evoxwht 4 лет назад
Родитель
Сommit
b97559887b

+ 122 - 1
crmeb/app/adminapi/controller/v1/application/routine/RoutineTemplate.php

@@ -10,13 +10,16 @@
 // +----------------------------------------------------------------------
 namespace app\adminapi\controller\v1\application\routine;
 
+use app\services\other\QrcodeServices;
 use app\services\other\TemplateMessageServices;
+use app\services\system\attachment\SystemAttachmentServices;
 use crmeb\exceptions\AdminException;
+use think\exception\ValidateException;
 use think\facade\App;
 use think\Request;
 use think\facade\Route as Url;
 use app\adminapi\controller\AuthController;
-use crmeb\services\{FormBuilder as Form, MiniProgramService, template\Template};
+use crmeb\services\{FileService, FormBuilder as Form, MiniProgramService, template\Template, UploadService};
 use think\facade\Cache;
 
 /**
@@ -276,4 +279,122 @@ class RoutineTemplate extends AuthController
         $msg = $errData ? implode('\n', $errData) : '同步成功';
         return app('json')->success($msg);
     }
+
+    /**
+     * 下载小程序
+     * @return mixed
+     */
+    public function downloadTemp()
+    {
+        [$name, $is_live] = $this->request->postMore([
+            ['name', ''],
+            ['is_live', 0]
+        ], true);
+        try {
+            @unlink(public_path() . 'view/download/routine.zip');
+            //拷贝源文件
+            /** @var FileService $fileService */
+            $fileService = app(FileService::class);
+            $fileService->copyDir(public_path() . 'view/mp_view', public_path() . 'view/download');
+            //替换appid和名称
+            $this->updateConfigJson(sys_config('routine_appId'), $name != '' ? $name : sys_config('routine_name'));
+            //是否开启直播
+            if ($is_live == 0) $this->updateAppJson();
+            //替换url
+            $this->updateUrl(sys_config('site_url'));
+            //压缩文件
+            $fileService->addZip(public_path() . 'view/download', public_path() . 'view/download/routine.zip', public_path() . 'view/download');
+            $data['url'] = sys_config('site_url') . '/view/download/routine.zip';
+            return app('json')->success($data);
+        } catch (\Throwable $throwable) {
+
+        }
+    }
+
+    /**
+     * 替换url
+     * @param $url
+     */
+    public function updateUrl($url)
+    {
+        $fileUrl = app()->getRootPath() . "public/view/download/common/vendor.js";
+        $string = file_get_contents($fileUrl); //加载配置文件
+        $string = str_replace('https://demo.crmeb.com', $url, $string); // 正则查找然后替换
+        $newFileUrl = app()->getRootPath() . "public/view/download/common/vendor.js";
+        @file_put_contents($newFileUrl, $string); // 写入配置文件
+
+    }
+
+    /**
+     * 判断是否开启直播
+     * @param int $iszhibo
+     */
+    public function updateAppJson()
+    {
+        $fileUrl = app()->getRootPath() . "public/view/download/app.json";
+        $string = file_get_contents($fileUrl); //加载配置文件
+        $pats = '/,
+      "plugins": \{
+        "live-player-plugin": \{
+          "version": "(.*?)",
+          "provider": "(.*?)"
+        }
+      }/';
+        $string = preg_replace($pats, '', $string); // 正则查找然后替换
+        $newFileUrl = app()->getRootPath() . "public/view/download/app.json";
+        @file_put_contents($newFileUrl, $string); // 写入配置文件
+    }
+
+    /**
+     * 替换appid
+     * @param string $appid
+     * @param string $projectanme
+     */
+    public function updateConfigJson($appId = '', $projectName = '')
+    {
+        $fileUrl = app()->getRootPath() . "public/view/download/project.config.json";
+        $string = file_get_contents($fileUrl); //加载配置文件
+        // 替换appid
+        $appIdOld = '/"appid"(.*?),/';
+        $appIdNew = '"appid"' . ': ' . '"' . $appId . '",';
+        $string = preg_replace($appIdOld, $appIdNew, $string); // 正则查找然后替换
+        // 替换小程序名称
+        $projectNameOld = '/"projectname"(.*?),/';
+        $projectNameNew = '"projectname"' . ': ' . '"' . $projectName . '",';
+        $string = preg_replace($projectNameOld, $projectNameNew, $string); // 正则查找然后替换
+        $newFileUrl = app()->getRootPath() . "public/view/download/project.config.json";
+        @file_put_contents($newFileUrl, $string); // 写入配置文件
+    }
+
+    public function getDownloadInfo()
+    {
+        $data['routine_name'] = sys_config('routine_name', '');
+        $name = $data['routine_name'] . '.jpg';
+        /** @var SystemAttachmentServices $systemAttachmentModel */
+        $systemAttachmentModel = app()->make(SystemAttachmentServices::class);
+        $imageInfo = $systemAttachmentModel->getInfo(['name' => $name]);
+        if (!$imageInfo) {
+            /** @var QrcodeServices $qrcode */
+            $qrcode = app()->make(QrcodeServices::class);
+            $resForever = $qrcode->qrCodeForever(0, 'code');
+            if ($resForever) {
+                $resCode = MiniProgramService::qrcodeService()->appCodeUnlimit($resForever->id, '', 280);
+                $res = ['res' => $resCode, 'id' => $resForever->id];
+            } else {
+                $res = false;
+            }
+            if (!$res) throw new ValidateException('二维码生成失败');
+            $upload = UploadService::init(1);
+            if ($upload->to('routine/code')->stream((string)$res['res'], $name) === false) {
+                return $upload->getError();
+            }
+            $imageInfo = $upload->getUploadInfo();
+            $imageInfo['image_type'] = 1;
+            $systemAttachmentModel->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
+            $qrcode->update($res['id'], ['status' => 1, 'time' => time(), 'qrcode_url' => $imageInfo['dir']]);
+            $data['code'] = sys_config('site_url') . $imageInfo['dir'];
+        } else $data['code'] = sys_config('site_url') . $imageInfo['att_dir'];
+        $data['help'] = 'https://help.crmeb.net/crmeb-v4/1863455';
+        return app('json')->success($data);
+    }
 }

+ 6 - 1
crmeb/app/adminapi/route/app.php

@@ -21,7 +21,7 @@ Route::group('app', function () {
     //一键同步订阅消息
     Route::get('routine/syncSubscribe', 'v1.application.routine.RoutineTemplate/syncSubscribe')->name('syncSubscribe')->option(['real_name' => '一键同步订阅消息']);
     //一键同步微信模版消息消息
-    Route::get('wechat/syncSubscribe', 'v1.application.wechat.WechatTemplate/syncSubscribe')->name('syncSubscribe')->option(['real_name' => '一键同步订阅消息']);
+    Route::get('wechat/syncSubscribe', 'v1.application.wechat.WechatTemplate/syncSubscribe')->name('syncSubscribe')->option(['real_name' => '一键同步模版消息']);
     //修改状态
     Route::put('routine/set_status/:id/:status', 'v1.application.routine.RoutineTemplate/set_status')->name('RoutineSetStatus')->option(['real_name' => '修改订阅消息状态']);
     //菜单值
@@ -88,6 +88,11 @@ Route::group('app', function () {
     //查看对话
     Route::get('wechat/kefu/chat_list', 'v1.application.wechat.StoreService/chat_list')->option(['real_name' => '查看对话']);
 
+    //下载小程序模版页面数据
+    Route::get('routine/info', 'v1.application.routine.RoutineTemplate/getDownloadInfo')->option(['real_name' => '下载小程序页面数据']);
+    //下载小程序模版
+    Route::post('routine/download', 'v1.application.routine.RoutineTemplate/downloadTemp')->option(['real_name' => '下载小程序模版']);
+
 })->middleware([
     \app\http\middleware\AllowOriginMiddleware::class,
     \app\adminapi\middleware\AdminAuthTokenMiddleware::class,

+ 38 - 1
crmeb/crmeb/services/FileService.php

@@ -156,7 +156,7 @@ class FileService
             $file2 = $toDir . '/' . $fileName;
             if ($fileName != '.' && $fileName != '..') {
                 if (is_dir($file1)) {
-                    self::copy_dir($file1, $file2);
+                    $this->copyDir($file1, $file2);
                 } else {
                     copy($file1, $file2);
                 }
@@ -984,4 +984,41 @@ class FileService
     {
         return is_object($value) ? $value->__toString() : $value;
     }
+
+    /**
+     * 压缩文件夹及文件
+     * @param string $source 需要压缩的文件夹/文件路径
+     * @param string $destination 压缩后的保存地址
+     * @param string $folder 文件夹前缀,保存时需要去掉的父级文件夹
+     * @return boolean
+     */
+    function addZip($source, $destination, $folder = '')
+    {
+        if (!extension_loaded('zip') || !file_exists($source)) {
+            return false;
+        }
+
+        $zip = new \ZipArchive;
+        if (!$zip->open($destination, $zip::CREATE)) {
+            return false;
+        }
+        $source = str_replace('\\', '/', $source);
+        $folder = str_replace('\\', '/', $folder);
+        if (is_dir($source) === true) {
+            $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);
+            foreach ($files as $file) {
+                $file = str_replace('\\', '/', $file);
+                if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..'))) continue;
+                if (is_dir($file) === true) {
+                    $zip->addEmptyDir(str_replace($folder . '/', '', $file . '/'));
+                } else if (is_file($file) === true) {
+                    $zip->addFromString(str_replace($folder . '/', '', $file), file_get_contents($file));
+                }
+            }
+        } else if (is_file($source) === true) {
+            $zip->addFromString(basename($source), file_get_contents($source));
+        }
+        return $zip->close();
+    }
+
 }