Skip to content

Commit c7612c5

Browse files
committed
Refactor the backend ( Api resources )
1 parent 5b7094a commit c7612c5

File tree

9 files changed

+101
-63
lines changed

9 files changed

+101
-63
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-cli-electron-template",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"private": true,
55
"scripts": {
66
"serve": "vue-cli-service serve",

src/backend.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/backend/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import resources from './resources'
2+
3+
export default resources

src/backend/requester.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import axios from 'axios'
2+
3+
// Full config: https://github.com/axios/axios#request-config
4+
/* axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
5+
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
6+
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; */
7+
8+
const config = {
9+
/* baseURL: process.env.baseURL || process.env.apiUrl || ""
10+
timeout: 60 * 1000, // Timeout
11+
withCredentials: true, // Check cross-site Access-Control
12+
headers: { 'Content-Type': 'application/json' } */
13+
}
14+
15+
const $axios = axios.create(config)
16+
17+
$axios.interceptors.request.use(
18+
function (config) {
19+
// Do something before request is sent
20+
return config
21+
},
22+
function (error) {
23+
// Do something with request error
24+
return Promise.reject(error)
25+
}
26+
)
27+
28+
// Add a response interceptor
29+
$axios.interceptors.response.use(
30+
function (response) {
31+
// Do something with response data
32+
return response
33+
},
34+
function (error) {
35+
// Do something with response error
36+
return Promise.reject(error)
37+
}
38+
)
39+
40+
export default $axios

src/backend/resources/api_v1.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import axios from '../requester'
2+
3+
// Api resources here
4+
export default {
5+
// Async await
6+
async demoGetV1 () {
7+
return (await axios.get('http://example.com/')).data
8+
},
9+
demoPostV1 () {
10+
return axios.post('http://example.com/').then(response => response.data)
11+
}
12+
}

src/backend/resources/api_v2.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import axios from '../requester'
2+
3+
// Api resources here
4+
export default {
5+
// Async await
6+
async demoGetV2 () {
7+
return (await axios.get('http://example.com/')).data
8+
},
9+
demoPostV2 () {
10+
return axios.post('http://example.com/').then(response => response.data)
11+
}
12+
}

src/backend/resources/common.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import axios from '../requester'
2+
3+
// Api resources here
4+
export default {
5+
// Async await
6+
async publicGet () {
7+
return (await axios.get('http://example.com/')).data
8+
},
9+
publicPost () {
10+
return axios.post('http://example.com/').then(response => response.data)
11+
}
12+
}

src/backend/resources/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import apiV1 from './api_v1'
2+
import apiV2 from './api_v2'
3+
import common from './common'
4+
5+
export default {
6+
apiV1,
7+
apiV2,
8+
...common // Deconstruction
9+
}

src/views/About.vue

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
<h2>Author: PurePeace</h2>
1616
<h2
1717
style="cursor: pointer; color: #FFF;"
18-
@click="()=>{const {shell} = require('electron'); shell.openExternal(link)}"
18+
@click="
19+
() => {
20+
const { shell } = require('electron')
21+
shell.openExternal(link)
22+
}
23+
"
1924
>
2025
Github: <span style="user-select: text;">{{ link }}</span>
2126
</h2>
2227
</div>
2328
</template>
2429

2530
<script>
26-
2731
export default {
2832
data () {
2933
return {
@@ -32,17 +36,13 @@ export default {
3236
}
3337
},
3438
methods: {
35-
sendRequest () {
39+
// Async request
40+
async sendRequest () {
41+
console.log(this.$backend)
3642
this.buttonText = 'requestDemo.requesting'
37-
this.$backend.demoGet(
38-
// 参数 / parameter
39-
).then(res => {
40-
console.log(res)
41-
this.buttonText = 'requestDemo.requestSuccess'
42-
}).catch(error => {
43-
console.log(error)
44-
this.buttonText = 'requestDemo.requestFail'
45-
})
43+
const res = await this.$backend.apiV2.demoGetV2()
44+
this.buttonText = 'requestDemo.requestSuccess'
45+
console.log(res)
4646
}
4747
}
4848
}

0 commit comments

Comments
 (0)