Skip to content

Commit ff5d152

Browse files
author
levy
committed
Merge branch 'dev'
2 parents 5b36da0 + 3471322 commit ff5d152

27 files changed

+347
-168
lines changed

bin/deploy/build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class BuildProcess {
6464
this.options = {
6565
cwd: path.join(releaseDir, templateName),
6666
env: {
67-
API_SERVER: 'https://mockapi.eolinker.com/IeZWjzy87c204a1f7030b2a17b00f3776ce0a07a5030a1b',
67+
API_SERVER: 'https://mockapi.eolinker.com/jttjNwp60fc1c9e944fdf1cc494b28a7ca4cfe66bbafee1',
6868
...process.env,
6969
...env,
7070
PUBLIC_PATH: `${process.env.PUBLIC_PATH || ''}/${this.folderName}`

bin/deploy/env.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"nuxt-admin": {
3-
"API_SERVER": "https://mockapi.eolinker.com/IeZWjzy87c204a1f7030b2a17b00f3776ce0a07a5030a1b",
3+
"API_SERVER": "https://mockapi.eolinker.com/jttjNwp60fc1c9e944fdf1cc494b28a7ca4cfe66bbafee1",
44
"APP_ID": "1204701543597604893"
55
}
66
}

docs/api.md

+61-16
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ this.$axios.$post('/security/users', body)
2323
## 使用
2424

2525
```javascript
26-
// 创建一个 API 资源,修改文件 src/api/index.js
26+
// 创建一个 API 资源,修改文件 src/services/basis.js
2727

2828
// 创建了一个菜单资源的 CRUD 接口方法
2929
+ export const menus = new Repository(`${DEEPEXI_CLOUD_TENANT}/${VERSION}/menus`)
@@ -33,23 +33,23 @@ this.$axios.$post('/security/users', body)
3333
// 在 page 中
3434
mounted() {
3535
// 获取资源的服务器路径
36-
this.$http.menus.uri()
36+
this.$services.basis.menus.uri()
3737
// 获取所有菜单资源,返回一个列表
38-
this.$http.menus.list()
38+
this.$services.basis.menus.list()
3939
// 获取某个菜单资源的详情
40-
this.$http.menus.detail(MENUS_ID)
40+
this.$services.basis.menus.detail(MENUS_ID)
4141
// 创建一个菜单资源
42-
this.$http.menus.create(payload)
42+
this.$services.basis.menus.create(payload)
4343
// 更新一个菜单资源
44-
this.$http.menus.update(MENUS_ID, payload)
44+
this.$services.basis.menus.update(MENUS_ID, payload)
4545
// 删除一个菜单资源
46-
this.$http.menus.delete(MENUS_ID)
46+
this.$services.basis.menus.delete(MENUS_ID)
4747
}
4848

4949
// 在 store 中
5050
export const actions = {
5151
async getMenus(store, payload) {
52-
const data = await this.$http.menus.detail(payload)
52+
const data = await this.$services.basis.menus.detail(payload)
5353
...
5454
}
5555
}
@@ -60,7 +60,7 @@ export const actions = {
6060
有些时候,后端的接口并不是严格遵循 RESTful 的最佳实践,这个时候就需要自己重新实现默认的方法
6161

6262
```javascript
63-
// 在 src/api/repository.js 中增加一个类,继承 Repository
63+
// 在 src/services/repository.js 中增加一个类,继承 Repository
6464
export class ExampleRepository extends Repository {
6565
constructor(resource, id) {
6666
super(resource)
@@ -78,13 +78,58 @@ export class ExampleRepository extends Repository {
7878
}
7979

8080
// 基于 ExampleRepository 创建一个 API
81-
export const example = new ExampleRepository('/example/api')
81+
export default new ExampleRepository('/example/api')
8282

8383
// 调用
84-
this.$http.example.uri(appId)
85-
this.$http.example.detail(id)
86-
this.$http.example.list()
87-
this.$http.example.create(payload)
88-
this.$http.example.update(appId, payload)
89-
this.$http.example.delete(id)
84+
this.$services.example.uri(appId)
85+
this.$services.example.detail(id)
86+
this.$services.example.list()
87+
this.$services.example.create(payload)
88+
this.$services.example.update(appId, payload)
89+
this.$services.example.delete(id)
9090
```
91+
## 注意
92+
93+
services 会根据文件名作为 scope,将该文件 export 出去的所有值挂在这个 scope 下。
94+
95+
比如 `basic.js`
96+
```js
97+
export const login = new Repository(`${SECURITY_CLOUD}/${VERSION}/login`)
98+
99+
export const userInfo = new Repository(`${SECURITY_CLOUD}/${VERSION}/token`)
100+
101+
export const menus = new Repository(`${SECURITY_CLOUD_TENANT}/${VERSION}/menus`)
102+
103+
export const subMenus = new Repository(
104+
`${SECURITY_CLOUD_TENANT}/${VERSION}/sub-menus`,
105+
)
106+
```
107+
108+
就会将它们挂在 `this.$services.basic` 下:
109+
```js
110+
this.$services.basic.menus.list()
111+
```
112+
113+
如果文件中有 `export default`,则会有些不同。
114+
115+
比如 `example.js`
116+
```js
117+
export default new Repository(`${VERSION}/example/api`)
118+
119+
// 使用
120+
this.$services.example.list()
121+
```
122+
123+
如果文件中既有 `export default` 又有 `export const`
124+
```js
125+
export default new Repository(`${VERSION}/example/api`)
126+
127+
export const other = new Repository(`${VERSION}/example/api/other`)
128+
129+
// 使用
130+
this.$services.example.list()
131+
this.$services.example.other.list()
132+
```
133+
134+
**需要注意**:导出的 `service` 名字不能与 `Repository` 的接口名字重复,接口名字列表可以在这里看到:
135+
https://github.com/femessage/create-nuxt-app/blob/dev/template/modules/service/services/common/repository.js#L8

template/framework-admin/store/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const mutations = {
104104

105105
export const actions = {
106106
async login({commit, dispatch}, {body, redirect = '/'}) {
107-
const userInfo = await this.$http.login.create(body)
107+
const userInfo = await this.$services.basic.login.create(body)
108108
commit('setUserInfo', userInfo.payload)
109109
dispatch('getHeaderMenu')
110110
dispatch('getSiderMenu')
@@ -114,7 +114,7 @@ export const actions = {
114114
},
115115

116116
async refresh({commit, dispatch}, token) {
117-
const userInfo = await this.$http.userInfo.list({
117+
const userInfo = await this.$services.basic.userInfo.list({
118118
params: {
119119
token,
120120
},
@@ -125,7 +125,7 @@ export const actions = {
125125
},
126126

127127
async getHeaderMenu({commit}) {
128-
const menus = await this.$http.menus.list({
128+
const menus = await this.$services.basic.menus.list({
129129
params: {
130130
appId: process.env.APP_ID,
131131
},
@@ -134,7 +134,7 @@ export const actions = {
134134
},
135135

136136
async getSiderMenu({commit}) {
137-
const menus = await this.$http.subMenus.list({
137+
const menus = await this.$services.basic.subMenus.list({
138138
params: {
139139
appId: process.env.APP_ID,
140140
},

template/framework-base/README.md

+25-18
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ yarn generate
7474
├── nuxt.config.js 框架配置文件
7575
├── package.json
7676
├── src 开发目录
77-
│   ├── api api 资源管理
78-
│   │   ├── index.js 统一入口,定义 RESTful API 资源
79-
│   │   ├── repository.js RESTful 生成类,可以继承实现满足业务需求
80-
│   │   └── serverList.js 统一管理服务路径和 API version
77+
│   ├── services api 资源管理
78+
│   │   ├── common service 公用的类库、BASE URL 定义等
79+
│   │   │   ├── api.js 统一管理服务路径和 API version
80+
│   │   │   └── repository.js RESTful 生成类,可以继承实现满足业务需求
81+
│   │   ├── basic.js 一些基本的 services
82+
│   │   ├── example.js 一个 example 的 service
83+
│   │   └── index.js 统一入口,会导出所有 services
8184
│   ├── assets 资源,包括样式文件与图片
8285
│   │   ├── global.less 全局样式类
8386
│   │   └── var.less 样式变量,支持less变量自动引入,即不用在less中import就能直接使用变量
@@ -130,28 +133,32 @@ Nuxt.js 会依据 `pages` 目录中的所有 `*.vue` 文件生成应用的路由
130133

131134
[推荐使用 service 层管理 API:](https://github.com/FEMessage/create-nuxt-app/blob/dev/docs/api.md)
132135

133-
1.`api/index.js` 中定义一个 API
136+
1.`services` 中定义一个 API
134137

138+
`src/services` 下新建一个 `example.js`
135139
```js
136-
// 创建了一个菜单资源的 RESTful API
137-
export const menus = new Repository(`${SERVICE}/${VERSION}/menus`)
140+
// 创建了一个 example 的 RESTful API
141+
import {Repository} from './common/repository'
142+
import {VERSION} from './common/api'
143+
144+
export default new Repository(`${VERSION}/example/api`)
138145
```
139146

140147
2.`*.vue``store/*.js``actions` 都可以调用
141148

142149
```js
143150
// 获取资源的服务器路径
144-
this.$http.menus.uri()
145-
// 获取所有菜单资源,返回一个列表
146-
this.$http.menus.list()
147-
// 获取某个菜单资源的详情
148-
this.$http.menus.detail(MENUS_ID)
149-
// 创建一个菜单资源
150-
this.$http.menus.create(payload)
151-
// 更新一个菜单资源
152-
this.$http.menus.update(MENUS_ID, payload)
153-
// 删除一个菜单资源
154-
this.$http.menus.delete(MENUS_ID)
151+
this.$services.example.uri()
152+
// 获取所有资源,返回一个列表
153+
this.$services.example.list()
154+
// 获取某个资源的详情
155+
this.$services.example.detail(ID)
156+
// 创建一个资源
157+
this.$services.example.create(payload)
158+
// 更新一个资源
159+
this.$services.example.update(ID, payload)
160+
// 删除一个资源
161+
this.$services.example.delete(ID)
155162
```
156163

157164
3. 如果接口是非标准的 RESTful API 可以参考此[文档](https://github.com/FEMessage/create-nuxt-app/blob/dev/docs/api.md#%E8%BF%9B%E9%98%B6)

template/framework-base/_package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"eslint-config-prettier": "6.15.0",
5858
"eslint-friendly-formatter": "4.0.1",
5959
"eslint-plugin-jest": "24.1.3",
60-
"eslint-plugin-nuxt": "^1.0.0",
60+
"eslint-plugin-nuxt": "1.0.0",
6161
"eslint-plugin-prettier": "3.1.4",
6262
"github-release-notes": "0.17.1",
6363
"husky": "1.3.1",

template/framework-base/nuxt.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ module.exports = {
233233
plugins: [
234234
{src: '~plugins/axios'},
235235
{src: '~plugins/filters'},
236-
{src: '~plugins/api'},
236+
{src: '~plugins/services'},
237237
<%_ if (template === 'mobile') { _%>
238238
{src: '~plugins/vant'},
239239
<%_ } else { _%>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const {parseServices} = require('@/utils')
2+
import {Repository} from '@/services/common/repository'
3+
4+
const moackAxios = {
5+
$get() {},
6+
$post() {},
7+
$put() {},
8+
$delete() {},
9+
}
10+
11+
describe('测试 utils.parseServices 函数', () => {
12+
test('普通处理 scope', () => {
13+
const serviceModules = {
14+
basis: {
15+
login: new Repository('login'),
16+
menus: new Repository('menus'),
17+
subMenus: new Repository('subMenus'),
18+
token: new Repository('token'),
19+
},
20+
}
21+
const expected = {
22+
basis: {
23+
login: serviceModules.basis.login.init(moackAxios),
24+
menus: serviceModules.basis.menus.init(moackAxios),
25+
subMenus: serviceModules.basis.subMenus.init(moackAxios),
26+
token: serviceModules.basis.token.init(moackAxios),
27+
},
28+
}
29+
expect(parseServices(serviceModules, moackAxios)).toMatchObject(expected)
30+
})
31+
32+
test('正常处理导出 default 时直接挂在 scope', () => {
33+
const serviceModules = {
34+
example: {
35+
default: new Repository('example'),
36+
},
37+
}
38+
const expected = {
39+
example: serviceModules.example.default.init(moackAxios),
40+
}
41+
expect(parseServices(serviceModules, moackAxios)).toMatchObject(expected)
42+
})
43+
44+
test('正常处理同时导出 default 和 const', () => {
45+
const serviceModules = {
46+
example: {
47+
default: new Repository('example'),
48+
other: new Repository('example/other'),
49+
},
50+
}
51+
const expected = {
52+
example: {
53+
...serviceModules.example.default.init(moackAxios),
54+
other: serviceModules.example.other.init(moackAxios),
55+
},
56+
}
57+
expect(parseServices(serviceModules, moackAxios)).toMatchObject(expected)
58+
})
59+
})

template/modules/service/api/index.js

-25
This file was deleted.

template/modules/service/api/serviceList.js

-17
This file was deleted.

template/modules/service/plugins/api.js

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @see https://github.com/FEMessage/create-nuxt-app/blob/dev/docs/api.md
3+
*/
4+
5+
import modules from '~/services'
6+
import {parseServices} from '~/utils'
7+
8+
export default (ctx, inject) => {
9+
const services = parseServices(modules, ctx.$axios)
10+
11+
inject('services', services)
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Repository} from './common/repository'
2+
import {VERSION, SECURITY_CLOUD, SECURITY_CLOUD_TENANT} from './common/api'
3+
4+
export const login = new Repository(`${SECURITY_CLOUD}/${VERSION}/login`)
5+
6+
export const userInfo = new Repository(`${SECURITY_CLOUD}/${VERSION}/token`)
7+
8+
export const menus = new Repository(`${SECURITY_CLOUD_TENANT}/${VERSION}/menus`)
9+
10+
export const subMenus = new Repository(
11+
`${SECURITY_CLOUD_TENANT}/${VERSION}/sub-menus`,
12+
)

0 commit comments

Comments
 (0)