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

97 lines
3.2 KiB

<?php
namespace App\Admin\Extensions\Grid;
use App\Common\UserStatus;
use App\Models\AdminSetting;
use App\Models\Agent;
use App\Models\MiniProgramTemplate;
use App\Models\MiniProgramUploadLog;
use App\Models\Supplier;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\RowAction;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* 查询小程序发布后某个版本的审核状态
* 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, 'template_id' => $template_id])->orderBy('id', 'desc')->first();
if (!$log) {
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失败');
}
$res = $code->getAuditStatus($log->audit_id);
if (isset($res['errcode'], $res['errmsg'], $res['status']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
$statusArr = ['审核成功', '审核被拒绝', '审核中', '已撤回', '审核延后'];
if (isset($statusArr[$res['status']])) {
return $this->response()->success($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];
}
}