InfoController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Http\Controllers\Admin\Interaction;
  3. use App\Models\BaseAttachmentModel;
  4. use App\Models\CommentInfoModel;
  5. use App\Models\DreamInfoModel;
  6. use App\Models\InteractionInfo;
  7. use App\Models\UserInfoModel;
  8. use App\Widget\Tools\VideoUpload;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Admin\Controller;
  11. class InfoController extends Controller
  12. {
  13. public function view(Request $request)
  14. {
  15. $dream_id = $request->id;
  16. $dream = DreamInfoModel::where('id',$request->id)->with(['interactions'=> function ($query) {
  17. $query->orderBy('id','desc');
  18. }])->first();
  19. $list = $dream->interactions;
  20. foreach ($list as $item){
  21. $comments = CommentInfoModel::where('interaction_id',$item->id)->orderBy('created_at')->get();
  22. $item->comms = $comments;
  23. }
  24. return view('admin.dream.interaction.view',compact('list','dream_id'));
  25. }
  26. public function update(Request $request,$id)
  27. {
  28. $data = (array) request('data');
  29. $ok =CommentInfoModel::where('id',$id)->update($data);
  30. if($ok) {
  31. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  32. return $this->showMessage('操作成功',urldecode(request('_referer')));
  33. }else{
  34. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  35. return $this->showWarning('操作失败',$url);
  36. }
  37. }
  38. // 新增动态
  39. public function create(Request $reqeust)
  40. {
  41. if($reqeust->method() == 'POST') {
  42. return $this->_createSave();
  43. }
  44. $dream_id = $reqeust->dream_id;
  45. return view('admin.interaction.info.edit',compact('dream_id'));
  46. }
  47. /**
  48. * 保存修改
  49. */
  50. private function _createSave(){
  51. $data = (array) request('data');
  52. $pics = (array) request('pic');
  53. if($pics){
  54. foreach ($pics['url'] as $key => $pic) {
  55. $data['pic'.($key+1)] = getenv('APP_URL').$pic;
  56. }
  57. }
  58. if (request("file")) {
  59. $file = request("file");
  60. $fileSize = $file->getSize();
  61. $size = 200 * 1024 * 1024;
  62. if ($fileSize > $size) {
  63. return back()->with('error','请上传小于200MB的文件!');
  64. }
  65. $mimeType = [
  66. 'video/mp4',
  67. ];
  68. $fileMimeType = $file->getMimeType();
  69. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  70. return back()->with('error','File type allow MP4!');
  71. }
  72. if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
  73. $data["video"] = $file;
  74. }
  75. $id = InteractionInfo::create($data);
  76. if($id) {
  77. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  78. $this->showMessage('添加成功',$url);
  79. }else{
  80. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  81. return $this->showWarning('添加失败',$url);
  82. }
  83. }
  84. // 修改动态
  85. public function updateInteraction(Request $reqeust) {
  86. if($reqeust->method() == 'POST') {
  87. return $this->_updateSave();
  88. }
  89. $data = InteractionInfo::find($reqeust->get('id'));
  90. $arr = [];
  91. for ($i = 1; $i <=9; $i++) {
  92. if (!empty($data['pic'.($i)])) {
  93. $arr[] = $data['pic'.($i)];
  94. }
  95. }
  96. $data->imgs = $arr;
  97. if (empty($data->video)) {
  98. $data->is_video = 0;
  99. }else{
  100. $data->is_video = 1;
  101. }
  102. return view('admin.interaction.info.edit',compact('data'));
  103. }
  104. /**
  105. * 保存修改
  106. */
  107. private function _updateSave() {
  108. $data = (array) request('data');
  109. $interaction_id = request('id');
  110. $interaction = InteractionInfo::find($interaction_id)->toArray();
  111. $old_pics = [];
  112. for ($i = 1; $i <=9; $i++) {
  113. if (!empty($interaction['pic'.($i)])) {
  114. $old_pics[] = $interaction['pic'.($i)];
  115. }
  116. }
  117. $new_pics = (array) request('pic');
  118. $a = array_diff($new_pics['url'],$old_pics);
  119. $b = array_diff($old_pics,$new_pics['url']);
  120. if (!empty($a) || !empty($b)) { //有图片变化执行
  121. // 删除以前图片 重新插入
  122. if (!empty($b)) {
  123. foreach ($b as $old_pic){
  124. $key = array_search($old_pic,$interaction);
  125. $interaction[$key] = '';
  126. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  127. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  128. }
  129. BaseAttachmentModel::where('url',$old_pic)->delete();
  130. InteractionInfo::find(request('id'))->update($interaction);
  131. }
  132. }
  133. $a = array_values($a);
  134. $c = -1;
  135. for ($i = 1; $i <=9; $i++) {
  136. if (empty($interaction['pic'.($i)])) {
  137. $c++;
  138. if (array_key_exists($c,$a)) {
  139. $interaction['pic'.($i)] = getenv('APP_URL').$a[$c];
  140. }
  141. }
  142. }
  143. }
  144. if (!empty(request("file"))) {
  145. $file = request("file");
  146. $fileSize = $file->getSize();
  147. $size = 200 * 1024 * 1024;
  148. if ($fileSize > $size) {
  149. return back()->with('error','请上传小于200 MB的文件!');
  150. }
  151. $mimeType = [
  152. 'video/mp4',
  153. ];
  154. $fileMimeType = $file->getMimeType();
  155. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  156. return back()->with('error','File type allow MP4!');
  157. }
  158. if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
  159. $interaction["video"] = $file;
  160. }else{
  161. // 更新是视频不存在则删除
  162. $info = InteractionInfo::find(request('id'));
  163. if (is_file('.'.str_replace(getenv('APP_URL'),'',$info->video))) {
  164. unlink('.'.str_replace(getenv('APP_URL'),'',$info->video));
  165. }
  166. // $info->update(['video'=>'']);
  167. $info->video = null;
  168. $info->save();
  169. // dd($info);
  170. }
  171. $ok = InteractionInfo::find(request('id'))->update($interaction);
  172. if($ok) {
  173. $url[] = array('url'=>U( 'Interaction/Info/index'),'title'=>'返回列表');
  174. return $this->showMessage('操作成功',urldecode(request('_referer')));
  175. }else{
  176. $url[] = array('url'=>U( 'Interaction/Info/index'),'title'=>'返回列表');
  177. return $this->showWarning('操作失败',$url);
  178. }
  179. }
  180. /**
  181. * 删除
  182. */
  183. public function destroy(Request $reqeust)
  184. {
  185. // 删除动态的图片 视频 评论...
  186. $data = InteractionInfo::find($reqeust->get('id'));
  187. $arr = [];
  188. for ($i = 1; $i <=9; $i++) {
  189. if (!empty($data['pic'.($i)])) {
  190. $arr[] = $data['pic'.($i)];
  191. }
  192. }
  193. // 删除图片
  194. if (!empty($arr)) {
  195. foreach ($arr as $old_pic){
  196. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  197. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  198. }
  199. BaseAttachmentModel::where('url',$old_pic)->delete();
  200. }
  201. }
  202. // 删除视频
  203. if (!empty($data->video)) {
  204. if (is_file('.'.str_replace(getenv('APP_URL'),'',$data->video))) {
  205. unlink('.'.str_replace(getenv('APP_URL'),'',$data->video));
  206. }
  207. }
  208. // 删除评论
  209. CommentInfoModel::where('interaction_id',$reqeust->get('id'))->delete();
  210. $ok = InteractionInfo::destroy($reqeust->get('id'));
  211. if ($ok) {
  212. return $this->showMessage('操作成功');
  213. } else {
  214. return $this->showWarning("操作失败");
  215. }
  216. }
  217. public function comment(Request $request)
  218. {
  219. $interaction_id = $request->input('id');
  220. $comment_id = $request->input('comment_id');
  221. if ($request->isMethod('POST')) {
  222. $comment = $request->input('comment');
  223. $interaction = InteractionInfo::find($interaction_id);
  224. $user_id = empty($interaction->dream) ? 0 : $interaction->dream->user_id;
  225. $user = UserInfoModel::find($user_id);
  226. if(!empty($comment_id)){ //回复
  227. $comment_info = CommentInfoModel::find($comment_id);
  228. $interaction = InteractionInfo::find($comment_info->interaction_id);
  229. $user_id = empty($interaction->dream) ? 0 : $interaction->dream->user_id;
  230. $user = UserInfoModel::find($user_id);
  231. $arr = [
  232. 'interaction_id'=>$comment_info->interaction_id,
  233. 'user_id'=>$comment_info->user_id,
  234. 'user_avatar'=>$comment_info->user_avatar,
  235. 'user_nickname'=>$comment_info->user_nickname,
  236. 'to_user_id'=>$user_id,
  237. 'content'=>$comment,
  238. 'to_user_avatar'=>empty($user) ? '' : $user->avatar ,
  239. 'to_user_nickname'=>empty($user) ? '' : $user->nickname ,
  240. ];
  241. $ok = CommentInfoModel::create($arr);
  242. }else{
  243. $arr = [
  244. 'interaction_id'=>$interaction_id,
  245. 'user_id'=>$user_id,
  246. 'content'=>$comment,
  247. 'user_avatar'=>empty($user) ? '' : $user->avatar ,
  248. 'user_nickname'=>empty($user) ? '' : $user->nickname ,
  249. ];
  250. $ok = CommentInfoModel::create($arr);
  251. }
  252. if ($ok) {
  253. return $this->showMessage('操作成功');
  254. } else {
  255. return $this->showWarning("操作失败");
  256. }
  257. }
  258. return view('admin.comment.edit',compact('interaction_id'));
  259. }
  260. }