返回管理员的添加,......登陆

管理员的添加,修改,编辑,删除操作

靖哥哥2019-05-05 22:04:47689

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

<?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()

{

// 查询出管理员数据

$num = 8;

$type = false;

$config = [

'type' => 'bootstrap',

'var_page' => 'page',

];

// 获取数据

$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' => '该用户名已经存在'];

}

// 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()  //根据主键Id预查询一条数据在编辑页面上面

{

// 获取url传递Id

$userId = Request::param('id');

// 根据主键Id查询一条数据

$res = UserModel::get($userId);

// 赋值给页面

$this->view->assign('urlid',$res);

// 渲染页面

return $this->view->fetch();

}

public function DoEdit()  //编辑更新操作

{

// 获取ajax提交的数据

$data = Request::param();

// 执行更行操作

$dataId $data['id'];

$res = UserModel::update([

'username' => $data['username'],

'tel' => $data['tel'],

'email' => $data['email'],

'time' => time(),

],function($queryuse ($dataId){

$query->where('id','=',$dataId);

});

// 返回结果

if($res){

return ['res' => 1,'msg' => '编辑成功!'];

}else{

return ['res' => 0,'msg' => '编辑失败!'];

}

}

public function del()

{

// 获取前台的主键id

$delId = Request::param('id');

// 执行删除

$res = UserModel::where('id','=',$delId)->delete();

// 返回结果

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);

//关闭当前frame

parent.layer.close(index);

});

} else {

layer.alert(data.msg, {

icon: 6

}, function () {

// 获得frame索引

var index = parent.layer.getFrameIndex(window.name);

//关闭当前frame

parent.layer.close(index);

});

}

},

})

sdfsf.png

所有的功能测试完全成功!老师可以看我的github,项目全都push上去了(https://github.com/heyguojing/enterweb)

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送