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

158 lines
5.4 KiB

4 years ago
  1. <?php
  2. namespace App\Admin\Extensions\Grid;
  3. use App\Models\Agent;
  4. use App\Models\MiniProgramTemplate;
  5. use App\Models\MiniProgramUploadLog;
  6. use App\Service\OpenPlatform;
  7. use Dcat\Admin\Grid\RowAction;
  8. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  9. use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
  10. use GuzzleHttp\Exception\GuzzleException;
  11. use Illuminate\Http\Request;
  12. /**
  13. * 查询小程序发布后某个版本的审核状态
  14. * Class AuditSupplier
  15. * @package App\Admin\Extensions\Grid
  16. */
  17. class MiniProgramAuditStatus extends RowAction
  18. {
  19. private $action;
  20. public function __construct($title = null, $action = 1)
  21. {
  22. parent::__construct($title);
  23. $this->action = $action;
  24. $this->title = $action == 1 ? '审核状态' : '审核撤销';
  25. }
  26. protected function html()
  27. {
  28. $class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger';
  29. $this->appendHtmlAttribute('class', $class);
  30. $this->defaultHtmlAttribute('href', 'javascript:;');
  31. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  32. }
  33. public function handle()
  34. {
  35. return $this->action == 1 ? $this->auditStatus() : $this->undoAudit();
  36. }
  37. private function auditStatus()
  38. {
  39. $agent = Agent::find($this->getKey());
  40. if (empty($agent->appid)) {
  41. return $this->response()->error('该代理商未注册过小程序,请先注册');
  42. }
  43. $log = MiniProgramUploadLog::where(['agent_id' => $agent->id])->orderBy('id', 'desc')->first();
  44. if (!$log) {
  45. return $this->response()->error('该代理商未上传及发布过最新版本的小程序');
  46. } else if (!$log->audit_id) {
  47. return $this->response()->error('未找到审核记录audit_id');
  48. }
  49. try {
  50. $openPlatform = new OpenPlatform();
  51. $refreshToken = $openPlatform->refreshToken($agent->appid);
  52. if (!$refreshToken) {
  53. return $this->response()->error('获取refresh_token失败');
  54. }
  55. /** @var Client $code */
  56. $code = $openPlatform->code($agent->appid, $refreshToken);
  57. if (!$code) {
  58. return $this->response()->error('获取code失败');
  59. }
  60. $res = $code->getAuditStatus($log->audit_id);
  61. if (isset($res['errcode'], $res['errmsg'], $res['status']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  62. //保存审核状态
  63. $log->is_success = $res['status'];
  64. $log->save();
  65. $statusArr = [0 => '审核成功', 1 => '审核被拒绝', 2 => '审核中', 3 => '已撤回', 4 => '审核延后'];
  66. if ($res['status'] === 0) { //如果审核成功则发布
  67. $res_release = $code->release();
  68. //发布结果处理
  69. $releaseArr = [-1 => '系统繁忙', 85019 => '没有审核版本', 85020 => '审核状态未满足发布'];
  70. if (isset($res_release['errcode'], $res_release['errmsg']) && $res_release['errcode'] == 0 && $res_release['errmsg'] == 'ok') {
  71. return $this->response()->success("模板ID {$log->template_id} 的小程序已审核成功且发布")->refresh();
  72. } else {
  73. return $this->response()->warning('审核成功,发布结果为:' . ($releaseArr[$res_release['errcode']] ?? join(',', $res_release)))->refresh();
  74. }
  75. } else if (isset($statusArr[$res['status']])) {
  76. return $this->response()->warning($statusArr[$res['status']])->refresh();
  77. } else {
  78. return $this->response()->error($res['reason'] ?? join(',', $res));
  79. }
  80. } else {
  81. return $this->response()->error($res['errmsg'] ?? join(',', $res));
  82. }
  83. } catch (InvalidConfigException | GuzzleException | \Exception $e) {
  84. return $this->response()->error($e->getMessage());
  85. }
  86. }
  87. private function undoAudit()
  88. {
  89. $agent = Agent::find($this->getKey());
  90. if (empty($agent->appid)) {
  91. return $this->response()->error('该代理商未注册过小程序,请先注册');
  92. }
  93. $log = MiniProgramUploadLog::where(['agent_id' => $agent->id])->orderBy('id', 'desc')->first();
  94. if (!$log) {
  95. return $this->response()->error('该代理商未上传及发布过最新版本的小程序');
  96. } else if (!$log->audit_id) {
  97. return $this->response()->error('未找到审核记录audit_id');
  98. }
  99. try {
  100. $openPlatform = new OpenPlatform();
  101. $refreshToken = $openPlatform->refreshToken($agent->appid);
  102. if (!$refreshToken) {
  103. return $this->response()->error('获取refresh_token失败');
  104. }
  105. /** @var Client $code */
  106. $code = $openPlatform->code($agent->appid, $refreshToken);
  107. if (!$code) {
  108. return $this->response()->error('获取code失败');
  109. }
  110. $res = $code->withdrawAudit();
  111. if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  112. //保存审核状态为已撤回
  113. $log->is_success = 3;
  114. $log->save();
  115. return $this->response()->success("撤回最后审核单成功")->refresh();
  116. } else if (isset($res['errcode']) && $res['errcode'] != 0) {
  117. $error = [
  118. -1 => '腾讯内部系统错误',
  119. 87013 => '撤回次数达到上限(每天5次,每个月 10 次)',
  120. ];
  121. return $this->response()->error(isset($error[$res['errcode']]) ? $error[$res['errcode']] : ($res['errmsg'] ?? join(',', $res)));
  122. } else {
  123. return $this->response()->error($res['errmsg'] ?? join(',', $res));
  124. }
  125. } catch (InvalidConfigException | GuzzleException | \Exception $e) {
  126. return $this->response()->error($e->getMessage());
  127. }
  128. }
  129. public function confirm()
  130. {
  131. if ($this->action == 2) {
  132. return ['撤回次数每天最多5次,每个月最多10次,是否继续?', ''];
  133. }
  134. }
  135. public function parameters()
  136. {
  137. return ['action' => $this->action];
  138. }
  139. }