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

147 lines
4.5 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
  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\Component\Client;
  11. use Illuminate\Http\Request;
  12. /**
  13. * 上传(注册)小程序
  14. * Class UploadMiniProgram
  15. * @package App\Admin\Extensions\Grid
  16. */
  17. class MiniProgramReg 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';
  29. $this->appendHtmlAttribute('class', $class);
  30. $this->defaultHtmlAttribute('href', 'javascript:;');
  31. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  32. }
  33. public function handle(Request $request)
  34. {
  35. return $request->action == 1 ? $this->register() : $this->search();
  36. }
  37. private function register()
  38. {
  39. $agent = Agent::find($this->getKey());
  40. $openPlatform = new OpenPlatform();
  41. $component = $openPlatform->component();
  42. if (empty($component)) {
  43. return $this->response()->error('获取component失败');
  44. }
  45. $res = $component->registerMiniProgram([
  46. 'name' => $agent->company_name, // 企业名
  47. 'code' => $agent->credit_codes, // 企业代码
  48. 'code_type' => 1, // 企业代码类型(1:统一社会信用代码, 2:组织机构代码,3:营业执照注册号)
  49. 'legal_persona_wechat' => $agent->legal_persona_wechat, // 法人微信
  50. 'legal_persona_name' => $agent->legal_persona_name, // 法人姓名
  51. 'component_phone' => AdminSetting::val('service_component_phone'), //第三方联系电话
  52. ]);
  53. $errorArr = $this->regErrorCode();
  54. if (!isset($res['errcode'], $res['errmsg'])) {
  55. return $this->response()->error('注册错误,原因未知');
  56. } else if ($res['errcode'] != 0 && isset($errorArr[$res['errcode']])) {
  57. return $this->response()->error($errorArr[$res['errcode']]);
  58. } else if ($res['errcode'] != 0 || $res['errmsg'] != 'ok') {
  59. return $this->response()->error(join(',', $res));
  60. }
  61. return $this->response()->success('注册小程序成功')->refresh();
  62. }
  63. private function search()
  64. {
  65. $agent = Agent::find($this->getKey());
  66. if (!empty($agent->appid)) {
  67. return $this->response()->error("该代理商已经注册过小程序并分配了APPID:" . $agent->appid);
  68. }
  69. try {
  70. $openPlatform = new OpenPlatform();
  71. /** @var Client $component */
  72. $component = $openPlatform->component();
  73. if (empty($component)) {
  74. return $this->response()->error('获取component失败,请检查配置信息是否正确');
  75. }
  76. $res = $component->getRegistrationStatus($agent->company_name, $agent->legal_persona_wechat, $agent->legal_persona_name);
  77. $errorArr = $this->searchErrorCode();
  78. if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') {
  79. return $this->response()->success("成功")->refresh();
  80. } else if (isset($res['errcode'], $errorArr[$res['errcode']])) {
  81. throw new \Exception($errorArr[$res['errcode']]);
  82. } else {
  83. throw new \Exception(join(',', $res));
  84. }
  85. } catch (\Exception $e) {
  86. return $this->response()->error($e->getMessage());
  87. }
  88. }
  89. public function confirm()
  90. {
  91. if ($this->action == 1) {
  92. return ["确定要{$this->title}吗?", ''];
  93. }
  94. }
  95. public function parameters()
  96. {
  97. return ['action' => $this->action];
  98. }
  99. private function regErrorCode(): array
  100. {
  101. return [
  102. -1 => '非法 action 参数',
  103. 89249 => '该主体已有任务执行中,距上次任务 24小时 后再试',
  104. 89247 => '腾讯服务器内部错误',
  105. 86004 => '无效微信号',
  106. 61070 => '法人姓名与微信号不一致',
  107. 89248 => '企业代码类型无效,请选择正确类型填写',
  108. 89250 => '未找到该任务',
  109. 89251 => '待法人人脸核身校验',
  110. 89252 => '法人&企业信息一致性校验中',
  111. 89253 => '缺少参数',
  112. 89254 => '第三方权限集不全,请补充权限集后重试',
  113. 89255 => 'code参数无效,请检查code长度以及内容是否正确 ;注意code_type的值不同需要传的code长度不一样',
  114. ];
  115. }
  116. private function searchErrorCode(): array
  117. {
  118. return [
  119. -1 => '非法请求',
  120. 89247 => '内部错误',
  121. 89250 => '未找到该企业的注册任务',
  122. 89251 => '模板消息已下发,待法人人脸核身校验',
  123. 89252 => '法人&企业信息一致性校验中',
  124. 89253 => '缺少参数',
  125. ];
  126. }
  127. }