WxPostPublishController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace App\Admin\Controllers\Posts;
  3. use App\Admin\Renderable\UsersRender;
  4. use App\Http\Controllers\Api\Repositories\PostsRepositores;
  5. use App\Jobs\CalculateFollowsNumJob;
  6. use App\Jobs\CalculateLikesNumJob;
  7. use App\Jobs\Posts\UpdatePostOnlyTextJob;
  8. use App\Models\Posts\WxPostsAddress;
  9. use App\Models\Posts\WxTag;
  10. use App\Models\Circle\WxCircle;
  11. use App\Models\Shop\WxShopGoods;
  12. use App\Models\User\WxUser;
  13. use App\Models\WxTenant;
  14. use App\Wen\Utils\CollecterUtils;
  15. use App\Wen\Utils\PostUtils;
  16. use App\Wen\Utils\Settings;
  17. use Dcat\Admin\Admin;
  18. use Dcat\Admin\Http\Auth\Permission;
  19. use Dcat\Admin\Http\Controllers\AdminController;
  20. use Dcat\Admin\FormStep\Form as StepForm;
  21. use Dcat\Admin\Form;
  22. use Dcat\Admin\Grid;
  23. use Dcat\Admin\Layout\Content;
  24. use Dcat\Admin\Show;
  25. use Dcat\Admin\Widgets\Alert;
  26. class WxPostPublishController extends AdminController
  27. {
  28. protected $title = '发文章';
  29. public function create(Content $content)
  30. {
  31. // 检查权限,有create-post权限的用户或者角色可以访问创建文章页面
  32. Permission::check('posts_publish');
  33. return parent::create($content);
  34. }
  35. /**
  36. * Make a grid builder.
  37. *
  38. * @return Grid
  39. */
  40. protected function grid()
  41. {
  42. return $this->form();
  43. }
  44. /**
  45. * 保存
  46. *
  47. * @return \Symfony\Component\HttpFoundation\Response
  48. */
  49. public function store()
  50. {
  51. return $this->form()->saving(function (Form $form) {
  52. global $__MINI_GLOBAL_TENANT_ID__;
  53. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  54. return $this->fail(416001);
  55. }
  56. $posts_title = trim((strip_tags(_empty_default_($form->posts_title, ''))));
  57. $posts_content = $form->posts_content;
  58. $user_id = (int)($form->user_id);
  59. $circle_id = $form->circle_id;
  60. $tenant_id = $form->tenant_id;
  61. if(_empty_($tenant_id)){
  62. $tenant_id = 0;
  63. }
  64. $tags = $form->tags;
  65. $img_urls = $form->img_urls;
  66. $video = $form->video;
  67. $goods = $form->goods;
  68. $address_id = $form->address_id;
  69. $data = ['posts_content'=>$posts_content, 'user_id'=>$user_id,
  70. 'circle_id'=>$circle_id, 'tags'=>$tags, 'img_urls'=>$img_urls,
  71. 'video'=>$video, 'goods'=>$goods, 'address_id'=>$address_id];
  72. $form->data__ = json_encode($data);
  73. $posts_content_raw = preg_replace("/<(img|video).*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", '',$posts_content);
  74. if( ( strlen($posts_content_raw) != strlen($posts_content) ) || mb_strlen($posts_content_raw) > 2000 ){
  75. if(!_empty_($video)){
  76. $posts_content .= '<video src="' . $video . '"></video>';
  77. }else if(!_empty_($img_urls)){
  78. $img_arr = explode(',', $img_urls);
  79. if($img_arr){
  80. foreach ($img_arr as $url){
  81. $posts_content .= '<img src="'.$url.'">';
  82. }
  83. }
  84. }
  85. }
  86. $posts_content = PostUtils::post_icon_inline_process($posts_content);
  87. // $posts_content = CollecterUtils::content_media_process( $posts_content, 1);
  88. $res = PostsRepositores::admin_add('', $user_id, $posts_title, $posts_content, $circle_id, $tags, $goods, $img_urls, $video, $address_id, '', '', $tenant_id);
  89. if ($res) {
  90. CalculateLikesNumJob::dispatch($res);
  91. CalculateFollowsNumJob::dispatch($user_id);
  92. UpdatePostOnlyTextJob::dispatch(0);
  93. // 清空缓存
  94. $form->multipleSteps()->flushStash();
  95. // 拦截保存操作
  96. return response(
  97. $form->multipleSteps()
  98. ->done()
  99. ->render()
  100. );
  101. } else {
  102. // 抛出错误
  103. return $this->fail(416001, $res);
  104. }
  105. })->store();
  106. }
  107. /**
  108. * Make a show builder.
  109. *
  110. * @param mixed $id
  111. *
  112. * @return Show
  113. */
  114. protected function detail($id)
  115. {
  116. }
  117. /**
  118. * Make a form builder.
  119. *
  120. * @return Form
  121. */
  122. protected function form()
  123. {
  124. return Form::make(null, function (Form $form) {
  125. $form->action('/posts/publish/step');
  126. $form->disableListButton();
  127. $form->multipleSteps()
  128. ->remember() // 记住表单步骤,默认不开启
  129. ->width('950px')
  130. ->add('基本信息', function (StepForm $step) {
  131. if(__system_is_model_enable('ycookies', 'caijiwebinfo')) {
  132. $step->caijiwebinfo('采集web信息')->titleClass('art_title') // 插入文章标题到对应的class
  133. ->contentClass('art_content'); // 插入文章内容到对应的class;
  134. }
  135. $step->html(function (){
  136. return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">攻略:如何控制最后发布的是笔记还是图文,关键点在于编辑器里面是否上传了图片,如果编辑器里面有图片,系统就会认定它为图文</div>';
  137. }, '');
  138. if(__system_is_model_enable('laradocs', 'dcat-neditor')){
  139. $info = '<i class="fa fa-exclamation-circle"></i> 此功能用于管理员新增内容。';
  140. $step->html(Alert::make($info)->info());
  141. $step->text('posts_title', '图文标题(选填)')->help('普通笔记不会显示标题')->setElementClass('art_title');
  142. $step->neditor('posts_content', '文章内容')->setElementClass('art_content');
  143. }else{
  144. $info = '<i class="fa fa-exclamation-circle"></i> 此功能用于管理员新增内容,目前使用的是默认编辑器<br/>1、如需上传图片需要在.env文件里配置cos<br/>
  145. 2、如含有表格,请在前端发布,否则表格显示会不友好<br/>3、若不喜欢用该编辑器,群主也适配了另外一个Neditor编辑器(可135编辑器配合),请在<a href="'.admin_url('extension').'" target="_blank">[扩展模块]</a>菜单里启用';
  146. $step->html(function () use ($info) {
  147. return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">'.$info.'</div>';
  148. }, '');
  149. $step->text('posts_title', '图文标题(选填)')->help('普通笔记不会显示标题')->setElementClass('art_title');
  150. $step->editor('posts_content', '文章内容')->required()->setElementClass('art_content');
  151. }
  152. // $step->multipleSelectTable('user_id', '用户')
  153. // ->title('用户')
  154. // ->max(2)
  155. // ->from(UsersRender::make())
  156. // ->model(WxUser::class, 'id', 'user_name')->saving(function ($v) {
  157. // if(is_array($v) && count($v) > 0){
  158. // return $v[0];
  159. // }else if(is_numeric($v)){
  160. // return $v;
  161. // }
  162. // return 0;
  163. // })->required();
  164. $step->select('user_id', '用户')->options(function ($id) {
  165. if($id){
  166. $user = \App\Models\User\WxUser::find($id);
  167. if ($user) {
  168. return [$user->id => $user->user_name];
  169. }
  170. }
  171. })->ajax('select/users')->required();
  172. $step->select('circle_id',env('circle_call', '圈子'))
  173. ->options(WxCircle::pluck('circle_name','id'));
  174. $step->select('tenant_id','分站')
  175. ->options(WxTenant::pluck('name','tenant_id'))->default(0)->required();
  176. })
  177. ->add('关联信息', function (StepForm $step) {
  178. // $step->multipleSelect('tags','话题')
  179. // ->options(WxTag::limit(10)->pluck('tags_name','id'));
  180. $step->multipleSelect('tags', '话题')->options(function ($id) {
  181. if(!is_array($id) && $id) {
  182. $tag = WxTag::find($id);
  183. if (!_empty_($tag)) {
  184. return [$tag->id => $tag->tags_name];
  185. }
  186. }else{
  187. return null;
  188. }
  189. })->ajax('select/tags');
  190. $step->multipleImage('img_urls', '图片')->uniqueName()->url('files/uploads')
  191. ->autoUpload()->sortable()->help('如果存在视频的情况下,则第一张图片为该视频的封面使用,其余图片丢弃');
  192. $step->file('video', '视频')->uniqueName()->url('files/uploads')->maxSize(999999999)
  193. ->autoUpload();
  194. $step->multipleSelect('goods', '商品')->options(function ($id) {
  195. if(!is_array($id) && $id) {
  196. $good = WxShopGoods::find($id);
  197. if (!_empty_($good)) {
  198. return [$good->id => $good->name];
  199. }
  200. }else{
  201. return null;
  202. }
  203. })->ajax('select/shop/goods');
  204. $step->select('address_id', '地址')->options(function ($id) {
  205. if($id){
  206. $address = WxPostsAddress::find($id);
  207. if ($address) {
  208. return [$address->id => $address->address_name];
  209. }
  210. }
  211. })->ajax('select/posts/address');
  212. })
  213. ->done(function () use ($form) {
  214. $data = [
  215. 'title' => '发表成功',
  216. 'description' => '点击返回可到达文章列表,也可以继续发表新文章',
  217. 'createUrl' => ''.admin_url('posts/publish'),
  218. 'backUrl' => ''.admin_url('posts/index'),
  219. ];
  220. return view('dcat-admin.form-step::completion-page', $data);
  221. });
  222. });
  223. }
  224. }