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

158 lines
4.4 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
}
/**
2016-09-24 22:49:20 +08:00
* Handle User Checking In
*
* @return void
*/
2016-09-24 22:49:20 +08:00
public function checkIn()
2016-07-21 22:01:57 +08:00
{
2016-09-24 22:49:20 +08:00
if ($aquired_score = $this->user->checkIn()) {
2016-09-10 21:39:45 +08:00
return json([
'errno' => 0,
2016-09-24 22:49:20 +08:00
'msg' => trans('user.checkin-success', ['score' => $aquired_score]),
'score' => $this->user->getScore(),
2016-09-24 22:49:20 +08:00
'remaining_time' => $this->user->canCheckIn(true)
2016-07-21 22:01:57 +08:00
]);
} else {
2016-09-24 22:49:20 +08:00
return json(trans('user.cant-checkin-until', ['time' => $this->user->canCheckIn(true)]), 1);
2016-07-21 22:01:57 +08:00
}
}
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))
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.nickname.success', ['nickname' => $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')))
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.password.wrong-password'), 1);
if ($this->user->changePasswd($request->input('new_password')))
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.password.success'), 0);
2016-07-21 22:01:57 +08:00
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')))
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.email.wrong-password'), 1);
2016-07-21 22:01:57 +08:00
if ($this->user->setEmail($request->input('new_email')))
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.email.success'), 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')))
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.delete.wrong-password'), 1);
if ($this->user->delete()) {
setcookie('uid', '', time() - 3600, '/');
setcookie('token', '', time() - 3600, '/');
Session::flush();
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.delete.success'), 0);
}
break;
default:
2016-09-24 22:49:20 +08:00
return json(trans('general.illegal-parameters'), 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")
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.avatar.wrong-type'), 1);
2016-07-21 22:01:57 +08:00
if ($this->user->setAvatar($request->input('tid'))) {
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.avatar.success'), 0);
2016-07-21 22:01:57 +08:00
}
} else {
2016-09-24 22:49:20 +08:00
return json(trans('user.profile.avatar.non-existent'), 1);
2016-07-21 22:01:57 +08:00
}
}
}