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

89 lines
2.8 KiB

4 years ago
  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\Category;
  7. use App\Models\Channel;
  8. use App\Models\Advertising;
  9. use App\Models\MiniProgramTemplateList;
  10. use App\Models\MiniProgramUploadLog;
  11. use Dcat\Admin\Admin;
  12. use Dcat\Admin\Grid\RowAction;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. /**
  16. * 上传(注册)小程序
  17. * Class UploadMiniProgram
  18. * @package App\Admin\Extensions\Grid
  19. */
  20. class UploadMiniProgram extends RowAction
  21. {
  22. protected $title = '上传小程序';
  23. protected function html()
  24. {
  25. $this->appendHtmlAttribute('class', 'btn btn-sm btn-primary');
  26. $this->defaultHtmlAttribute('href', 'javascript:;');
  27. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  28. }
  29. public function handle(Request $request)
  30. {
  31. $template = MiniProgramTemplateList::orderBy('template_id', 'desc')->first();
  32. if (MiniProgramUploadLog::query()->where(['agent_id' => $this->getKey(), 'template_id' => $template->template_id])->exists()) {
  33. return $this->response()->error('该代理商已经上传过小程序,无需重复上传');
  34. }
  35. $agent = Agent::find($this->getKey());
  36. if (empty($agent->appid)) {
  37. return $this->response()->error('该代理商未注册过小程序,请先注册');
  38. }
  39. try {
  40. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  41. $config = [
  42. 'app_id' => $setting['service_appid'],
  43. 'secret' => $setting['service_appsecret'],
  44. 'token' => $setting['service_token'],
  45. 'aes_key' => $setting['service_aeskey'],
  46. ];
  47. $openPlatform = \EasyWeChat\Factory::openPlatform($config);
  48. $refreshToken = $openPlatform->getAuthorizer($agent->appid)['authorization_info']['authorizer_refresh_token'] ?? null;
  49. if (!$refreshToken) {
  50. return $this->response()->error('获取refresh_token失败');
  51. }
  52. $code = $openPlatform->miniProgram($agent->appid, $refreshToken)['code'];
  53. $templateId = $template->template_id;
  54. $extJson = json_encode(['extAppid' => $agent->appid]);
  55. $version = $template->user_version;
  56. $description = $agent->company_name;
  57. $commit = $code->commit($templateId, $extJson, $version, $description);
  58. // $qrcode = $code->getQrCode();
  59. if (isset($commit['errcode'], $commit['errmsg']) && $commit['errcode'] == 0 && $commit['errmsg'] == 'ok') {
  60. MiniProgramUploadLog::insert([
  61. 'agent_id' => $agent->id,
  62. 'appid' => $agent->appid,
  63. 'template_id' => $templateId,
  64. ]);
  65. return $this->response()->success("上传成功")->refresh();
  66. } else {
  67. throw new \Exception(join(',', $commit));
  68. }
  69. } catch (\Exception $e) {
  70. return $this->response()->error($e->getMessage());
  71. }
  72. }
  73. public function confirm()
  74. {
  75. return ['确定要上传小程序吗?', ''];
  76. }
  77. }