海南旅游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
2.8 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
  1. <?php
  2. namespace App\Admin\Extensions\Grid;
  3. use App\Common\UserStatus;
  4. use App\Jobs\BatchUploadMiniProgram;
  5. use App\Models\Agent;
  6. use App\Models\MiniProgramTemplate;
  7. use App\Models\MiniProgramUploadLog;
  8. use App\Service\OpenPlatform;
  9. use App\Service\UploadMiniProgram;
  10. use Dcat\Admin\Grid\RowAction;
  11. use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Code\Client;
  12. use GuzzleHttp\Exception\GuzzleException;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Redis;
  15. use Illuminate\Support\Facades\Storage;
  16. /**
  17. * 上传(注册)小程序
  18. * Class UploadMiniProgram
  19. * @package App\Admin\Extensions\Grid
  20. */
  21. class MiniProgramUpload extends RowAction
  22. {
  23. private $action;
  24. public function __construct($title = null, $action = 1)
  25. {
  26. parent::__construct($title);
  27. $this->action = $action;
  28. $this->title = $action == 1 ? '上传小程序' : '上传给所有代理商';
  29. }
  30. protected function html()
  31. {
  32. $class = 'btn btn-sm btn-primary';
  33. $this->appendHtmlAttribute('class', $class);
  34. $this->defaultHtmlAttribute('href', 'javascript:;');
  35. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  36. }
  37. public function handle(Request $request)
  38. {
  39. return $request->action == 1 ? $this->uploadOne() : $this->uploadAll();
  40. }
  41. //注:uploadOne在代理商列表页使用
  42. private function uploadOne()
  43. {
  44. $template = MiniProgramTemplate::orderBy('template_id', 'desc')->first();
  45. $agent = Agent::find($this->getKey());
  46. if (empty($agent->appid)) {
  47. return $this->response()->error('该代理商未注册过小程序,请先注册');
  48. }
  49. try {
  50. new UploadMiniProgram($agent, $template);
  51. return $this->response()->success("上传成功,并已提交审核")->refresh();
  52. } catch (GuzzleException | \Exception $e) {
  53. return $this->response()->error($e->getMessage());
  54. }
  55. }
  56. //注:uploadAll在小程序模板列表页使用
  57. private function uploadAll()
  58. {
  59. //上传单个时,$this->getKey()是代理商ID,上传多个时,是小程序模板ID
  60. $template_id = $this->getKey();
  61. $ids = Agent::whereNotNull('appid')
  62. ->where([
  63. ['status', '=', UserStatus::NORMAL],
  64. ['appid', '<>', ''],
  65. ])->pluck('id');
  66. foreach ($ids as $id) {
  67. BatchUploadMiniProgram::dispatch($template_id, $id);
  68. }
  69. return $this->response()->success('已经为所有已注册过小程序,且状态正常的代理商上传小程序。上传需要一定时间,请耐心等待');
  70. }
  71. public function confirm()
  72. {
  73. if ($this->action == 1) {
  74. $last_template_id = MiniProgramTemplate::max('template_id');
  75. return ["确定要上传模板ID为{$last_template_id}的小程序吗?", ''];
  76. } else {
  77. return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?'];
  78. }
  79. }
  80. public function parameters()
  81. {
  82. return ['action' => $this->action];
  83. }
  84. }