Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Commit

Permalink
feat: plug-in Management list Page
Browse files Browse the repository at this point in the history
  • Loading branch information
PBK-B committed Sep 29, 2021
1 parent e4cf0a0 commit b59a757
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 6 deletions.
26 changes: 26 additions & 0 deletions controllers/apis/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,29 @@ func (c *PluginController) ApiUploadPlugin() {
callBackResult(&c.Controller, 200, "", plugin.ToMap())
c.Finish()
}

// 获取插件列表接口
func (c *PluginController) ApiPluginList() {
// 要求登陆助理函数
userAssistant(&c.Controller)
u_count, _ := c.GetInt("count", 10)
u_page, _ := c.GetInt("page", 0)

plugins, err := models.AllPlugin(u_count, u_page)

if err != nil {
callBackResult(&c.Controller, 403, "服务器错误", nil)
c.Finish()
return
}

var new_plugins []interface{}

for item := range plugins {
i_p := plugins[item]
new_u := i_p.ToMap()
new_plugins = append(new_plugins, new_u)
}

callBackResult(&c.Controller, 200, "", new_plugins)
}
46 changes: 42 additions & 4 deletions models/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"plugin"
"strconv"

"reflect"
Expand Down Expand Up @@ -103,6 +105,27 @@ func (p *Plugin) ToMap() map[string]interface{} {
}
}

// Plugin 注册到 MiraiGo 中
func (p *Plugin) LoadRegisterModule() (err error) {
// 获取插件 SO 文件绝对路径
var pluginPathSO = filepath.Join(helper.GetCurrentAbPath(), p.Path+"/build.so")

// 加载 so 文件,调用 RegisterModule 方法注册插件
plu, err := plugin.Open(pluginPathSO)
if plu == nil || err != nil {
p.AddLog("加载插件 so 文件失败," + err.Error())
return
}
registerModule, err := plu.Lookup("RegisterModule")
if err != nil {
p.AddLog("加载插件 RegisterModule 方法未实现," + err.Error())
return
}
registerModule.(func())()
p.AddLog("插件 (" + p.Package + ") 注册成功。")
return
}

// AddPlugin insert a new Plugin into database and returns
// last inserted Id on success.
func AddPlugin(m *Plugin) (p *Plugin, err error) {
Expand Down Expand Up @@ -206,10 +229,11 @@ func UpdatePluginById(m *Plugin) (err error) {
v := Plugin{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
// var num int64
// if num, err = o.Update(m); err == nil {
// fmt.Println("Number of records updated in database:", num)
// }
_, err = o.Update(m)
}
return
}
Expand All @@ -229,6 +253,14 @@ func DeletePlugin(id int) (err error) {
return
}

// 获取全部插件
func AllPlugin(limit int, page int) (plugin []Plugin, err error) {
o := orm.NewOrm()
qs := o.QueryTable(&Plugin{})
_, err = qs.Filter("id__isnull", false).Limit(limit, page).All(&plugin)
return
}

// 安装插件文件
func InstallPlugin(file string) (p *Plugin, err error) {

Expand Down Expand Up @@ -283,6 +315,9 @@ func InstallPlugin(file string) (p *Plugin, err error) {

p.AddLog("插件升级安装 (" + strconv.FormatFloat(manifestObj.Version, 'f', -1, 64) + ") 成功!")

// 注册插件
p.LoadRegisterModule()

return p, nil
}
}
Expand Down Expand Up @@ -318,6 +353,9 @@ func InstallPlugin(file string) (p *Plugin, err error) {
}
}

// 注册插件
p.LoadRegisterModule()

return
}

Expand Down
1 change: 1 addition & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func init() {
// 插件相关 API
web.NSNamespace("/plugin",
web.NSRouter("/upload", &apis.PluginController{}, "post:ApiUploadPlugin"),
web.NSRouter("/list", &apis.PluginController{}, "get:ApiPluginList"),
),
)

Expand Down
2 changes: 2 additions & 0 deletions src/pages/admin/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import UserControll from './UserControll';
import TimeControll from './TimeControll';
import CourseControll from './CourseControll';
import SystemSetup from './SystemSetup';
import PluginControl from './PluginControl';

import { UserStore } from './stores';

Expand Down Expand Up @@ -68,6 +69,7 @@ export default function App() {
3: <TimeControll />,
4: <CourseControll />,
5: <SystemSetup />,
6: <PluginControl />,
};

