add http demo

This commit is contained in:
gsx
2022-04-12 14:55:48 +08:00
parent db00d3b93b
commit 77262f5c4f
9 changed files with 135 additions and 9 deletions

View File

@@ -84,7 +84,7 @@ module.exports = (appInfo) => {
/* 内置http服务 */
config.httpServer = {
enable: true, // 是否启用
enable: false, // 是否启用
port: 7071, // 默认端口(如果端口被使用,则随机获取一个)
cors: {
origin: "*" // 更多参数: https://www.npmjs.com/package/koa2-cors

View File

@@ -526,6 +526,48 @@ class ExampleController extends Controller {
// return uploadRes;
}
/**
* 检测http服务是否开启
*/
async checkHttpServer () {
const httpServerConfig = this.app.config.httpServer;
const url = httpServerConfig.protocol + httpServerConfig.host + ':' + httpServerConfig.port;
const data = {
enable: httpServerConfig.enable,
server: url
}
return data;
}
/**
* 一个http请求访问此方法
*/
async doHttpRequest () {
// http方法
const method = this.app.request.method;
// http get 参数
let params = this.app.request.query;
params = (params instanceof Object) ? JSON.parse(JSON.stringify(params)) : {};
// http post 参数
const body = this.app.request.body;
const httpInfo = {
method,
params,
body
}
console.log('httpInfo:', httpInfo);
if (!body.id) {
return false;
}
const dir = electronApp.getPath(body.id);
shell.openPath(dir);
return true;
}
}
module.exports = ExampleController;

View File

@@ -24,6 +24,8 @@ const ipcApiRoute = {
downloadApp: 'controller.example.downloadApp',
dbOperation: 'controller.example.dbOperation',
uploadFile: 'controller.example.uploadFile',
checkHttpServer: 'controller.example.checkHttpServer',
doHttpRequest: 'controller.example.doHttpRequest'
}
const specialIpcRoute = {

View File

@@ -62,10 +62,15 @@ export const constantRouterMap = [
component: () => import('@/views/base/theme/Index')
},
{
path: '/base/software/open',
path: '/base/software/index',
name: 'BaseSoftwareIndex',
component: () => import('@/views/base/software/Index')
},
{
path: '/base/httpserver/index',
name: 'BaseHttpServerIndex',
component: () => import('@/views/base/httpserver/Index')
},
{
path: '/base/system/index',
name: 'BaseSystemIndex',

View File

@@ -69,6 +69,12 @@ export default {
pageName: 'BaseSoftwareIndex',
params: {}
},
'menu_510' : {
icon: 'profile',
title: 'http服务',
pageName: 'BaseHttpServerIndex',
params: {}
},
'menu_900' : {
icon: 'profile',
title: '测试',

View File

@@ -47,17 +47,13 @@ export default {
};
},
created () {
//console.log('[sider] [created] 1');
},
mounted () {
//console.log('[sider] [mounted] 2');
this.menuHandle()
},
methods: {
menuHandle (e) {
this.current = e ? e.key : this.default_key;
//console.log('[sider] [methods] 3');
//console.log('[sider] [methods] current', this.current);
const linkInfo = this.menu[this.current]
console.log('[home] load page:', linkInfo.pageName);
this.$router.push({ name: linkInfo.pageName, params: linkInfo.params})

View File

@@ -0,0 +1,73 @@
<template>
<div id="app-base-httpserver">
<div class="one-block-1">
<span>
1. 内置http server服务
</span>
</div>
<div class="one-block-2">
<a-space>
<p>* 状态{{ currentStatus }}</p>
</a-space>
<p>* 地址{{ servicAddress }}</p>
</div>
<div class="one-block-1">
<span>
2. 发送http请求
</span>
</div>
<div class="one-block-2">
<a-space>
<a-button @click="sendRequest('pictures')"> 打开我的图片 </a-button>
</a-space>
</div>
</div>
</template>
<script>
import { ipcApiRoute, requestHttp } from '@/api/main'
export default {
data() {
return {
currentStatus: '关闭',
servicAddress: '无'
};
},
mounted () {
this.init();
},
methods: {
init () {
const self = this;
this.$ipcCall(ipcApiRoute.checkHttpServer, {}).then(r => {
if (r.enable) {
self.currentStatus = '开启';
self.servicAddress = r.server;
}
})
},
sendRequest (id) {
const params = {
id: id
}
requestHttp(ipcApiRoute.doHttpRequest, params).then(res => {
//console.log('res:', res)
})
},
}
};
</script>
<style lang="less" scoped>
#app-base-httpserver {
padding: 0px 10px;
text-align: left;
width: 100%;
.one-block-1 {
font-size: 16px;
padding-top: 10px;
}
.one-block-2 {
padding-top: 10px;
}
}
</style>

View File

@@ -7,7 +7,7 @@
</div>
<div class="one-block-2">
<a-space>
<p>当前状态{{ currentStatus }}</p>
<p>* 当前状态{{ currentStatus }}</p>
</a-space>
<p>* 拔掉电源使用电池供电</p>
<p>* 接入电源</p>

View File

@@ -6,8 +6,10 @@
</span>
</div>
<div class="one-block-2">
<a-button @click="exec(1)"> 点击 </a-button>
<a-button @click="exec2(1)"> 点击2 </a-button>
<a-space>
<a-button @click="exec(1)"> 点击 </a-button>
<a-button @click="exec2(1)"> 点击2 </a-button>
</a-space>
</div>
</div>
</template>