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

94 lines
2.6 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
  1. <?php
  2. namespace App\Admin\Extensions\Grid;
  3. use App\Common\UserStatus;
  4. use App\Jobs\UploadMiniProgramQueue;
  5. use App\Models\Agent;
  6. use App\Models\MiniProgramTemplate;
  7. use App\Service\UploadMiniProgram;
  8. use Dcat\Admin\Grid\RowAction;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. use Illuminate\Http\Request;
  11. /**
  12. * 上传(注册)小程序
  13. * Class UploadMiniProgram
  14. * @package App\Admin\Extensions\Grid
  15. */
  16. class MiniProgramUpload 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 = 'btn btn-sm btn-primary';
  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->uploadOne() : $this->uploadAll();
  35. }
  36. //注:uploadOne在代理商列表页使用
  37. private function uploadOne()
  38. {
  39. $template = MiniProgramTemplate::orderBy('template_id', 'desc')->first();
  40. $agent = Agent::find($this->getKey());
  41. if (empty($agent->appid)) {
  42. return $this->response()->error('该代理商未注册过小程序,请先注册');
  43. }
  44. try {
  45. new UploadMiniProgram($agent, $template);
  46. return $this->response()->success("上传成功,并已提交审核")->refresh();
  47. } catch (GuzzleException | \Exception $e) {
  48. return $this->response()->error($e->getMessage());
  49. }
  50. }
  51. //注:uploadAll在小程序模板列表页使用
  52. private function uploadAll()
  53. {
  54. //上传单个时,$this->getKey()是代理商ID,上传多个时,是小程序模板ID
  55. $template_id = $this->getKey();
  56. $ids = Agent::whereNotNull('appid')
  57. ->where([
  58. ['id', '>', 1], //ID=1是支付小程序,管理员账号
  59. ['status', '=', UserStatus::NORMAL],
  60. ['appid', '<>', ''],
  61. ])->pluck('id');
  62. foreach ($ids as $id) {
  63. UploadMiniProgramQueue::dispatch($template_id, $id);
  64. }
  65. return $this->response()->success('已经为所有已注册过小程序,且状态正常的代理商上传小程序。上传需要一定时间,请耐心等待');
  66. }
  67. public function confirm()
  68. {
  69. if ($this->action == 1) {
  70. $last_template_id = MiniProgramTemplate::max('template_id');
  71. return ["确定要上传模板ID为{$last_template_id}的小程序吗?", ''];
  72. } else {
  73. return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?'];
  74. }
  75. }
  76. public function parameters()
  77. {
  78. return ['action' => $this->action];
  79. }
  80. }