我这次使用的ThinkPHP版本是:3.2.3版本,还有会使用到一个弹出层插件,叫 layer,官网地址是:http://layer.layui.com/。废话不多说,进入撸码环节。
1、通用方法编写
这个是后端公共方法,现在暂时写两个方法,再往后开发想到有需要的话,就会继续添加更多的公共方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
/**
* JSON数据返回
*/
function
jsonResult(
$status
,
$message
,
$data
){
$result
=
array
({
'status'
=>
$status
,
'message'
=>
$message
,
'data'
=>
$data
});
exit
(json_encode(
$result
));
}
/**
* MD5加密密码
*/
function
getMd5Password(
$password
){
return
md5(
$password
.C(
'MD5_PRE'
));
}
?>
|
公共弹出JS方法封装
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
|
var
dialog = {
/**
* 错误弹出层
* @param {String} 内容
*/
error:
function
(message) {
layer.open({
content: message,
icon: 2,
title:
'错误提示'
});
},
/**
* 成功弹出层
* @param {String} 内容
* @param {String} 跳转地址
*/
success:
function
(message, url) {
layer.open({
content: message,
icon: 1,
yes:
function
() {
location.href = url;
}
});
},
/**
* 确认弹出层
* @param {String} 内容
* @param {String} 跳转地址
*/
confirm:
function
(message, url) {
layer.open({
content: message,
icon: 3,
btn: [
'是'
,
'否'
],
yes:
function
() {
location.href = url;
}
});
},
/**
* 无需跳转到指定页面的确认弹出层
* @param {string} 内容
*/
toconfirm:
function
(message) {
layer.open({
content: message,
icon: 3,
btn: [
'确定'
]
});
},
/**
* 加载层
*/
load:
function
(){
var
index = layer.load(1, {
shade: [0.6,
'#000'
]
//0.1透明度的白色背景
});
return
index;
}
}
|
2、登录功能:
后台用户操作类,添加在Model层,主要用于一些数据操作
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
|
<?php
namespace
Common\Model;
use
Think\Model;
/**
* 后台用户操作类
*/
class
AdminModel
extends
Model{
private
$_db
=null;
public
function
__construct(){
$this
->_db=M(
'admin'
);
}
/**
* 根据用户名获取用户信息
* $username string 用户名
*/
public
function
getAdminByUserName(
$username
=
''
){
$ret
=
$this
->_db->where(
"user_name='{$username}'"
)->find();
return
$ret
;
}
/**
* 根据adminid更新数据
* $id int id
* $data object 需更新的数据
*/
public
function
updateByAdminId(
$id
,
$data
){
if
(!
$id
|| !
is_numeric
(
$id
)){
throw_exception(
"ID不合法"
);
}
if
(!
$data
|| !
is_array
(
$data
)){
throw_exception(
'更新的数据不合法'
);
}
return
$this
->_db->where(
"admin_id={$id}"
).save(
$data
);
}
}
?>
|
登录功能后端实现逻辑
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
|
<?php
namespace
Admin\Controller;
use
Think\Controller;
class
LoginController
extends
Controller{
public
function
index(){
if
(session(
'adminUser'
)){
$this
->redirect(
'/admin.php?c=index'
);
}
$this
->display();
}
public
function
check(){
$username
=
$_POST
[
'username'
];
$password
=
$_POST
[
'password'
];
if
(!trim(
$username
)){
return
jsonResult(0,
'用户名不能为空'
);
}
if
(!trim(
$password
)){
return
jsonResult(0,
'密码不能为空'
);
}
$ret
=D(
'Admin'
)->getAdminByUsername(
$username
);
if
(!ret ||
$ret
[
'status'
]!=1){
return
jsonResult(0,
'该用户不存在'
);
}
if
(
$ret
[
'password'
]!=getMd5Password(
$password
)){
return
jsonResult(0,
'用户名或密码错误'
);
}
D(
"Admin"
)->updateByAdminId(
$ret
[
'admin_id'
],
array
(
'last_login_time'
=>time()));
session(
'adminUser'
,
$ret
);
return
jsonResult(1,
'登录成功'
);
}
}
?>
|
前端JS登录逻辑实现
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
|
var
login={
check:
function
(){
//获取登录页面中的用户名 和 密码
var
username=$(
'input[name="username"]'
).val(),
password=$(
'input[name="password"]'
).val();
if
(!username){
dialog.error(
'用户名不能为空'
);
}
if
(!password){
dialog.error(
'密码不能为空'
);
}
var
url=
"/index.php?m=admin&c=login&a=check"
,
data={
"username"
:username,
"password"
:password
};
var
load = dialog.load();
$.post(url,data,
function
(result){
layer.close(load);
if
(result.status==0){
return
dialog.error(result.message);
}
if
(result.status==1){
return
dialog.success(result.message,
'/admin.php?c=index'
);
}
},
'JSON'
);
}
}
|
今天就简单的做到这里了,项目的开始,造轮子的时间比较长,轮子造好了,车就可以开快了!(๑╹◡╹)ノ"""
源码地址:https://github.com/YoZiLin/TP-CMS