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.
		
		
		
		
		
			
		
			
				
					
					
						
							162 lines
						
					
					
						
							5.2 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							162 lines
						
					
					
						
							5.2 KiB
						
					
					
				
								<?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;
							 | 
						|
										$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()
							 | 
						|
									{
							 | 
						|
										if ($this->action == 1) {
							 | 
						|
											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 => '缺少参数',
							 | 
						|
										];
							 | 
						|
									}
							 | 
						|
								}
							 |