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.
		
		
		
		
		
			
		
			
				
					
					
						
							83 lines
						
					
					
						
							2.3 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							83 lines
						
					
					
						
							2.3 KiB
						
					
					
				
								<?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 ['确定要删除此模板吗?', ''];
							 | 
						|
									}
							 | 
						|
								}
							 |