Zabbix架构的改进方案(二)

比如要做一个用户管理模块UserManager, 通过UserManager.do访问主页面, 而新增用户的地址为UserManager!add.do, 删除用户的后台请求地址为UserManager!delete.do

1, 建一个后台php文件继承BaseController,名为UserManagerController.php

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
// 
class UserManagerController extends BaseController {

function userList() { // 用户列表,通过UserManager!userList.do来访问
$this->data = [
[
'username' => 'sun',
'sex' => 'f'
], [
'username' => 'aaa',
'sex' => 'f'
], [
'username' => 'aaa',
'sex' => 'f'
], [
'username' => 'aaa',
'sex' => 'f'
]
];
}

function add() { // 新增用户,通过UserManager!add.do来访问
$this->data = [
'success'=> true,
'msg'=>'add success'
];
}

function delete() { // 删除用户,通过UserManager!delete.do来访问
$this->data = [
'success'=> true,
'msg'=>'delete success'
];
}
}

如果需要权限控制,则在该Controller中重写父类方法(默认是管理员及以上可以访问)

1
2
3
function checkPermissions() {
return ($this->getUserType() >= USER_TYPE_ZABBIX_ADMIN);
}

效果:
访问 http://localhost/dam/UserManager!userList.do时返回数据如下

1
2
3
4
5
6
[
{"username":"sun","sex":"f"},
{"username":"aaa","sex":"f"},
{"username":"aaa","sex":"f"},
{"username":"aaa","sex":"f"},
]

2, 建一个对应前端的js文件,名为UserManager.js, 继承自TSWidget或TSWidget的子类

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
// demo
define("js/widgets/UserManager", [
"ts/widgets/TSDataGrid",
"ts/widgets/TSGridColumn",
"ts/widgets/TSButton",
'ts/widgets/GenericDialog',
], function(TSDataGrid, TSGridColumn, TSButton, Dialog){

var i18n=TSDataGrid.prototype.i18n.createBranch({});
function UserManager(opts){
TSDataGrid.call(this);

this.dataSource = 'UserManager!userList.do';
defineProperties.call(this);
}

function defineProperties(){
var self=this;
var username=new TSGridColumn({
dataField:"username",
headerText:'username',
width:250,
});

var sex=new TSGridColumn({
dataField:"sex",
headerText:'sex',
width:250,
});

this.columns=[username, sex];
this.actions = [new TSButton({
name:"测试",
buttonName:'测试',
iconClass:"glyphicon glyphicon-plus",
click:function(){
Dialog.create({
title: 'fdafds'
})

}
})];
this.usePager=true;
this.checkable=false;
this.showToolbar=true;
this.showFooter=true;
}

ExtendClass(UserManager, TSDataGrid);

return UserManager;

});

如果想加到菜单里,则在menu.inc.php中配置下

1
2
3
4
5
], [
'label' => _('测试demo'),
'url' => 'UserManager.do',
'active_if' => ['UserManager'],
],[