Skip to content

Commit 5eab5fd

Browse files
committed
feat: 将相关操作接口提取出来
1 parent 3323b06 commit 5eab5fd

File tree

9 files changed

+301
-156
lines changed

9 files changed

+301
-156
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { dataDb } from '@/dataStore'
2+
const collection = dataDb.defaults({ data: [] }).get('data')
3+
4+
/**
5+
* 清空缓存数据
6+
*/
7+
export function clearData () {
8+
return dataDb.set('data', []).write()
9+
}
10+
11+
/**
12+
* 获取数据
13+
*/
14+
export function getDatas () {
15+
return dataDb.get('data').value()
16+
}
17+
18+
/**
19+
* 添加数据
20+
* @param {Object} data 数据
21+
*/
22+
export function addData (data) {
23+
return collection.insert({ ...data }).write()
24+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { globalDb } from '@/dataStore'
2+
import store from '@/store'
3+
const appCollection = globalDb.get('apps')
4+
5+
/**
6+
* 获取应用列表
7+
*/
8+
export function getApps () {
9+
return JSON.parse(JSON.stringify(globalDb.get('apps').value()))
10+
}
11+
12+
/**
13+
* 通过id获取应用
14+
* @param {String} id 应用id
15+
*/
16+
export function getAppById (id) {
17+
return appCollection.find({ id: id }).value()
18+
}
19+
20+
/**
21+
* 通过id修改应用
22+
* @param {String} id 应用id
23+
* @param {Object} app 应用配置
24+
*/
25+
export function editAppById (id, app) {
26+
return appCollection.find({ id: id }).assign({ ...app }).write()
27+
}
28+
29+
/**
30+
* 新增应用
31+
* @param {Object} app 应用配置
32+
*/
33+
export function addApp (app) {
34+
return appCollection.insert({...app}).write()
35+
}
36+
37+
/**
38+
* 上传应用
39+
* @param {Object} app 应用配置
40+
*/
41+
export function uploadApp (app) {
42+
return fetch(`${store.state.ServerUrl}/apps`, {
43+
method: 'post',
44+
headers: {
45+
'Content-Type': 'application/json'
46+
},
47+
body: JSON.stringify({
48+
appName: app.appName,
49+
ruleConfig: app.ruleConfig,
50+
localId: app.id,
51+
type: app.type
52+
})
53+
}).then(res => res.json())
54+
}
55+
56+
/**
57+
* 拉取远程应用
58+
*/
59+
export function getRemoteApp () {
60+
return fetch(`${store.state.ServerUrl}/apps`).then(res => res.json())
61+
}
62+
63+
/**
64+
* 删除应用
65+
* @param {String} id 应用id
66+
*/
67+
export function deleteApp (id) {
68+
return appCollection.remove({ id: id }).write()
69+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { ruleDb } from '@/dataStore'
2+
3+
/**
4+
* 清除规则
5+
*/
6+
export function clearRule () {
7+
ruleDb.set('config', {}).write()
8+
ruleDb.set('contentUrls', {}).write()
9+
ruleDb.set('publishConfig', []).write()
10+
}
11+
12+
/**
13+
* 获取规则
14+
* @param {Object} obj 规则
15+
*/
16+
export function addRule (obj) {
17+
ruleDb.set('config', obj.config).write()
18+
ruleDb.set('contentUrls', obj.contentUrls).write()
19+
ruleDb.set('publishConfig', obj.publishConfig).write()
20+
}
21+
22+
/**
23+
* 获取config
24+
*/
25+
export function getConfig () {
26+
return ruleDb.get('config').value()
27+
}
28+
29+
/**
30+
* 获取内容页链接
31+
*/
32+
export function getContentUrls () {
33+
return ruleDb.get('contentUrls').value()
34+
}
35+
36+
/**
37+
* 存储内容页链接
38+
*/
39+
export function setContentUrls (urls) {
40+
return ruleDb.set('contentUrls', urls).write()
41+
}
42+
43+
const publishCollection = ruleDb.get('publishConfig')
44+
/**
45+
* 获取发布配置
46+
*/
47+
export function getPublishConfig () {
48+
return ruleDb.get('publishConfig').value()
49+
}
50+
51+
/**
52+
* 添加发布配置
53+
* @param {Object} obj 发布配置
54+
*/
55+
export function addPublish (obj) {
56+
return publishCollection.insert({ ...obj }).write()
57+
}
58+
59+
/**
60+
* 修改指定发布配置
61+
* @param {String} id 发布id
62+
* @param {Object} obj 发布配置
63+
*/
64+
export function editPublishById (id, obj) {
65+
return publishCollection.find({id: id}).assign({...obj}).write()
66+
}
67+
68+
/**
69+
* 删除指定发布配置
70+
* @param {String} id 发布id
71+
*/
72+
export function deletePublish (id) {
73+
return publishCollection.remove({ id: id }).write()
74+
}

src/renderer/views/homePage/index.vue

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
</div>
3636
</template>
3737
<script>
38-
import { globalDb, ruleDb, dataDb } from '@/dataStore'
39-
const appCollection = globalDb.get('apps')
38+
import { getApps, getAppById, editAppById, addApp, uploadApp, getRemoteApp, deleteApp } from '@/service/global.service'
39+
import { clearRule, addRule } from '@/service/rule.service'
40+
import { clearData } from '@/service/data.service'
4041
const { remote } = require('electron')
4142
const fs = require('fs')
4243
@@ -53,35 +54,22 @@ export default {
5354
},
5455
methods: {
5556
getAppList () {
56-
this.appList = JSON.parse(JSON.stringify(globalDb.get('apps').value()))
57+
this.appList = getApps()
5758
},
5859
handleCtrl ($event, app) {
5960
if ($event === 'delete') {
6061
this.deleteApp(app.id)
6162
} else if ($event === 'upload') {
62-
fetch(`${this.$store.state.ServerUrl}/apps`, {
63-
method: 'post',
64-
headers: {
65-
'Content-Type': 'application/json'
66-
},
67-
body: JSON.stringify({
68-
appName: app.appName,
69-
ruleConfig: app.ruleConfig,
70-
localId: app.id,
71-
type: app.type
63+
uploadApp(app).then(res => {
64+
remote.dialog.showMessageBox({
65+
type: 'info',
66+
title: '上传结果',
67+
message: res.message,
68+
buttons: ['ok']
7269
})
7370
})
74-
.then(res => res.json())
75-
.then(res => {
76-
remote.dialog.showMessageBox({
77-
type: 'info',
78-
title: '上传结果',
79-
message: res.message,
80-
buttons: ['ok']
81-
})
82-
})
8371
} else if ($event === 'export') {
84-
const details = globalDb.get('apps').find({ id: app.id }).value()
72+
const details = getAppById(app.id)
8573
const path = remote.dialog.showSaveDialog({
8674
title: '选择保存路径',
8775
filters: [{
@@ -113,10 +101,8 @@ export default {
113101
* 新建触发
114102
*/
115103
handleCreate (command) {
116-
ruleDb.set('config', {}).write()
117-
ruleDb.set('contentUrls', {}).write()
118-
ruleDb.set('publishConfig', []).write()
119-
dataDb.set('data', []).write()
104+
clearRule()
105+
clearData()
120106
if (command === 'rule') {
121107
this.$router.push('/ruleSetting')
122108
} else {
@@ -135,25 +121,18 @@ export default {
135121
buttons: ['ok', 'no']
136122
}, index => {
137123
if (index === 0) {
138-
fetch(`${this.$store.state.ServerUrl}/apps`)
139-
.then(res => res.json())
124+
getRemoteApp()
140125
.then(res => {
141126
res.data.forEach(v => {
142-
const localApp = appCollection
143-
.find({ id: v.localId })
144-
.value()
145-
console.log(localApp)
127+
const localApp = getAppById(v.localId)
146128
if (localApp) {
147129
const remoteApp = JSON.parse(JSON.stringify(v))
148130
remoteApp.id = remoteApp.localId
149-
appCollection
150-
.find({ id: v.localId })
151-
.assign({...remoteApp})
152-
.write()
131+
editAppById(v.localId, remoteApp)
153132
} else {
154133
const remoteApp = JSON.parse(JSON.stringify(v))
155134
remoteApp.id = remoteApp.localId
156-
appCollection.insert({...remoteApp}).write()
135+
addApp(remoteApp)
157136
this.getAppList()
158137
}
159138
})
@@ -179,13 +158,11 @@ export default {
179158
fs.readFile(filePath[0], 'utf8', (e, res) => {
180159
if (e) throw e
181160
const data = JSON.parse(res)
182-
appCollection
183-
.insert({
184-
appName: filePath[0].match(/([^\.\/\\]+)\.([a-z]+)$/i)[1],
185-
ruleConfig: data.ruleConfig,
186-
type: data.type
187-
})
188-
.write()
161+
addApp({
162+
appName: filePath[0].match(/([^\.\/\\]+)\.([a-z]+)$/i)[1],
163+
ruleConfig: data.ruleConfig,
164+
type: data.type
165+
})
189166
this.getAppList()
190167
})
191168
/* eslint-enable */
@@ -199,23 +176,19 @@ export default {
199176
buttons: ['ok', 'no']
200177
}, index => {
201178
if (index === 0) {
202-
appCollection
203-
.remove({ id: id })
204-
.write()
179+
deleteApp(id)
205180
this.getAppList()
206181
} else {
207182
}
208183
})
209184
},
210185
toDetails (data) {
211186
if (data.type === 'rule') {
212-
const details = globalDb.get('apps').find({ id: data.id }).value()
187+
const details = getAppById(data.id)
213188
if (details) {
214189
const obj = JSON.parse(details.ruleConfig)
215-
ruleDb.set('config', obj.config).write()
216-
ruleDb.set('contentUrls', obj.contentUrls).write()
217-
ruleDb.set('publishConfig', obj.publishConfig).write()
218-
dataDb.set('data', []).write()
190+
addRule(obj)
191+
clearData()
219192
this.$nextTick(_ => {
220193
this.$router.push(`/ruleSetting?id=${details.id}&appName=${details.appName}`)
221194
})

0 commit comments

Comments
 (0)