af-mobile-client-vue3 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ // https://github.com/pengzhanbo/vite-plugin-mock-dev-server
2
+ import { defineMock } from 'vite-plugin-mock-dev-server'
3
+
4
+ export default defineMock([
5
+ {
6
+ url: '/api/demo/post',
7
+ method: 'POST',
8
+ body: (params) => {
9
+ return {
10
+ code: 200,
11
+ msg: '操作成功',
12
+ data: {
13
+ ...params,
14
+ id: Math.floor(Math.random() * 1000),
15
+ timestamp: new Date().toISOString(),
16
+ },
17
+ }
18
+ },
19
+ },
20
+ ])
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "af-mobile-client-vue3",
3
3
  "type": "module",
4
- "version": "1.1.1",
4
+ "version": "1.1.3",
5
5
  "description": "Vue + Vite component lib",
6
6
  "license": "MIT",
7
7
  "engines": {
@@ -16,6 +16,7 @@ import XFormView from '@af-mobile-client-vue3/views/component/XFormView/index.vu
16
16
  import XReportFormIframeView from '@af-mobile-client-vue3/views/component/XReportFormIframeView/index.vue'
17
17
  import XReportFormView from '@af-mobile-client-vue3/views/component/XReportFormView/index.vue'
18
18
  import XReportGridView from '@af-mobile-client-vue3/views/component/XReportGridView/index.vue'
19
+ import XRequestView from '@af-mobile-client-vue3/views/component/XRequestView/index.vue'
19
20
  import XSignatureView from '@af-mobile-client-vue3/views/component/XSignatureView/index.vue'
20
21
  import login from '@af-mobile-client-vue3/views/user/login/index.vue'
21
22
 
@@ -128,6 +129,11 @@ const routes: Array<RouteRecordRaw> = [
128
129
  name: 'GridView',
129
130
  component: GridView,
130
131
  },
132
+ {
133
+ path: '/Component/XRequestView',
134
+ name: 'XRequestView',
135
+ component: XRequestView,
136
+ },
131
137
  ],
132
138
  },
