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-10-23 11:41:52 +08:00
|
|
|
use App;
|
2016-09-04 15:35:12 +08:00
|
|
|
use View;
|
|
|
|
use Utils;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Texture;
|
2016-09-04 15:35:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2016-11-18 23:06:15 +08:00
|
|
|
use App\Events\UserProfileUpdated;
|
2016-08-29 19:47:30 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-10-23 11:41:52 +08:00
|
|
|
use App\Services\Repositories\UserRepository;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +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
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
public function __construct(Request $request, UserRepository $users)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-10-23 11:41:52 +08:00
|
|
|
$this->action = $request->input('action', '');
|
|
|
|
$this->user = $users->get(session('uid'));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
return view('user.index')->with('user', $this->user);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
2016-09-24 22:49:20 +08:00
|
|
|
* Handle User Checking In
|
2016-09-04 15:35:12 +08:00
|
|
|
*
|
|
|
|
* @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([
|
2016-07-24 12:33:11 +08:00
|
|
|
'errno' => 0,
|
2016-09-24 22:49:20 +08:00
|
|
|
'msg' => trans('user.checkin-success', ['score' => $aquired_score]),
|
2016-07-24 12:33:11 +08:00
|
|
|
'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()
|
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
return view('user.profile')->with('user', $this->user);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
|
|
|
* Handle Changing Profile
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handleProfile(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +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
|
|
|
|
2016-09-04 15:35:12 +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
|
|
|
|
2016-09-04 15:35:12 +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);
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
break;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +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
|
|
|
|
2016-09-04 15:35:12 +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
|
|
|
|
2016-09-04 15:35:12 +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
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
break;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
case 'delete':
|
|
|
|
$this->validate($request, [
|
|
|
|
'password' => 'required|min:8|max:16'
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +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);
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
if ($this->user->delete()) {
|
|
|
|
setcookie('uid', '', time() - 3600, '/');
|
|
|
|
setcookie('token', '', time() - 3600, '/');
|
|
|
|
|
2016-11-17 17:32:12 +08:00
|
|
|
session()->flush();
|
2016-09-04 15:35:12 +08:00
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.delete.success'), 0);
|
2016-09-04 15:35:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('general.illegal-parameters'), 1);
|
2016-09-04 15:35:12 +08:00
|
|
|
break;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-11-18 23:06:15 +08:00
|
|
|
event(new UserProfileUpdated($this->action, $this->user));
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
|
|
|
* Set Avatar for User
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
public function setAvatar(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$result = Texture::find($request->input('tid'));
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
if ($result) {
|
2016-09-04 15:35:12 +08:00
|
|
|
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
|
|
|
|
2016-09-04 15:35:12 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|