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

136 lines
4.4 KiB

4 years ago
  1. <?php
  2. namespace App\Admin\Extensions\Grid;
  3. use App\Models\AdminSetting;
  4. use App\Models\Agent;
  5. use App\Models\MiniProgramTemplate;
  6. use App\Models\MiniProgramUploadLog;
  7. use Dcat\Admin\Grid\RowAction;
  8. use EasyWeChat\Factory;
  9. use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Storage;
  12. /**
  13. * 上传(注册)小程序
  14. * Class UploadMiniProgram
  15. * @package App\Admin\Extensions\Grid
  16. */
  17. class MiniProgramUpload extends RowAction
  18. {
  19. private $action;
  20. public function __construct($title = null, $action = 1)
  21. {
  22. parent::__construct($title);
  23. $this->action = $action; //$action:1=为指定代理商上传小程序;2=批量上传小程序(一键分发小程序给所有代理商)
  24. $this->title = $action == 1 ? '上传小程序' : '为所有代理商上传';
  25. }
  26. protected function html()
  27. {
  28. $class = 'btn btn-sm btn-primary';
  29. $this->appendHtmlAttribute('class', $class);
  30. $this->defaultHtmlAttribute('href', 'javascript:;');
  31. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  32. }
  33. public function handle(Request $request)
  34. {
  35. return $this->action == 1 ? $this->uploadOne() : $this->uploadAll();
  36. }
  37. //注:uploadOne在代理商列表页使用
  38. private function uploadOne()
  39. {
  40. $template = MiniProgramTemplate::orderBy('template_id', 'desc')->first();
  41. /*if (MiniProgramUploadLog::query()->where(['agent_id' => $this->getKey(), 'template_id' => $template->template_id])->exists()) {
  42. return $this->response()->error("该代理商已经上传过模板ID为 {$template->template_id} 的小程序,无需重复上传");
  43. }*/
  44. $agent = Agent::find($this->getKey());
  45. if (empty($agent->appid)) {
  46. return $this->response()->error('该代理商未注册过小程序,请先注册');
  47. }
  48. try {
  49. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  50. $config = [
  51. 'app_id' => $setting['service_appid'],
  52. 'secret' => $setting['service_appsecret'],
  53. 'token' => $setting['service_token'],
  54. 'aes_key' => $setting['service_aeskey'],
  55. ];
  56. $openPlatform = Factory::openPlatform($config);
  57. $refreshToken = $openPlatform->getAuthorizer($agent->appid)['authorization_info']['authorizer_refresh_token'] ?? null;
  58. if (!$refreshToken) {
  59. return $this->response()->error('获取refresh_token失败');
  60. }
  61. /** @var Client $code */
  62. $code = $openPlatform->miniProgram($agent->appid, $refreshToken)['code'] ?? null;
  63. if (!$code) {
  64. return $this->response()->error('获取code失败');
  65. }
  66. $templateId = $template->template_id;
  67. $extJson = json_encode(['extAppid' => $agent->appid]);
  68. $version = $template->user_version;
  69. $description = $agent->company_name;
  70. //提交上传
  71. $commit = $code->commit($templateId, $extJson, $version, $description);
  72. //获取体验二维码并保存
  73. $qrcode = $code->getQrCode(storage_path("app/public/{$agent->id}-{$agent->appid}.jpg"));
  74. Storage::put("public/mini_program_qrcode/{$agent->id}-{$agent->appid}.jpg", $qrcode);
  75. if (!isset($commit['errcode'], $commit['errmsg']) || $commit['errcode'] != 0 || $commit['errmsg'] != 'ok') {
  76. throw new \Exception(isset($commit['errmsg']) ? $commit['errmsg'] : join(',', $commit));
  77. }
  78. //提交审核,文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/submit_audit.html
  79. $res = $code->submitAudit([]);
  80. if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  81. //保存上传记录
  82. MiniProgramUploadLog::insert([
  83. 'agent_id' => $agent->id,
  84. 'appid' => $agent->appid,
  85. 'template_id' => $templateId,
  86. 'qrcode' => "mini_program_qrcode/{$agent->id}-{$agent->appid}.jpg",
  87. 'audit_id' => $res['auditid'],
  88. 'created_at' => now(),
  89. ]);
  90. return $this->response()->success("上传成功,并已提交审核")->refresh();
  91. } else {
  92. throw new \Exception($res['errmsg'] ?? join(',', $res));
  93. }
  94. } catch (\Exception $e) {
  95. return $this->response()->error($e->getMessage());
  96. }
  97. }
  98. //注:uploadAll在小程序模板列表页使用
  99. private function uploadAll()
  100. {
  101. }
  102. public function confirm()
  103. {
  104. if ($this->action == 1) {
  105. return ["确定要为此代理商上传小程序吗?", ''];
  106. } else {
  107. return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?'];
  108. }
  109. }
  110. public function parameters()
  111. {
  112. return ['action' => $this->action];
  113. }
  114. }