381 lines
13 KiB
PHP
381 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\UserInfo;
|
|
use App\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\Response;
|
|
use App\GoodCat;
|
|
use App\GoodInfo;
|
|
use App\GoodTag;
|
|
use App\Transaction;
|
|
use App\TransactionLog;
|
|
use App\Messages;
|
|
use App\Tag;
|
|
use App\FavList;
|
|
use App\Http\Controllers\Controller;
|
|
use Storage;
|
|
use Image;
|
|
use App\Http\Requests\AddGoodRequest;
|
|
|
|
class GoodController extends Controller
|
|
{
|
|
/**
|
|
* @function GoodController@getList
|
|
* @input Request $request
|
|
* @return View
|
|
* @description Get the list of all goods.
|
|
*/
|
|
public function getList(Request $request)
|
|
{
|
|
$data = [];
|
|
$input = $request->all();
|
|
$query = "";
|
|
if(isset($input['query'])) $query = $input['query'];
|
|
$data['cats'] = GoodCat::orderby('cat_index', 'asc')->get();
|
|
$data['goods'] = GoodInfo::where('good_name', 'like', "%$query%");
|
|
$data['cat_id'] = 0;
|
|
if(isset($input['cat_id'])){
|
|
$data['goods'] = GoodInfo::where('cat_id', $input['cat_id']);
|
|
$data['cat_id'] = $input['cat_id'];
|
|
}
|
|
$data['goods'] = $data['goods']->orderby('id', 'asc')->where('baned', 0)->paginate(16);
|
|
if($request->session()->has('user_id'))
|
|
$data['user_id'] = $request->session()->get('user_id');
|
|
else
|
|
$data['user_id'] = NULL;
|
|
if($request->session()->has('is_admin'))
|
|
$data['is_admin'] = $request->session()->get('is_admin');
|
|
else
|
|
$data['is_admin'] = NULL;
|
|
$data['page'] = 1;
|
|
if(isset($input['page'])) $data['page'] = $input['page'];
|
|
return view::make('good.goodList')->with($data);
|
|
}
|
|
|
|
public function check(Request $request)
|
|
{
|
|
$data = [];
|
|
$user_id = $request->session()->get('user_id');
|
|
$data['goods'] = Transaction::where('seller_id',$user_id)->where('status',1)->get();
|
|
$data['sells'] = Transaction::where('seller_id',$user_id)->where('status',2)->get();
|
|
$data['mysells'] = Transaction::where('seller_id',$user_id)->where('status',4)->get();
|
|
$data['users'] = Transaction::where('buyer_id',$user_id)->where('status',3)->get();
|
|
$data['transactions'] = Transaction::where('buyer_id',$user_id)->where('status',2)->get();
|
|
return view::make('good.goodCheck')->with($data);
|
|
}
|
|
|
|
/**
|
|
* @function GoodController@getInfo
|
|
* @input Request $request, $good_id
|
|
* @return View
|
|
* @description Get the information of a specify goods.
|
|
*/
|
|
public function getInfo(Request $request, $good_id)
|
|
{
|
|
$data = [];
|
|
$data['good'] = GoodInfo::where('id', $good_id)->first();
|
|
if($data['good'] == NULL) return View::make('common.errorPage')->withErrors('商品ID错误!');
|
|
$data['user'] = UserInfo::where('id', $data['good']->user_id)->first();
|
|
$data['sell'] = Transaction::where('good_id',$good_id)->first();
|
|
if($request->session()->has('user_id'))
|
|
{
|
|
$data['user_id'] = $request->session()->get('user_id');
|
|
$data['inFvlst'] = FavList::where('user_id', $data['user_id'])->where('good_id', $good_id)->get();
|
|
}
|
|
else
|
|
$data['user_id'] = NULL;
|
|
if($request->session()->has('is_admin'))
|
|
$data['is_admin'] = $request->session()->get('is_admin');
|
|
else
|
|
$data['is_admin'] = NULL;
|
|
$good = GoodInfo::where('id', $good_id)->first();
|
|
return view::make('good.goodInfo')->with($data);
|
|
}
|
|
|
|
public function allow(Request $request, $trans_id)
|
|
{
|
|
$data = [];
|
|
$user_id = $request->session()->get('user_id');
|
|
$data['buyers'] = Transaction::where('id',$trans_id)->first();
|
|
$good = Transaction::find($trans_id);
|
|
if($good->status==1)
|
|
$good->status = 2;
|
|
else if($good->status==2&&$user_id==$good->seller_id)
|
|
$good->status = 3;
|
|
else if($good->status==2&&$user_id==$good->buyer_id)
|
|
$good->status = 4;
|
|
else if($good->status==4||$good->status==3)
|
|
{
|
|
$good->status=5;
|
|
$goodinfo = GoodInfo::find($good->good_id);
|
|
$goodinfo->count = $goodinfo->count-$good->number;
|
|
$goodinfo->save();
|
|
}
|
|
$good->save();
|
|
return Redirect::to('/good');
|
|
}
|
|
|
|
public function getGood(Request $request, $good_id)
|
|
{
|
|
$input = $request->all();
|
|
$data = GoodInfo::find($good_id);
|
|
if($input['count']<=0||$input['count']>$data->count)
|
|
return Redirect::to('/good/'.$good_id)->withErrors('购买数量不符合规范');
|
|
$buy = new Transaction;
|
|
$buy->good_id = $good_id;
|
|
$buy->buyer_id = $request->session()->get('user_id');
|
|
$buy->seller_id = $data->user_id;
|
|
$buy->status = 1;
|
|
$buy->number = $input['count'];
|
|
$buy->save();
|
|
$buyername = User::find($data->user_id);
|
|
$goodname = GoodInfo::where('id',$good_id)->first();
|
|
$message = new Messages;
|
|
$message->sender_id = 0;
|
|
$message->receiver_id = $data->user_id;
|
|
$message->content = '您好!名叫'.$buyername->username.'购买了你的'.$goodname->good_name.'请及时前往确认。';
|
|
$message->title = '您有一个新的交♂易';
|
|
$message->save();
|
|
return Redirect::to('/good');
|
|
}
|
|
|
|
public function myGood(Request $request)
|
|
{
|
|
$data = [];
|
|
$user_id = $request->session()->get('user_id');
|
|
$data['goods'] = GoodInfo::where('user_id',$user_id)->get();
|
|
return view::make('good.myGood')->with($data);
|
|
}
|
|
/**
|
|
* @function GoodController@addGood
|
|
* @input Request $request
|
|
* @return Redirect
|
|
* @description Add a new good.
|
|
*/
|
|
public function showAddGood(AddGoodRequest $request)
|
|
{
|
|
$data = [];
|
|
if(!$request->session()->has('user_id'))
|
|
return Redirect::back();
|
|
$data['cats'] = GoodCat::orderby('cat_index', 'asc')->get();
|
|
//$data['tags'] = Tag::orderby('id', 'asc')->get();
|
|
return view::make('good.addPage')->with($data);
|
|
}
|
|
|
|
public function addGood(AddGoodRequest $request)
|
|
{
|
|
if(!$request->session()->has('user_id'))
|
|
return Redirect::back();
|
|
$this->validate($request, [
|
|
'good_name' => 'required',
|
|
'description' => 'required',
|
|
'price' => 'required',
|
|
'count' => 'required',
|
|
//'good_tag' => 'required',
|
|
]);
|
|
$input = $request->all();
|
|
$good = new GoodInfo;
|
|
$good->good_name = $input['good_name'];
|
|
$good->cat_id = $input['cat_id'];
|
|
$good->description = $input['description'];
|
|
$good->price = $input['price'];
|
|
$good->type = $input['type'];
|
|
$good->count = $input['count'];
|
|
$good->user_id = $request->session()->get('user_id');
|
|
$good->baned = '0';
|
|
$good->save();
|
|
|
|
/*$good_tags = $input['good_tag'];
|
|
foreach($good_tags as $tag_id)
|
|
{
|
|
$tag = new GoodTag;
|
|
if(!$tag_id)
|
|
{
|
|
$otherTag = new Tag;
|
|
$otherTag->tag_name = $input['other_tag'];
|
|
$otherTag->save();
|
|
$tag->tag_id = $otherTag->id;
|
|
}
|
|
else if($tag_id)
|
|
{
|
|
$tag->tag_id = $tag_id;
|
|
}
|
|
$tag->good_id = $good->id;
|
|
$tag->save();
|
|
}*/
|
|
|
|
Storage::put(
|
|
'good/titlepic/'.sha1($good->id),
|
|
Image::make($request->file('goodTitlePic'))->crop(round($input['crop_width']),round($input['crop_height']),round($input['crop_x']),round($input['crop_y']))->resize(800, 450)->encode('data-url')
|
|
);
|
|
return Redirect::to('/good/'.$good->id);
|
|
}
|
|
|
|
/**
|
|
* @function GoodController@editGood
|
|
* @input Request $request, $good_id
|
|
* @return Redirect
|
|
* @description Edit a specify good.
|
|
*/
|
|
public function showEditGood(Request $request, $good_id)
|
|
{
|
|
$data = [];
|
|
$data['cats'] = GoodCat::orderby('cat_index', 'asc')->get();
|
|
$data['goods'] = GoodInfo::where('id', $good_id)->get();
|
|
/*$data['tags'] = Tag::orderby('id', 'asc')->get();
|
|
$collection = GoodTag::where('good_id', $good_id)->pluck('tag_id');
|
|
$data['this_good_tags'] = $collection->toArray();*/
|
|
return view::make('good.editPage')->with($data);
|
|
}
|
|
|
|
public function editGood(Request $request, $good_id)
|
|
{
|
|
if(!$request->session()->has('user_id'))
|
|
return Redirect::back();
|
|
$this->validate($request, [
|
|
'good_name' => 'required',
|
|
'cat_id' => 'required',
|
|
'description' => 'required',
|
|
'price' => 'required',
|
|
'type' => 'required',
|
|
'count' => 'required',
|
|
//'good_tag' => 'required',
|
|
]);
|
|
$input = $request->all();
|
|
$good = GoodInfo::find($good_id);
|
|
if($request->session()->get('user_id')!=$good->user_id && !$request->session()->has('is_admin'))
|
|
return Redirect::back();
|
|
$good->good_name=$input['good_name'];
|
|
$good->cat_id=$input['cat_id'];
|
|
$good->description=$input['description'];
|
|
$good->price=$input['price'];
|
|
$good->type=$input['type'];
|
|
$good->count=$input['count'];
|
|
$good->update();
|
|
|
|
$deleteoldtags = GoodTag::where('good_id', $good_id)->delete();
|
|
|
|
/*$good_tags = $input['good_tag'];
|
|
foreach($good_tags as $tag_id)
|
|
{
|
|
$tag = new GoodTag;
|
|
if(!$tag_id)
|
|
{
|
|
$otherTag = new Tag;
|
|
$otherTag->tag_name = $input['other_tag'];
|
|
$otherTag->save();
|
|
$tag->tag_id = $otherTag->id;
|
|
}
|
|
else if($tag_id)
|
|
{
|
|
$tag->tag_id = $tag_id;
|
|
}
|
|
$tag->good_id = $good->id;
|
|
$tag->save();
|
|
}*/
|
|
|
|
return Redirect::to('/good/'.$good_id);
|
|
}
|
|
|
|
/**
|
|
* @function GoodController@deleteGood
|
|
* @input Request $request, $good_id
|
|
* @return Redirect
|
|
* @description Delete a specify good.
|
|
*/
|
|
public function deleteGood(Request $request, $good_id)
|
|
{
|
|
if(!$request->session()->has('user_id'))
|
|
return Redirect::back();
|
|
$good = GoodInfo::find($good_id);
|
|
if($request->session()->get('user_id') != $good->user_id)
|
|
return Redirect::back();
|
|
$good->delete();
|
|
//$deleteGoodTag = GoodTag::where('good_id', $good_id)->delete();
|
|
$deleteFavList = Favlist::where('good_id', $good_id)->delete();
|
|
return Redirect::to('/good');
|
|
}
|
|
|
|
/*
|
|
* @function quickAccess
|
|
* @input $request (use query)
|
|
*
|
|
* @return Redirect or View
|
|
* @description Process the query and redirect to
|
|
* certain good or list
|
|
*/
|
|
public function quickAccess(Request $request)
|
|
{
|
|
$data = [];
|
|
$input = $request->all();
|
|
$query = $input['query'];
|
|
$data['cats'] = GoodCat::orderby('cat_index', 'asc')->get();
|
|
$data['goods'] = GoodInfo::where('good_name', 'like', "%$query%")->get();
|
|
if($request->session()->has('user_id'))
|
|
$data['user_id'] = $request->session()->get('user_id');
|
|
else
|
|
$data['user_id'] = NULL;
|
|
if($request->session()->has('is_admin'))
|
|
$data['is_admin'] = $request->session()->get('is_admin');
|
|
else
|
|
$data['is_admin'] = NULL;
|
|
return view::make('good.goodList')->with($data);
|
|
}
|
|
|
|
/*
|
|
* @funtion about Favlist
|
|
* @input $request (use query) $good_id
|
|
=======
|
|
* @funtion addFavlist
|
|
* @input $request (use query)
|
|
>>>>>>> Stashed changes
|
|
*
|
|
* @return Redirect or View
|
|
* @description Process the query and add good to certain user's
|
|
* favorite list or delete good from certain user's
|
|
* favorite list
|
|
*/
|
|
|
|
public function addFavlist(Request $request, $good_id)
|
|
{
|
|
if(!$request->session()->has('user_id'))
|
|
return Redirect::back();
|
|
$fav = new FavList;
|
|
$fav->user_id = $request->session()->get('user_id');
|
|
$fav->good_id = $good_id;
|
|
$fav->save();
|
|
return Redirect::to('/good/'.$good_id);
|
|
}
|
|
|
|
public function delFavlist(Request $request, $good_id)
|
|
{
|
|
if(!$request->session()->has('user_id'))
|
|
return Redirect::back();
|
|
$user_id = $request->session()->get('user_id');
|
|
$items = FavList::where('user_id', $user_id)->where('good_id', $good_id)->get();
|
|
foreach($items as $item) $del_id = FavList::find($item->id);
|
|
$del_id->delete();
|
|
return Redirect::to('/good/'.$good_id);
|
|
}
|
|
|
|
public function getSimpleTitlePic(Request $request, $good_id)
|
|
{
|
|
$file = Storage::get('good/titlepic/'.$good_id);
|
|
$image = Image::make($file)->resize(800, 450);
|
|
return $image->response('jpg');
|
|
}
|
|
|
|
public function getTitlePic(Request $request, $good_id, $width, $height)
|
|
{
|
|
$file = Storage::get('good/titlepic/'.$good_id);
|
|
$image = Image::make($file)->resize($width, $height);
|
|
return $image->response('jpg');
|
|
}
|
|
|
|
}
|