摘要:<?phpnamespace app\admin\controller;//use think\facade\View; //view創(chuàng)建靜態(tài)代理use app\admin\model\UserModel; //導(dǎo)入模型 use think\facade\Request;//系統(tǒng)控制器use think\Controller;class User extends Controll
<?php
namespace app\admin\controller;
//use think\facade\View; //view創(chuàng)建靜態(tài)代理
use app\admin\model\UserModel; //導(dǎo)入模型
use think\facade\Request;//系統(tǒng)控制器
use think\Controller;
class User extends Controller
{
public function index()
{
//實(shí)例化模型
$user = new UserModel();
//halt($user);
//按照id降序排列 并且每頁(yè)展示8條數(shù)據(jù)
$users = $user->order('id', 'desc')->paginate(8);
//將數(shù)據(jù)賦值給模板
$this->view->users = $users;
//渲染管理員界面
return $this->fetch();
}
public function add()
{
//渲染管理員添加界面
return $this->fetch();
}
public function DoAdd()
{
// 獲取前臺(tái)提交過(guò)來(lái)的數(shù)據(jù)
$data = Request::param();
// 獲取添加的時(shí)間
$data['time'] = time();
$username = $data['username'];
// 使用用戶名來(lái)查詢數(shù)據(jù)庫(kù)是否有對(duì)應(yīng)的數(shù)據(jù)
$res = UserModel::where('username', $username)->find();
// 判斷數(shù)據(jù)是否存在
if ($res == true) {
return ['res' => 0, 'msg' => '用戶名已存在!'];
}
// 實(shí)例化模型
$user = new UserModel();
// 驗(yàn)證數(shù)據(jù)是否存入數(shù)據(jù)庫(kù)
if ($user->save($data)) {
return ['res' => 1, 'msg' => '添加成功!'];
} else {
return ['res' => 0, 'msg' => '添加失??!'];
}
}
public function edit()
{
// 獲取提交過(guò)來(lái)的id
$userId = Request::param('id');
//通過(guò)用戶id查詢需要更改的用戶數(shù)據(jù)
$user = UserModel::get($userId);
//將數(shù)據(jù)賦值給模板
$this->view->user = $user;
//渲染管理員編輯界面
return $this->fetch();
}
public function DoEdit()
{
// 獲取前臺(tái)提交過(guò)來(lái)的數(shù)據(jù)
$data = Request::param();
//實(shí)例化模型
$user = new UserModel();
//對(duì)數(shù)庫(kù)中的數(shù)據(jù)進(jìn)行修改更新
$res = $user->save([
'username' => $data['username'],
'password' => $data['password'],
'phone' => $data['phone'],
'email' => $data['email'],
'time' => time(),
],['id' => $data['id']]);
if($res) {
return['res'=>1,'msg'=>'修改成功!'];
}
}
public function del()
{
// 獲取提交過(guò)來(lái)的id
$userId = Request::param('id');
//實(shí)例化模型
$user = new UserModel();
//刪除并驗(yàn)證操作
if($user->destroy($userId)){
//返回提示信息
return['res'=>1,'msg'=>'刪除成功!'];
}
}
}
批改老師:查無(wú)此人批改時(shí)間:2019-06-14 14:19:17
老師總結(jié):完成的不錯(cuò)。老師只是跟你講邏輯,流程。你可以自己添加一些判斷,讓代碼更安全。繼續(xù)加油