2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
namespace App\Http\Controllers;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
use View;
|
|
|
|
use Option;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Closet;
|
2016-09-04 15:35:12 +08:00
|
|
|
use App\Models\Texture;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\ClosetModel;
|
2016-09-04 15:35:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2016-08-29 19:47:30 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
class ClosetController extends Controller
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
|
|
|
* Instance of Closet.
|
|
|
|
*
|
|
|
|
* @var \App\Models\Closet
|
|
|
|
*/
|
2016-08-16 22:52:00 +08:00
|
|
|
private $closet;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
public function __construct()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-08-28 10:05:21 +08:00
|
|
|
$this->closet = new Closet(session('uid'));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function index(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$category = $request->input('category', 'skin');
|
|
|
|
$page = $request->input('page', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
$items = array_slice($this->closet->getItems($category), ($page-1)*6, 6);
|
|
|
|
|
|
|
|
$total_pages = ceil(count($this->closet->getItems($category)) / 6);
|
|
|
|
|
|
|
|
echo View::make('user.closet')->with('items', $items)
|
|
|
|
->with('page', $page)
|
|
|
|
->with('category', $category)
|
|
|
|
->with('total_pages', $total_pages)
|
2016-08-28 10:05:21 +08:00
|
|
|
->with('user', (new User(session('uid'))))
|
2016-07-21 22:01:57 +08:00
|
|
|
->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function info()
|
|
|
|
{
|
|
|
|
View::json($this->closet->getItems());
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function add(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer',
|
|
|
|
'name' => 'required|nickname',
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if ($this->closet->add($request->tid, $request->name)) {
|
|
|
|
$t = Texture::find($request->tid);
|
2016-07-21 22:01:57 +08:00
|
|
|
$t->likes += 1;
|
|
|
|
$t->save();
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
View::json('材质 '.$request->input('name').' 收藏成功~', 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function remove(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer'
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if ($this->closet->remove($request->tid)) {
|
|
|
|
$t = Texture::find($request->tid);
|
2016-07-21 22:01:57 +08:00
|
|
|
$t->likes = $t->likes - 1;
|
|
|
|
$t->save();
|
|
|
|
|
|
|
|
View::json('材质已从衣柜中移除', 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|