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

97 lines
3.2 KiB

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