SmmPostController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Admin\Repositories\BaseProductImage;
  4. use App\Distributor\Repositories\SmmPost;
  5. use App\Distributor\Repositories\SmmUserAccount;
  6. use Carbon\Carbon;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Grid\Model;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Http\Controllers\AdminController;
  12. use Dcat\Admin\Layout\Content;
  13. use Dcat\Admin\Admin;
  14. use Dcat\Admin\FormStep\Form as StepForm;
  15. use Dcat\Admin\Traits\HasUploadedFile;
  16. use Dcat\Admin\Widgets\Alert;
  17. class SmmPostController extends AdminDistController
  18. {
  19. use HasUploadedFile;
  20. /**
  21. * page index
  22. */
  23. public function index(Content $content)
  24. {
  25. return $content
  26. ->header('发报帖子')
  27. ->body($this->form());
  28. }
  29. protected function form()
  30. {
  31. return Form::make(new SmmPost(), function (Form $form) {
  32. $form->title('本地素材发布');
  33. $form->action('ssm-post');
  34. $form->disableListButton();
  35. $form->multipleSteps()
  36. ->remember(false)
  37. ->width('950px')
  38. ->add('选择本地媒体', function ($step) {
  39. $step->radio('send_type',admin_trans_label('send_type'))
  40. ->when([1], function ($step) {
  41. $step->datetime('send_time', admin_trans_label('send_time'))->required();
  42. })
  43. ->options([ 0=>admin_trans_label('immediate'), 1=>admin_trans_label('timing')])->required()->default(0);
  44. $step->text('message', admin_trans_label('post_title'))->required()->maxLength(20);
  45. $step->radio('media_type',admin_trans_label('media_type'))
  46. ->when([0], function ($step) {
  47. $step->multipleImage('image_url', admin_trans_label('images'))
  48. ->disk('local')
  49. ->retainable()//禁止删OSS图
  50. ->sortable() // 可拖动排序
  51. ->removable() // 可移除图片
  52. ->autoUpload() // 自动上传
  53. ->uniqueName()
  54. ->limit(config('admin.upload.oss_image.limit'))
  55. ->accept(config('admin.upload.oss_image.accept'))
  56. ->maxSize(config('admin.upload.oss_image.max_size'))
  57. ->required()
  58. ->saving(function ($reslut) {
  59. return json_encode($reslut);
  60. });
  61. })
  62. ->when([1], function ($step) {
  63. $step->file('video_url')
  64. ->disk('local') // 使用本地存储
  65. ->uniqueName()
  66. ->autoUpload()
  67. ->required()
  68. ->accept(config('admin.upload.oss_video.accept'))
  69. ->maxSize(config('admin.upload.oss_video.max_size'))
  70. ->chunkSize(1024)
  71. ->required()
  72. ->saving(function ($reslut) {
  73. return json_encode($reslut);
  74. });
  75. })
  76. ->options([ 0=>admin_trans_label('images'), 1=>admin_trans_label('videos')])->required()->default(0);
  77. })
  78. ->add('选择传单社媒平台', function ($step) {
  79. $rootAccounts = SmmUserAccount::getUserAccounts();
  80. $listBoxOptions = [];
  81. foreach ($rootAccounts as $account) {
  82. $listBoxOptions[$account->id] = $account->name . ' ('.$account->getParent->name.')';
  83. }
  84. $step->listbox('account_ids', admin_trans_label('accountsSelect'))->required()->options($listBoxOptions);
  85. });
  86. });
  87. }
  88. /*
  89. * 保存数据
  90. */
  91. public function store() {
  92. $post = $_POST;
  93. if (isset($post['upload_column']) && $post['upload_column'] == 'image_url') {
  94. // 上传图片或视频
  95. return $this->upload();
  96. }
  97. if (isset($post['ALL_STEPS']) && $post['ALL_STEPS'] == '1') {
  98. $media_type = $post['media_type'];
  99. if ($media_type == 0) {
  100. $image_video_url = $post['image_url'];
  101. } else {
  102. $image_video_url = $post['video_url'];
  103. }
  104. if ($this->checkStoragePath($image_video_url) === false) {
  105. return '发送失败,请检查上传文件是否存在';
  106. }
  107. if ($post['send_type'] == 0) {
  108. $send_time = Carbon::now();
  109. } else {
  110. //转换时间格式
  111. $send_time = Carbon::createFromFormat('Y-m-d H:i:s', $post['send_time']);
  112. }
  113. //保存数据
  114. SmmPost::create($post,$send_time,$image_video_url);
  115. //最后一步
  116. $data = [
  117. 'title' => '操作成功',
  118. 'description' => '系统已生成发送队列,详情请查看日志。',
  119. ];
  120. return view('distributor.form_custom.completion-page', $data);
  121. }
  122. }
  123. /**
  124. * 上传图片到本地
  125. */
  126. public function upload() {
  127. $disk = $this->disk('local');
  128. // 获取上传的文件
  129. $file = $this->file();
  130. $dir = 'ssm/'.getDistributorId();
  131. $newName = md5(uniqid() . mt_rand()) .'.'.$file->getClientOriginalExtension();
  132. $result = $disk->putFileAs($dir, $file, $newName);
  133. return $result
  134. ? $this->responseUploaded($result, $disk->url($result))
  135. : $this->responseErrorMessage(admin_trans_label('upload_failed'));
  136. }
  137. /*
  138. * 判断路径是否正确
  139. */
  140. public function checkStoragePath ($filePath) {
  141. $storagePath = 'ssm/'.getDistributorId();
  142. if (strpos($filePath, $storagePath) === 0) {
  143. return true;
  144. }
  145. return false;
  146. }
  147. }