前端到后台ThinkPHP开发整站--php开发案例
总结
还是需要做几个案例,一天一个为佳,那样才能做得快。
从需求分析着手,任务体系要构建好,这样才能非常高效。
转自:
前端到后台ThinkPHP开发整站(1) - 颓废的后生 - 博客园
http://www.cnblogs.com/lzy138/p/7197829.html
1
1、前言:
我个人从来没有写过博客文章,作为一个程序员没有自己的博客算是一个合格的程序员,所以我地想想也要经营起一个的博客,做一个小项目,写这博客算就做这个项目的一个项目笔记吧!现在自学着ThinkPHP,就借此框架做一个CMS系统。废话不多说了,赶紧进入学习了。
2、需求分析:
功能分析:
一、登录退出功能。
二、菜单功能:涉及前端菜单导航设置。
三、文章管理:文章编写,编辑插件掌握,异步图片上传。
四、推荐位管理:让用户自行设定首页推荐文章显示的设定。
五、用户管理:管理后台登录的用户和权限管理。
六、基本管理:也就是配置管理,用于修改操作网站的头部关键字,和设置是否进行生成缓存与是否自动备份数据库。
3、表设计:
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
|
CREATE
DATABASE
`tp_cms`;
CREATE
TABLE
`cms_admin`(
`admin_id` mediumint(6) unsigned
NOT
NULL
AUTO_INCREMENT,
`user_name`
varchar
(20)
not
null
default
''
COMMENT
'管理员ID'
,
`
password
`
varchar
(32)
not
null
default
''
COMMENT
'密码'
,
`last_login_ip`
varchar
(15)
default
'0'
COMMENT
'最后登录IP'
,
`last_login_time`
int
(10) unsigned
default
'0'
comment
'最后登录时间'
,
`email`
varchar
(40)
default
''
comment
'邮箱地址'
,
`real_name`
varchar
(50)
not
null
default
''
comment
'真实姓名'
,
`status` tinyint(1)
not
null
default
'1'
comment
'状态'
,
primary
key
(`admin_id`),
key
`user_name` (`user_name`)
)COMMENT=
'后台用户表'
ENGINE=MyISAM AUTO_INCREMENT=1
DEFAULT
CHARSET=utf8;
create
table
`cms_menu` (
`menu_id`
smallint
(6) unsigned
not
null
auto_increment comment
'菜单ID'
,
`
name
`
varchar
(40)
not
null
default
''
comment
'菜单名'
,
`parentid`
smallint
(6)
not
null
default
'0'
comment
'父级菜单'
,
`m`
varchar
(20)
not
null
default
''
,
`c`
varchar
(20)
not
null
default
''
,
`f`
varchar
(20)
not
null
default
''
,
`listorder`
smallint
(6) unsigned
not
null
default
'0'
comment
'序号'
,
`status` tinyint(1) unsigned
not
null
default
'1'
comment
'状态'
,
`type` tinyint(1) unsigned
not
null
default
'0'
comment
'类型'
,
primary
key
(`menu_id`),
key
`listorder` (`listorder`),
key
`parentid` (`parentid`),
key
`module` (`m`,`c`,`f`)
)COMMENT=
'菜单表'
ENGINE=MyISAM AUTO_INCREMENT=1
DEFAULT
CHARSET=UTF8;
create
table
`cms_news` (
`news_id` mediumint(8) unsigned
not
null
auto_increment comment
'新闻ID'
,
`catid`
smallint
(5) unsigned
not
null
default
'0'
comment
'栏目ID'
,
`title`
varchar
(80)
not
null
default
'标题'
,
`small_title`
varchar
(30)
not
null
default
'小标题'
,
`title_font_color`
varchar
(250)
default
null
comment
'标题颜色'
,
`thumb`
varchar
(100)
not
null
default
''
comment
'主题'
,
`keywords`
char
(40)
not
null
default
''
comment
'关键字'
,
`description`
varchar
(250)
not
null
comment
'文章描述'
,
`listorder` tinyint(3) unsigned
not
null
default
'0'
comment
'序号'
,
`status` tinyint(1)
not
null
default
'1'
comment
'状态'
,
`copyfrom`
varchar
(250)
default
null
comment
'文章来源'
,
`user_name`
char
(20)
not
null
comment
'用户'
,
`create_time`
int
(10) unsigned
not
null
default
'0'
comment
'创建时间'
,
`update_time`
int
(10) unsigned
not
null
default
'0'
comment
'更新时间'
,
`
count
`
int
(10) unsigned
not
null
default
'0'
comment
'总数'
,
primary
key
(`news_id`),
key
`listorder`(`listorder`),
key
`catid`(`catid`)
)COMMENT=
'新闻文章主表'
ENGINE=MyISAM AUTO_INCREMENT=1
DEFAULT
CHARSET=UTF8;
create
table
`cms_news_content`(
`id` mediumint(8) unsigned
not
null
auto_increment comment
'Id'
,
`news_id` mediumint(8) unsigned
not
null
comment
'新闻ID'
,
`content` mediumtext
not
null
comment
'内容'
,
`create_time`
int
(10) unsigned
not
null
default
'0'
comment
'创建时间'
,
`update_time`
int
(10) unsigned
not
null
default
'0'
comment
'更新时间'
,
primary
key
(`id`),
key
`news_id` (`news_id`)
)COMMENT=
'新闻文章内容副表'
ENGINE=MyISAM AUTO_INCREMENT=1
DEFAULT
CHARSET=UTF8;
create
table
`cms_position`(
`id`
smallint
(5) unsigned
not
null
auto_increment comment
'id'
,
`
name
`
char
(30)
not
null
default
''
comment
'名称'
,
`status` tinyint(1)
not
null
default
'1'
comment
'状态'
,
`description`
char
(100)
default
null
comment
'描述'
,
`create_time`
int
(10) unsigned
not
null
default
'0'
comment
'创建时间'
,
`update_time`
int
(10) unsigned
not
null
default
'0'
comment
'更新时间'
,
primary
key
(`id`)
)COMMENT=
'推荐位管理表'
ENGINE=MyISAM AUTO_INCREMENT=1
DEFAULT
CHARSET=UTF8;
create
table
`cms_position_content`(
`id`
smallint
(5) unsigned
not
null
auto_increment comment
'id'
comment
'id'
,
`positon_id`
int
(5) unsigned
not
null
comment
'推荐表ID'
,
`title`
varchar
(30)
not
null
default
''
comment
'标题'
,
`thumb`
varchar
(100)
not
null
default
''
comment
'主题'
,
`url`
varchar
(100)
default
null
comment
'地址'
,
`news_id` mediumint(8) unsigned
not
null
comment
'新闻ID'
,
`listorder` tinyint(3) unsigned
not
null
default
'0'
comment
'排序ID'
,
`status` tinyint(1)
not
null
default
'1'
comment
'状态'
,
`create_time`
int
(10) unsigned
not
null
default
'0'
comment
'创建时间'
,
`update_time`
int
(10) unsigned
not
null
default
'0'
comment
'更新时间'
,
primary
key
(`id`),
key
`positon_id` (`positon_id`)
)COMMENT=
'推荐位内容表'
ENGINE=MyISAM AUTO_INCREMENT=1
DEFAULT
CHARSET=UTF8;
|
今天第一天写到这里,已经22点33分了,不要熬夜容易长痘,今天先把表设计好,明天进行编码!
2
我这次使用的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
3
继续我的这个项目的第三晚的开发了,时间比较少,今晚写的代码不多,今晚仍然是造轮子写一个公共的控制器和一个公共的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
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
|
<?php
namespace
Admin\Controller;
use
Think\Controller;
/**
* 后台管理公共控制器
*/
class
CommonController
extends
Controller{
public
function
__construct(){
parent::__construct();
$this
->_init();
}
/**
* 初始化
*/
private
function
_init(){
// 如果已经登录
$isLogin
=
$this
->isLogin();
if
(!
$isLogin
){
//跳转到登录页面
$this
->redirect(
'/admin.php?c=login'
);
}
}
/**
* 获取当前登录用户信息
*/
public
function
getLoginUser(){
return
session(
'adminUser'
);
}
/**
* 判断是否登录
*/
public
function
isLogin(){
$user
=
$this
->getLoginUser();
return
(
$user
&&
is_array
(
$user
));
}
/**
* 更新数据状态
*/
public
function
setStatus(
$data
,
$models
){
try
{
if
(
$_POST
){
$id
=
$data
[
'id'
];
$status
=
$data
[
'status'
];
if
(!
$id
){
return
jsonResult(0,
'ID不存在'
);
}
$ret
=D(
$models
)->updateStatusById(
$id
,
$status
);
if
(
$ret
){
return
jsonResult(1,
'操作成功'
);
}
else
{
return
jsonResult(0,
'操作失败'
);
}
}
return
jsonResult(0,
'没有提交的内容'
);
}
catch
(Exception
$ex
){
return
jsonResult(0,
$e
->getMessage());
}
}
/**
* 数据排序
*/
public
function
listorder(
$model
=
''
){
$listorder
=
$_POST
[
'listorder'
];
$jumpUrl
=
$_SERVER
[
'HTTP_REFERER'
];
$errors
=
array
();
$resultData
=
array
(
'jump_url'
=>
$jumpUrl
);
try
{
if
(
$listorder
){
foreach
(
$listorder
as
$id
=>
$v
){
$id
=D(
$model
)->updateListorderById(
$id
,
$v
);
if
(
$id
===FALSE){
$errors
[]=
$id
;
}
}
if
(
array_count_values
(
$errors
)>0){
$group
=implode(
','
,
$errors
);
return
jsonResult(0,
"排序失败-{$group}"
,
$data
,
$resultData
);
}
return
jsonResult(1,
'排序成功'
,
$resultData
);
}
}
catch
(Exception
$ex
){
return
jsonResult(0,
$ex
->getMessage());
}
return
jsonResult(0,
'排序失败'
,
$resultData
);
}
}
?>
|
以下一段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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
var
common =
function
(queryDom) {
if
(!queryDom){
console.error(
'请传入需要操作的DOM选择字符'
);
return
;
}
function
commonObj() {
this
.dom =
''
;
}
function
todelete(url, data) {
$.post(
url,
data,
function
(s) {
if
(s.status == 1) {
return
dialog.success(s.message,
''
);
// 跳转到相关页面
}
else
{
return
dialog.error(s.message);
}
},
"JSON"
);
}
/**
* 提交form表单操作
*/
commonObj.prototype.add =
function
(formDom, func) {
$(
this
.dom).click(
function
() {
var
data = $(formDom).serializeArray();
postData = {};
$(data).each(
function
(i) {
postData[
this
.name] =
this
.value;
});
console.log(postData);
// 将获取到的数据post给服务器
url = SCOPE.save_url;
jump_url = SCOPE.jump_url;
$.post(url, postData,
function
(result) {
if
(result.status == 1) {
//成功
if
(
typeof
(func) ==
'function'
) {
func();
}
else
{
return
dialog.success(result.message, jump_url);
}
}
else
if
(result.status == 0) {
// 失败
return
dialog.error(result.message);
}
},
"JSON"
);
});
}
/**
* 编辑模块
*/
commonObj.prototype.click =
function
() {
$(
this
.dom).on(
'click'
,
function
() {
var
id = $(
this
).attr(
'attr-id'
);
var
url = SCOPE.edit_url +
'&id='
+ id;
window.location.href = url;
});
}
/*
* 删除操作
*/
commonObj.prototype.
delete
=
function
() {
$(
this
.dom).on(
'click'
,
function
() {
var
id = $(
this
).attr(
'attr-id'
);
var
a = $(
this
).attr(
"attr-a"
);
var
message = $(
this
).attr(
"attr-message"
);
var
url = SCOPE.set_status_url;
data = {};
data[
'id'
] = id;
data[
'status'
] = -1;
layer.open({
type: 0,
title:
'是否提交?'
,
btn: [
'yes'
,
'no'
],
icon: 3,
closeBtn: 2,
content:
"是否确定"
+ message,
scrollbar:
true
,
yes:
function
() {
// 执行相关跳转
todelete(url, data);
},
});
});
}
/**
* 排序操作
*/
commonObj.prototype.order =
function
() {
$(
this
.dom).click(
function
() {
// 获取 listorder内容
var
data = $(
"#singcms-listorder"
).serializeArray();
postData = {};
$(data).each(
function
(i) {
postData[
this
.name] =
this
.value;
});
console.log(data);
var
url = SCOPE.listorder_url;
$.post(url, postData,
function
(result) {
if
(result.status == 1) {
//成功
return
dialog.success(result.message, result[
'data'
][
'jump_url'
]);
}
else
if
(result.status == 0) {
// 失败
return
dialog.error(result.message, result[
'data'
][
'jump_url'
]);
}
},
"JSON"
);
});
}
/**
* 更改状态
*/
commonObj.prototype.updateStatus =
function
() {
$(
this
.dom).on(
'click'
,
function
() {
var
id = $(
this
).attr(
'attr-id'
);
var
status = $(
this
).attr(
"attr-status"
);
var
url = SCOPE.set_status_url;
data = {};
data[
'id'
] = id;
data[
'status'
] = status;
layer.open({
type: 0,
title:
'是否提交?'
,
btn: [
'yes'
,
'no'
],
icon: 3,
closeBtn: 2,
content:
"是否确定更改状态"
,
scrollbar:
true
,
yes:
function
() {
// 执行相关跳转
todelete(url, data);
},
});
});
}
commonObj.prototype.push =
function
() {
$(
this
.dom).click(
function
() {
var
id = $(
"#select-push"
).val();
if
(id == 0) {
return
dialog.error(
"请选择推荐位"
);
}
push = {};
postData = {};
$(
"input[name='pushcheck']:checked"
).each(
function
(i) {
push[i] = $(
this
).val();
});
postData[
'push'
] = push;
postData[
'position_id'
] = id;
//console.log(postData);return;
var
url = SCOPE.push_url;
$.post(url, postData,
function
(result) {
if
(result.status == 1) {
// TODO
return
dialog.success(result.message, result[
'data'
][
'jump_url'
]);
}
if
(result.status == 0) {
// TODO
return
dialog.error(result.message);
}
},
"json"
);
});
}
return
new
commonObj();
}
|
今晚就弄了那么点,反正慢慢来,不会有人催我项目进度的,主要是自己能坚持把这个项目做完。代码写到这里天色已晚,困了,都没去运行下,肯定会有些BUG的了,等一个模块开发完再去调试代码吧!
源码地址:https://github.com/YoZiLin/TP-CMS
4
今晚继续我的这个项目的开发,今晚也是写的不多,主要写了一个菜单管理功能的CURD方法,前端界面还没有进行编写。
菜单管理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
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
Common\Model;
use
Think\Model;
class
MenuModel
extends
Model{
private
$_db
=
''
;
public
function
__construct(){
$this
->_db=M(
"menu"
);
}
/**
* 插入菜单数据
*/
public
function
insert(
$data
=
array
()){
if
(!data || !
is_array
(
$data
)){
return
0;
}
return
$this
->_db->add(
$data
);
}
/**
* 获取菜单数据
*/
public
function
getMenus(
$data
,
$pageIndex
,
$pageSize
=10){
$data
[
'status'
]=
array
(
'neq'
,-1);
$offset
=(
$pageIndex
-1)*
$pageSize
;
$list
=
$this
->_db->where(
$data
)->order(
'listorder desc,menu_id desc'
)->limit(
$offset
,
$pageSize
);
return
$list
;
}
/**
* 获取菜单总数
*/
public
function
getMenusCount(
$data
=
array
()){
$data
[
'status'
]=
array
(
'neq'
,-1);
return
$this
->_db->where(
$data
)->
count
();
}
/**
* 根据ID获取菜单ID
*/
public
function
find(
$id
){
if
(!
$id
|| !
is_numeric
(
$id
)){
return
array
();
}
return
$this
->_db->where(
"menu_id={}$id"
)->find();
}
/**
* 根据ID更新菜单
*/
public
function
updateMenuById(
$id
,
$data
){
if
(!
$id
|| !
is_numeric
(
$id
)){
throw_exception(
"ID不合法"
);
}
if
(!
$data
|| !
is_array
(
$data
)){
throw_exception(
'更新的数据不合法'
);
}
return
$this
->_db->where(
"menu_id={$id}"
)->save(
$data
);
}
/**
* 更新排队序号
*/
public
function
updateMenuListOrderById(
$id
,
$listorder
){
if
(!
$id
|| !
is_numeric
(
$id
)){
throw_exception(
'ID不合法'
);
}
$data
=
array
(
'listorder'
=>
intval
(
$listorder
);
);
return
$this
->_db->where(
"menu_id={$id}"
)->save(
$data
);
}
/**
* 获取后台菜单
*/
public
function
getAdminMenus(){
$data
=
array
(
'status'
=>
array
(
'neq'
,-1),
'type'
=>1
);
return
$this
->_db->where(
$data
)->order(
'listorder desc,menu_id desc'
)->select();
}
/**
* 获取前台菜单
*/
public
function
getBarMenus(){
$data
=
array
(
'status'
=>1,
'type'
=>0
);
return
$this
->_db->where(
$data
)->order(
'listordre desc,menu_id desc'
)->select();
}
}
?>
|
菜单管理控制器类的代码:
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
104
105
106
107
108
109
110
|
<?php
namespace
Admin\Controller;
use
Think\Controller;
class
MenuController
extends
CommonController{
public
function
index(){
$data
=
array
();
if
(isset(
$_REQUEST
[
'type'
]) && in_array(
$_REQUEST
,
array
(0,1))){
$data
[
'type'
]=
intval
(
$_REQUEST
[
'type'
]);
$this
->assign(
'type'
,
$data
[
'type'
]);
}
else
{
$this
->assign(
'type'
,-100);
}
}
public
function
add(){
if
(
$_POST
){
if
(!isset(
$_POST
[
'name'
]) || !
$_POST
[
'name'
]){
return
jsonResult(0,
'菜单名不能为空'
);
}
if
(!isset(
$_POST
[
'm'
]) || !
$_POST
[
'm'
]){
return
jsonResult(0,
'模块名不能为空'
);
}
if
(!isset(
$_POST
[
'c'
]) || !
$_POST
[
'c'
]){
return
jsonResult(0,
'控制器不能为空'
);
}
if
(!isset(
$_POST
[
'f'
]) || !
$_POST
[
'f'
]){
return
jsonResult(0,
'方法名不能为空'
);
}
if
(
$_POST
[
'menu_id'
]){
return
$this
->save(
$_POST
);
}
$menuId
=D(
"Menu"
)->insert(
$_POST
);
if
(
$menuId
){
return
jsonResult(1,
'新增成功'
,
$menuId
);
}
return
jsonResult(0,
'新增失败'
,
$menuId
);
}
else
{
$this
->display();
}
}
public
function
edit(){
$menuId
=
$_REQUEST
[
'id'
];
$menu
=D(
"Menu"
)->find(
$menuId
);
$this
->assign(
'menu'
,
$menu
);
$this
->display();
}
public
function
save(
$data
){
$menuId
=
$data
[
'menu_id'
];
unset(
$data
[
'menu_id'
]);
try
{
$id
=D(
"Menu"
)->updateMenuById(
$menuid
,
$data
);
if
(
$id
===FALSE){
return
jsonResult(0,
'保存失败'
);
}
return
jsonResult(0,
'保存成'
);
}
catch
(Exception
$ex
){
return
jsonResult(0,
$ex
->getMessage());
}
}
public
function
setStatus(){
try
{
if
(
$_POST
){
$id
=
$_POST
[
'id'
];
$status
=
$_POST
[
'status'
];
$ret
=D(
"Menu"
)->updateStatusById(
$id
,
$status
);
if
(
$ret
){
return
jsonResult(1,
'操作成功'
);
}
else
{
return
jsonResult(0,
'操作失败'
);
}
}
}
catch
(Exception
$ex
){
return
jsonResult(0,
$ex
->getMessage());
}
return
jsonResult(0,
'没有提交数据'
);
}
/**
* 数据排序
*/
public
function
listorder(){
$listoreder
=
$_POST
[
'listorder'
];
$data
=
array
(
'jump_url'
=>
$_SERVER
[
'HTTP_REFERER'
]);
$errors
=
array
();
if
(
$listoreder
){
try
{
foreach
(
$listorder
as
$emnuId
=>
$v
){
$id
=D(
"Menu"
)->updateMenuListorderById(
$menuId
,
$v
);
if
(
$id
===false){
$errors
[]=
$menuId
;
}
}
}
catch
(Exception
$ex
){
return
jsonResult(0,
$ex
->getMessage(),
$data
)
}
if
(
$errors
){
return
jsonResult(0,
"排序失败-"
.implode(
','
,
$errors
),
$data
);
}
return
jsonResult(1,
'排序成功'
,
$data
)
}
return
jsonResult(0,
'数据排序失败'
,
$data
);
}
}
?>
|
今晚就暂时写这么点,明晚开始做前端的开发,明天就周五了,如果周六不用加班,我会加大马力在这周内结束该项目的。(^_−)☆
源码地址:https://github.com/YoZiLin/TP-CMS
5
今天周五了,这个项目做了五个晚上了,明天周末不用上班有一整天的时间来结束这个项目了,今晚主要把后台界面给弄出来了。
大概的整个后台界面就是这个样子了,接下来的工作就是搬砖了,一个个菜单功能填上去就是了。
还有补充了下多个公共方法,为后面菜单开发而准备。
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
<?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'
));
}
/**
*获取导航菜单
*/
function
getMenuType(
$type
){
return
$type
==1?
'后台菜单'
:
'前端导航'
;
}
/**
*获取状态
*/
function
status(
$status
){
if
(
$status
==0){
$str
=
'关闭'
;
}
elseif
(
$status
==1){
$str
=
'正常'
;
}
elseif
(
$status
==-1){
$str
=
'删除'
;
}
return
$str
;
}
/**
*获取后台菜单URL地址
*/
function
getAdminMenuUrl(
$nav
){
$url
=
'/admin.php?c='
.
$nav
[
'c'
].
'&a='
.
$nav
[
'a'
];
if
(
$nav
[
'f'
]==
'index'
){
$url
=
"/admin.php?c="
.
$nav
[
'c'
];
}
return
$url
;
}
/**
*获取控制器
*/
function
getActive(
$nav_controller
){
$controller
=
strtolower
(CONTROLLER_NAME);
if
(
strtolower
(
$nav_controller
)==
$controller
){
return
'class="active"'
;
}
return
''
;
}
/**
*文件上传结果返回
*/
function
showKind(
$status
,
$data
){
header(
'Content-type:application/json;charset=UTF-8'
);
if
(
$status
==0){
exit
(json_encode(
array
(
'error'
=>0,
'url'
=>
$data
)));
}
exit
(json_encode(
array
(
'error'
=>1,
'message'
=>
'上传失败'
)));
}
/**
*获取登录用户名
*/
function
getLoginUsername(){
return
$_SESSION
[
'adminUser'
][
'username'
]?
$_SESSION
[
'adminUser'
][
'username'
]:
''
;
}
/**
*获取菜单名
*/
function
getCatName(
$navs
,
$id
){
foreach
(
$navs
as
$nav
){
$navList
[
$nav
[
'menu_id'
]]=
$nav
[
'name'
];
}
return
isset(
$navList
[
$id
])?
$navList
[
$id
]:
''
;
}
function
getCopyFromById(
$id
){
$copyFrom
=C(
"COPY_FORM"
);
return
$copyFrom
[
$id
]?
$copyFrom
[
$id
]:
''
;
}
function
isThumb(
$thumb
){
if
(
$thumb
){
return
'<span style="color:red">有</span>'
;
}
return
'无'
;
}
/**
*文章截取预览
*/
function
msubstr(
$str
,
$start
=0,
$length
,
$charset
=
'utf-8'
,
$suffix
=true){
$len
=
strlen
(
$str
);
if
(function_exists(
'mb_substr'
)){
if
(
$suffix
){
return
mb_substr(
$str
,
$start
,
$length
,
$charset
).
'...'
;
}
else
{
return
mb_substr(
$str
,
$start
,
$length
,
$charset
);
}
}
elseif
(function_exists(
'iconv_substr'
)){
if
(
$suffix
&&
$len
>
$length
){
return
mb_substr(
$str
,
$start
,
$length
,
$charset
).
'...'
;
}
else
{
return
mb_substr(
$str
,
$start
,
$length
,
$charset
);
}
}
$re
[
'utf-8'
] =
"/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"
;
$re
[
'gb2312'
] =
"/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"
;
$re
[
'gbk'
] =
"/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"
;
$re
[
'big5'
] =
"/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"
;
preg_match_all(
$re
[
$charset
],
$str
,
$match
);
$slice
=join(
""
,
array_slice
(
$match
[0],
$start
,
$length
));
if
(
$suffix
){
return
$slice
.
'...'
;
}
return
$slice
;
}
?>
|
就到这里了,明天早起,把这个项目赶起进度来!
源码地址:https://github.com/YoZiLin/TP-CMS
6
今天终于把整个后台管理系统弄好了,其实没什么难点,只是作为一个Thinphp学习的练手项目,这个项目,现在还只能算是做了一半,还有前台展示方面的功能没有完成。先过一遍后台的功能吧!
1、首页
2、菜单管理
3、推荐位管理
4、推荐位内容管理
5、用户管理
6、基本管理
功能就以上的那么几个了,不是什么大系统,练手项目。在ThinkPHP 遇到一些小坑,比如自定义的Model没有进行到数据库操作就不要 集成ThinkPHP 的 Model 类,集成了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
Admin\Controller;
use
Think\Controller;
class
BasicController
extends
CommonController
{
public
function
index()
{
$result
=D(
'Basic'
)->select();
$this
->assign(
'vo'
,
$result
);
$this
->assign(
'type'
, 1);
$this
->display();
}
public
function
add()
{
if
(
$_POST
) {
if
(!
$_POST
[
'title'
]) {
return
jsonResult(0,
'站点信息不能为空'
);
}
if
(!
$_POST
[
'keywords'
]) {
return
jsonResult(0,
'站点关键词不能为空'
);
}
if
(!
$_POST
[
'description'
]) {
return
jsonResult(0,
'站点描述不能为空'
);
}
D(
'Basic'
)->save(
$_POST
);
}
else
{
return
jsonResult(0,
'没有提交的数据'
);
}
return
jsonResult(1,
'配置成功!'
);
}
public
function
cache()
{
$this
->assign(
'type'
, 2);
$this
->display();
}
}
|
这个是基础管理Model类,没有继续数据库操作就不用集成Model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
namespace
Common\Model;
class
BasicModel {
public
function
save(
$data
=
array
()){
if
(!
$data
){
throw_exception(
'没有提交的数据'
);
}
$id
=F(
'basic_web_config'
,
$data
);
}
public
function
select(){
return
F(
'basic_web_config'
);
}
}
?>
|
这个后台开发主要涉及的知识点是 Thinphp,和Thinkphp的内置插件的使用还有KindEditor富文本编辑器的使用。接下来的计划就是继续进行前台页面的开发!
源码地址:https://github.com/YoZiLin/TP-CMS
7
今晚我继续这个项目的前台开发,把前台的做出来了,现在项目进行一个收尾工作了,还有栏目页和一个文章页的开发,做完这两个算是完成了。说到这里感觉有点松懈了,把剩下两个功能页面做完在吹吧,先看看今天弄的代码吧!
前台公共控制器:
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
|
<?php
namespace
Home\Controller;
use
Think\Controller;
class
CommonController
extends
Controller
{
public
function
__construct()
{
header(
'Content-type:text/html;charset=utf-8'
);
parent::__construct();
}
/**
*@return 获取排序数据
*/
public
function
getRank()
{
$conds
[
'status'
]=1;
$news
=D(
'News'
)->getRank(
$conds
, 10);
return
$news
;
}
public
function
error(
$message
=
''
)
{
$message
=
$message
?
$message
:
'系统发生错误'
;
$this
->assign(
'message'
,
$message
);
$this
->display(
'Index/error'
);
}
}
|
前台首页控制器:
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
|
<?php
namespace
Home\Controller;
use
Think\Controller;
class
IndexController
extends
CommonController
{
public
function
index(
$type
=
''
)
{
//获取排序数据
$rankNews
=
$this
->getRank();
//获取首页大图数据
$topPicNews
=D(
'PositionContent'
)->select(
array
(
'status'
=>1,
'position_id'
=>2
), 1
);
// 首页小图推荐
$topSmailNews
=D(
'PositionContent'
)->select(
array
(
'status'
=>1,
'position_id'
=>3), 3
);
$listNews
=D(
'News'
)->select(
array
(
'status'
=>1,
'thumb'
=>
array
(
'neq'
,
''
)), 30);
$addNews
=D(
'PositionContent'
)->select(
array
(
'status'
=>1,
'position_id'
=>5), 2);
$this
->assign(
'result'
,
array
(
'topPicNews'
=>
$topPicNews
,
'topSmailNews'
=>
$topSmailNews
,
'listNews'
=>
$listNews
,
'advNews'
=>
$advNews
,
'rankNews'
=>
$rankNews
,
'catId'
=>0,
));
/**
*生成静态页面
*/
if
(
$type
==
'buildHtml'
) {
$this
->buildHtml(
'index'
, HTML_PATH,
'Index/index'
);
}
else
{
$this
->display();
}
}
public
function
build_html()
{
$this
->index(
'buildHtml'
);
return
jsonResult(1,
'首页缓存生成成功'
);
}
public
function
crontab_build_html()
{
if
(APP_CRONTAB != 1) {
die
(
'the_file_must_exec_crontab'
);
}
$result
=D(
'Basic'
)->select();
if
(!
$result
[
'cacheindex'
]) {
die
(
'系统没有设置开启自动生成首页缓存的内容'
);
}
$this
->index(
'buildHtml'
);
}
public
function
getCount()
{
if
(!
$_POST
) {
return
jsonResult(0,
'没有任何内容'
);
}
$newsIds
=
array_unique
(
$_POST
);
try
{
$list
=D(
'News'
)->getNewsByNewsIdIn(
$newsIds
);
}
catch
(Exception
$e
) {
return
jsonResult(0,
$e
->getMessage());
}
if
(!
$list
) {
return
jsonResult(0,
'notdata'
);
}
$data
=
array
();
foreach
(
$list
as
$k
=>
$v
) {
$data
[
$v
[
'news_id'
]]=
$v
[
'count'
];
}
return
jsonResult(1,
'success'
,
$data
);
}
}
|
今天就写了这两个类,其实已经不难了,都是那么两板斧了。今天就到这睡觉了!
源码地址:https://github.com/YoZiLin/TP-CMS
8
久违了,今天终于抽空把最后的写完了,这是这个项目的最后一篇文章了,把前台的栏目控制器和文章内容控制器的功能实现了。
栏目控制器:
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
|
<?php
namespace
Home\Controller;
use
Think\Controller;
class
CatController
extends
CommonController{
public
function
index(){
$id
=
intval
(
$_GET
[
'id'
]);
if
(!
$id
){
return
$this
->error(
'ID不存在'
);
}
$nav
=D(
'Menu'
)->find(
$id
);
if
(!
$nav
||
$nav
[
'status'
]!=1){
return
$this
->error(
'栏目id不存在或者状态不为正常'
);
}
$advNews
=D(
'PositionContent'
)->select(
array
(
'status'
=>1,
'position_id'
=>5),2);
$rankNews
=
$this
->getRank();
$page
=
$_REQUEST
[
'p'
]?
$_REQUEST
[
'p'
]:1;
$pageSize
=20;
$conds
=
array
(
'status'
=>1,
'thumb'
=>
array
(
'neq'
,
''
),
'catid'
=>
$id
);
$news
=D(
'News'
)->getNews(
$conds
,
$page
,
$pageSize
);
$count
=D(
'News'
)->getNewsCount(
$conds
);
$res
=
new
\Think\Page(
$count
,
$pageSize
);
$pageres
=
$res
->show();
$this
->assign(
'result'
,
array
(
'advNews'
=>
$advNews
,
'rankNews'
=>
$rankNews
,
'catId'
=>
$id
,
'listNews'
=>
$news
,
'pageres'
=>
$pageres
));
$this
->display();
}
}
?>
|
文章内容控制器:
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
|
<?php
namespace
Home\Controller;
use
Think\Controller;
class
DetailController
extends
CommonController{
public
function
index(){
$id
=
intval
(
$_GET
[
'id'
]);
if
(!
$id
||
$id
<0){
return
$this
->error(
'ID不合法'
);
}
$news
=D(
'News'
)->find(
$id
);
if
(!
$news
||
$news
[
'status'
]!=1){
return
$this
->error(
'ID不存在或者资讯被关闭'
);
}
$count
=
intval
(
$news
[
'count'
])+1;
D(
'News'
)->updateCount(
$id
,
$count
);
$content
=D(
'NewsContent'
)->find(
$id
);
$news
[
'content'
]=\htmlspecialchars_decode(
$content
[
'content'
]);
$advNews
=D(
'PositionContent'
)->select(
array
(
'status'
=>1,
'position_id'
=>5),2);
$rankNews
=
$this
->getRank();
$this
->assign(
'result'
,
array
(
'rankNews'
=>
$rankNews
,
'advNews'
=>
$advNews
,
'catId'
=>
$news
[
'catid'
],
'news'
=>
$news
));
$this
->display(
'Detail/index'
);
}
public
function
view(){
if
(!getLoginUsername()){
$this
->error(
'您没有权限访问该页面'
);
}
$this
->index();
}
}
?>
|
终于坚持的把该项目做完了,这个项目我也是上淘宝买的教程,边学边做的!可能还会有很多BUG和可改善的地方,日后慢慢检查作修改。
源码地址:https://github.com/YoZiLin/TP-CMS