You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.9 KiB
112 lines
3.9 KiB
<?php
|
|
|
|
namespace App\Service;
|
|
use App\Models\Agent;
|
|
use App\Models\MiniProgramTemplate;
|
|
use App\Models\MiniProgramUploadLog;
|
|
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
|
|
use Exception;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* 上传小程序
|
|
* Class UploadMiniProgram
|
|
* @package App\Service
|
|
*/
|
|
class UploadMiniProgram
|
|
{
|
|
/**
|
|
* UploadMiniProgram constructor.
|
|
* @param Agent $agent
|
|
* @param MiniProgramTemplate $template
|
|
* @throws GuzzleException
|
|
* @throws Exception
|
|
*/
|
|
public function __construct(Agent $agent, MiniProgramTemplate $template)
|
|
{
|
|
try {
|
|
$openPlatform = new OpenPlatform();
|
|
$refreshToken = $openPlatform->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());
|
|
}
|
|
}
|
|
}
|