133
139
  {
@@ -33,7 +33,7 @@ export function getConfigByName(configName: string, callback: Function, serviceN
33
33
 
34
34
  export async function getConfigByNameAsync(configName: string, serviceName = import.meta.env.VITE_APP_SYSTEM_NAME): Promise<any> {
35
35
  return new Promise((resolve, reject) => {
36
- indexedDB.getByWeb(configName, `/${serviceName}/${commonApi.getConfig}`, { configName }, resolve, reject)
36
+ indexedDB.getByWeb(configName, `/${serviceName}/${commonApi.getConfig}`, { configName }, resolve, null)
37
37
  })
38
38
  }
39
39
 
@@ -1,8 +1,16 @@
1
1
  import { post } from '@af-mobile-client-vue3/services/restTools'
2
2
 
3
- export function getUserPermissions(userid: string) {
4
- return post(`/af-system/search`, {
3
+ export async function getUserPermissions(userid: string) {
4
+ interface permissions {
5
+ name: string
6
+ }
7
+
8
+ const res = await post<permissions[]>(`/af-system/search`, {
5
9
  source: 'this.getRights().where(row.getType()==$function$ && row.getPath($name$).indexOf($功能权限$) != -1)',
6
10
  userid,
7
11
  })
12
+
13
+ return res.map((row) => {
14
+ return row.name
15
+ })
8
16
  }
@@ -1,4 +1,3 @@
1
- import type { BasicResponseModel } from '@af-mobile-client-vue3/api/user'
2
1
  import { http } from '@af-mobile-client-vue3/utils/http'
3
2
 
4
3
  /**
@@ -6,8 +5,8 @@ import { http } from '@af-mobile-client-vue3/utils/http'
6
5
  * @param url 请求地址
7
6
  * @param params 路径参数
8
7
  */
9
- export function get(url: string, params?: any) {
10
- return http.request<BasicResponseModel>({
8
+ export function get<T = any>(url: string, params?: any): Promise<T> {
9
+ return http.request<T>({
11
10
  url,
12
11
  method: 'GET',
13
12
  params,
@@ -19,16 +18,21 @@ export function get(url: string, params?: any) {
19
18
  * @param url 请求地址
20
19
  * @param data 请求参数
21
20
  */
22
- export function post(url: string, data: any) {
23
- return http.request<BasicResponseModel>({
21
+ /**
22
+ * POST请求
23
+ * @param url 请求地址
24
+ * @param data 请求参数
25
+ */
26
+ export function post<T = any>(url: string, data: any): Promise<T> {
27
+ return http.request <T>({
24
28
  url,
25
29
  method: 'POST',
26
30
  data,
27
31
  })
28
32
  }
29
33
 
30
- export function postWithConfig(url: string, data: any, config: any) {
31
- return http.request<BasicResponseModel>({
34
+ export function postWithConfig<T = any>(url: string, data: any, config: any): Promise<T> {
35
+ return http.request<T>({
32
36
  url,
33
37
  method: 'POST',
34
38
  data,
@@ -42,8 +46,8 @@ export function postWithConfig(url: string, data: any, config: any) {
42
46
  * @param data 查询参数
43
47
  * @param config 额外的配置项(如自定义头信息等)
44
48
  */
45
- export function del(url: string, data?: any, config?: any) {
46
- return http.request<BasicResponseModel>({
49
+ export function del<T = any>(url: string, data?: any, config?: any): Promise<T> {
50
+ return http.request<T>({
47
51
  url,
48
52
  method: 'DELETE',
49
53
  data,
@@ -30,7 +30,7 @@ interface IUserState {
30
30
  token?: string
31
31
  userInfo: UserInfo
32
32
  lastUpdateTime: number
33
- permissions: [{ id: string, operation: string[] }]
33
+ permissions: string[]
34
34
  roles: [{ id: string, operation: string[] }]
35
35
  routesConfig: Array<any>
36
36
  single: Array<ExtraRouteRecordRaw>
@@ -119,13 +119,13 @@ export const useUserStore = defineStore('app-user', () => {
119
119
  userState.value.single.push(page)
120
120
  }
121
121
  }
122
- const setPermissions = (permissions: [{ id: string, operation: string[] }]) => {
122
+ const setPermissions = (permissions: []) => {
123
123
  userState.value.permissions = permissions
124
124
  Storage.set(APP_PERMISSIONS_KEY, permissions)
125
125
  }
126
126
  const setRoles = (roles: [{ id: string, operation: string[] }]) => {
127
127
  userState.value.roles = roles
128
- Storage.set(APP_PERMISSIONS_KEY, roles)
128
+ Storage.set(APP_ROLES_KEY, roles)
129
129
  }
130
130
  const setRoutesConfig = (routesConfig: Array<any>) => {
131
131
  userState.value.routesConfig = routesConfig
@@ -49,16 +49,17 @@ class Http {
49
49
  Http.axiosInstance.interceptors.response.use(
50
50
  async (response: AxiosResponse) => {
51
51
  const compatible = import.meta.env.VITE_APP_COMPATIBLE
52
- if (compatible !== 'V4')
52
+ if (compatible !== 'V4') {
53
53
  return response.data
54
+ }
54
55
  // 与后端协定的返回字段
55
56
  const { code, msg, data } = response.data
56
57
  // 临时向v3请求上传服务,因为没有code,未来会改
57
- if (code === undefined && response.data.id !== undefined)
58
+ if (code === undefined) {
58
59
  return response.data
60
+ }
59
61
  // 判断请求是否成功
60
- const isSuccess
61
- = code === ResultEnum.SUCCESS
62
+ const isSuccess = code === ResultEnum.SUCCESS
62
63
  if (isSuccess) {
63
64
  return data
64
65
  }
@@ -0,0 +1,234 @@
1
+ <script setup lang="ts">
2
+ import { queryProse } from '@af-mobile-client-vue3/api/mock'
3
+ import { getUserPermissions } from '@af-mobile-client-vue3/services/api/search'
4
+ import { post } from '@af-mobile-client-vue3/services/restTools'
5
+ import useUserStore from '@af-mobile-client-vue3/stores/modules/user'
6
+ import { showToast, Button as VanButton, Cell as VanCell, CellGroup as VanCellGroup } from 'vant'
7
+ import { reactive, ref } from 'vue'
8
+ import { useRouter } from 'vue-router'
9
+
10
+ // 路由
11
+ const router = useRouter()
12
+ // 用户store
13
+ const userStore = useUserStore()
14
+
15
+ // 数据定义
16
+ const requestTypes = ref('GET, POST, DELETE')
17
+ const getResult = ref('')
18
+ const postResult = ref('')
19
+ const permissionsResult = ref('')
20
+ const storePermissionsResult = ref('')
21
+ const postData = reactive({
22
+ name: '',
23
+ value: '',
24
+ })
25
+
26
+ // 返回上一页
27
+ function onClickLeft() {
28
+ router.back()
29
+ }
30
+
31
+ // 发送GET请求示例
32
+ async function handleGetRequest() {
33
+ try {
34
+ // 这里使用项目中的模拟API
35
+ const result = await queryProse()
36
+ getResult.value = JSON.stringify(result, null, 2)
37
+ showToast('请求成功')
38
+ }
39
+ catch (error) {
40
+ console.error('GET请求失败:', error)
41
+ showToast('请求失败')
42
+ }
43
+ }
44
+
45
+ // 获取用户权限
46
+ async function handleGetPermissions() {
47
+ try {
48
+ const userInfo = userStore.getUserInfo()
49
+ if (!userInfo || !userInfo.id) {
50
+ showToast('请先登录')
51
+ return
52
+ }
53
+
54
+ // 从API获取权限
55
+ const permissions = await getUserPermissions(userInfo.id.toString())
56
+ permissionsResult.value = JSON.stringify(permissions, null, 2)
57
+
58
+ // 从Store获取权限
59
+ const storePermissions = userStore.getPermissions()
60
+ storePermissionsResult.value = JSON.stringify(storePermissions, null, 2)
61
+
62
+ showToast('获取权限成功')
63
+ }
64
+ catch (error) {
65
+ console.error('获取权限失败:', error)
66
+ showToast('获取权限失败')
67
+ }
68
+ }
69
+
70
+ // 定义请求数据类型
71
+ interface RequestData {
72
+ name: string
73
+ value: string
74
+ }
75
+
76
+ // 定义返回结果类型
77
+ interface ResponseData {
78
+ code: number
79
+ msg: string
80
+ data: any
81
+ }
82
+
83
+ // 发送POST请求示例
84
+ async function handlePostRequest() {
85
+ try {
86
+ // 这里演示使用通用post方法发送请求
87
+ // 使用泛型指定返回类型
88
+ const result = await post<ResponseData>('/api/demo/post', {
89
+ name: '测试',
90
+ value: '测试',
91
+ })
92
+ postResult.value = JSON.stringify(result, null, 2)
93
+ showToast('请求成功')
94
+ }
95
+ catch (error) {
96
+ console.error('POST请求失败:', error)
97
+ showToast('请求失败')
98
+ }
99
+ }
100
+
101
+ // 类型定义展示
102
+ const typeDefinitions = `
103
+
104
+ // 使用泛型扩展特定业务数据类型
105
+ import { get } from '@af-mobile-client-vue3/services/restTools'
106
+
107
+ interface UserInfo {
108
+ id: string
109
+ name: string
110
+ roles: string[]
111
+ }
112
+
113
+ // 调用示例
114
+ const userInfo = await get<UserInfo>('/api/user/info')
115
+ // userInfo 类型为 UserInfo,而非 BasicResponseModel<UserInfo>
116
+ // 不用使用 userInfo.data 获取数据
117
+ // 直接使用 userInfo 即可
118
+ `.trim()
119
+ </script>
120
+
121
+ <template>
122
+ <div class="request-view">
123
+ <div class="container">
124
+ <VanCellGroup title="API请求演示">
125
+ <VanCell title="基本请求类型" :value="requestTypes" />
126
+
127
+ <!-- GET请求部分 -->
128
+ <div class="request-section">
129
+ <h3>GET请求示例</h3>
130
+ <VanButton type="primary" size="small" @click="handleGetRequest">
131
+ 发送GET请求
132
+ </VanButton>
133
+ <div v-if="getResult" class="result-box">
134
+ <h4>响应结果:</h4>
135
+ <pre>{{ getResult }}</pre>
136
+ </div>
137
+ </div>
138
+
139
+ <!-- POST请求部分 -->
140
+ <div class="request-section">
141
+ <h3>POST请求示例</h3>
142
+
143
+ <VanButton type="primary" size="small" @click="handlePostRequest">
144
+ 发送POST请求
145
+ </VanButton>
146
+ <div v-if="postResult" class="result-box">
147
+ <h4>响应结果:</h4>
148
+ <pre>{{ postResult }}</pre>
149
+ </div>
150
+ </div>
151
+
152
+ <!-- 用户权限部分 -->
153
+ <div class="request-section">
154
+ <h3>用户权限获取</h3>
155
+ <VanButton type="primary" size="small" @click="handleGetPermissions">
156
+ 获取用户权限
157
+ </VanButton>
158
+
159
+ <div v-if="permissionsResult" class="result-box">
160
+ <h4>API请求权限结果:</h4>
161
+ <pre>{{ permissionsResult }}</pre>
162
+ </div>
163
+
164
+ <div v-if="storePermissionsResult" class="result-box">
165
+ <h4>Store缓存权限结果:</h4>
166
+ <pre>{{ storePermissionsResult }}</pre>
167
+ </div>
168
+ </div>
169
+
170
+ <!-- 类型定义说明 -->
171
+ <div class="code-section">
172
+ <h3>类型定义</h3>
173
+ <pre>{{ typeDefinitions }}</pre>
174
+ </div>
175
+ </VanCellGroup>
176
+ </div>
177
+ </div>
178
+ </template>
179
+
180
+ <style scoped>
181
+ .request-view {
182
+ height: 100%;
183
+ display: flex;
184
+ flex-direction: column;
185
+ }
186
+
187
+ .container {
188
+ flex: 1;
189
+ padding: 16px;
190
+ overflow-y: auto;
191
+ }
192
+
193
+ .request-section {
194
+ margin: 16px 0;
195
+ padding: 16px;
196
+ background-color: #f7f8fa;
197
+ border-radius: 8px;
198
+ }
199
+
200
+ .result-box {
201
+ margin-top: 16px;
202
+ padding: 12px;
203
+ background-color: #ffffff;
204
+ border-radius: 4px;
205
+ border: 1px solid #ebedf0;
206
+ }
207
+
208
+ pre {
209
+ white-space: pre-wrap;
210
+ word-break: break-all;
211
+ font-size: 12px;
212
+ font-family: Consolas, Monaco, monospace;
213
+ background-color: #f5f7fa;
214
+ padding: 8px;
215
+ border-radius: 4px;
216
+ overflow-x: auto;
217
+ }
218
+
219
+ .code-section {
220
+ margin: 16px 0;
221
+ }
222
+
223
+ h3 {
224
+ margin: 0 0 12px;
225
+ font-size: 15px;
226
+ color: #323233;
227
+ }
228
+
229
+ h4 {
230
+ margin: 0 0 8px;
231
+ font-size: 14px;
232
+ color: #323233;
233
+ }
234
+ </style>
@@ -54,6 +54,10 @@ const list = ref([
54
54
  name: 'XOlmap 地图组件',
55
55
  to: '/Component/XOlMapView',
56
56
  },
57
+ {
58
+ name: '请求示例',
59
+ to: '/Component/XRequestView',
60
+ },
57
61
  ])
58
62
 
59
63
  function cleanConfigCache() {
@@ -158,7 +158,7 @@ function closeWindows() {
158
158
  }
159
159
  }
160
160
 
161
- function afterGeneral(result) {
161
+ async function afterGeneral(result) {
162
162
  const user: UserInfo = {
163
163
  id: result.id,
164
164
  username: result.ename,
@@ -171,7 +171,7 @@ function afterGeneral(result) {
171
171
  userState.setUserInfo(user)
172
172
  // 如果result中没有返回 权限 需要主动获取权限列表
173
173
  if (!result.permissions) {
174
- result.permissions = getUserPermissions(result.id)
174
+ result.permissions = await getUserPermissions(result.id)
175
175
  }
176
176
  userState.setPermissions(result.permissions)
177
177
  userState.setRoles([{ id: 'admin', operation: ['add', 'edit', 'delete'] }])