feat(service): openapi 适配 · feige996/unibest@fee5f23 · GitHub
Skip to content

Commit fee5f23

Browse files
author
feige996
committed
feat(service): openapi 适配
1 parent 563b23c commit fee5f23

13 files changed

Lines changed: 120 additions & 764 deletions

File tree

openapi-ts-request.config.ts

Lines changed: 1 addition & 1 deletion

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"build:quickapp-webview-huawei": "uni build -p quickapp-webview-huawei",
8888
"build:quickapp-webview-union": "uni build -p quickapp-webview-union",
8989
"type-check": "vue-tsc --noEmit",
90-
"openapi-ts-request": "openapi-ts",
90+
"openapi": "openapi-ts",
9191
"prepare": "git init && husky && node ./scripts/create-base-files.js",
9292
"docker:prepare": "node ./scripts/create-base-files.js",
9393
"lint": "eslint",

src/http/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function http<T>(options: CustomRequestOptions) {
2424
// 状态码 2xx,参考 axios 的设计
2525
if (res.statusCode >= 200 && res.statusCode < 300) {
2626
// 2.1 处理业务逻辑错误
27-
const { code = 0, message = '', data = null } = res.data as IResponse<T>
27+
const { code = 0, message = '', msg = '', data = null } = res.data as IResponse<T>
2828
// 0和200当做成功都很普遍,这里直接兼容两者,见 ResultEnum
2929
if (code !== ResultEnum.Success0 && code !== ResultEnum.Success200) {
3030
throw new Error(`请求错误[${code}]:${message || msg}`)

src/pages/about/about.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isApp, isAppAndroid, isAppHarmony, isAppIOS, isAppPlus, isH5, isMpWeixi
33
import { LOGIN_PAGE } from '@/router/config'
44
import { useTokenStore } from '@/store'
55
import { tabbarStore } from '@/tabbar/store'
6+
import RequestOpenApiComp from './components/request-openapi.vue'
67
import RequestComp from './components/request.vue'
78
import VBindCss from './components/VBindCss.vue'
89
@@ -111,6 +112,7 @@ onShow(() => {
111112
<button class="mt-4 w-60 text-center" @click="setTabbarBadge">
112113
设置tabbarBadge
113114
</button>
115+
<RequestOpenApiComp />
114116
<RequestComp />
115117
<VBindCss />
116118
<view class="mb-6 h-1px bg-#eee" />
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<script lang="ts" setup>
2+
import type { UserItem } from '@/service'
3+
import { infoUsingGet } from '@/service/info'
4+
5+
const loading = ref(false)
6+
const error = ref<Error | null>(null)
7+
const data = ref<UserItem>()
8+
9+
async function getUserInfo() {
10+
try {
11+
loading.value = true
12+
const res = await (await infoUsingGet({})).promise
13+
console.log(res)
14+
data.value = res
15+
error.value = null
16+
}
17+
catch (err) {
18+
error.value = err as Error
19+
data.value = null
20+
}
21+
finally {
22+
loading.value = false
23+
}
24+
}
25+
const { data: data2, loading: loading2, run } = useRequest(() => infoUsingGet({}), {
26+
immediate: false,
27+
})
28+
</script>
29+
30+
<template>
31+
<view class="p-6 text-center">
32+
<view class="my-4 text-center">
33+
1)直接使用 openapi 生成的请求
34+
</view>
35+
<view class="my-4 text-center">
36+
<button type="primary" size="mini" class="w-160px" @click="getUserInfo">
37+
发送请求
38+
</button>
39+
<view class="text-xl">
40+
请求数据如下
41+
</view>
42+
<view class="text-green leading-8">
43+
{{ JSON.stringify(data) }}
44+
</view>
45+
</view>
46+
<view class="my-4 text-center">
47+
2)直接使用 openapi + useRequest 生成的请求
48+
</view>
49+
<view class="my-4 text-center">
50+
<button type="primary" size="mini" class="w-160px" @click="run">
51+
发送请求
52+
</button>
53+
<view class="text-xl">
54+
请求数据如下
55+
</view>
56+
<view class="text-green leading-8">
57+
{{ JSON.stringify(data2) }}
58+
</view>
59+
</view>
60+
</view>
61+
</template>

src/service/displayEnumLabel.ts

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

src/service/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable */
22
// @ts-ignore
33
export * from './types';
4-
export * from './displayEnumLabel';
54

6-
export * from './pet';
7-
export * from './store';
8-
export * from './user';
5+
export * from './listAll';
6+
export * from './info';

src/service/info.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-disable */
2+
// @ts-ignore
3+
import request from '@/http/vue-query';
4+
import { CustomRequestOptions } from '@/http/types';
5+
6+
import * as API from './types';
7+
8+
/** 用户信息 GET /user/info */
9+
export async function infoUsingGet({
10+
options,
11+
}: {
12+
options?: CustomRequestOptions;
13+
}) {
14+
return request<API.UserItem>('/user/info', {
15+
method: 'GET',
16+
...(options || {}),
17+
});
18+
}

src/service/listAll.ts

Lines changed: 18 additions & 0 deletions

src/service/pet.ts

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

0 commit comments

Comments
 (0)