23 changed files with 1494 additions and 613 deletions
-
17MySQL_change.sql
-
53app/Admin/Controllers/AgentController.php
-
4app/Admin/Controllers/MiniProgramDraftController.php
-
1app/Admin/Controllers/MiniProgramListController.php
-
20app/Admin/Controllers/MiniProgramTemplateController.php
-
83app/Admin/Extensions/Grid/MiniProgramDelTemp.php
-
89app/Admin/Extensions/Grid/MiniProgramPull.php
-
160app/Admin/Extensions/Grid/MiniProgramReg.php
-
136app/Admin/Extensions/Grid/MiniProgramUpload.php
-
56app/Admin/Extensions/Grid/PullTemplateList.php
-
89app/Admin/Extensions/Grid/UploadMiniProgram.php
-
2app/Admin/Forms/Setting.php
-
53app/Admin/Repositories/MiniProgramDraft.php
-
2app/Admin/Repositories/MiniProgramTemplateList.php
-
6app/AdminAgent/Forms/AgentInfo.php
-
10app/AdminSettled/Controllers/AgentController.php
-
60app/Http/Controllers/Api/MiniProgramController.php
-
1app/Http/Controllers/Api/TestController.php
-
17app/Models/MiniProgramDraft.php
-
17app/Models/MiniProgramTemplate.php
-
11app/Models/MiniProgramTemplateList.php
-
1217composer.lock
-
3resources/lang/zh_CN/agent.php
@ -0,0 +1,83 @@ |
|||
<?php |
|||
|
|||
namespace App\Admin\Extensions\Grid; |
|||
use App\Models\AdminSetting; |
|||
use App\Models\MiniProgramTemplate; |
|||
use Dcat\Admin\Actions\Response; |
|||
use Dcat\Admin\Grid\RowAction; |
|||
use EasyWeChat\Factory; |
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Facades\DB; |
|||
|
|||
class MiniProgramDelTemp extends RowAction |
|||
{ |
|||
/** |
|||
* @return string |
|||
*/ |
|||
protected $title = '删除'; |
|||
|
|||
protected function html() |
|||
{ |
|||
$this->appendHtmlAttribute('class', 'btn btn-sm btn-danger'); |
|||
$this->defaultHtmlAttribute('href', 'javascript:;'); |
|||
|
|||
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>"; |
|||
} |
|||
|
|||
/** |
|||
* Handle the action request. |
|||
* |
|||
* @param Request $request |
|||
* |
|||
* @return Response |
|||
*/ |
|||
public function handle(Request $request) |
|||
{ |
|||
$id = $this->getKey(); //待删除的ID
|
|||
|
|||
//最大ID不允许删除,因为草稿箱已经记录创建模板,即使是更新列表也不再创建模板,删除之后就没有可用模板了
|
|||
if ($id == MiniProgramTemplate::query()->max('template_id')) { |
|||
return $this->response()->error('第一个模板用于注册及创建小程序,不允许删除'); |
|||
} |
|||
|
|||
DB::beginTransaction(); |
|||
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'])) { |
|||
throw new \Exception('获取code_template失败'); |
|||
} |
|||
$codeTemplate = $openPlatform['code_template']; |
|||
$res = $codeTemplate->delete($id); |
|||
|
|||
if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') { |
|||
throw new \Exception(isset($res['errmsg']) ? join(',', $res) : '删除失败,原因未知'); |
|||
} |
|||
|
|||
//删除数据库记录
|
|||
MiniProgramTemplate::where('template_id', $id)->delete(); |
|||
|
|||
DB::commit(); |
|||
return $this->response()->success('删除成功')->refresh(); |
|||
} catch (\Exception $e) { |
|||
DB::rollBack(); |
|||
return $this->response()->error($e->getMessage()); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* @return string|array|void |
|||
*/ |
|||
public function confirm() |
|||
{ |
|||
return ['确定要删除此模板吗?', '']; |
|||
} |
|||
} |
|||
@ -0,0 +1,89 @@ |
|||
<?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'])) { |
|||
return $this->response()->error('获取模板失败或模板为空'); |
|||
} |
|||
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 ['此操作将数据与腾讯服务器数据同步,是否继续?', '']; |
|||
} |
|||
} |
|||
@ -0,0 +1,160 @@ |
|||
<?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\Component\Client; |
|||
use Illuminate\Http\Request; |
|||
|
|||
/** |
|||
* 上传(注册)小程序 |
|||
* Class UploadMiniProgram |
|||
* @package App\Admin\Extensions\Grid |
|||
*/ |
|||
class MiniProgramReg 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'; |
|||
$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->register() : $this->search(); |
|||
} |
|||
|
|||
private function register() |
|||
{ |
|||
$agent = Agent::find($this->getKey()); |
|||
|
|||
$setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey', 'service_component_phone']); |
|||
$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['component'])) { |
|||
return $this->response()->error('获取component失败,请检查配置信息是否正确'); |
|||
} |
|||
$component = $openPlatform['component']; |
|||
|
|||
$res = $component->registerMiniProgram([ |
|||
'name' => $agent->company_name, // 企业名
|
|||
'code' => $agent->credit_codes, // 企业代码
|
|||
'code_type' => 1, // 企业代码类型(1:统一社会信用代码, 2:组织机构代码,3:营业执照注册号)
|
|||
'legal_persona_wechat' => $agent->legal_persona_wechat, // 法人微信
|
|||
'legal_persona_name' => $agent->legal_persona_name, // 法人姓名
|
|||
'component_phone' => $setting['service_component_phone'], //第三方联系电话
|
|||
]); |
|||
|
|||
$errorArr = $this->regErrorCode(); |
|||
|
|||
if (!isset($res['errcode'], $res['errmsg'])) { |
|||
return $this->response()->error('注册错误,原因未知'); |
|||
} else if ($res['errcode'] != 0 && isset($errorArr[$res['errcode']])) { |
|||
return $this->response()->error($errorArr[$res['errcode']]); |
|||
} else if ($res['errcode'] != 0 || $res['errmsg'] != 'ok') { |
|||
return $this->response()->error(join(',', $res)); |
|||
} |
|||
|
|||
return $this->response()->success('注册小程序成功')->refresh(); |
|||
} |
|||
|
|||
private function search() |
|||
{ |
|||
$agent = Agent::find($this->getKey()); |
|||
if (!empty($agent->appid)) { |
|||
return $this->response()->error("该代理商已经注册过小程序并分配了APPID:" . $agent->appid); |
|||
} |
|||
|
|||
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['component'])) { |
|||
return $this->response()->error('获取component失败,请检查配置信息是否正确'); |
|||
} |
|||
/** @var Client $component */ |
|||
$component = $openPlatform['component']; |
|||
$res = $component->getRegistrationStatus($agent->company_name, $agent->legal_persona_wechat, $agent->legal_persona_name); |
|||
|
|||
$errorArr = $this->searchErrorCode(); |
|||
|
|||
if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') { |
|||
return $this->response()->success("成功")->refresh(); |
|||
} else if (isset($res['errcode'], $errorArr[$res['errcode']])) { |
|||
throw new \Exception($errorArr[$res['errcode']]); |
|||
} else { |
|||
throw new \Exception(join(',', $res)); |
|||
} |
|||
} catch (\Exception $e) { |
|||
return $this->response()->error($e->getMessage()); |
|||
} |
|||
} |
|||
|
|||
public function confirm() |
|||
{ |
|||
return ["确定要{$this->title}吗?", '']; |
|||
} |
|||
|
|||
public function parameters() |
|||
{ |
|||
return ['action' => $this->action]; |
|||
} |
|||
|
|||
private function regErrorCode(): array |
|||
{ |
|||
return [ |
|||
-1 => '非法 action 参数', |
|||
89249 => '该主体已有任务执行中,距上次任务 24小时 后再试', |
|||
89247 => '腾讯服务器内部错误', |
|||
86004 => '无效微信号', |
|||
61070 => '法人姓名与微信号不一致', |
|||
89248 => '企业代码类型无效,请选择正确类型填写', |
|||
89250 => '未找到该任务', |
|||
89251 => '待法人人脸核身校验', |
|||
89252 => '法人&企业信息一致性校验中', |
|||
89253 => '缺少参数', |
|||
89254 => '第三方权限集不全,请补充权限集后重试', |
|||
89255 => 'code参数无效,请检查code长度以及内容是否正确 ;注意code_type的值不同需要传的code长度不一样', |
|||
]; |
|||
} |
|||
|
|||
private function searchErrorCode(): array |
|||
{ |
|||
return [ |
|||
-1 => '非法请求', |
|||
89247 => '内部错误', |
|||
89250 => '未找到该企业的注册任务', |
|||
89251 => '模板消息已下发,待法人人脸核身校验', |
|||
89252 => '法人&企业信息一致性校验中', |
|||
89253 => '缺少参数', |
|||
]; |
|||
} |
|||
} |
|||
@ -0,0 +1,136 @@ |
|||
<?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]; |
|||
} |
|||
|
|||
|
|||
} |
|||
@ -1,56 +0,0 @@ |
|||
<?php |
|||
|
|||
namespace App\Admin\Extensions\Grid; |
|||
use App\Models\AdminSetting; |
|||
use App\Models\MiniProgramTemplateList; |
|||
use Dcat\Admin\Grid\RowAction; |
|||
use EasyWeChat\Factory; |
|||
use Illuminate\Http\Request; |
|||
|
|||
/** |
|||
* 上传(注册)小程序 |
|||
* Class UploadMiniProgram |
|||
* @package App\Admin\Extensions\Grid |
|||
*/ |
|||
class PullTemplateList extends RowAction |
|||
{ |
|||
protected $title = '更新列表'; |
|||
|
|||
protected function html() |
|||
{ |
|||
$this->appendHtmlAttribute('class', 'btn btn-primary'); |
|||
$this->defaultHtmlAttribute('href', 'javascript:;'); |
|||
|
|||
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>"; |
|||
} |
|||
|
|||
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); |
|||
$codeTemplate = $openPlatform['code_template']; |
|||
$list = $codeTemplate->list(); |
|||
|
|||
MiniProgramTemplateList::insertOrIgnore($list['template_list']); |
|||
//删除不存在的数据
|
|||
MiniProgramTemplateList::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 ['此操作将数据与腾讯服务器数据同步,是否继续?', '']; |
|||
} |
|||
} |
|||
@ -1,89 +0,0 @@ |
|||
<?php |
|||
|
|||
namespace App\Admin\Extensions\Grid; |
|||
use App\Common\UserStatus; |
|||
use App\Models\AdminSetting; |
|||
use App\Models\Agent; |
|||
use App\Models\Category; |
|||
use App\Models\Channel; |
|||
use App\Models\Advertising; |
|||
use App\Models\MiniProgramTemplateList; |
|||
use App\Models\MiniProgramUploadLog; |
|||
use Dcat\Admin\Admin; |
|||
use Dcat\Admin\Grid\RowAction; |
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Facades\DB; |
|||
|
|||
/** |
|||
* 上传(注册)小程序 |
|||
* Class UploadMiniProgram |
|||
* @package App\Admin\Extensions\Grid |
|||
*/ |
|||
class UploadMiniProgram extends RowAction |
|||
{ |
|||
protected $title = '上传小程序'; |
|||
|
|||
protected function html() |
|||
{ |
|||
$this->appendHtmlAttribute('class', 'btn btn-sm btn-primary'); |
|||
$this->defaultHtmlAttribute('href', 'javascript:;'); |
|||
|
|||
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>"; |
|||
} |
|||
|
|||
public function handle(Request $request) |
|||
{ |
|||
$template = MiniProgramTemplateList::orderBy('template_id', 'desc')->first(); |
|||
if (MiniProgramUploadLog::query()->where(['agent_id' => $this->getKey(), 'template_id' => $template->template_id])->exists()) { |
|||
return $this->response()->error('该代理商已经上传过小程序,无需重复上传'); |
|||
} |
|||
|
|||
$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 = \EasyWeChat\Factory::openPlatform($config); |
|||
$refreshToken = $openPlatform->getAuthorizer($agent->appid)['authorization_info']['authorizer_refresh_token'] ?? null; |
|||
if (!$refreshToken) { |
|||
return $this->response()->error('获取refresh_token失败'); |
|||
} |
|||
$code = $openPlatform->miniProgram($agent->appid, $refreshToken)['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();
|
|||
|
|||
if (isset($commit['errcode'], $commit['errmsg']) && $commit['errcode'] == 0 && $commit['errmsg'] == 'ok') { |
|||
MiniProgramUploadLog::insert([ |
|||
'agent_id' => $agent->id, |
|||
'appid' => $agent->appid, |
|||
'template_id' => $templateId, |
|||
]); |
|||
return $this->response()->success("上传成功")->refresh(); |
|||
} else { |
|||
throw new \Exception(join(',', $commit)); |
|||
} |
|||
} catch (\Exception $e) { |
|||
return $this->response()->error($e->getMessage()); |
|||
} |
|||
} |
|||
|
|||
public function confirm() |
|||
{ |
|||
return ['确定要上传小程序吗?', '']; |
|||
} |
|||
} |
|||
@ -1,60 +1,21 @@ |
|||
<?php |
|||
|
|||
namespace App\Admin\Repositories; |
|||
use App\Models\AdminSetting; |
|||
use Dcat\Admin\Grid; |
|||
use Dcat\Admin\Repositories\Repository; |
|||
use EasyWeChat\Factory; |
|||
use GuzzleHttp\Exception\GuzzleException; |
|||
use Illuminate\Pagination\LengthAwarePaginator; |
|||
use Illuminate\Support\Facades\Cache; |
|||
use App\Models\MiniProgramDraft as Model; |
|||
use Dcat\Admin\Repositories\EloquentRepository; |
|||
|
|||
/** |
|||
* 小程序草稿箱 |
|||
* Class MiniProgramDraft |
|||
* @package App\Admin\Repositories |
|||
*/ |
|||
class MiniProgramDraft extends Repository |
|||
class MiniProgramDraft extends EloquentRepository |
|||
{ |
|||
protected $keyName = 'draft_id'; |
|||
protected $eloquentClass = Model::class; |
|||
|
|||
/** |
|||
* 查询表格数据 |
|||
* |
|||
* @param Grid\Model $model |
|||
* @return LengthAwarePaginator |
|||
* @throws GuzzleException |
|||
*/ |
|||
public function get(Grid\Model $model): LengthAwarePaginator |
|||
public function __construct() |
|||
{ |
|||
$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'], |
|||
]; |
|||
|
|||
$cache_key = 'mini_program:draft_list'; |
|||
if ($data['subjects'] = Cache::get($cache_key)) { |
|||
$data['total'] = count($data['subjects']); |
|||
} else { |
|||
$openPlatform = Factory::openPlatform($config); |
|||
$codeTemplate = $openPlatform['code_template']; |
|||
|
|||
if (empty($codeTemplate) || (!$list = $codeTemplate->getDrafts())) { |
|||
$data['total'] = 0; |
|||
$data['subjects'] = []; |
|||
} else { |
|||
$data['total'] = count($list['draft_list']); |
|||
$data['subjects'] = $list['draft_list'] ?? []; |
|||
Cache::put($cache_key, $list['draft_list'], 180); |
|||
} |
|||
} |
|||
|
|||
return $model->makePaginator( |
|||
$data['total'] ?? 0, // 传入总记录数
|
|||
$data['subjects'] ?? [] // 传入数据二维数组
|
|||
); |
|||
parent::__construct(); |
|||
$this->setKeyName('draft_id'); |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
<?php |
|||
|
|||
namespace App\Models; |
|||
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
|
|||
class MiniProgramDraft extends Model |
|||
{ |
|||
use HasFactory; |
|||
|
|||
public function __construct(array $attributes = []) |
|||
{ |
|||
parent::__construct($attributes); |
|||
$this->timestamps = false; |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
<?php |
|||
|
|||
namespace App\Models; |
|||
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
|
|||
class MiniProgramTemplate extends Model |
|||
{ |
|||
use HasFactory; |
|||
|
|||
public function __construct(array $attributes = []) |
|||
{ |
|||
parent::__construct($attributes); |
|||
$this->timestamps = false; |
|||
} |
|||
} |
|||
@ -1,11 +0,0 @@ |
|||
<?php |
|||
|
|||
namespace App\Models; |
|||
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
|
|||
class MiniProgramTemplateList extends Model |
|||
{ |
|||
use HasFactory; |
|||
} |
|||
1217
composer.lock
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
Write
Preview
Loading…
Cancel
Save
Reference in new issue