return (
Expand Down
185 changes: 185 additions & 0 deletions src/pages/admin/PluginControl.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import React, { useState, useEffect } from 'react';
import useAxios from 'axios-hooks';
import axios from 'axios';

import {
Table,
Panel,
Loader,
FlexboxGrid,
Button,
Modal,
Notification,
Form,
FormControl,
FormGroup,
ControlLabel,
Row,
} from 'rsuite';

import { UserStore } from './stores';
import { observer } from 'mobx-react';

const { Column, HeaderCell, Cell, Pagination } = Table;

export default function PluginControl() {
const [{ data, loading, error }, refetch] = useAxios({
url: '/api/plugin/list',
});

// 用户列表数据处理
let users = [];

if (data) {
let { data: data_array } = data;
data_array = data_array?.map((item, index) => {
return {
...item,
status: item.status == 1 ? '启用' : '禁用',
time: new Date().toUTCString(),
};
});
users = data_array;
}

if (loading) return <Loader backdrop content="loading..." vertical />;

return (
<div style={{ marginTop: 25, marginBottom: 25 }}>
<p>插件管理页面</p>
<br />

<FlexboxGrid style={{ marginBottom: 15 }} justify="end">
<FlexboxGrid.Item>
<Button appearance="ghost" onClick={() => {}} disabled={UserStore?.me?.id !== 1}>
安装插件
</Button>
</FlexboxGrid.Item>
</FlexboxGrid>

<Panel bordered bodyFill>
<Table
autoHeight
data={users}
onRowClick={(data) => {
console.log(data);
}}
>
<Column width={70} align="center" fixed>
<HeaderCell>ID</HeaderCell>
<Cell dataKey="id" />
</Column>

<Column width={200} fixed>
<HeaderCell>名称</HeaderCell>
<Cell dataKey="name" />
</Column>

<Column width={200}>
<HeaderCell>包名</HeaderCell>
<Cell dataKey="package" />
</Column>

<Column width={100}>
<HeaderCell>作者</HeaderCell>
<Cell>
{(rowData) => (
<a href={rowData?.website || '#'} target="_blank">
{'@' + rowData?.author}
</a>
)}
</Cell>
</Column>

<Column width={70}>
<HeaderCell>版本</HeaderCell>
<Cell dataKey="version" />
</Column>

<Column width={70}>
<HeaderCell>状态</HeaderCell>
<Cell dataKey="status" />
</Column>

<Column width={260}>
<HeaderCell>安装时间</HeaderCell>
<Cell dataKey="time" />
</Column>

<Column width={160} fixed="right">
<HeaderCell>操作</HeaderCell>

<Cell style={{ padding: 0, display: 'flex', alignItems: 'center' }}>
{(rowData) => {
function editAction(e) {
setupdateUser({
id: rowData.id,
name: rowData.name,
passwd: '',
});
setshowUpdateUser(true);

// 结束事件分发
e.stopPropagation();
}

function disableAction(e) {
// 结束事件分发
e.stopPropagation();
APIDisableUser(rowData.id, rowData.status === '启用');
}

return (
<Row style={{ overflow: 'hidden' }}>
<Button
appearance="link"
onClick={editAction}
disabled={
rowData.id === UserStore?.me?.id || UserStore?.me?.id === 1
? false
: true
}
>
配置
</Button>
|
<Button appearance="link" onClick={() => {}}>
日志
</Button>
|
<Button
appearance="link"
onClick={disableAction}
disabled={UserStore?.me?.id !== 1}
>
{rowData.status === '启用' ? ' 禁用 ' : ' 启用 '}
</Button>
</Row>
);
}}
</Cell>
</Column>
</Table>
</Panel>

<Modal show={false} onHide={() => {}} backdrop="static">
<Modal.Header>
<Modal.Title>安装插件</Modal.Title>
</Modal.Header>
<Modal.Body>
<Form fluid onChange={() => {}} formValue={() => {}}>
<FormGroup>
<ControlLabel>账号:</ControlLabel>
<FormControl name="name" />
</FormGroup>

<FormGroup>
<ControlLabel>密码:</ControlLabel>
<FormControl name="passwd" />
</FormGroup>
</Form>
</Modal.Body>
</Modal>
</div>
);
}
3 changes: 3 additions & 0 deletions src/pages/admin/components/AppMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default function AppMenu(props) {
<Nav.Item eventKey="5" icon={<Icon icon="gear2" />}>
<p className="app-menu-text">系统设置</p>
</Nav.Item>
<Nav.Item eventKey="6" icon={<Icon icon="creative" />}>
<p className="app-menu-text">高级功能</p>
</Nav.Item>
</Nav>
</Sidenav.Body>
</Sidenav>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/admin/screens/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CourseList from "./CourseList";
import CreateCourse from "./CreateCourse";
import CourseList from './CourseList';
import CreateCourse from './CreateCourse';

export { CourseList, CreateCourse };

0 comments on commit b59a757

Please sign in to comment.