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

98 lines
3.3 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. $statusArr = [0 => '审核成功', 1 => '审核被拒绝', 2 => '审核中', 3 => '已撤回', 4 => '审核延后'];
  68. //保存审核状态
  69. $log->is_success = $res['status'];
  70. $log->save();
  71. if (isset($statusArr[$res['status']])) {
  72. return $this->response()->success($statusArr[$res['status']])->refresh();
  73. } else {
  74. return $this->response()->error($res['reason'] ?? join(',', $res));
  75. }
  76. } else {
  77. return $this->response()->error($res['errmsg'] ?? join(',', $res));
  78. }
  79. } catch (InvalidConfigException | GuzzleException | \Exception $e) {
  80. return $this->response()->error($e->getMessage());
  81. }
  82. }
  83. public function parameters()
  84. {
  85. return ['action' => $this->action];
  86. }
  87. }