海南旅游SAAS
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.
 
 
 

102 lines
3.7 KiB

<?php
namespace App\Admin\Extensions\Grid;
use App\Models\Agent;
use App\Models\MiniProgramTemplate;
use App\Models\MiniProgramUploadLog;
use App\Service\OpenPlatform;
use Dcat\Admin\Grid\RowAction;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
/**
* 查询小程序发布后某个版本的审核状态
* Class AuditSupplier
* @package App\Admin\Extensions\Grid
*/
class MiniProgramAuditStatus 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 = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger';
$this->appendHtmlAttribute('class', $class);
$this->defaultHtmlAttribute('href', 'javascript:;');
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
}
public function handle(Request $request)
{
$agent = Agent::find($this->getKey());
if (empty($agent->appid)) {
return $this->response()->error('该代理商未注册过小程序,请先注册');
}
// $template_id = MiniProgramTemplate::max('template_id');
$log = MiniProgramUploadLog::where(['agent_id' => $agent->id])->orderBy('id', 'desc')->first();
if (!$log) {
return $this->response()->error('该代理商未上传及发布过最新版本的小程序');
} else if (!$log->audit_id) {
return $this->response()->error('未找到审核记录audit_id');
}
try {
$openPlatform = new OpenPlatform();
$refreshToken = $openPlatform->refreshToken($agent->appid);
if (!$refreshToken) {
return $this->response()->error('获取refresh_token失败');
}
/** @var Client $code */
$code = $openPlatform->code($agent->appid, $refreshToken);
if (!$code) {
return $this->response()->error('获取code失败');
}
$res = $code->getAuditStatus($log->audit_id);
if (isset($res['errcode'], $res['errmsg'], $res['status']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
//保存审核状态
$log->is_success = $res['status'];
$log->save();
$statusArr = [0 => '审核成功', 1 => '审核被拒绝', 2 => '审核中', 3 => '已撤回', 4 => '审核延后'];
if ($res['status'] === 0) { //如果审核成功则发布
$res_release = $code->release();
//发布结果处理
$releaseArr = [-1 => '系统繁忙', 85019 => '没有审核版本', 85020 => '审核状态未满足发布'];
if (isset($res_release['errcode'], $res_release['errmsg']) && $res_release['errcode'] == 0 && $res_release['errmsg'] == 'ok') {
return $this->response()->success("模板ID {$log->template_id} 的小程序已审核成功且发布")->refresh();
} else if (isset($releaseArr[$res_release['errcode']])) {
return $this->response()->warning('审核成功,发布结果为:' . $releaseArr[$res_release['errcode']])->refresh();
} else {
return $this->response()->warning('审核成功,但发布出错:' . join(',', $res_release))->refresh();
}
} else if (isset($statusArr[$res['status']])) {
return $this->response()->warning($statusArr[$res['status']])->refresh();
} else {
return $this->response()->error($res['reason'] ?? join(',', $res));
}
} else {
return $this->response()->error($res['errmsg'] ?? join(',', $res));
}
} catch (InvalidConfigException | GuzzleException | \Exception $e) {
return $this->response()->error($e->getMessage());
}
}
public function parameters()
{
return ['action' => $this->action];
}
}