396 lines
14 KiB
PHP
396 lines
14 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;
|
|
use App\Http\Requests\EditGoodRequest;
|
|
|
|
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'];
|
|
}
|
|
//同时给定价格上下限
|
|
if(isset($input['start_price']) && isset($input['end_price'])){
|
|
//确保上限大于下限
|
|
if($input['start_price'] > $input['end_price']){
|
|
$temp = $input['start_price'];
|
|
$input['start_price'] = $input['end_price'];
|
|
$input['end_price'] = $temp;
|
|
}
|
|
$data['goods'] = $data['goods']->where([
|
|
['price', '>=', $input['start_price']],
|
|
['price', '<=', $input['end_price']]
|
|
]);
|
|
}
|
|
//只设上限或下限时
|
|
else{
|
|
if(isset($input['start_price']))
|
|
$data['goods'] = $data['goods']->where('price', '>=', $input['start_price']);
|
|
if(isset($input['end_price']))
|
|
$data['goods'] = $data['goods']->where('price', '<=', $input['end_price']);
|
|
}
|
|
//对数量筛选只设下限
|
|
if(isset($input['start_count']))
|
|
$data['goods'] = $data['goods']->where('count', '>=', $input['start_count']);
|
|
// 最后在排序筛选baned==0以及paginate分页
|
|
// p为价格,c为数量,d为倒序
|
|
if(isset($input['sort'])){
|
|
if ($input['sort'] == 'p'){
|
|
$data['goods'] = $data['goods']->orderby('price', 'asc')->where('baned', 0)->paginate(16);
|
|
}
|
|
elseif ($input['sort'] == 'pd') {
|
|
$data['goods'] = $data['goods']->orderby('price', 'desc')->where('baned', 0)->paginate(16);
|
|
}
|
|
elseif ($input['sort'] == 'c') {
|
|
$data['goods'] = $data['goods']->orderby('count', 'asc')->where('baned', 0)->paginate(16);
|
|
}
|
|
elseif ($input['sort'] == 'cd') {
|
|
$data['goods'] = $data['goods']->orderby('count', 'desc')->where('baned', 0)->paginate(16);
|
|
}
|
|
}
|
|
//未指定排序规则时按id正序排序
|
|
else
|
|
$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(Request $request)
|
|
{
|
|
$data = [];
|
|
$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)
|
|
{
|
|
$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();
|
|
$good = GoodInfo::find($good_id);
|
|
if($good == NULL) return View::make('common.errorPage')->withErrors('商品ID错误!');
|
|
$data['goods'] = [];
|
|
array_push($data['goods'], $good);
|
|
if($request->session()->get('user_id')!=$good->user_id && $request->session()->get('is_admin')!=2)
|
|
return Redirect::to('/good/'.$good_id);
|
|
/*$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(EditGoodRequest $request, $good_id)
|
|
{
|
|
$input = $request->all();
|
|
$good = GoodInfo::find($good_id);
|
|
if($good == NULL) return View::make('common.errorPage')->withErrors('商品ID错误!');
|
|
if($request->session()->get('user_id')!=$good->user_id && $request->session()->get('is_admin')!=2)
|
|
return Redirect::to('/good/'.$good_id);
|
|
$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();
|
|
}*/
|
|
if($request->hasFile('goodTitlePic'))
|
|
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@deleteGood
|
|
* @input Request $request, $good_id
|
|
* @return Redirect
|
|
* @description Delete a specify good.
|
|
*/
|
|
public function deleteGood(Request $request, $good_id)
|
|
{
|
|
$good = GoodInfo::find($good_id);
|
|
if($request->session()->get('user_id') != $good->user_id && $request->session()->get('is_admin')!=2)
|
|
return Redirect::to('/good/'.$good_id);
|
|
Storage::delete('good/titlepic/'.sha1($good->id));
|
|
$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);
|
|
}
|
|
|
|
public function addFavlist(Request $request, $good_id)
|
|
{
|
|
$fav = new FavList;
|
|
$fav->user_id = $request->session()->get('user_id');
|
|
$fav->good_id = $good_id;
|
|
$fav->save();
|
|
$good = GoodInfo::find($good_id);
|
|
$good->fav_num++;
|
|
$good->save();
|
|
return json_encode(['msg' => 'success']);
|
|
}
|
|
|
|
public function delFavlist(Request $request, $good_id)
|
|
{
|
|
$user_id = $request->session()->get('user_id');
|
|
FavList::where('user_id', $user_id)->where('good_id', $good_id)->delete();
|
|
$good = GoodInfo::find($good_id);
|
|
$good->fav_num--;
|
|
$good->save();
|
|
return json_encode(['msg' => 'success']);
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
}
|