@zfqh/uniapp 0.1.2 → 0.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.
@@ -28,7 +28,7 @@ import { getTencentToken, getDictData } from '@zfqh/uniapp/lib/apis/core';
28
28
  - `GET` 会把 data 作为 query params。
29
29
  - `POST`、`PUT`、`DELETE` 会把 data 作为请求体或方法参数传给底层请求实例。
30
30
  - URL 中的 `:id`、`/:id` 会由 `formatURL` 使用 data 同名字段替换,并从 data 中删除该字段。
31
- - 响应拦截器要求后端成功码为 `code == 0`;失败时会按配置弹 toast,并在 401 时触发 `uni.$store.dispatch('logout')`。
31
+ - 响应拦截器要求后端成功码为 `code == 0`;失败时会按配置弹 toast,并在 401 时触发 `store.dispatch('logout')`。
32
32
 
33
33
  ## 接口清单
34
34
 
@@ -26,9 +26,9 @@ const request = uni.$uv.http;
26
26
 
27
27
  | Header | 来源 | 默认值 |
28
28
  | --- | --- | --- |
29
- | `Authorization` | `uni.$store.state.login.info.token` | 无 |
30
- | `Tenant-Id` | `uni.$store.state.login.info.tenantId` | `1` |
31
- | `X-Language` | `uni.$store.state.system.info.appLanguage` 映射到 `LANGUAGE` | 无 |
29
+ | `Authorization` | `store.state.login.info.token` | 无 |
30
+ | `Tenant-Id` | `store.state.login.info.tenantId` | `1` |
31
+ | `X-Language` | `store.state.system.info.appLanguage` 映射到 `LANGUAGE` | 无 |
32
32
  | `Channel` | `query.channel` 或 `config.app.channel` | `config.app.channel` |
33
33
 
34
34
  业务请求的 `baseURL` 来自 `config.api.baseUrl`。
