blessing-skin-server/app/Controllers/ClosetController.php

73 lines
1.8 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Controllers;
use App\Models\User;
use App\Models\Texture;
use App\Models\Closet;
use App\Models\ClosetModel;
use App\Exceptions\E;
use View;
use Option;
class ClosetController extends BaseController
{
private $closet;
2016-07-21 22:01:57 +08:00
public function __construct()
2016-07-21 22:01:57 +08:00
{
$this->closet = new Closet($_SESSION['uid']);
2016-07-21 22:01:57 +08:00
}
public function index()
{
$category = isset($_GET['category']) ? $_GET['category'] : "skin";
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$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)
->with('user', (new User($_SESSION['uid'])))
2016-07-21 22:01:57 +08:00
->render();
}
public function info()
{
View::json($this->closet->getItems());
}
public function add()
{
2016-08-06 19:38:37 +08:00
\Validate::checkPost(['tid', 'name']);
2016-07-21 22:01:57 +08:00
2016-07-22 10:45:36 +08:00
if ($this->closet->add($_POST['tid'], $_POST['name'])) {
2016-07-21 22:01:57 +08:00
$t = Texture::find($_POST['tid']);
$t->likes += 1;
$t->save();
2016-07-22 10:45:36 +08:00
View::json('材质 '.$_POST['name'].' 收藏成功~', 0);
2016-07-21 22:01:57 +08:00
}
}
public function remove()
{
if (!is_numeric(\Utils::getValue('tid', $_POST)))
throw new E('非法参数', 1);
2016-07-21 22:01:57 +08:00
if ($this->closet->remove($_POST['tid'])) {
$t = Texture::find($_POST['tid']);
$t->likes = $t->likes - 1;
$t->save();
View::json('材质已从衣柜中移除', 0);
}
}
}