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

83 lines
2.3 KiB

4 years ago
  1. <?php
  2. namespace App\Admin\Extensions\Grid;
  3. use App\Models\AdminSetting;
  4. use App\Models\MiniProgramTemplate;
  5. use Dcat\Admin\Actions\Response;
  6. use Dcat\Admin\Grid\RowAction;
  7. use EasyWeChat\Factory;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. class MiniProgramDelTemp extends RowAction
  11. {
  12. /**
  13. * @return string
  14. */
  15. protected $title = '删除';
  16. protected function html()
  17. {
  18. $this->appendHtmlAttribute('class', 'btn btn-sm btn-danger');
  19. $this->defaultHtmlAttribute('href', 'javascript:;');
  20. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  21. }
  22. /**
  23. * Handle the action request.
  24. *
  25. * @param Request $request
  26. *
  27. * @return Response
  28. */
  29. public function handle(Request $request)
  30. {
  31. $id = $this->getKey(); //待删除的ID
  32. //最大ID不允许删除,因为草稿箱已经记录创建模板,即使是更新列表也不再创建模板,删除之后就没有可用模板了
  33. if ($id == MiniProgramTemplate::query()->max('template_id')) {
  34. return $this->response()->error('第一个模板用于注册及创建小程序,不允许删除');
  35. }
  36. DB::beginTransaction();
  37. try {
  38. //删除腾讯服务器上的
  39. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  40. $config = [
  41. 'app_id' => $setting['service_appid'],
  42. 'secret' => $setting['service_appsecret'],
  43. 'token' => $setting['service_token'],
  44. 'aes_key' => $setting['service_aeskey'],
  45. ];
  46. $openPlatform = Factory::openPlatform($config);
  47. if (empty($openPlatform['code_template'])) {
  48. throw new \Exception('获取code_template失败');
  49. }
  50. $codeTemplate = $openPlatform['code_template'];
  51. $res = $codeTemplate->delete($id);
  52. if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') {
  53. throw new \Exception(isset($res['errmsg']) ? join(',', $res) : '删除失败,原因未知');
  54. }
  55. //删除数据库记录
  56. MiniProgramTemplate::where('template_id', $id)->delete();
  57. DB::commit();
  58. return $this->response()->success('删除成功')->refresh();
  59. } catch (\Exception $e) {
  60. DB::rollBack();
  61. return $this->response()->error($e->getMessage());
  62. }
  63. }
  64. /**
  65. * @return string|array|void
  66. */
  67. public function confirm()
  68. {
  69. return ['确定要删除此模板吗?', ''];
  70. }
  71. }