管理员增 删 改 查

Original 2019-01-26 03:31:17 169
abstract:<?phpnamespace app\admin\controller;use think\Controller;use think\facade\Session;use think\facade\Request;use app\admin\controller\Common;use app\admin\model\User as UserModel;class User extends C

<?php

namespace app\admin\controller;

use think\Controller;

use think\facade\Session;

use think\facade\Request;

use app\admin\controller\Common;

use app\admin\model\User as UserModel;

class User extends Common

{

public function index()

{

$user = UserModel::order('id','desc')->paginate(8);

$count = UserModel::count();

$this->view->assign('user',$user);

$this->view->assign('count',$count);

return $this->fetch();

}


public function add()

{

return $this->fetch();

}



public function Create()

{


$data = request::param();

$username = $data['username'];

$res = UserModel::getByusername($username);

if($res == true){

return ['res' => 0, 'msg' => '用户已存在'];

}


$res = $this->validate($data,'app\admin\validate\User.edit');

if (true !== $res) {  

return ['res' => 0, 'msg' => $res];

}

$data['password'] = md5($data['password']);

$field = ['username','password','phone','email'];

$User = UserModel::create($data,$field);

if($User){

return ['res' => 1, 'msg' => '添加成功:'];

}else{

return ['res' => 0, 'msg' => '添加失败:'];

}


}


public function Edit()

{

$data = request::param('id');

$User = UserModel::get($data);

$this->view->assign('user',$User);

return $this->fetch();

}


public function Update()

{

$data = request::param();

$data['password'] = md5($data['password']);

$User = UserModel::update($data);

if($User){

return ['res' => 1, 'msg' => '修改成功'];

}else{

return ['res' => 0, 'msg' => '修改失败'];

}

}


public function Delete()

{

$data = request::param('id');

$User = UserModel::destroy($data);

if($User){

return ['res' => 1, 'msg' => '删除成功'];

}


}


}



Correcting teacher:韦小宝Correction time:2019-01-26 09:22:06
Teacher's summary:写的很棒 代码写的也很规范 没有错误

Release Notes

Popular Entries