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

194 lines
7.2 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
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 App\Service\OpenPlatform;
  8. use Dcat\Admin\Grid\RowAction;
  9. use EasyWeChat\Factory;
  10. use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Redis;
  13. use Illuminate\Support\Facades\Storage;
  14. /**
  15. * 上传(注册)小程序
  16. * Class UploadMiniProgram
  17. * @package App\Admin\Extensions\Grid
  18. */
  19. class MiniProgramUpload extends RowAction
  20. {
  21. private $action;
  22. public function __construct($title = null, $action = 1)
  23. {
  24. parent::__construct($title);
  25. $this->action = $action; //$action:1=为指定代理商上传小程序;2=暂未确定
  26. $this->title = $action == 1 ? '上传小程序' : '为所有代理商上传';
  27. }
  28. protected function html()
  29. {
  30. $class = 'btn btn-sm btn-primary';
  31. $this->appendHtmlAttribute('class', $class);
  32. $this->defaultHtmlAttribute('href', 'javascript:;');
  33. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  34. }
  35. public function handle(Request $request)
  36. {
  37. return $this->action == 1 ? $this->uploadOne() : $this->uploadAll();
  38. }
  39. //注:uploadOne在代理商列表页使用
  40. private function uploadOne()
  41. {
  42. $template = MiniProgramTemplate::orderBy('template_id', 'desc')->first();
  43. /*if (MiniProgramUploadLog::query()->where(['agent_id' => $this->getKey(), 'template_id' => $template->template_id])->exists()) {
  44. return $this->response()->error("该代理商已经上传过模板ID为 {$template->template_id} 的小程序,无需重复上传");
  45. }*/
  46. $agent = Agent::find($this->getKey());
  47. if (empty($agent->appid)) {
  48. return $this->response()->error('该代理商未注册过小程序,请先注册');
  49. }
  50. try {
  51. $openPlatform = new OpenPlatform();
  52. $refreshToken = $openPlatform->refreshToken($agent->appid);
  53. if (!$refreshToken) {
  54. return $this->response()->error('获取refresh_token失败');
  55. }
  56. $miniProgram = $openPlatform->miniProgram($agent->appid, $refreshToken);
  57. /** @var Client $code */
  58. $code = $miniProgram['code'] ?? null;
  59. if (!$code) {
  60. return $this->response()->error('获取code失败');
  61. }
  62. $templateId = $template->template_id;
  63. $extJson = json_encode(['extAppid' => $agent->appid]);
  64. $version = $template->user_version;
  65. $description = $agent->company_name;
  66. //提交上传
  67. $commit = $code->commit($templateId, $extJson, $version, $description);
  68. if (!isset($commit['errcode'], $commit['errmsg']) || $commit['errcode'] != 0 || $commit['errmsg'] != 'ok') {
  69. throw new \Exception(isset($commit['errmsg']) ? $commit['errmsg'] : join(',', $commit));
  70. }
  71. //提交审核,文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/submit_audit.html
  72. $res = $code->submitAudit([]);
  73. $audit_err_code = $this->auditErrorCode();
  74. if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  75. $filename = "mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg";
  76. //保存上传记录
  77. MiniProgramUploadLog::insert([
  78. 'agent_id' => $agent->id,
  79. 'appid' => $agent->appid,
  80. 'user_version' => $template->user_version,
  81. 'template_id' => $templateId,
  82. 'qrcode' => $filename,
  83. 'audit_id' => $res['auditid'],
  84. 'created_at' => now(),
  85. ]);
  86. //获取体验二维码并保存,30天内只获取一次
  87. $qrcode_path = Storage::path("mini_program_qrcode/{$agent->id}-{$agent->appid}-demo.jpg");
  88. if (!file_exists($qrcode_path) || time() - filemtime($qrcode_path) < 86400 * 30) {
  89. $qrcode = $code->getQrCode();
  90. Storage::put("public/$filename", $qrcode);
  91. }
  92. //设置域名,如果之前已经设置过了将记录下来,不再设置
  93. $host = env('APP_URL');
  94. $redis_key = 'mini_program_set_host:' . $host;
  95. if (!Redis::hget($redis_key, $agent->appid)) {
  96. /** @var \EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Domain\Client $domain */
  97. $domain = $miniProgram['domain'];
  98. $param = [
  99. "action" => "add",
  100. "requestdomain" => [$host],
  101. "wsrequestdomain" => [str_replace('http', 'ws', $host)],
  102. "uploaddomain" => [$host],
  103. "downloaddomain" => [$host],
  104. ];
  105. $domain->modify($param); //服务器域名,服务器域名多次设置仅第一次成功,这里不校验返回结果正确性
  106. $res = $domain->setWebviewDomain([$host]); //业务域名
  107. if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') {
  108. throw new \Exception("设置业务域名{$host}失败,原因:" . $res['errmsg'] ?? (is_array($res) ? join(',', $res) : '未知'));
  109. }
  110. Redis::hset($redis_key, $agent->appid, 1);
  111. }
  112. return $this->response()->success("上传成功,并已提交审核")->refresh();
  113. } else if (isset($audit_err_code[$res['errcode']])) {
  114. throw new \Exception($audit_err_code[$res['errcode']]);
  115. } else {
  116. throw new \Exception($res['errmsg'] ?? join(',', $res));
  117. }
  118. } catch (\Exception $e) {
  119. return $this->response()->error($e->getMessage());
  120. }
  121. }
  122. //注:uploadAll在小程序模板列表页使用
  123. private function uploadAll()
  124. {
  125. return true;
  126. }
  127. public function confirm()
  128. {
  129. $last_template_id = MiniProgramTemplate::max('template_id');
  130. if ($this->action == 1) {
  131. return ["确定要上传模板ID为{$last_template_id}的小程序吗?", ''];
  132. } else {
  133. return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?'];
  134. }
  135. }
  136. public function parameters()
  137. {
  138. return ['action' => $this->action];
  139. }
  140. private function auditErrorCode(): array
  141. {
  142. return [
  143. -1 => '系统繁忙',
  144. 86000 => '不是由第三方代小程序进行调用',
  145. 86001 => '不存在第三方的已经提交的代码',
  146. 85006 => '标签格式错误',
  147. 85007 => '页面路径错误',
  148. 85008 => '当前小程序没有已经审核通过的类目,请添加类目成功后重试',
  149. 85009 => '已经有正在审核的版本',
  150. 85010 => 'item_list 有项目为空',
  151. 85011 => '标题填写错误',
  152. 85023 => '审核列表填写的项目数不在 1-5 以内',
  153. 85077 => '小程序类目信息失效(类目中含有官方下架的类目,请重新选择类目)',
  154. 86002 => '小程序还未设置昵称、头像、简介。请先设置完后再重新提交',
  155. 85085 => '小程序提审数量已达本月上限,请点击查看《临时quota申请流程》',
  156. 85086 => '提交代码审核之前需提前上传代码',
  157. 85087 => '小程序已使用 api navigateToMiniProgram,请声明跳转 appid 列表后再次提交',
  158. 87006 => '小游戏不能提交',
  159. 86007 => '小程序禁止提交',
  160. 85051 => 'version_desc或者preview_info超限',
  161. 85092 => 'preview_info格式错误',
  162. 85093 => 'preview_info 视频或者图片个数超限',
  163. 85094 => '需提供审核机制说明信息',
  164. 86009 => '服务商新增小程序代码提审能力被限制',
  165. 86010 => '服务商迭代小程序代码提审能力被限制',
  166. 9400001 => '该开发小程序已开通小程序直播权限,不支持发布版本。如需发版,请解绑开发小程序后再操作。',
  167. 9402202 => '请勿频繁提交,待上一次操作完成后再提交',
  168. ];
  169. }
  170. }