SmmPostController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 App\Services\SmmService;
  7. use Carbon\Carbon;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Grid\Model;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use Dcat\Admin\Layout\Content;
  14. use Dcat\Admin\Admin;
  15. use Dcat\Admin\FormStep\Form as StepForm;
  16. use Dcat\Admin\Traits\HasUploadedFile;
  17. use Dcat\Admin\Widgets\Alert;
  18. class SmmPostController extends AdminDistController
  19. {
  20. use HasUploadedFile;
  21. /**
  22. * page index
  23. */
  24. public function index(Content $content)
  25. {
  26. // $ssmService = new SmmService('youtube');
  27. // $videoCategories = $ssmService->getVideoCategories();
  28. // var_dump($videoCategories['data']);
  29. // exit;
  30. return $content
  31. ->header('发报帖子')
  32. ->body($this->form());
  33. }
  34. protected function form()
  35. {
  36. return Form::make(new SmmPost(), function (Form $form) {
  37. $form->title('本地素材发布');
  38. $form->action('ssm-post');
  39. $form->disableListButton();
  40. $form->multipleSteps()
  41. ->remember()
  42. ->width('950px')
  43. ->add('选择本地媒体', function ($step) {
  44. $step->radio('send_type',admin_trans_label('send_type'))
  45. ->when([1], function ($step) {
  46. $step->datetime('send_time', '<span style="color:#bd4147;">*</span> '.admin_trans_label('send_time'))->placeholder(' ');
  47. })
  48. ->options([ 0=>admin_trans_label('immediate'), 1=>admin_trans_label('timing')])->default(0);
  49. $step->radio('post_type',admin_trans_label('media_type'))
  50. ->when([0], function ($step) {
  51. $step->textarea('image_message', '<span style="color:#bd4147;">*</span> '.admin_trans_label('post_message'))->rows(3)->placeholder(' ');
  52. $step->multipleImage('image_url', '<span style="color:#bd4147;">*</span> '.admin_trans_label('images'))
  53. ->retainable()//禁止删OSS图
  54. ->sortable() // 可拖动排序
  55. ->removable() // 可移除图片
  56. ->autoUpload() // 自动上传
  57. ->uniqueName()
  58. ->limit(4)
  59. ->accept(config('admin.upload.oss_image.accept'))
  60. ->maxSize(config('admin.upload.oss_image.max_size'));
  61. })
  62. ->when([1], function ($step) {
  63. $step->textarea('video_message', '<span style="color:#bd4147;">*</span> '.admin_trans_label('post_message'))->rows(3)->placeholder(' ');
  64. $step->file('video_url','<span style="color:#bd4147;">*</span> '.admin_trans_label('video'))
  65. ->uniqueName()
  66. ->autoUpload()
  67. ->accept(config('admin.upload.oss_video.accept'))
  68. ->maxSize(config('admin.upload.oss_video.max_size'))
  69. ->chunkSize(1024);
  70. })
  71. ->options([ 0=>admin_trans_label('images'), 1=>admin_trans_label('videos')])->default(0);
  72. $this->stepLeaving($step,0);
  73. })
  74. ->add('选择传单社媒平台', function ($step) {
  75. $rootAccounts = SmmUserAccount::getUserAccounts();
  76. $listBoxOptions = [];
  77. foreach ($rootAccounts as $account) {
  78. $listBoxOptions[$account->id] = $account->name . ' ('.$account->getParent->name.')';
  79. }
  80. $step->listbox('account_ids', '<span style="color:#bd4147;">*</span> '.admin_trans_label('accountsSelect'))
  81. ->options($listBoxOptions);
  82. $step->select('youtube_category')->setView('distributor.form_custom.select_hide')->options(SmmUserAccount::getYoutubeCategory())->default(22)->required();
  83. $this->stepLeaving($step,1);
  84. });
  85. $this->addJs();
  86. });
  87. }
  88. /*
  89. * 保存数据
  90. */
  91. public function store() {
  92. $post = $_POST;
  93. if (isset($post['upload_column']) && ($post['upload_column'] == 'image_url' || $post['upload_column'] == 'video_url')) {
  94. // 上传图片或视频
  95. return $this->upload();
  96. }
  97. if (isset($post['ALL_STEPS']) && $post['ALL_STEPS'] == '1') {
  98. $post_type = $post['post_type'];
  99. if ($post_type == 0) {
  100. $image_video_url = $post['image_url'];
  101. $post['message'] = $post['image_message'];
  102. } else {
  103. $image_video_url = $post['video_url'];
  104. $post['message'] = $post['video_message'];
  105. }
  106. if ($this->checkStoragePath($image_video_url) === false) {
  107. return '发送失败,请检查上传文件是否存在';
  108. }
  109. if ($post['send_type'] == 0) {
  110. $send_time = Carbon::now();
  111. } else {
  112. //转换时间格式
  113. $send_time = Carbon::createFromFormat('Y-m-d H:i:s', $post['send_time']);
  114. }
  115. //保存数据
  116. SmmPost::create($post,$send_time,$image_video_url);
  117. //最后一步
  118. $data = [
  119. 'title' => '操作成功',
  120. 'description' => '系统已生成发送队列,详情请查看日志。',
  121. ];
  122. return view('distributor.form_custom.completion-page', $data);
  123. }
  124. }
  125. /**
  126. * 上传图片到本地
  127. */
  128. public function upload() {
  129. // 获取上传的文件
  130. $file = $this->file();
  131. $dir = 'ssm/'.getDistributorId();
  132. $newName = md5(uniqid() . mt_rand()) .'.'.$file->getClientOriginalExtension();
  133. //保存到本地
  134. $disk = $this->disk('local');
  135. $result = $disk->putFileAs($dir, $file, $newName);
  136. //oss 保存
  137. $disk = $this->disk('oss');
  138. $result = $disk->putFileAs($dir, $file, $newName);
  139. return $result
  140. ? $this->responseUploaded($result, $disk->url($result))
  141. : $this->responseErrorMessage(admin_trans_label('upload_failed'));
  142. }
  143. /*
  144. * 判断路径是否正确
  145. */
  146. public function checkStoragePath ($filePath) {
  147. $storagePath = 'ssm/'.getDistributorId();
  148. if (strpos($filePath, $storagePath) === 0) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. private function stepLeaving($step,$index=0)
  154. {
  155. $lang = config('app.locale');//当前语言
  156. //JS 验证参数不能为空
  157. if ($index == 0) {
  158. $step->leaving(<<<JS
  159. function validateForm(formArray) {
  160. lang = '{$lang}';
  161. // 仅获取radio类型的post_type值
  162. let postType = formArray.find(item =>
  163. item.name === 'post_type' && item.type === 'radio'
  164. )?.value;
  165. // 仅获取radio类型的send_type值
  166. let sendType = formArray.find(item =>
  167. item.name === 'send_type' && item.type === 'radio'
  168. )?.value;
  169. // 验证post_type相关规则
  170. if (postType === '0') {
  171. const imageMessage = formArray.find(item => item.name === 'image_message')?.value;
  172. const imageUrl = formArray.find(item => item.name === 'image_url')?.value;
  173. if (!imageMessage || !imageUrl) {
  174. if (lang === 'en') {
  175. return 'Post message and images cannot be empty';
  176. } else {
  177. return '帖子留言和图片不能为空';
  178. }
  179. }
  180. } else if (postType === '1') {
  181. const videoMessage = formArray.find(item => item.name === 'video_message')?.value;
  182. const videoUrl = formArray.find(item => item.name === 'video_url')?.value;
  183. if (!videoMessage || !videoUrl) {
  184. if (lang === 'en') {
  185. return 'Post message and video cannot be empty';
  186. } else {
  187. return '帖子留言和视频不能为空';
  188. }
  189. }
  190. }
  191. // 验证send_type规则(仅处理radio类型的send_type)
  192. if (sendType === '1') {
  193. const sendTime = formArray.find(item => item.name === 'send_time')?.value;
  194. if (!sendTime) {
  195. if (lang === 'en') {
  196. return 'Send time cannot be empty';
  197. } else {
  198. return '定时发送时间不能为空';
  199. }
  200. }
  201. }
  202. return "";
  203. }
  204. var formArray = args.formArray;
  205. rs = validateForm(formArray);
  206. if (rs != "") {
  207. Dcat.error(rs);
  208. return false;
  209. }
  210. JS);
  211. } else {
  212. $step->leaving(<<<JS
  213. var formArray = args.formArray;
  214. var lang = '{$lang}';
  215. //找account_ids
  216. let accountIds = formArray.find(item => item.name === 'account_ids[]')?.value;
  217. if (!accountIds || accountIds.length === 0) {
  218. if (lang === 'en') {
  219. Dcat.error('Please select accounts');
  220. } else {
  221. Dcat.error('请选择社媒帐号');
  222. }
  223. return false;
  224. }
  225. JS);
  226. }
  227. }
  228. public function addJs()
  229. {
  230. Admin::script(
  231. <<<JS
  232. function checkYouTubeOption(select) {
  233. // 检查是否存在包含"YouTube"的option
  234. var hasYouTube = select.find('option:contains("YouTube")').length > 0;
  235. // 切换目标元素的显示状态
  236. $('#select_hide_youtube_category').toggle(hasYouTube);
  237. }
  238. var select = $('select[name="account_ids\\[\\]_helper2"]');
  239. var select_lenght = select.children().length;
  240. let intervalId = setInterval(() => {
  241. var select = $('select[name="account_ids\\[\\]_helper2"]');
  242. // 处理变化的逻辑
  243. if (select_lenght != select.children().length) {
  244. checkYouTubeOption(select);
  245. }
  246. }, 300);
  247. JS
  248. );
  249. }
  250. }