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

181 lines
7.2 KiB

4 years ago
4 years ago
  1. <?php
  2. namespace App\Service;
  3. use App\Models\AdminSetting;
  4. use App\Models\Agent;
  5. use App\Models\MiniProgramTemplate;
  6. use App\Models\MiniProgramUploadLog;
  7. use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
  8. use Exception;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. use Illuminate\Support\Facades\Redis;
  11. use Illuminate\Support\Facades\Storage;
  12. /**
  13. * 上传小程序
  14. * Class UploadMiniProgram
  15. * @package App\Service
  16. */
  17. class UploadMiniProgram
  18. {
  19. /**
  20. * UploadMiniProgram constructor.
  21. * @param Agent $agent
  22. * @param MiniProgramTemplate $template
  23. * @throws GuzzleException
  24. * @throws Exception
  25. */
  26. public function __construct(Agent $agent, MiniProgramTemplate $template)
  27. {
  28. try {
  29. $openPlatform = new OpenPlatform();
  30. $refreshToken = $openPlatform->refreshToken($agent->appid);
  31. if (!$refreshToken) {
  32. throw new Exception('获取refresh_token失败');
  33. }
  34. $miniProgram = $openPlatform->miniProgram($agent->appid, $refreshToken);
  35. /** @var Client $code */
  36. $code = $miniProgram['code'] ?? null;
  37. if (!$code) {
  38. throw new Exception('获取code失败');
  39. }
  40. $templateId = $template->template_id;
  41. $extJson = json_encode(['extAppid' => $agent->appid]);
  42. $version = $template->user_version;
  43. $description = $agent->company_name;
  44. //提交上传
  45. $commit = $code->commit($templateId, $extJson, $version, $description);
  46. if (!isset($commit['errcode'], $commit['errmsg']) || $commit['errcode'] != 0 || $commit['errmsg'] != 'ok') {
  47. throw new Exception($commit['errmsg'] ?? join(',', $commit));
  48. }
  49. //配置小程序用户隐私保护指引:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/privacy_config/set_privacy_setting.html
  50. //说明文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/product/privacy_setting.html
  51. //配置后,需重新提交代码审核,审核通过且需要重新发布上线后才会在小程序端生效。
  52. $redis_key = 'mini_program_set_privacy';
  53. if (!Redis::hget($redis_key, $agent->appid)) {
  54. $data = [
  55. 'privacy_ver' => 2, //1表示现网版本;2表示开发版。默认是2开发版
  56. 'owner_setting' => [
  57. 'contact_phone' => AdminSetting::val('service_component_phone'),
  58. 'notice_method' => '系统公告',
  59. ],
  60. 'setting_list' => [
  61. [
  62. "privacy_key" => "Location",
  63. "privacy_text" => "获取您的位置信息,以便展示产品距离",
  64. "privacy_label" => "位置信息",
  65. ],
  66. [
  67. "privacy_key" => "PhoneNumber",
  68. "privacy_text" => "获取您的手机号,以便商家联系售后等",
  69. "privacy_label" => "手机号",
  70. ],
  71. [
  72. "privacy_key" => "UserInfo",
  73. "privacy_text" => "获取您的昵称和头像,以便展示",
  74. "privacy_label" => "手机号",
  75. ],
  76. ],
  77. ];
  78. $res = $code->httpPostJson('cgi-bin/component/setprivacysetting', $data);
  79. if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') {
  80. throw new Exception('配置小程序用户隐私保护指引失败,原因:' . $res['errmsg'] ?? (is_array($res) ? join(',', $res) : '未知'));
  81. }
  82. Redis::hset($redis_key, $agent->appid, 1);
  83. }
  84. //提交审核,文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/submit_audit.html
  85. $res = $code->submitAudit([]);
  86. $audit_err_code = $this->auditErrorCode();
  87. if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  88. $filename = "mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg";
  89. //保存上传记录
  90. MiniProgramUploadLog::insert([
  91. 'agent_id' => $agent->id,
  92. 'appid' => $agent->appid,
  93. 'user_version' => $template->user_version,
  94. 'template_id' => $templateId,
  95. 'qrcode' => $filename,
  96. 'audit_id' => $res['auditid'],
  97. 'created_at' => now(),
  98. ]);
  99. //获取体验二维码并保存,30天内只获取一次
  100. $qrcode_path = Storage::path("mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg");
  101. if (!file_exists($qrcode_path) || time() - filemtime($qrcode_path) < 86400 * 30) {
  102. $qrcode = $code->getQrCode();
  103. Storage::put("public/$filename", $qrcode);
  104. }
  105. //设置域名,如果之前已经设置过了将记录下来,不再设置
  106. $hosts = [env('APP_URL'), 'https://apis.map.qq.com'];
  107. $redis_key = 'mini_program_set_host:' . substr(md5(join($hosts)), 0, 10);
  108. if (!Redis::hget($redis_key, $agent->appid)) {
  109. /** @var \EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Domain\Client $domain */
  110. $domain = $miniProgram['domain'];
  111. $param = [
  112. "action" => "add",
  113. "requestdomain" => $hosts,
  114. "wsrequestdomain" => [str_replace('http', 'ws', $hosts)],
  115. "uploaddomain" => $hosts,
  116. "downloaddomain" => $hosts,
  117. ];
  118. $domain->modify($param); //服务器域名,服务器域名多次设置仅第一次成功,这里不校验返回结果正确性
  119. $res = $domain->setWebviewDomain(array_slice($hosts, 0, -1)); //业务域名
  120. if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') {
  121. throw new Exception('设置业务域名'.join(';', $hosts).'失败,原因:' . $res['errmsg'] ?? (is_array($res) ? join(',', $res) : '未知'));
  122. }
  123. Redis::hset($redis_key, $agent->appid, 1);
  124. }
  125. return "上传成功,并已提交审核";
  126. } else if (isset($audit_err_code[$res['errcode']])) {
  127. throw new Exception($audit_err_code[$res['errcode']]);
  128. } else {
  129. throw new Exception($res['errmsg'] ?? join(',', $res));
  130. }
  131. } catch (Exception $e) {
  132. throw new Exception($e->getMessage());
  133. }
  134. }
  135. private function auditErrorCode(): array
  136. {
  137. return [
  138. -1 => '系统繁忙',
  139. 86000 => '不是由第三方代小程序进行调用',
  140. 86001 => '不存在第三方的已经提交的代码',
  141. 85006 => '标签格式错误',
  142. 85007 => '页面路径错误',
  143. 85008 => '当前小程序没有已经审核通过的类目,请添加类目成功后重试',
  144. 85009 => '已经有正在审核的版本',
  145. 85010 => 'item_list 有项目为空',
  146. 85011 => '标题填写错误',
  147. 85023 => '审核列表填写的项目数不在 1-5 以内',
  148. 85077 => '小程序类目信息失效(类目中含有官方下架的类目,请重新选择类目)',
  149. 86002 => '小程序还未设置昵称、头像、简介。请先设置完后再重新提交',
  150. 85085 => '小程序提审数量已达本月上限,请点击查看《临时quota申请流程》',
  151. 85086 => '提交代码审核之前需提前上传代码',
  152. 85087 => '小程序已使用 api navigateToMiniProgram,请声明跳转 appid 列表后再次提交',
  153. 87006 => '小游戏不能提交',
  154. 86007 => '小程序禁止提交',
  155. 85051 => 'version_desc或者preview_info超限',
  156. 85092 => 'preview_info格式错误',
  157. 85093 => 'preview_info 视频或者图片个数超限',
  158. 85094 => '需提供审核机制说明信息',
  159. 86009 => '服务商新增小程序代码提审能力被限制',
  160. 86010 => '服务商迭代小程序代码提审能力被限制',
  161. 9400001 => '该开发小程序已开通小程序直播权限,不支持发布版本。如需发版,请解绑开发小程序后再操作。',
  162. 9402202 => '请勿频繁提交,待上一次操作完成后再提交',
  163. ];
  164. }
  165. }