摘要:<?php namespace app\admin\controller; use think\Controller; use app\admin\model\UserModel; use think\facade\Request; use think\Db; class User extends Control
<?php namespace app\admin\controller; use think\Controller; use app\admin\model\UserModel; use think\facade\Request; use think\Db; class User extends Controller { public function index() { // 查詢出管理員數(shù)據(jù) $num = 8; $type = false; $config = [ 'type' => 'bootstrap', 'var_page' => 'page', ]; // 獲取數(shù)據(jù) $pagedata = UserModel::order('id','asc')->paginate($num,$type,$config); // 獲取分頁 $page = $pagedata->render(); $this->view->assign('user',$pagedata); $this->view->assign('page',$page); // 渲染管理員界面 return $this->view->fetch(); } public function add() { // 渲染添加界面 return $this->fetch(); } public function DoAdd() { // request $data = Request::param(); $data['time'] = time(); $username = $data['username']; // if $res = UserModel::where('username','=',$username)->find(); if($res == true){ return ['res' => 0,'msg' => '該用戶名已經(jīng)存在']; } // add $data['password'] = sha1($data['password']); if(UserModel::insert($data)){ return ['res' => 1,'msg' => '用戶添加成功']; }else{ return ['res' => 0,'msg' => '用戶添加失敗']; } // $user = new userModel(); // if($user->save($data)){ // return ['res' => '1','msg' => '用戶添加成功']; // }else{ // return ['res' => '0','msg' => '用戶添加失敗']; // } } public function edit() //根據(jù)主鍵Id預(yù)查詢一條數(shù)據(jù)在編輯頁面上面 { // 獲取url傳遞Id $userId = Request::param('id'); // 根據(jù)主鍵Id查詢一條數(shù)據(jù) $res = UserModel::get($userId); // 賦值給頁面 $this->view->assign('urlid',$res); // 渲染頁面 return $this->view->fetch(); } public function DoEdit() //編輯更新操作 { // 獲取ajax提交的數(shù)據(jù) $data = Request::param(); // 執(zhí)行更行操作 $dataId = $data['id']; $res = UserModel::update([ 'username' => $data['username'], 'tel' => $data['tel'], 'email' => $data['email'], 'time' => time(), ],function($query) use ($dataId){ $query->where('id','=',$dataId); }); // 返回結(jié)果 if($res){ return ['res' => 1,'msg' => '編輯成功!']; }else{ return ['res' => 0,'msg' => '編輯失?。?#39;]; } } public function del() { // 獲取前臺的主鍵id $delId = Request::param('id'); // 執(zhí)行刪除 $res = UserModel::where('id','=',$delId)->delete(); // 返回結(jié)果 if($res){ return ['res' => 1,'msg' => '刪除成功!']; }else{ return ['res' => 0,'msg' => '刪除失敗!']; } } }
ajax異步請求添加
$.ajax({
type: 'post',
url: '{:url(\'User/DoAdd\')}',
dataType: 'json',
data: {
"username": $('#username').val(),
"password": $('#password').val(),
'tel': $('#tel').val(),
'email': $('#email').val(),
},
success: function (data) {
console.log(data);
if (data.res == 1) {
layer.alert(data.msg, {
icon: 6
}, function () {
// 獲得frame索引
var index = parent.layer.getFrameIndex(
window.name);
//關(guān)閉當(dāng)前frame
parent.layer.close(index);
});
} else {
layer.alert(data.msg, {
icon: 6
}, function () {
// 獲得frame索引
var index = parent.layer.getFrameIndex(window.name);
//關(guān)閉當(dāng)前frame
parent.layer.close(index);
});
}
},
})
所有的功能測試完全成功!老師可以看我的github,項目全都push上去了(https://github.com/heyguojing/enterweb)
批改老師:查無此人批改時間:2019-05-06 09:41:51
老師總結(jié):完成的不錯。后臺管理系統(tǒng),最多的操作就是增刪查改數(shù)據(jù)庫。繼續(xù)加油。