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

98 lines
3.3 KiB

<?php
namespace App\Admin\Extensions\Grid;
use App\Models\AdminSetting;
use App\Models\MiniProgramDraft;
use App\Models\MiniProgramTemplate;
use Dcat\Admin\Grid\RowAction;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
/**
* 获取小程序草稿,并创建模板,并保存草稿和模板列表
* Class UploadMiniProgram
* @package App\Admin\Extensions\Grid
*/
class MiniProgramPull extends RowAction
{
protected $title = '更新列表';
protected function html()
{
$this->appendHtmlAttribute('class', 'btn btn-primary');
$this->defaultHtmlAttribute('href', 'javascript:;');
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
}
/**
* 更新逻辑:
* 1、先获取草稿箱,并保存到数据库;
* 2、如果草稿未创建过模板,通过草稿创建模板;
* 3、将模板列表保存至数据库;
*/
public function handle(Request $request)
{
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);
if (empty($openPlatform['code_template'])) {
return $this->response()->error('获取code_template失败');
}
$codeTemplate = $openPlatform['code_template'];
//草稿列表
$list = $codeTemplate->getDrafts();
if (empty($list['draft_list'])) {
return $this->response()->error('获取草稿箱失败或草稿箱为空');
}
MiniProgramDraft::insertOrIgnore($list['draft_list']);
MiniProgramDraft::whereNotIn('draft_id', array_column($list['draft_list'], 'draft_id'))->delete(); //删除不存在的数据
//从草稿创建模板
$ids = MiniProgramDraft::query()->where('is_create_template', 0)->pluck('draft_id');
foreach ($ids as $draft_id) {
$res = $codeTemplate->createFromDraft($draft_id, 1); //第二个参数,0:普通模板;1:标准模板
if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') {
return $this->response()->error("草稿ID $draft_id 创建模板失败");
} else {
MiniProgramDraft::where('draft_id', $draft_id)->update(['is_create_template' => 1]);
}
}
//模板列表
$list = $codeTemplate->list();
if (empty($list['template_list']) || !is_array($list['template_list'])) {
return $this->response()->error('获取模板失败或模板为空');
}
//2021-09-26发现增加了category_list返回参数,
$list['template_list'] = array_map(function ($v) {
if (isset($v['category_list']) && is_array($v['category_list'])) {
$v['category_list'] = json_encode($v['category_list']);
}
return $v;
}, $list['template_list']);
MiniProgramTemplate::insertOrIgnore($list['template_list']);
MiniProgramTemplate::whereNotIn('template_id', array_column($list['template_list'], 'template_id'))->delete(); //删除不存在的数据
return $this->response()->success("更新列表成功")->refresh();
} catch (\Exception $e) {
return $this->response()->error($e->getMessage());
}
}
public function confirm()
{
return ['此操作将数据与腾讯服务器数据同步,是否继续?', ''];
}
}