123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace App\Http\Controllers\Admin\Interaction;
- use App\Models\BaseAttachmentModel;
- use App\Models\CommentInfoModel;
- use App\Models\DreamInfoModel;
- use App\Models\InteractionInfo;
- use App\Models\UserInfoModel;
- use App\Widget\Tools\VideoUpload;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Admin\Controller;
- class InfoController extends Controller
- {
- public function view(Request $request)
- {
- $dream_id = $request->id;
- $dream = DreamInfoModel::where('id',$request->id)->with(['interactions'=> function ($query) {
- $query->orderBy('id','desc');
- }])->first();
- $list = $dream->interactions;
- foreach ($list as $item){
- $comments = CommentInfoModel::where('interaction_id',$item->id)->orderBy('created_at')->get();
- $item->comms = $comments;
- }
- return view('admin.dream.interaction.view',compact('list','dream_id'));
- }
- public function update(Request $request,$id)
- {
- $data = (array) request('data');
- $ok =CommentInfoModel::where('id',$id)->update($data);
- if($ok) {
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- return $this->showMessage('操作成功',urldecode(request('_referer')));
- }else{
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- return $this->showWarning('操作失败',$url);
- }
- }
- // 新增动态
- public function create(Request $reqeust)
- {
- if($reqeust->method() == 'POST') {
- return $this->_createSave();
- }
- $dream_id = $reqeust->dream_id;
- return view('admin.interaction.info.edit',compact('dream_id'));
- }
- /**
- * 保存修改
- */
- private function _createSave(){
- $data = (array) request('data');
- $pics = (array) request('pic');
- if($pics){
- foreach ($pics['url'] as $key => $pic) {
- $data['pic'.($key+1)] = $pic;
- }
- }
- if (request("video")) {
- $file = request("video");
- $fileSize = $file->getSize();
- $size = 200 * 1024 * 1024;
- if ($fileSize > $size) {
- return back()->with('error','请上传小于200MB的文件!');
- }
- $mimeType = [
- 'video/mp4',
- ];
- $fileMimeType = $file->getMimeType();
- if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
- return back()->with('error','File type allow MP4!');
- }
- if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
- $data["video"] = $file;
- }
- $id = InteractionInfo::create($data);
- if($id) {
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- $this->showMessage('添加成功',$url);
- }else{
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- return $this->showWarning('添加失败',$url);
- }
- }
- // 修改动态
- public function updateInteraction(Request $reqeust) {
- if($reqeust->method() == 'POST') {
- return $this->_updateSave();
- }
- $data = InteractionInfo::find($reqeust->get('id'));
- $arr = [];
- for ($i = 1; $i <=9; $i++) {
- if (!empty($data['pic'.($i)])) {
- $arr[] = $data['pic'.($i)];
- }
- }
- $data->imgs = $arr;
- if (empty($data->video)) {
- $data->is_video = 0;
- }else{
- $data->is_video = 1;
- }
- return view('admin.interaction.info.edit',compact('data'));
- }
- /**
- * 保存修改
- */
- private function _updateSave() {
- $data = (array) request('data');
- $interaction_id = request('id');
- $interaction = InteractionInfo::find($interaction_id)->toArray();
- $interaction['title'] = $data['title'];
- $old_pics = [];
- for ($i = 1; $i <=9; $i++) {
- if (!empty($interaction['pic'.($i)])) {
- $old_pics[$i] = $interaction['pic'.($i)];
- }
- }
- $new_pics = (array) request('pic');
- if (!empty($new_pics)) {
- // 图片不为空
- //操作成功,删除原来的图片
- foreach ($new_pics['url'] as $key => $pic) {
- $interaction['pic'.($key+1)] = $pic;
- }
- foreach ($old_pics as $k => $pic) {
- if (!in_array($pic, $new_pics['url'])) {
- $md5 = $this->getarea($pic);
- $attache = new \App\Services\Base\Attachment();
- $attache->deleteAttachment($md5);
- $interaction['pic'.($k+1)] = '';
- }
- }
- }
- if (!empty(request("video"))) {
- $file = request("video");
- $fileSize = $file->getSize();
- $size = 200 * 1024 * 1024;
- if ($fileSize > $size) {
- return back()->with('error','请上传小于200 MB的文件!');
- }
- $mimeType = [
- 'video/mp4',
- ];
- $fileMimeType = $file->getMimeType();
- if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
- return back()->with('error','File type allow MP4!');
- }
- if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
- $interaction["video"] = $file;
- }
- $ok = InteractionInfo::find(request('id'))->update($interaction);
- if($ok) {
- $url[] = array('url'=>U( 'Interaction/Info/index'),'title'=>'返回列表');
- return $this->showMessage('操作成功',urldecode(request('_referer')));
- }else{
- $url[] = array('url'=>U( 'Interaction/Info/index'),'title'=>'返回列表');
- return $this->showWarning('操作失败',$url);
- }
- }
- /**
- * 删除
- */
- public function destroy(Request $reqeust)
- {
- // 删除动态的图片 视频 评论...
- $data = InteractionInfo::find($reqeust->get('id'));
- $arr = [];
- for ($i = 1; $i <=9; $i++) {
- if (!empty($data['pic'.($i)])) {
- $arr[] = $data['pic'.($i)];
- }
- }
- // 删除图片
- if (!empty($arr)) {
- foreach ($arr as $old_pic){
- if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
- unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
- }
- BaseAttachmentModel::where('url',$old_pic)->delete();
- }
- }
- // 删除视频
- if (!empty($data->video)) {
- if (is_file('.'.str_replace(getenv('APP_URL'),'',$data->video))) {
- unlink('.'.str_replace(getenv('APP_URL'),'',$data->video));
- }
- }
- // 删除评论
- CommentInfoModel::where('interaction_id',$reqeust->get('id'))->delete();
- $ok = InteractionInfo::destroy($reqeust->get('id'));
- if ($ok) {
- return $this->showMessage('操作成功');
- } else {
- return $this->showWarning("操作失败");
- }
- }
- public function comment(Request $request)
- {
- $interaction_id = $request->input('id');
- $comment_id = $request->input('comment_id');
- if ($request->isMethod('POST')) {
- $comment = $request->input('comment');
- $interaction = InteractionInfo::find($interaction_id);
- $user_id = empty($interaction->dream) ? 0 : $interaction->dream->user_id;
- $user = UserInfoModel::find($user_id);
- if(!empty($comment_id)){ //回复
- $comment_info = CommentInfoModel::find($comment_id);
- $interaction = InteractionInfo::find($comment_info->interaction_id);
- $user_id = empty($interaction->dream) ? 0 : $interaction->dream->user_id;
- $user = UserInfoModel::find($user_id);
- $arr = [
- 'interaction_id'=>$comment_info->interaction_id,
- 'user_id'=>$comment_info->user_id,
- 'user_avatar'=>$comment_info->user_avatar,
- 'user_nickname'=>$comment_info->user_nickname,
- 'to_user_id'=>$user_id,
- 'content'=>$comment,
- 'to_user_avatar'=>empty($user) ? '' : $user->avatar ,
- 'to_user_nickname'=>empty($user) ? '' : $user->nickname ,
- ];
- $ok = CommentInfoModel::create($arr);
- }else{
- $arr = [
- 'interaction_id'=>$interaction_id,
- 'user_id'=>$user_id,
- 'content'=>$comment,
- 'user_avatar'=>empty($user) ? '' : $user->avatar ,
- 'user_nickname'=>empty($user) ? '' : $user->nickname ,
- ];
- $ok = CommentInfoModel::create($arr);
- }
- if ($ok) {
- return $this->showMessage('操作成功');
- } else {
- return $this->showWarning("操作失败");
- }
- }
- return view('admin.comment.edit',compact('interaction_id'));
- }
- // 获取视频图片后缀码
- public function getarea($str)
- {
- $start = strripos($str, '/');
- $first = substr($str, $start + 1);
- $end = strripos($first, '.');
- $last = substr($first, 0, $end);
- return $last;
- }
- }
|