SmmPostController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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);
  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. //dd($post);
  116. //保存数据
  117. SmmPost::create($post,$send_time,$image_video_url);
  118. //最后一步
  119. $data = [
  120. 'title' => '操作成功',
  121. 'description' => '系统已生成发送队列,详情请查看日志。',
  122. ];
  123. return view('distributor.form_custom.completion-page', $data);
  124. }
  125. }
  126. /**
  127. * 上传图片到本地
  128. */
  129. public function upload() {
  130. $disk = $this->disk('oss');
  131. // 获取上传的文件
  132. $file = $this->file();
  133. $dir = 'ssm/'.getDistributorId();
  134. $newName = md5(uniqid() . mt_rand()) .'.'.$file->getClientOriginalExtension();
  135. $result = $disk->putFileAs($dir, $file, $newName);
  136. return $result
  137. ? $this->responseUploaded($result, $disk->url($result))
  138. : $this->responseErrorMessage(admin_trans_label('upload_failed'));
  139. }
  140. /*
  141. * 判断路径是否正确
  142. */
  143. public function checkStoragePath ($filePath) {
  144. $storagePath = 'ssm/'.getDistributorId();
  145. if (strpos($filePath, $storagePath) === 0) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. private function stepLeaving($step,$index=0)
  151. {
  152. $lang = config('app.locale');//当前语言
  153. //JS 验证参数不能为空
  154. if ($index == 0) {
  155. $step->leaving(<<<JS
  156. function validateForm(formArray) {
  157. lang = '{$lang}';
  158. // 仅获取radio类型的post_type值
  159. let postType = formArray.find(item =>
  160. item.name === 'post_type' && item.type === 'radio'
  161. )?.value;
  162. // 仅获取radio类型的send_type值
  163. let sendType = formArray.find(item =>
  164. item.name === 'send_type' && item.type === 'radio'
  165. )?.value;
  166. // 验证post_type相关规则
  167. if (postType === '0') {
  168. const imageMessage = formArray.find(item => item.name === 'image_message')?.value;
  169. const imageUrl = formArray.find(item => item.name === 'image_url')?.value;
  170. if (!imageMessage || !imageUrl) {
  171. if (lang === 'en') {
  172. return 'Post message and images cannot be empty';
  173. } else {
  174. return '帖子留言和图片不能为空';
  175. }
  176. }
  177. } else if (postType === '1') {
  178. const videoMessage = formArray.find(item => item.name === 'video_message')?.value;
  179. const videoUrl = formArray.find(item => item.name === 'video_url')?.value;
  180. if (!videoMessage || !videoUrl) {
  181. if (lang === 'en') {
  182. return 'Post message and video cannot be empty';
  183. } else {
  184. return '帖子留言和视频不能为空';
  185. }
  186. }
  187. }
  188. // 验证send_type规则(仅处理radio类型的send_type)
  189. if (sendType === '1') {
  190. const sendTime = formArray.find(item => item.name === 'send_time')?.value;
  191. if (!sendTime) {
  192. if (lang === 'en') {
  193. return 'Send time cannot be empty';
  194. } else {
  195. return '定时发送时间不能为空';
  196. }
  197. }
  198. }
  199. return "";
  200. }
  201. var formArray = args.formArray;
  202. rs = validateForm(formArray);
  203. if (rs != "") {
  204. Dcat.error(rs);
  205. return false;
  206. }
  207. JS);
  208. } else {
  209. $step->leaving(<<<JS
  210. var formArray = args.formArray;
  211. var lang = '{$lang}';
  212. //找account_ids
  213. let accountIds = formArray.find(item => item.name === 'account_ids[]')?.value;
  214. if (!accountIds || accountIds.length === 0) {
  215. if (lang === 'en') {
  216. Dcat.error('Please select accounts');
  217. } else {
  218. Dcat.error('请选择社媒帐号');
  219. }
  220. return false;
  221. }
  222. JS);
  223. }
  224. }
  225. public function addJs()
  226. {
  227. Admin::script(
  228. <<<JS
  229. function checkYouTubeOption(select) {
  230. // 检查是否存在包含"YouTube"的option
  231. var hasYouTube = select.find('option:contains("YouTube")').length > 0;
  232. // 切换目标元素的显示状态
  233. $('#select_hide_youtube_category').toggle(hasYouTube);
  234. }
  235. var select = $('select[name="account_ids\\[\\]_helper2"]');
  236. var select_lenght = select.children().length;
  237. let intervalId = setInterval(() => {
  238. var select = $('select[name="account_ids\\[\\]_helper2"]');
  239. // 处理变化的逻辑
  240. if (select_lenght != select.children().length) {
  241. checkYouTubeOption(select);
  242. }
  243. }, 300);
  244. JS
  245. );
  246. }
  247. }