前端到后台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