123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?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')->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)] = getenv('APP_URL').$pic;
- }
- }
- if (request("file")) {
- $file = request("file");
- $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('file')) 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;
- 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();
- $old_pics = [];
- for ($i = 1; $i <=9; $i++) {
- if (!empty($interaction['pic'.($i)])) {
- $old_pics[] = $interaction['pic'.($i)];
- }
- }
- $new_pics = (array) request('pic');
- $a = array_diff($new_pics['url'],$old_pics);
- $b = array_diff($old_pics,$new_pics['url']);
- if (!empty($a) || !empty($b)) { //有图片变化执行
- // 删除以前图片 重新插入
- if (!empty($b)) {
- foreach ($b as $old_pic){
- $key = array_search($old_pic,$interaction);
- $interaction[$key] = '';
- if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
- unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
- }
- BaseAttachmentModel::where('url',$old_pic)->delete();
- InteractionInfo::find(request('id'))->update($interaction);
- }
- }
- $a = array_values($a);
- for ($i = 1; $i <=9; $i++) {
- if (empty($interaction['pic'.($i)])) {
- $c = -1;
- $c++;
- $interaction['pic'.($i)] = getenv('APP_URL').$a[$c];
- }
- }
- }
- $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)
- {
- $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'));
- }
- }
|