Skip to content

Commit

Permalink
#446
Browse files Browse the repository at this point in the history
  • Loading branch information
orvice committed Apr 9, 2016
1 parent ce95163 commit f3927f1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
40 changes: 23 additions & 17 deletions app/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,54 @@

namespace App\Controllers\Admin;

use App\Models\User;
use App\Controllers\AdminController;
use App\Models\User;
use App\Utils\Hash;
use App\Utils\Tools;

class UserController extends AdminController
{
public function index($request, $response, $args){
public function index($request, $response, $args)
{
$pageNum = 1;
if(isset($request->getQueryParams()["page"])){
if (isset($request->getQueryParams()["page"])) {
$pageNum = $request->getQueryParams()["page"];
}
$users = User::paginate(15,['*'],'page',$pageNum);
$users = User::paginate(15, ['*'], 'page', $pageNum);
$users->setPath('/admin/user');
return $this->view()->assign('users',$users)->display('admin/user/index.tpl');
return $this->view()->assign('users', $users)->display('admin/user/index.tpl');
}

public function edit($request, $response, $args){
public function edit($request, $response, $args)
{
$id = $args['id'];
$user = User::find($id);
if ($user == null){
if ($user == null) {

}
return $this->view()->assign('user',$user)->display('admin/user/edit.tpl');
return $this->view()->assign('user', $user)->display('admin/user/edit.tpl');
}

public function update($request, $response, $args){
public function update($request, $response, $args)
{
$id = $args['id'];
$user = User::find($id);

$user->email = $request->getParam('email');
$user->email = $request->getParam('email');
if ($request->getParam('pass') != '') {
$user->pass = Hash::passwordHash($request->getParam('pass'));
}
if($request->getParam('passwd') != ''){
if ($request->getParam('passwd') != '') {
$user->passwd = $request->getParam('passwd');
}
$user->port = $request->getParam('port');
$user->transfer_enable = $request->getParam('transfer_enable');
$user->port = $request->getParam('port');
$user->transfer_enable = Tools::toGB($request->getParam('transfer_enable'));
$user->invite_num = $request->getParam('invite_num');
$user->method = $request->getParam('method');
$user->enable = $request->getParam('enable');
$user->is_admin = $request->getParam('is_admin');
$user->ref_by = $request->getParam('ref_by');
if(!$user->save()){
if (!$user->save()) {
$rs['ret'] = 0;
$rs['msg'] = "修改失败";
return $response->getBody()->write(json_encode($rs));
Expand All @@ -55,10 +59,11 @@ public function update($request, $response, $args){
return $response->getBody()->write(json_encode($rs));
}

public function delete($request, $response, $args){
public function delete($request, $response, $args)
{
$id = $args['id'];
$user = User::find($id);
if(!$user->delete()){
if (!$user->delete()) {
$rs['ret'] = 0;
$rs['msg'] = "删除失败";
return $response->getBody()->write(json_encode($rs));
Expand All @@ -68,7 +73,8 @@ public function delete($request, $response, $args){
return $response->getBody()->write(json_encode($rs));
}

public function deleteGet($request, $response, $args){
public function deleteGet($request, $response, $args)
{
$id = $args['id'];
$user = User::find($id);
$user->delete();
Expand Down
11 changes: 8 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* User Model
*/

use App\Utils\Tools;
use App\Utils\Hash;
use App\Models\InviteCode;
use App\Services\Config;
use App\Utils\Hash;
use App\Utils\Tools;

class User extends Model

Expand Down Expand Up @@ -115,6 +114,12 @@ public function enableTraffic()
return Tools::flowAutoShow($transfer_enable);
}

public function enableTrafficInGB()
{
$transfer_enable = $this->attributes['transfer_enable'];
return Tools::flowToGB($transfer_enable);
}

public function usedTraffic()
{
$total = $this->attributes['u'] + $this->attributes['d'];
Expand Down
11 changes: 9 additions & 2 deletions app/Utils/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,25 @@ static function flowAutoShow($value = 0)
}
}

static function toMB($traffic)

public static function toMB($traffic)
{
$mb = 1048576;
return $traffic * $mb;
}

static function toGB($traffic)
public static function toGB($traffic)
{
$gb = 1048576 * 1024;
return $traffic * $gb;
}

public static function flowToGB($traffic)
{
$gb = 1048576 * 1024;
return $traffic / $gb;
}

//获取随机字符串
public static function genRandomChar($length = 8)
{
Expand Down
13 changes: 5 additions & 8 deletions resources/views/default/admin/user/edit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,20 @@
<div class="col-sm-9">
<div class="input-group">
<input class="form-control" id="transfer_enable" type="number"
value="{$user->transfer_enable}">
value="{$user->enableTrafficInGB()}">

<div class="input-group-addon">字节</div>
<div class="input-group-addon">GiB</div>
</div>
</div>
</div>


<div class="form-group">
<label class="col-sm-3 control-label">已用流量</label>

<div class="col-sm-9">
<div class="input-group">
<input class="form-control" id="usedTraffic" type="number"
value="{$user->u+$user->d}" readonly>

<div class="input-group-addon">字节</div>
</div>
<input class="form-control" id="invite_num" type="number"
value="{$user->usedTraffic()}" readonly>
</div>
</div>
</fieldset>
Expand Down

0 comments on commit f3927f1

Please sign in to comment.