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

190 lines
7.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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. $miniProgram = $openPlatform->miniProgram($agent->appid, $refreshToken);
  62. //设置域名
  63. /** @var \EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Domain\Client $domain */
  64. $domain = $miniProgram['domain'];
  65. $host = env('APP_URL');
  66. $param = [
  67. "action" => "add",
  68. "requestdomain" => [$host],
  69. "wsrequestdomain" => [str_replace('http', 'ws', $host)],
  70. "uploaddomain" => [$host],
  71. "downloaddomain" => [$host],
  72. ];
  73. $domain->modify($param); //服务器域名,服务器域名多次设置仅第一次成功,这里不校验返回结果正确性
  74. $res = $domain->setWebviewDomain([$host]); //业务域名
  75. if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') {
  76. throw new \Exception("设置业务域名{$host}失败,原因:" . $res['errmsg'] ?? (is_array($res) ? join(',', $res) : '未知'));
  77. }
  78. /** @var Client $code */
  79. $code = $miniProgram['code'] ?? null;
  80. if (!$code) {
  81. return $this->response()->error('获取code失败');
  82. }
  83. $templateId = $template->template_id;
  84. $extJson = json_encode(['extAppid' => $agent->appid]);
  85. $version = $template->user_version;
  86. $description = $agent->company_name;
  87. //提交上传
  88. $commit = $code->commit($templateId, $extJson, $version, $description);
  89. //获取体验二维码并保存
  90. $qrcode = $code->getQrCode(storage_path("app/public/{$agent->id}-{$agent->appid}.jpg"));
  91. Storage::put("public/mini_program_qrcode/{$agent->id}-{$agent->appid}.jpg", $qrcode);
  92. if (!isset($commit['errcode'], $commit['errmsg']) || $commit['errcode'] != 0 || $commit['errmsg'] != 'ok') {
  93. throw new \Exception(isset($commit['errmsg']) ? $commit['errmsg'] : join(',', $commit));
  94. }
  95. //提交审核,文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/submit_audit.html
  96. $res = $code->submitAudit([]);
  97. $audit_err_code = $this->auditErrorCode();
  98. if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  99. //保存上传记录
  100. MiniProgramUploadLog::insert([
  101. 'agent_id' => $agent->id,
  102. 'appid' => $agent->appid,
  103. 'user_version' => $template->user_version,
  104. 'template_id' => $templateId,
  105. 'qrcode' => "mini_program_qrcode/{$agent->id}-{$agent->appid}.jpg",
  106. 'audit_id' => $res['auditid'],
  107. 'created_at' => now(),
  108. ]);
  109. return $this->response()->success("上传成功,并已提交审核")->refresh();
  110. } else if (isset($audit_err_code[$res['errcode']])) {
  111. throw new \Exception($audit_err_code[$res['errcode']]);
  112. } else {
  113. throw new \Exception($res['errmsg'] ?? join(',', $res));
  114. }
  115. } catch (\Exception $e) {
  116. return $this->response()->error($e->getMessage());
  117. }
  118. }
  119. //注:uploadAll在小程序模板列表页使用
  120. private function uploadAll()
  121. {
  122. }
  123. public function confirm()
  124. {
  125. $last_template_id = MiniProgramTemplate::max('template_id');
  126. if ($this->action == 1) {
  127. return ["确定要上传模板ID为{$last_template_id}的小程序吗?", ''];
  128. } else {
  129. return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?'];
  130. }
  131. }
  132. public function parameters()
  133. {
  134. return ['action' => $this->action];
  135. }
  136. private function auditErrorCode(): array
  137. {
  138. return [
  139. -1 => '系统繁忙',
  140. 86000 => '不是由第三方代小程序进行调用',
  141. 86001 => '不存在第三方的已经提交的代码',
  142. 85006 => '标签格式错误',
  143. 85007 => '页面路径错误',
  144. 85008 => '当前小程序没有已经审核通过的类目,请添加类目成功后重试',
  145. 85009 => '已经有正在审核的版本',
  146. 85010 => 'item_list 有项目为空',
  147. 85011 => '标题填写错误',
  148. 85023 => '审核列表填写的项目数不在 1-5 以内',
  149. 85077 => '小程序类目信息失效(类目中含有官方下架的类目,请重新选择类目)',
  150. 86002 => '小程序还未设置昵称、头像、简介。请先设置完后再重新提交',
  151. 85085 => '小程序提审数量已达本月上限,请点击查看《临时quota申请流程》',
  152. 85086 => '提交代码审核之前需提前上传代码',
  153. 85087 => '小程序已使用 api navigateToMiniProgram,请声明跳转 appid 列表后再次提交',
  154. 87006 => '小游戏不能提交',
  155. 86007 => '小程序禁止提交',
  156. 85051 => 'version_desc或者preview_info超限',
  157. 85092 => 'preview_info格式错误',
  158. 85093 => 'preview_info 视频或者图片个数超限',
  159. 85094 => '需提供审核机制说明信息',
  160. 86009 => '服务商新增小程序代码提审能力被限制',
  161. 86010 => '服务商迭代小程序代码提审能力被限制',
  162. 9400001 => '该开发小程序已开通小程序直播权限,不支持发布版本。如需发版,请解绑开发小程序后再操作。',
  163. 9402202 => '请勿频繁提交,待上一次操作完成后再提交',
  164. ];
  165. }
  166. }