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.
136 lines
4.4 KiB
136 lines
4.4 KiB
<?php
|
|
|
|
namespace App\Admin\Extensions\Grid;
|
|
use App\Models\AdminSetting;
|
|
use App\Models\Agent;
|
|
use App\Models\MiniProgramTemplate;
|
|
use App\Models\MiniProgramUploadLog;
|
|
use Dcat\Admin\Grid\RowAction;
|
|
use EasyWeChat\Factory;
|
|
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* 上传(注册)小程序
|
|
* Class UploadMiniProgram
|
|
* @package App\Admin\Extensions\Grid
|
|
*/
|
|
class MiniProgramUpload extends RowAction
|
|
{
|
|
private $action;
|
|
|
|
public function __construct($title = null, $action = 1)
|
|
{
|
|
parent::__construct($title);
|
|
$this->action = $action; //$action:1=为指定代理商上传小程序;2=批量上传小程序(一键分发小程序给所有代理商)
|
|
$this->title = $action == 1 ? '上传小程序' : '为所有代理商上传';
|
|
}
|
|
|
|
protected function html()
|
|
{
|
|
$class = 'btn btn-sm btn-primary';
|
|
$this->appendHtmlAttribute('class', $class);
|
|
$this->defaultHtmlAttribute('href', 'javascript:;');
|
|
|
|
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
|
|
}
|
|
|
|
public function handle(Request $request)
|
|
{
|
|
return $this->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)) {
|
|
return $this->response()->error('该代理商未注册过小程序,请先注册');
|
|
}
|
|
|
|
try {
|
|
$setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
|
|
$config = [
|
|
'app_id' => $setting['service_appid'],
|
|
'secret' => $setting['service_appsecret'],
|
|
'token' => $setting['service_token'],
|
|
'aes_key' => $setting['service_aeskey'],
|
|
];
|
|
|
|
$openPlatform = Factory::openPlatform($config);
|
|
$refreshToken = $openPlatform->getAuthorizer($agent->appid)['authorization_info']['authorizer_refresh_token'] ?? null;
|
|
if (!$refreshToken) {
|
|
return $this->response()->error('获取refresh_token失败');
|
|
}
|
|
/** @var Client $code */
|
|
$code = $openPlatform->miniProgram($agent->appid, $refreshToken)['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);
|
|
|
|
//获取体验二维码并保存
|
|
$qrcode = $code->getQrCode(storage_path("app/public/{$agent->id}-{$agent->appid}.jpg"));
|
|
Storage::put("public/mini_program_qrcode/{$agent->id}-{$agent->appid}.jpg", $qrcode);
|
|
|
|
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([]);
|
|
|
|
if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
|
|
//保存上传记录
|
|
MiniProgramUploadLog::insert([
|
|
'agent_id' => $agent->id,
|
|
'appid' => $agent->appid,
|
|
'template_id' => $templateId,
|
|
'qrcode' => "mini_program_qrcode/{$agent->id}-{$agent->appid}.jpg",
|
|
'audit_id' => $res['auditid'],
|
|
'created_at' => now(),
|
|
]);
|
|
return $this->response()->success("上传成功,并已提交审核")->refresh();
|
|
} else {
|
|
throw new \Exception($res['errmsg'] ?? join(',', $res));
|
|
}
|
|
} catch (\Exception $e) {
|
|
return $this->response()->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
//注:uploadAll在小程序模板列表页使用
|
|
private function uploadAll()
|
|
{
|
|
|
|
}
|
|
|
|
public function confirm()
|
|
{
|
|
if ($this->action == 1) {
|
|
return ["确定要为此代理商上传小程序吗?", ''];
|
|
} else {
|
|
return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?'];
|
|
}
|
|
}
|
|
|
|
public function parameters()
|
|
{
|
|
return ['action' => $this->action];
|
|
}
|
|
|
|
|
|
}
|