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

162 lines
5.2 KiB

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