blessing-skin-server/app/Http/Controllers/UserController.php

158 lines
4.2 KiB
PHP
Raw Normal View History

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
use View;
use Utils;
2016-07-21 22:01:57 +08:00
use App\Models\User;
use App\Models\Texture;
use Illuminate\Http\Request;
use App\Exceptions\PrettyPageException;
2016-07-21 22:01:57 +08:00
class UserController extends Controller
2016-07-21 22:01:57 +08:00
{
private $action = "";
2016-08-06 19:38:37 +08:00
private $user = null;
2016-07-21 22:01:57 +08:00
public function __construct()
2016-07-21 22:01:57 +08:00
{
$this->action = isset($_GET['action']) ? $_GET['action'] : "";
2016-08-28 10:05:21 +08:00
$this->user = new User(session('uid'));
2016-07-21 22:01:57 +08:00
}
public function index()
{
return view('user.index')->with('user', $this->user);
2016-07-21 22:01:57 +08:00
}
/**
* Handle User Signing
*
* @return void
*/
2016-07-21 22:01:57 +08:00
public function sign()
{
if ($aquired_score = $this->user->sign()) {
View::json([
'errno' => 0,
'msg' => "签到成功,获得了 $aquired_score 积分~",
'score' => $this->user->getScore(),
'remaining_time' => $this->user->canSign(true)
2016-07-21 22:01:57 +08:00
]);
} else {
View::json($this->user->canSign(true).' 小时后才能再次签到哦~', 1);
}
}
public function profile()
{
return view('user.profile')->with('user', $this->user);
2016-07-21 22:01:57 +08:00
}
/**
* Handle Changing Profile
*
* @param Request $request
* @return void
*/
public function handleProfile(Request $request)
2016-07-21 22:01:57 +08:00
{
switch ($this->action) {
case 'nickname':
$this->validate($request, [
'new_nickname' => 'required|nickname|max:255'
]);
$nickname = $request->input('new_nickname');
2016-07-21 22:01:57 +08:00
if ($this->user->setNickName($nickname))
View::json("昵称已成功设置为 $nickname", 0);
2016-07-21 22:01:57 +08:00
break;
case 'password':
$this->validate($request, [
'current_password' => 'required|min:8|max:16',
'new_password' => 'required|min:8|max:16'
]);
if (!$this->user->checkPasswd($request->input('current_password')))
View::json('原密码错误', 1);
if ($this->user->changePasswd($request->input('new_password')))
2016-07-21 22:01:57 +08:00
View::json('密码修改成功,请重新登录', 0);
break;
2016-07-21 22:01:57 +08:00
case 'email':
$this->validate($request, [
'new_email' => 'required|email',
'password' => 'required|min:8|max:16'
]);
2016-07-21 22:01:57 +08:00
if (!$this->user->checkPasswd($request->input('password')))
View::json('密码错误', 1);
2016-07-21 22:01:57 +08:00
if ($this->user->setEmail($request->input('new_email')))
View::json('邮箱修改成功,请重新登录', 0);
2016-07-21 22:01:57 +08:00
break;
2016-07-21 22:01:57 +08:00
case 'delete':
$this->validate($request, [
'password' => 'required|min:8|max:16'
]);
2016-07-21 22:01:57 +08:00
if (!$this->user->checkPasswd($request->input('password')))
View::json('密码错误', 1);
if ($this->user->delete()) {
setcookie('uid', '', time() - 3600, '/');
setcookie('token', '', time() - 3600, '/');
Session::flush();
View::json('账号已被成功删除', 0);
}
break;
default:
View::json('非法参数', 1);
break;
2016-07-21 22:01:57 +08:00
}
}
public function config()
{
return view('user.config')->with('user', $this->user);
2016-07-21 22:01:57 +08:00
}
/**
* Set Avatar for User
*
* @param Request $request
*/
public function setAvatar(Request $request)
2016-07-21 22:01:57 +08:00
{
$this->validate($request, [
'tid' => 'required|integer'
]);
$result = Texture::find($request->input('tid'));
2016-07-21 22:01:57 +08:00
if ($result) {
if ($result->type == "cape")
View::json('披风可不能设置为头像哦~', 1);
2016-07-21 22:01:57 +08:00
if ($this->user->setAvatar($request->input('tid'))) {
2016-07-21 22:01:57 +08:00
View::json('设置成功!', 0);
}
} else {
View::json('材质不存在。', 1);
2016-07-21 22:01:57 +08:00
}
}
}