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.
		
		
		
		
		
			
		
			
				
					
					
						
							89 lines
						
					
					
						
							2.8 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							89 lines
						
					
					
						
							2.8 KiB
						
					
					
				
								<?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 ['确定要上传小程序吗?', ''];
							 | 
						|
									}
							 | 
						|
								}
							 |