diff --git a/app/Admin/Controllers/MiniProgramTemplateController.php b/app/Admin/Controllers/MiniProgramTemplateController.php index d804e39..69ba693 100644 --- a/app/Admin/Controllers/MiniProgramTemplateController.php +++ b/app/Admin/Controllers/MiniProgramTemplateController.php @@ -43,8 +43,8 @@ class MiniProgramTemplateController extends AdminController $grid->column('op') ->if(fn() => true) ->then(function ($column) { -// $column->append((new MiniProgramUpload(null, 2))->setKey($this->template_id))->append(' '); - $column->append((new MiniProgramDelTemp)->setKey($this->template_id))->append(' '); + $column->append((new MiniProgramUpload(null, 2))->setKey($this->template_id))->append(' '); + $column->append((new MiniProgramDelTemp)->setKey($this->template_id)); }); }); } diff --git a/app/Admin/Extensions/Grid/MiniProgramUpload.php b/app/Admin/Extensions/Grid/MiniProgramUpload.php index e11f90c..fc8a9be 100644 --- a/app/Admin/Extensions/Grid/MiniProgramUpload.php +++ b/app/Admin/Extensions/Grid/MiniProgramUpload.php @@ -1,14 +1,16 @@ action = $action; //$action:1=为指定代理商上传小程序;2=暂未确定 - $this->title = $action == 1 ? '上传小程序' : '为所有代理商上传'; + $this->action = $action; + $this->title = $action == 1 ? '上传小程序' : '上传给所有代理商'; } protected function html() @@ -40,16 +42,13 @@ class MiniProgramUpload extends RowAction public function handle(Request $request) { - return $this->action == 1 ? $this->uploadOne() : $this->uploadAll(); + return $request->action == 1 ? $this->uploadOne() : $this->uploadAll(); } //注:uploadOne在代理商列表页使用 private function uploadOne() { $template = MiniProgramTemplate::orderBy('template_id', 'desc')->first(); - /*if (MiniProgramUploadLog::query()->where(['agent_id' => $this->getKey(), 'template_id' => $template->template_id])->exists()) { - return $this->response()->error("该代理商已经上传过模板ID为 {$template->template_id} 的小程序,无需重复上传"); - }*/ $agent = Agent::find($this->getKey()); if (empty($agent->appid)) { @@ -57,85 +56,9 @@ class MiniProgramUpload extends RowAction } try { - $openPlatform = new OpenPlatform(); - $refreshToken = $openPlatform->refreshToken($agent->appid); - if (!$refreshToken) { - return $this->response()->error('获取refresh_token失败'); - } - $miniProgram = $openPlatform->miniProgram($agent->appid, $refreshToken); - - /** @var Client $code */ - $code = $miniProgram['code'] ?? null; - if (!$code) { - return $this->response()->error('获取code失败'); - } - - $templateId = $template->template_id; - $extJson = json_encode(['extAppid' => $agent->appid]); - $version = $template->user_version; - $description = $agent->company_name; - - //提交上传 - $commit = $code->commit($templateId, $extJson, $version, $description); - - if (!isset($commit['errcode'], $commit['errmsg']) || $commit['errcode'] != 0 || $commit['errmsg'] != 'ok') { - throw new \Exception(isset($commit['errmsg']) ? $commit['errmsg'] : join(',', $commit)); - } - - //提交审核,文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/submit_audit.html - $res = $code->submitAudit([]); - - $audit_err_code = $this->auditErrorCode(); - if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') { - $filename = "mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg"; - - //保存上传记录 - MiniProgramUploadLog::insert([ - 'agent_id' => $agent->id, - 'appid' => $agent->appid, - 'user_version' => $template->user_version, - 'template_id' => $templateId, - 'qrcode' => $filename, - 'audit_id' => $res['auditid'], - 'created_at' => now(), - ]); - - //获取体验二维码并保存,30天内只获取一次 - $qrcode_path = Storage::path("mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg"); - if (!file_exists($qrcode_path) || time() - filemtime($qrcode_path) < 86400 * 30) { - $qrcode = $code->getQrCode(); - Storage::put("public/$filename", $qrcode); - } - - //设置域名,如果之前已经设置过了将记录下来,不再设置 - $host = env('APP_URL'); - $redis_key = 'mini_program_set_host:' . substr(md5($host), 0, 10); - if (!Redis::hget($redis_key, $agent->appid)) { - /** @var \EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Domain\Client $domain */ - $domain = $miniProgram['domain']; - - $param = [ - "action" => "add", - "requestdomain" => [$host], - "wsrequestdomain" => [str_replace('http', 'ws', $host)], - "uploaddomain" => [$host], - "downloaddomain" => [$host], - ]; - $domain->modify($param); //服务器域名,服务器域名多次设置仅第一次成功,这里不校验返回结果正确性 - $res = $domain->setWebviewDomain([$host]); //业务域名 - if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') { - throw new \Exception("设置业务域名{$host}失败,原因:" . $res['errmsg'] ?? (is_array($res) ? join(',', $res) : '未知')); - } - Redis::hset($redis_key, $agent->appid, 1); - } - - return $this->response()->success("上传成功,并已提交审核")->refresh(); - } else if (isset($audit_err_code[$res['errcode']])) { - throw new \Exception($audit_err_code[$res['errcode']]); - } else { - throw new \Exception($res['errmsg'] ?? join(',', $res)); - } - } catch (\Exception $e) { + new UploadMiniProgram($agent, $template); + return $this->response()->success("上传成功,并已提交审核")->refresh(); + } catch (GuzzleException | \Exception $e) { return $this->response()->error($e->getMessage()); } } @@ -143,13 +66,25 @@ class MiniProgramUpload extends RowAction //注:uploadAll在小程序模板列表页使用 private function uploadAll() { - return true; + //上传单个时,$this->getKey()是代理商ID,上传多个时,是小程序模板ID + $template_id = $this->getKey(); + + $ids = Agent::whereNotNull('appid') + ->where([ + ['status', '=', UserStatus::NORMAL], + ['appid', '<>', ''], + ])->pluck('id'); + + foreach ($ids as $id) { + BatchUploadMiniProgram::dispatch($template_id, $id); + } + return $this->response()->success('已经为所有已注册过小程序,且状态正常的代理商上传小程序。上传需要一定时间,请耐心等待'); } public function confirm() { - $last_template_id = MiniProgramTemplate::max('template_id'); if ($this->action == 1) { + $last_template_id = MiniProgramTemplate::max('template_id'); return ["确定要上传模板ID为{$last_template_id}的小程序吗?", '']; } else { return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?']; diff --git a/app/Http/Controllers/Api/TestController.php b/app/Http/Controllers/Api/TestController.php index dbd6330..f6c3078 100644 --- a/app/Http/Controllers/Api/TestController.php +++ b/app/Http/Controllers/Api/TestController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api; use App\Common\ProductStatus; +use App\Jobs\BatchUploadMiniProgram; use App\Models\AgentProduct; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; @@ -17,7 +18,7 @@ class TestController { public function index() { - dd(AgentProduct::where('agent_id', 10)->withTrashed()->count()); + BatchUploadMiniProgram::dispatch('aaaa', 'bbbb')->delay(now()->addMinutes()); } /** diff --git a/app/Jobs/BatchUploadMiniProgram.php b/app/Jobs/BatchUploadMiniProgram.php new file mode 100644 index 0000000..f4a7e44 --- /dev/null +++ b/app/Jobs/BatchUploadMiniProgram.php @@ -0,0 +1,54 @@ +template_id = $template_id; + $this->agent_id = $agent_id; + } + + /** + * Execute the job. + * + * @return void + * @throws Exception + */ + public function handle() + { + $agent = Agent::find($this->agent_id); + $template = MiniProgramTemplate::find($this->template_id); + + if (!$agent || !$template) return; + + try { + new UploadMiniProgram($agent, $template); + } catch (GuzzleException | \Exception $e) { + throw new Exception($e->getMessage()); + } + } +} diff --git a/app/Service/UploadMiniProgram.php b/app/Service/UploadMiniProgram.php new file mode 100644 index 0000000..04edc4d --- /dev/null +++ b/app/Service/UploadMiniProgram.php @@ -0,0 +1,112 @@ +refreshToken($agent->appid); + if (!$refreshToken) { + throw new Exception('获取refresh_token失败'); + } + $miniProgram = $openPlatform->miniProgram($agent->appid, $refreshToken); + + /** @var Client $code */ + $code = $miniProgram['code'] ?? null; + if (!$code) { + throw new Exception('获取code失败'); + } + + $templateId = $template->template_id; + $extJson = json_encode(['extAppid' => $agent->appid]); + $version = $template->user_version; + $description = $agent->company_name; + + //提交上传 + $commit = $code->commit($templateId, $extJson, $version, $description); + + if (!isset($commit['errcode'], $commit['errmsg']) || $commit['errcode'] != 0 || $commit['errmsg'] != 'ok') { + throw new Exception(isset($commit['errmsg']) ? $commit['errmsg'] : join(',', $commit)); + } + + //提交审核,文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/submit_audit.html + $res = $code->submitAudit([]); + + $audit_err_code = $this->auditErrorCode(); + if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') { + $filename = "mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg"; + + //保存上传记录 + MiniProgramUploadLog::insert([ + 'agent_id' => $agent->id, + 'appid' => $agent->appid, + 'user_version' => $template->user_version, + 'template_id' => $templateId, + 'qrcode' => $filename, + 'audit_id' => $res['auditid'], + 'created_at' => now(), + ]); + + //获取体验二维码并保存,30天内只获取一次 + $qrcode_path = Storage::path("mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg"); + if (!file_exists($qrcode_path) || time() - filemtime($qrcode_path) < 86400 * 30) { + $qrcode = $code->getQrCode(); + Storage::put("public/$filename", $qrcode); + } + + //设置域名,如果之前已经设置过了将记录下来,不再设置 + $host = env('APP_URL'); + $redis_key = 'mini_program_set_host:' . substr(md5($host), 0, 10); + if (!Redis::hget($redis_key, $agent->appid)) { + /** @var \EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Domain\Client $domain */ + $domain = $miniProgram['domain']; + + $param = [ + "action" => "add", + "requestdomain" => [$host], + "wsrequestdomain" => [str_replace('http', 'ws', $host)], + "uploaddomain" => [$host], + "downloaddomain" => [$host], + ]; + $domain->modify($param); //服务器域名,服务器域名多次设置仅第一次成功,这里不校验返回结果正确性 + $res = $domain->setWebviewDomain([$host]); //业务域名 + if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') { + throw new Exception("设置业务域名{$host}失败,原因:" . $res['errmsg'] ?? (is_array($res) ? join(',', $res) : '未知')); + } + Redis::hset($redis_key, $agent->appid, 1); + } + + throw new Exception("上传成功,并已提交审核"); + } else if (isset($audit_err_code[$res['errcode']])) { + throw new Exception($audit_err_code[$res['errcode']]); + } else { + throw new Exception($res['errmsg'] ?? join(',', $res)); + } + } catch (Exception $e) { + throw new Exception($e->getMessage()); + } + } +} diff --git a/upload.bat b/upload.bat new file mode 100644 index 0000000..3985b73 --- /dev/null +++ b/upload.bat @@ -0,0 +1 @@ +git push \ No newline at end of file