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

93 lines
2.6 KiB

<?php
namespace App\Admin\Extensions\Grid;
use App\Common\UserStatus;
use App\Jobs\BatchUploadMiniProgram;
use App\Models\Agent;
use App\Models\MiniProgramTemplate;
use App\Service\UploadMiniProgram;
use Dcat\Admin\Grid\RowAction;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
/**
* 上传(注册)小程序
* 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;
$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 $request->action == 1 ? $this->uploadOne() : $this->uploadAll();
}
//注:uploadOne在代理商列表页使用
private function uploadOne()
{
$template = MiniProgramTemplate::orderBy('template_id', 'desc')->first();
$agent = Agent::find($this->getKey());
if (empty($agent->appid)) {
return $this->response()->error('该代理商未注册过小程序,请先注册');
}
try {
new UploadMiniProgram($agent, $template);
return $this->response()->success("上传成功,并已提交审核")->refresh();
} catch (GuzzleException | \Exception $e) {
return $this->response()->error($e->getMessage());
}
}
//注:uploadAll在小程序模板列表页使用
private function uploadAll()
{
//上传单个时,$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()
{
if ($this->action == 1) {
$last_template_id = MiniProgramTemplate::max('template_id');
return ["确定要上传模板ID为{$last_template_id}的小程序吗?", ''];
} else {
return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?'];
}
}
public function parameters()
{
return ['action' => $this->action];
}
}