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

112 lines
3.9 KiB

  1. <?php
  2. namespace App\Service;
  3. use App\Models\Agent;
  4. use App\Models\MiniProgramTemplate;
  5. use App\Models\MiniProgramUploadLog;
  6. use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
  7. use Exception;
  8. use GuzzleHttp\Exception\GuzzleException;
  9. use Illuminate\Support\Facades\Redis;
  10. use Illuminate\Support\Facades\Storage;
  11. /**
  12. * 上传小程序
  13. * Class UploadMiniProgram
  14. * @package App\Service
  15. */
  16. class UploadMiniProgram
  17. {
  18. /**
  19. * UploadMiniProgram constructor.
  20. * @param Agent $agent
  21. * @param MiniProgramTemplate $template
  22. * @throws GuzzleException
  23. * @throws Exception
  24. */
  25. public function __construct(Agent $agent, MiniProgramTemplate $template)
  26. {
  27. try {
  28. $openPlatform = new OpenPlatform();
  29. $refreshToken = $openPlatform->refreshToken($agent->appid);
  30. if (!$refreshToken) {
  31. throw new Exception('获取refresh_token失败');
  32. }
  33. $miniProgram = $openPlatform->miniProgram($agent->appid, $refreshToken);
  34. /** @var Client $code */
  35. $code = $miniProgram['code'] ?? null;
  36. if (!$code) {
  37. throw new Exception('获取code失败');
  38. }
  39. $templateId = $template->template_id;
  40. $extJson = json_encode(['extAppid' => $agent->appid]);
  41. $version = $template->user_version;
  42. $description = $agent->company_name;
  43. //提交上传
  44. $commit = $code->commit($templateId, $extJson, $version, $description);
  45. if (!isset($commit['errcode'], $commit['errmsg']) || $commit['errcode'] != 0 || $commit['errmsg'] != 'ok') {
  46. throw new Exception(isset($commit['errmsg']) ? $commit['errmsg'] : join(',', $commit));
  47. }
  48. //提交审核,文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/submit_audit.html
  49. $res = $code->submitAudit([]);
  50. $audit_err_code = $this->auditErrorCode();
  51. if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  52. $filename = "mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg";
  53. //保存上传记录
  54. MiniProgramUploadLog::insert([
  55. 'agent_id' => $agent->id,
  56. 'appid' => $agent->appid,
  57. 'user_version' => $template->user_version,
  58. 'template_id' => $templateId,
  59. 'qrcode' => $filename,
  60. 'audit_id' => $res['auditid'],
  61. 'created_at' => now(),
  62. ]);
  63. //获取体验二维码并保存,30天内只获取一次
  64. $qrcode_path = Storage::path("mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg");
  65. if (!file_exists($qrcode_path) || time() - filemtime($qrcode_path) < 86400 * 30) {
  66. $qrcode = $code->getQrCode();
  67. Storage::put("public/$filename", $qrcode);
  68. }
  69. //设置域名,如果之前已经设置过了将记录下来,不再设置
  70. $host = env('APP_URL');
  71. $redis_key = 'mini_program_set_host:' . substr(md5($host), 0, 10);
  72. if (!Redis::hget($redis_key, $agent->appid)) {
  73. /** @var \EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Domain\Client $domain */
  74. $domain = $miniProgram['domain'];
  75. $param = [
  76. "action" => "add",
  77. "requestdomain" => [$host],
  78. "wsrequestdomain" => [str_replace('http', 'ws', $host)],
  79. "uploaddomain" => [$host],
  80. "downloaddomain" => [$host],
  81. ];
  82. $domain->modify($param); //服务器域名,服务器域名多次设置仅第一次成功,这里不校验返回结果正确性
  83. $res = $domain->setWebviewDomain([$host]); //业务域名
  84. if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') {
  85. throw new Exception("设置业务域名{$host}失败,原因:" . $res['errmsg'] ?? (is_array($res) ? join(',', $res) : '未知'));
  86. }
  87. Redis::hset($redis_key, $agent->appid, 1);
  88. }
  89. throw new Exception("上传成功,并已提交审核");
  90. } else if (isset($audit_err_code[$res['errcode']])) {
  91. throw new Exception($audit_err_code[$res['errcode']]);
  92. } else {
  93. throw new Exception($res['errmsg'] ?? join(',', $res));
  94. }
  95. } catch (Exception $e) {
  96. throw new Exception($e->getMessage());
  97. }
  98. }
  99. }