@@ -274,7 +274,7 @@ GET('/app-api/demo/check', {}, {
274
274
  | 阶段 | 行为 |
275
275
  | --- | --- |
276
276
  | request | 当 `log.request` 为 true 时打印请求 URL 和配置。 |
277
- | request | 如果存在 `uni.$store.state.login.info.token`,注入 `Authorization: Bearer <token>`。 |
277
+ | request | 如果存在 `store.state.login.info.token`,注入 `Authorization: Bearer <token>`。 |
278
278
  | response | 当 `log.response` 为 true 时打印响应数据。 |
279
279
  | response | 当 `data.code != 0` 时将其作为错误响应处理。 |
280
280
  | error | 补齐非标准错误的 `response`、`status`、`data.msg`。 |
@@ -283,11 +283,11 @@ GET('/app-api/demo/check', {}, {
283
283
  `401` 处理依赖:
284
284
 
285
285
  ```ts
286
- uni.$store.state.login.info.token
287
- uni.$store.dispatch('logout')
286
+ store.state.login.info.token
287
+ store.dispatch('logout')
288
288
  ```
289
289
 
290
- 请求拦截器属于普通 TS 模块,不在 Vue 组件 setup 上下文中,因此这里使用 `uni.$store`。如果是在组件或 hooks 中读取同一份登录态,请使用:
290
+ 请求拦截器属于普通 TS 模块,不在 Vue 组件 setup 上下文中,因此直接引入 store 实例。如果是在组件或 hooks 中读取同一份登录态,请使用:
291
291
 
292
292
  ```ts
293
293
  import { useStore } from 'vuex';
@@ -322,8 +322,8 @@ const request = uni.$uv.http;
322
322
 
323
323
  | Header | 来源 |
324
324
  | --- | --- |
325
- | `Tenant-Id` | `uni.$store.state.login.info.tenantId`,默认 `1`。 |
326
- | `X-Language` | `uni.$store.state.system.info.appLanguage` 映射到 `LANGUAGE`。 |
325
+ | `Tenant-Id` | `store.state.login.info.tenantId`,默认 `1`。 |
326
+ | `X-Language` | `store.state.system.info.appLanguage` 映射到 `LANGUAGE`。 |
327
327
  | `Channel` | `query.channel`,没有 query 渠道时回退到 `config.app.channel`。 |
328
328
 
329
329
  业务请求的 `baseURL` 来自:
@@ -283,7 +283,6 @@ defineConfig({
283
283
 
284
284
  ```ts
285
285
  import { getTencentToken } from '@zfqh/uniapp/lib/apis';
286
- import store from '@/store';
287
286
 
288
287
  defineConfig({
289
288
  upload: {
@@ -296,7 +295,7 @@ defineConfig({
296
295
  create_time: data.createTime,
297
296
  expire_time: data.expirationTime,
298
297
  bucket: data.bucket,
299
- region: store.state.storeConfig.isGlobalAccelerate ? 'accelerate' : data.region,
298
+ region: uni.$store.state.storeConfig.isGlobalAccelerate ? 'accelerate' : data.region,
300
299
  };
301
300
  },
302
301
  },
@@ -10,7 +10,7 @@ import { useStore } from 'vuex';
10
10
  const store = useStore();
11
11
  ```
12
12
 
13
- `uni.$store` 主要用于请求、导航、权限、webview 消息等普通模块。
13
+ 普通 TS 模块中使用 `uni.$store`。
14
14
 
15
15
  | Hook | 说明 |
16
16
  | --- | --- |
@@ -44,7 +44,7 @@ import { useOverlay, usePopup, useVersion, useI18n } from '@zfqh/uniapp';
44
44
 
45
45
  ## Store 用法
46
46
 
47
- Hooks 运行在组件 setup 上下文中,需要读取 Vuex 时请使用 `useStore()`,不要直接读取 `uni.$store`:
47
+ Hooks 运行在组件 setup 上下文中,需要读取 Vuex 时请使用 `useStore()`:
48
48
 
49
49
  ```ts
50
50
  import { computed } from 'vue';
@@ -57,7 +57,7 @@ export function useUserName() {
57
57
  }
58
58
  ```
59
59
 
60
- `uni.$store` 主要给请求、导航、权限、webview 消息等非组件上下文的普通模块使用。完整约定见 [Store 使用位置](./../../guide/project-frame#store-使用位置)。
60
+ 普通 TS 模块中使用 `uni.$store`。完整约定见 [Store 使用位置](./../../guide/project-frame#store-使用位置)。
61
61
 
62
62
  ## useOverlay
63
63
 
@@ -172,7 +172,7 @@ const { t, locale } = useI18n();
172
172
 
173
173
  | 平台 | 行为 |
174
174
  | --- | --- |
175
- | APP-NVUE | 使用内部 `inject('t')` 和 `showLocale`。 |
175
+ | APP-NVUE | 使用内部 `inject('t')` 和 `getLocaleMessage`。 |
176
176
  | 其他平台 | 透传 `vue-i18n` 的 `useI18n`。 |
177
177
 
178
178
  ```vue
package/docs/api/index.md CHANGED
@@ -21,14 +21,19 @@ app.use(uniapp);
21
21
  安装逻辑:
22
22
 
23
23
  - 小程序端混入 `deep`。
24
- - 将 `app.config.globalProperties.$store` 挂到 `uni.$store`。
25
24
 
26
- `uni.$store` 用于请求、导航、权限、webview 消息等非组件上下文的普通模块。组件的 Composition API 和 hooks 中请使用 Vuex 官方入口:
25
+ Store 使用入口请按上下文区分:
27
26
 
28
27
  ```ts
28
+ // Composition API / <script setup>
29
29
  import { useStore } from 'vuex';
30
-
31
30
  const store = useStore();
31
+
32
+ // Options API 组件
33
+ this.$store
34
+
35
+ // 普通 TS 模块
36
+ uni.$store
32
37
  ```
33
38
 
34
39
  ## Hooks
@@ -16,13 +16,12 @@ App.vue 全局生命周期 mixin。发布路径:`@zfqh/uniapp/lib/mixins/app`
16
16
 
17
17
  ```ts
18
18
  import app from '@zfqh/uniapp/lib/mixins/app';
19
- import store from '@/store';
20
19
 
21
20
  export default {
22
21
  mixins: [app],
23
22
  onLaunch() {
24
23
  if (store.getters.isLogin) {
25
- store.dispatch('getInfo');
24
+ this.$store.dispatch('getInfo');
26
25
  }
27
26
  },
28
27
  };
@@ -20,8 +20,6 @@ const store = createStore({
20
20
  app,
21
21
  },
22
22
  });
23
-
24
- uni.$store = store;
25
23
  ```
26
24
 
27
25
  ```ts
@@ -2,7 +2,7 @@
2
2
 
3
3
  模块发布路径为 `@zfqh/uniapp/lib/modules`。根入口没有直接导出这些模块;如果业务项目要复用,请深度引入 `@zfqh/uniapp/lib/modules/&lt;name&gt;` 或从 `@zfqh/uniapp/lib/modules` 取具名导出。
4
4
 
5
- 这些模块默认假设项目使用 Vuex 4,并且插件安装后会把 Vuex store 暴露到 `uni.$store`,请求、导航、webview 消息等普通模块会读取它。组件和 hooks 中读取 Vuex 时仍应使用 `useStore()` 或 `this.$store`。
5
+ 这些模块默认假设项目使用 Vuex 4。请求、导航、webview 消息等普通模块会从 store 中读取状态。组件和 hooks 中读取 Vuex 时仍应使用 `useStore()` 或 `this.$store`。
6
6
 
7
7
  ## 基本使用
8
8
 
@@ -205,7 +205,6 @@ defineConfig({
205
205
 
206
206
  ```ts
207
207
  import { getTencentToken } from '@zfqh/uniapp/lib/apis';
208
- import store from '@/store';
209
208
 
210
209
  defineConfig({
211
210
  upload: {
@@ -218,7 +217,7 @@ defineConfig({
218
217
  create_time: data.createTime,
219
218
  expire_time: data.expirationTime,
220
219
  bucket: data.bucket,
221
- region: store.state.storeConfig.isGlobalAccelerate ? 'accelerate' : data.region,
220
+ region: uni.$store.state.storeConfig.isGlobalAccelerate ? 'accelerate' : data.region,
222
221
  };
223
222
  },
224
223
  },
@@ -12,7 +12,7 @@
12
12
  | [cos](./cos) | 腾讯云 COS 上传与图片压缩 URL。 |
13
13
  | [debounce](./debounce) | 防抖类和默认实例。 |
14
14
  | [file](./file) | URL、dataURL、base64、File、本地路径互转。 |
15
- | [locale](./locale) | 当前语言、回退语言和 nvue 文案读取。 |
15
+ | [locale](./locale) | 当前语言、回退语言、vue-i18n 实例创建和 nvue 文案读取。 |
16
16
  | [message](./message) | webview 与宿主环境通信的消息类型常量。 |
17
17
  | [navigate](./navigate) | 页面跳转、登录拦截、tabBar、webview、自定义协议。 |
18
18
  | [pages](./pages) | 根据页面配置生成页面集合。 |
@@ -1,32 +1,68 @@
1
- # 多语言辅助
2
-
3
- ::: tip 工具模块
4
- `@zfqh/uniapp/lib/utils/locale`
5
- :::
6
-
7
- 提供当前语言、回退语言和 nvue 可用的简易文案读取方法。
8
-
9
- ## 平台兼容性
10
-
11
- | App | H5 | 微信小程序 | 其他小程序 | WebView |
12
- | --- | --- | --- | --- | --- |
13
- | √ | √ | √ | 视平台 API 而定 | 视运行容器而定 |
14
-
15
- ## 基本使用
16
-
17
- ```ts
18
- import { defaultLocale, fallbackLocale, showLocale } from '@zfqh/uniapp';
19
- ```
20
-
21
- 具体方法、参数语义和返回值以发布包类型为准;下方表格按 `@zfqh/uniapp/lib/utils/locale` 的导出整理。
22
-
23
- ## API
24
-
25
- 发布路径:`@zfqh/uniapp/lib/utils/locale`
26
-
27
- | 名称 | 签名/声明 | 说明 |
28
- | --- | --- | --- |
29
- | `defaultLocale` | `export const defaultLocale = uni.getLocale();` | 默认语言。 |
30
- | `fallbackLocale` | `export const fallbackLocale = 'zh-Hans';` | 回退语言。 |
31
- | `showLocale` | `export function showLocale(key, values) {` | nvue 场景下的简易 i18n 文案获取函数。 |
32
-
1
+ # 多语言辅助
2
+
3
+ ::: tip 工具模块
4
+ `@zfqh/uniapp/lib/utils/locale`
5
+ :::
6
+
7
+ 提供当前语言、回退语言、vue-i18n 实例创建和 nvue 可用的简易文案读取方法。
8
+
9
+ ## 平台兼容性
10
+
11
+ | App | H5 | 微信小程序 | 其他小程序 | WebView |
12
+ | --- | --- | --- | --- | --- |
13
+ | √ | √ | √ | 视平台 API 而定 | 视运行容器而定 |
14
+
15
+ ## 基本使用
16
+
17
+ ```ts
18
+ import { defaultLocale, fallbackLocale, createI18n, getLocaleMessage } from '@zfqh/uniapp';
19
+ ```
20
+
21
+ 具体方法、参数语义和返回值以发布包类型为准;下方表格按 `@zfqh/uniapp/lib/utils/locale` 的导出整理。
22
+
23
+ ## 创建 vue-i18n 实例
24
+
25
+ 在 `locale/index.js` 中调用 `createI18n` 创建实例并导出:
26
+
27
+ ```js
28
+ import { createI18n, defaultLocale, fallbackLocale } from '@zfqh/uniapp';
29
+ import messages from './messages';
30
+
31
+ export const i18n = createI18n({
32
+ locale: defaultLocale,
33
+ fallbackLocale,
34
+ messages,
35
+ });
36
+ ```
37
+
38
+ `messages.js` 按语言键组织翻译文件:
39
+
40
+ ```js
41
+ import zhHans from './zh-Hans.json';
42
+ import en from './en.json';
43
+
44
+ export default {
45
+ 'zh-Hans': zhHans,
46
+ en,
47
+ };
48
+ ```
49
+
50
+ 实例创建后可在 App 启动时注册:
51
+
52
+ ```js
53
+ import { i18n } from './locale';
54
+
55
+ const app = createApp(App);
56
+ app.use(i18n);
57
+ ```
58
+
59
+ ## API
60
+
61
+ 发布路径:`@zfqh/uniapp/lib/utils/locale`
62
+
63
+ | 名称 | 签名/声明 | 说明 |
64
+ | --- | --- | --- |
65
+ | `defaultLocale` | `export const defaultLocale = uni.getLocale();` | 默认语言。 |
66
+ | `fallbackLocale` | `export const fallbackLocale = 'zh-Hans';` | 回退语言。 |
67
+ | `createI18n` | `export function createI18n(opts: I18nOptions)` | 创建 vue-i18n 实例,返回实例。 |
68
+ | `getLocaleMessage` | `export function getLocaleMessage(key: string): string` | 通过 key 获取当前语言文案,nvue 场景下的简易 i18n 文案读取。未匹配时返回 key 本身。 |
@@ -18,7 +18,7 @@
18
18
  import { navigate } from '@zfqh/uniapp';
19
19
  ```
20
20
 
21
- 导航工具脱离组件 setup 上下文,内部会通过 `uni.$store.getters.isLogin` 判断登录态。组件或 hooks 中判断登录态时请使用 `useStore()`。
21
+ 导航工具脱离组件 setup 上下文,内部会通过 `store.getters.isLogin` 判断登录态。组件或 hooks 中判断登录态时请使用 `useStore()`。
22
22
 
23
23
  ## 常用跳转
24
24
 
@@ -192,10 +192,10 @@ navigate.navigateTo({
192
192
  登录判断依赖:
193
193
 
194
194
  ```ts
195
- uni.$store.getters.isLogin
195
+ store.getters.isLogin
196
196
  ```
197
197
 
198
- `navigate` 是普通工具模块,脱离组件 setup 上下文,所以读取 `uni.$store`。如果是在组件或 hooks 中判断登录态,请使用:
198
+ `navigate` 是普通工具模块,脱离组件 setup 上下文,所以直接引入 store 实例。如果是在组件或 hooks 中判断登录态,请使用:
199
199
 
200
200
  ```ts
201
201
  import { computed } from 'vue';
@@ -124,7 +124,7 @@ defineConfig({
124
124
 
125
125
  `needLogin` 字段控制页面是否需要登录才能访问,默认为 `false`。
126
126
 
127
- 当 `needLogin: true` 时,`navigate` 会检查 `uni.$store.getters.isLogin`,如果未登录则自动跳转到 `getPage('login')` 返回的登录页路径。
127
+ 当 `needLogin: true` 时,`navigate` 会检查 `store.getters.isLogin`,如果未登录则自动跳转到 `getPage('login')` 返回的登录页路径。
128
128
 
129
129
  ```json
130
130
  {
@@ -18,7 +18,7 @@
18
18
  import { formatURL, createInterceptors, createRequest } from '@zfqh/uniapp';
19
19
  ```
20
20
 
21
- 请求工具脱离组件 setup 上下文,拦截器内部会通过 `uni.$store` 读取 token 并处理 `401`。组件或 hooks 中读取 store 时请使用 `useStore()`。
21
+ 请求工具脱离组件 setup 上下文,拦截器内部会从 store 中读取 token 并处理 `401`。组件或 hooks 中读取 store 时请使用 `useStore()`。
22
22
 
23
23
  具体方法、参数语义和返回值以发布包类型为准;下方表格按 `@zfqh/uniapp/lib/utils/request` 的导出整理。
24
24
 
@@ -41,7 +41,7 @@ import { useStore } from 'vuex';
41
41
  const store = useStore();
42
42
  ```
43
43
 
44
- Options API 组件可使用 `this.$store`。`uni.$store` 主要给请求、导航、权限、webview 消息等普通 TS 模块使用,不建议作为组件 setup 或 hooks 的首选入口。
44
+ Options API 组件可使用 `this.$store`。普通 TS 模块中使用 `uni.$store`。
45
45
 
46
46
  ## 组件分类
47
47
 
@@ -58,7 +58,7 @@ const isLogin = computed(() => store.getters.isLogin);
58
58
  </script>
59
59
  ```
60
60
 
61
- 如果组件仍使用 Options API,可使用 `this.$store`。`uni.$store` 只推荐在脱离组件上下文的普通 TS 模块中使用,例如请求拦截器、导航工具、权限工具和 webview 消息处理。
61
+ 如果组件仍使用 Options API,可使用 `this.$store`。脱离组件上下文的普通 TS 模块中使用 `uni.$store`。
62
62
 
63
63
  ## 页面骨架示例
64
64
 
package/docs/faq/index.md CHANGED
@@ -5,16 +5,15 @@
5
5
  检查:
6
6
 
7
7
  1. 是否先注册 store,再注册 `@zfqh/uniapp`。
8
- 2. 是否设置了 `app.config.globalProperties.$store = store`。
9
- 3. token 是否位于 `uni.$store.state.login.info.token`。
8
+ 2. token 是否位于 `store.state.login.info.token`。
10
9
 
11
- 这里检查 `uni.$store` 是因为请求拦截器属于普通 TS 模块。组件或 hooks 内读取同一份状态时应使用 `useStore()`。
10
+ 请求拦截器属于普通 TS 模块,内部会从 store 中读取状态。组件或 hooks 内读取同一份状态时应使用 `useStore()`。
12
11
 
13
12
  ## 401 后没有退出登录
14
13
 
15
14
  检查:
16
15
 
17
- 1. `uni.$store.state.login.info.token` 是否存在。
16
+ 1. `store.state.login.info.token` 是否存在。
18
17
  2. store 是否实现 `dispatch('logout')`。
19
18
  3. 请求配置是否设置了 `custom.reLogin: false` 或 `custom.needLogin: false`。
20
19
 
@@ -31,12 +30,12 @@
31
30
 
32
31
  检查:
33
32
 
34
- 1. `uni.$store.getters.isLogin` 是否正确。
33
+ 1. `store.getters.isLogin` 是否正确。
35
34
  2. `page.needLoginPages` 是否包含目标页。
36
35
  3. `getPage('login')` 是否能返回登录页路径。
37
36
  4. 当前跳转是否显式传入 `needLogin: false`。
38
37
 
39
- `navigate` 内部读取 `uni.$store.getters.isLogin`。如果页面组件里自己判断登录态,请使用 `import { useStore } from 'vuex'`。
38
+ `navigate` 内部读取 `store.getters.isLogin`。如果页面组件里自己判断登录态,请使用 `import { useStore } from 'vuex'`。
40
39
 
41
40
  ## tabBar 页面 navigateTo 失败
42
41
 
@@ -34,14 +34,14 @@ export function createApp() {
34
34
  }
35
35
  ```
36
36
 
37
- `@zfqh/uniapp` 插件安装时会把 `app.config.globalProperties.$store` 赋值给 `uni.$store`。请求、导航、权限、登录等能力会读取 `uni.$store`,所以请先挂载 store,再注册本插件。
37
+ 请求、导航、权限、登录等能力会读取 store 状态,所以请先挂载 store,再注册本插件。
38
38
 
39
39
  Store 使用入口请按上下文区分:
40
40
 
41
41
  - 组件的 Composition API 或 `<script setup>` 中使用 `import { useStore } from 'vuex'`。
42
42
  - 自定义 hooks / composables 中也使用 `useStore()`。
43
43
  - Options API 组件中可使用 `this.$store`。
44
- - 请求、导航、权限、webview 消息等普通 TS 模块中使用 `uni.$store`。
44
+ - 普通 TS 模块中使用 `uni.$store`。
45
45
 
46
46
  详见 [Store 使用位置](./project-frame.md#store-使用位置)。
47
47
 
@@ -124,7 +124,6 @@ import { defineConfig } from '@zfqh/uniapp';
124
124
  import { app } from '@zfqh/uniapp/lib/configs/app';
125
125
  import { brand } from '@zfqh/uniapp/lib/configs/brand';
126
126
  import { getTencentToken } from '@zfqh/uniapp/lib/apis';
127
- import store from '@/store';
128
127
 
129
128
  defineConfig({
130
129
  app: app.merchant,
@@ -149,7 +148,7 @@ defineConfig({
149
148
  create_time: data.createTime,
150
149
  expire_time: data.expirationTime,
151
150
  bucket: data.bucket,
152
- region: store.state.storeConfig.isGlobalAccelerate ? 'accelerate' : data.region,
151
+ region: uni.$store.state.storeConfig.isGlobalAccelerate ? 'accelerate' : data.region,
153
152
  };
154
153
  },
155
154
  },
@@ -110,7 +110,6 @@ import { defineConfig } from '@zfqh/uniapp';
110
110
  import { app } from '@zfqh/uniapp/lib/configs/app';
111
111
  import { brand } from '@zfqh/uniapp/lib/configs/brand';
112
112
  import { getTencentToken } from '@zfqh/uniapp/lib/apis';
113
- import store from '@/store';
114
113
 
115
114
  defineConfig({
116
115
  app: app.merchant,
@@ -135,7 +134,7 @@ defineConfig({
135
134
  create_time: data.createTime,
136
135
  expire_time: data.expirationTime,
137
136
  bucket: data.bucket,
138
- region: store.state.storeConfig.isGlobalAccelerate ? 'accelerate' : data.region,
137
+ region: uni.$store.state.storeConfig.isGlobalAccelerate ? 'accelerate' : data.region,
139
138
  };
140
139
  },
141
140
  },
@@ -197,13 +196,13 @@ export default defineConfig({
197
196
 
198
197
  | 路径 | 用途 |
199
198
  | --- | --- |
200
- | `uni.$store.state.login.info.token` | 请求拦截器注入 `Authorization`。 |
201
- | `uni.$store.state.login.info.tenantId` | 业务请求注入 `Tenant-Id`。 |
202
- | `uni.$store.state.system.info.appLanguage` | 业务请求注入 `X-Language`。 |
203
- | `uni.$store.state.system.info.appVersion` | 非 App 环境下读取版本名。 |
204
- | `uni.$store.state.system.info.appVersionCode` | 非 App 环境下读取版本号。 |
205
- | `uni.$store.getters.isLogin` | 导航登录拦截判断。 |
206
- | `uni.$store.dispatch('logout')` | `401` 时触发退出登录。 |
199
+ | `store.state.login.info.token` | 请求拦截器注入 `Authorization`。 |
200
+ | `store.state.login.info.tenantId` | 业务请求注入 `Tenant-Id`。 |
201
+ | `store.state.system.info.appLanguage` | 业务请求注入 `X-Language`。 |
202
+ | `store.state.system.info.appVersion` | 非 App 环境下读取版本名。 |
203
+ | `store.state.system.info.appVersionCode` | 非 App 环境下读取版本号。 |
204
+ | `store.getters.isLogin` | 导航登录拦截判断。 |
205
+ | `store.dispatch('logout')` | `401` 时触发退出登录。 |
207
206
 
208
207
  推荐在注册插件前完成:
209
208
 
@@ -219,9 +218,9 @@ app.use(uniapp);
219
218
  | 场景 | 推荐用法 | 说明 |
220
219
  | --- | --- | --- |
221
220
  | Vue 组件的 Composition API / `<script setup>` | `import { useStore } from 'vuex'` 后调用 `const store = useStore()` | 组件实例处于 Vue 上下文内,应使用 Vuex 官方组合式 API,便于类型推导、测试和组件复用。 |
222
- | 自定义 hooks / composables | `import { useStore } from 'vuex'` 后在 hook 内调用 `useStore()` | hooks 同样运行在组件 setup 上下文里,不要直接读取全局 `uni.$store`。 |
221
+ | 自定义 hooks / composables | `import { useStore } from 'vuex'` 后在 hook 内调用 `useStore()` | hooks 同样运行在组件 setup 上下文里。 |
223
222
  | Options API 组件 | `this.$store` | 组件实例已注入 `$store` 时可使用实例属性;现有 `g-*` 组件源码也采用这种方式读取部分系统状态。 |
224
- | 请求、导航、toast、权限、webview 消息等普通 TS 模块 | `uni.$store` | 这些模块不在 Vue setup 上下文中,无法调用 `useStore()`,因此通过插件挂载到 `uni.$store` 的全局 store 读取登录态和全局状态。 |
223
+ | 请求、导航、toast、权限、webview 消息等普通 TS 模块 | `uni.$store` | 这些模块不在 Vue setup 上下文中,无法调用 `useStore()`,因此直接引入 store 实例读取登录态和全局状态。 |
225
224
 
226
225
  组件或 hooks 中推荐写法:
227
226
 
@@ -241,16 +240,6 @@ export function useLoginInfo() {
241
240
  }
242
241
  ```
243
242
 
244
- 普通模块中推荐写法:
245
-
246
- ```ts
247
- export function getToken() {
248
- return uni.$store?.state?.login?.info?.token;
249
- }
250
- ```
251
-
252
- `uni.$store` 是为了让脱离组件树的工具函数能够访问 store,不建议在组件 setup 或 hooks 中把它作为首选入口。
253
-
254
243
  ## 样式接入
255
244
 
256
245
  `@zfqh/uniapp` 内置的静态样式仅覆盖平台差异类和使用 `$uv-*` SCSS 变量的语义色类,间距、尺寸、字号、圆角、透明度、阴影等通用原子类由 `@gmcb/vite` 的 unocss 插件在构建时动态生成。
package/docs/index.md CHANGED
@@ -74,7 +74,7 @@ export function createApp() {
74
74
 
75
75
  ## 使用约定
76
76
 
77
- - 项目需要提供符合约定的 Vuex store:请求读取 `uni.$store.state.login.info.token`,导航读取 `uni.$store.getters.isLogin`,`401` 会触发 `uni.$store.dispatch('logout')`。
78
- - Store 入口按上下文区分:组件和 hooks 的 Composition API 中使用 `import { useStore } from 'vuex'`,Options API 组件中使用 `this.$store`,请求、导航、权限、webview 消息等普通模块中使用 `uni.$store`。
77
+ - 项目需要提供符合约定的 Vuex store:请求读取 `store.state.login.info.token`,导航读取 `store.getters.isLogin`,`401` 会触发 `store.dispatch('logout')`。
78
+ - Store 入口按上下文区分:组件的 Composition API 中使用 `import { useStore } from 'vuex'`,Options API 组件中使用 `this.$store`,普通 TS 模块中使用 `uni.$store`。
79
79
  - 多环境配置推荐使用 `@env/.env.<env>.ts`,并通过 `defineEnvConfig` 输出配置。
80
80
  - 根入口未导出的 `apis`、`modules`、`mixins` 请按 [深度引用](./api/index.md#深度引用) 从构建产物路径引入。
@@ -1,16 +1,10 @@
1
- // #ifdef APP-NVUE
2
- import { ref, inject } from 'vue';
3
- import { showLocale, defaultLocale } from '../utils/locale';
4
- // #endif
5
1
  // #ifndef APP-NVUE
6
2
  import { useI18n as vueUseI18n } from 'vue-i18n';
7
3
  // #endif
8
4
  let _useI18n;
9
5
  // #ifdef APP-NVUE
10
6
  _useI18n = function () {
11
- const locale = ref(defaultLocale);
12
- const t = inject('t', showLocale);
13
- return { t, locale };
7
+ return uni.$i18n.global;
14
8
  };
15
9
  // #endif
16
10
  // #ifndef APP-NVUE
package/lib/index.d.ts CHANGED
@@ -5,6 +5,7 @@ import cos from './utils/cos';
5
5
  import './utils/uni';
6
6
  export * from './hooks';
7
7
  export * from './utils';
8
+ export * from './utils/locale';
8
9
  export * from './utils/file';
9
10
  export * from './utils/storage';
10
11
  export * from './utils/pages';
package/lib/index.js CHANGED
@@ -6,6 +6,7 @@ import cos from './utils/cos';
6
6
  import './utils/uni';
7
7
  export * from './hooks';
8
8
  export * from './utils';
9
+ export * from './utils/locale';
9
10
  export * from './utils/file';
10
11
  export * from './utils/storage';
11
12
  export * from './utils/pages';
@@ -25,8 +26,6 @@ const install = (app) => {
25
26
  // #ifdef MP
26
27
  app.mixin(deep);
27
28
  // #endif
28
- const { $store } = app.config.globalProperties;
29
- uni.$store = $store;
30
29
  };
31
30
  export default {
32
31
  install,
@@ -1,6 +1,5 @@
1
+ import { I18nOptions } from 'vue-i18n';
1
2
  export declare const defaultLocale: string;
2
3
  export declare const fallbackLocale = "zh-Hans";
3
- /**
4
- * nvue inject 未提供 t 时的回退;第二参数为命名占位 {n} 等
5
- */
6
- export declare function showLocale(key: any, values: any): any;
4
+ export declare function createI18n(opts: I18nOptions): import("vue-i18n").I18n<import("vue-i18n").LocaleMessages<import("vue-i18n").VueMessageType>, import("vue-i18n").IntlDateTimeFormats, import("vue-i18n").IntlNumberFormats, boolean>;
5
+ export declare function getLocaleMessage(key: string): string;
@@ -1,19 +1,10 @@
1
+ import { createI18n as _createI18n } from 'vue-i18n';
1
2
  export const defaultLocale = uni.getLocale();
2
3
  export const fallbackLocale = 'zh-Hans';
3
- function applyNamedPlaceholders(str, values) {
4
- if (!values || typeof str !== 'string') {
5
- return str;
6
- }
7
- let out = str;
8
- for (const k of Object.keys(values)) {
9
- out = out.split(`{${k}}`).join(String(values[k]));
10
- }
11
- return out;
4
+ export function createI18n(opts) {
5
+ const i18n = _createI18n(opts);
6
+ return (uni.$i18n = i18n);
12
7
  }
13
- /**
14
- * nvue 下 inject 未提供 t 时的回退;第二参数为命名占位 {n} 等
15
- */
16
- export function showLocale(key, values) {
17
- const raw = uni.$messages[uni.getLocale()]?.[key] || uni.$messages[fallbackLocale]?.[key] || key;
18
- return applyNamedPlaceholders(raw, values);
8
+ export function getLocaleMessage(key) {
9
+ return uni.$i18n?.global?.t(key) || key;
19
10
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@zfqh/uniapp",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "uniapp公共模块",
5
5
  "author": "yinjiazeng@163.com",
6
6
  "license": "MIT",
7
7
  "main": "lib/index.js",
8
8
  "types": "lib/index.d.ts",
9
- "homepage": "https://git.keeprisk.cn/zfqh/front-end/tree/master/packages/uniapp-zfqh",
9
+ "homepage": "https://git.keeprisk.cn/zfqh/front-end/tree/master/packages/zfqh-uniapp",
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
@@ -45,5 +45,5 @@
45
45
  "@types/qs": "^6.9.16",
46
46
  "@types/url-parse": "^1.4.11"
47
47
  },
48
- "gitHead": "4eadd74fdfef951065e002cb9f65a30a7de03021"
48
+ "gitHead": "51d155379d352d8507622d2ccb89a6442892b2b2"
49
49
  }
package/types/index.d.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  /// <reference path="./virtual.d.ts" />
2
2
 
3
3
  import type { Uni as UniTypes } from '@dcloudio/types';
4
+ import type { Store } from 'vuex';
5
+ import type { I18n } from 'vue-i18n';
4
6
  import color from '../lib/configs/color';
5
7
 
6
8
  declare global {
7
9
  interface Uni extends UniTypes {
8
- $store: any;
9
- $messages: any;
10
+ $store: Store;
11
+ $i18n: I18n;
10
12
  $uv: any;
11
13
  webView: any;
12
14
  showAppFromBackground: boolean;