SmmPostController.php 10 KB

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