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

111 lines
4.1 KiB

  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\Kernel\Exceptions\InvalidConfigException;
  10. use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
  11. use GuzzleHttp\Exception\GuzzleException;
  12. use Illuminate\Http\Request;
  13. /**
  14. * 查询小程序发布后某个版本的审核状态
  15. * Class AuditSupplier
  16. * @package App\Admin\Extensions\Grid
  17. */
  18. class MiniProgramAuditStatus extends RowAction
  19. {
  20. private $action;
  21. public function __construct($title = null, $action = 1)
  22. {
  23. parent::__construct($title);
  24. $this->action = $action; //$action:1=通过;2=拒绝
  25. $this->title = $action == 1 ? '审核状态' : '拒绝';
  26. }
  27. protected function html()
  28. {
  29. $class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger';
  30. $this->appendHtmlAttribute('class', $class);
  31. $this->defaultHtmlAttribute('href', 'javascript:;');
  32. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  33. }
  34. public function handle(Request $request)
  35. {
  36. $agent = Agent::find($this->getKey());
  37. if (empty($agent->appid)) {
  38. return $this->response()->error('该代理商未注册过小程序,请先注册');
  39. }
  40. $template_id = MiniProgramTemplate::max('template_id');
  41. $log = MiniProgramUploadLog::where(['agent_id' => $agent->id, 'template_id' => $template_id])->orderBy('id', 'desc')->first();
  42. if (!$log) {
  43. return $this->response()->error('该代理商未上传及发布过最新版本的小程序');
  44. } else if (!$log->audit_id){
  45. return $this->response()->error('未找到审核记录audit_id');
  46. }
  47. try {
  48. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  49. $config = [
  50. 'app_id' => $setting['service_appid'],
  51. 'secret' => $setting['service_appsecret'],
  52. 'token' => $setting['service_token'],
  53. 'aes_key' => $setting['service_aeskey'],
  54. ];
  55. $openPlatform = Factory::openPlatform($config);
  56. $refreshToken = $openPlatform->getAuthorizer($agent->appid)['authorization_info']['authorizer_refresh_token'] ?? null;
  57. if (!$refreshToken) {
  58. return $this->response()->error('获取refresh_token失败');
  59. }
  60. /** @var Client $code */
  61. $code = $openPlatform->miniProgram($agent->appid, $refreshToken)['code'] ?? null;
  62. if (!$code) {
  63. return $this->response()->error('获取code失败');
  64. }
  65. $res = $code->getAuditStatus($log->audit_id);
  66. if (isset($res['errcode'], $res['errmsg'], $res['status']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  67. //保存审核状态
  68. $log->is_success = $res['status'];
  69. $log->save();
  70. $statusArr = [0 => '审核成功', 1 => '审核被拒绝', 2 => '审核中', 3 => '已撤回', 4 => '审核延后'];
  71. if ($res['status'] === 0) { //如果审核成功则发布
  72. $res_release = $code->release();
  73. //发布结果处理
  74. $releaseArr = [-1 => '系统繁忙', 85019 => '没有审核版本', 85020 => '审核状态未满足发布'];
  75. if (isset($res_release['errcode'], $res_release['errmsg']) && $res_release['errcode'] == 0 && $res_release['errmsg'] == 'ok') {
  76. return $this->response()->success('审核成功且已发布')->refresh();
  77. } else if (isset($releaseArr[$res_release['errcode']])) {
  78. return $this->response()->success('审核成功,发布结果为:' . $releaseArr[$res_release['errcode']])->refresh();
  79. } else {
  80. return $this->response()->success('审核成功,失败出错:' . join(',', $res_release))->refresh();
  81. }
  82. } else if (isset($statusArr[$res['status']])) {
  83. return $this->response()->success($statusArr[$res['status']])->refresh();
  84. } else {
  85. return $this->response()->error($res['reason'] ?? join(',', $res));
  86. }
  87. } else {
  88. return $this->response()->error($res['errmsg'] ?? join(',', $res));
  89. }
  90. } catch (InvalidConfigException | GuzzleException | \Exception $e) {
  91. return $this->response()->error($e->getMessage());
  92. }
  93. }
  94. public function parameters()
  95. {
  96. return ['action' => $this->action];
  97. }
  98. }