@liujitcn/kratos-taro-app-core 0.0.53 → 0.0.55

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.
package/README.md CHANGED
@@ -40,6 +40,6 @@ pnpm test
40
40
  pnpm check:exports
41
41
  ```
42
42
 
43
- 构建产物按模式与平台隔离:开发使用 `dist/dev/h5`、`dist/dev/mp-weixin`,生产默认使用 `dist/build/h5`、`dist/build/mp-weixin`。`KRATOS_TARO_OUTPUT_ROOT` 可覆盖默认目录;仓库宿主及 Kratos 配套项目的 H5 生产构建输出到 `backend/data/taro-app`。微信开发者工具默认指向开发目录,发布时导入生产目录。
43
+ 构建产物按模式与平台隔离:开发使用 `dist/dev/h5`、`dist/dev/mp-weixin`,生产默认使用 `dist/build/h5`、`dist/build/mp-weixin`。`KRATOS_TARO_OUTPUT_ROOT` 可覆盖默认目录;仓库宿主及 Kratos 配套项目的 H5 生产构建输出到 `backend/web/taro-app`。微信开发者工具默认指向开发目录,发布时导入生产目录。
44
44
 
45
45
  应用配置加载成功后,core 会将 `i18n_customs` 按 `locale + key` 覆盖已注册的本地文案,并刷新页面翻译。仅覆盖已有 key 的非空文案;未配置或已删除的覆盖项恢复本地默认值,未知语言和未知 key 忽略。切换语言时沿用配置重载流程。
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "type": "git",
8
8
  "url": "git@github.com:liujitcn/kratos-admin.git"
9
9
  },
10
- "version": "0.0.53",
10
+ "version": "0.0.55",
11
11
  "type": "module",
12
12
  "files": [
13
13
  "dist",
@@ -1,8 +1,9 @@
1
1
  import Taro from '@tarojs/taro'
2
- import type {
3
- DownloadFileRequest,
4
- FileInfo,
5
- MultiUploadFileResponse,
2
+ import {
3
+ BaseFileAccessMode,
4
+ type DownloadFileRequest,
5
+ type FileInfo,
6
+ type MultiUploadFileResponse,
6
7
  } from '../../../rpc/base/v1/file'
7
8
  import { uploadFile, uploadFileList } from '../../../utils/file'
8
9
  import { getLocaleRequestHeaders, t } from '../../../locales'
@@ -13,26 +14,26 @@ const FILE_URL = '/v1/base/file'
13
14
  /** 文件服务。 */
14
15
  export class FileServiceImpl {
15
16
  /** 上传多个浏览器文件。 */
16
- async MultiUploadFile(files: File[], fileType: string): Promise<MultiUploadFileResponse> {
17
+ async MultiUploadFile(files: File[], fileType: string, accessMode = BaseFileAccessMode.BASE_FILE_ACCESS_MODE_AUTHORIZED): Promise<MultiUploadFileResponse> {
17
18
  if (process.env.TARO_ENV !== 'h5') {
18
19
  throw new Error(t('core.file.temporary_path_required'))
19
20
  }
20
21
  const paths = files.map((file) => URL.createObjectURL(file))
21
22
  try {
22
- return { files: await uploadFileList(fileType, paths) }
23
+ return { files: await uploadFileList(fileType, paths, accessMode) }
23
24
  } finally {
24
25
  paths.forEach((path) => URL.revokeObjectURL(path))
25
26
  }
26
27
  }
27
28
 
28
29
  /** 上传单个浏览器文件。 */
29
- async UploadFile(file: File, fileType: string): Promise<FileInfo> {
30
+ async UploadFile(file: File, fileType: string, accessMode = BaseFileAccessMode.BASE_FILE_ACCESS_MODE_AUTHORIZED): Promise<FileInfo> {
30
31
  if (process.env.TARO_ENV !== 'h5') {
31
32
  throw new Error(t('core.file.temporary_path_required'))
32
33
  }
33
34
  const path = URL.createObjectURL(file)
34
35
  try {
35
- return await uploadFile(fileType, path)
36
+ return await uploadFile(fileType, path, accessMode)
36
37
  } finally {
37
38
  URL.revokeObjectURL(path)
38
39
  }
@@ -1,12 +1,22 @@
1
1
  // Code generated by protoc-gen-ts_proto. DO NOT EDIT.
2
2
  // versions:
3
- // protoc-gen-ts_proto v2.11.8
3
+ // protoc-gen-ts_proto v2.12.1
4
4
  // protoc unknown
5
5
  // source: base/v1/file.proto
6
6
 
7
7
  /* eslint-disable */
8
8
  import type { BytesValue } from "../../google/protobuf/wrappers";
9
9
 
10
+ /** 文件访问方式。 */
11
+ export enum BaseFileAccessMode {
12
+ /** BASE_FILE_ACCESS_MODE_UNSPECIFIED - 未指定文件访问方式,服务端按授权访问处理。 */
13
+ BASE_FILE_ACCESS_MODE_UNSPECIFIED = 0,
14
+ /** BASE_FILE_ACCESS_MODE_PUBLIC - 允许匿名公开访问。 */
15
+ BASE_FILE_ACCESS_MODE_PUBLIC = 1,
16
+ /** BASE_FILE_ACCESS_MODE_AUTHORIZED - 必须携带有效访问令牌。 */
17
+ BASE_FILE_ACCESS_MODE_AUTHORIZED = 2,
18
+ }
19
+
10
20
  /** 批量上传文件请求参数 */
11
21
  export interface MultiUploadFileRequest {
12
22
  /** 文件名 */
@@ -35,6 +45,8 @@ export interface UploadFileInfo {
35
45
  path: string;
36
46
  /** 二进制内容将通过 base64 编码传输 */
37
47
  content: Uint8Array;
48
+ /** 文件访问方式,未指定时默认需要访问令牌 */
49
+ access_mode: BaseFileAccessMode;
38
50
  }
39
51
 
40
52
  /** 下载文件请求参数 */
package/src/utils/file.ts CHANGED
@@ -1,17 +1,21 @@
1
1
  import Taro from '@tarojs/taro'
2
- import type { FileInfo, MultiUploadFileResponse } from '../rpc/base/v1/file'
2
+ import { BaseFileAccessMode, type FileInfo, type MultiUploadFileResponse } from '../rpc/base/v1/file'
3
3
  import { getLocaleRequestHeaders, t } from '../locales'
4
4
  import { formatSrc } from './index'
5
5
  import { getRequestAccessToken, requestBaseURL } from './http'
6
6
 
7
7
  /** 上传单个文件。 */
8
- export async function uploadFile(fileType: string, filePath: string): Promise<FileInfo> {
8
+ export async function uploadFile(
9
+ fileType: string,
10
+ filePath: string,
11
+ accessMode = BaseFileAccessMode.BASE_FILE_ACCESS_MODE_AUTHORIZED,
12
+ ): Promise<FileInfo> {
9
13
  const token = await getRequestAccessToken()
10
14
  const response = await Taro.uploadFile({
11
15
  url: `${requestBaseURL}/v1/base/file`,
12
16
  name: 'file',
13
17
  filePath,
14
- formData: { fileType },
18
+ formData: { fileType, accessMode: String(accessMode) },
15
19
  header: {
16
20
  ...getLocaleRequestHeaders(),
17
21
  'source-client': 'miniapp',
@@ -23,8 +27,12 @@ export async function uploadFile(fileType: string, filePath: string): Promise<Fi
23
27
  }
24
28
 
25
29
  /** 并发上传文件列表。 */
26
- export async function uploadFileList(fileType: string, filePaths: string[]): Promise<FileInfo[]> {
27
- const results = await Promise.allSettled(filePaths.map((filePath) => uploadFile(fileType, filePath)))
30
+ export async function uploadFileList(
31
+ fileType: string,
32
+ filePaths: string[],
33
+ accessMode = BaseFileAccessMode.BASE_FILE_ACCESS_MODE_AUTHORIZED,
34
+ ): Promise<FileInfo[]> {
35
+ const results = await Promise.allSettled(filePaths.map((filePath) => uploadFile(fileType, filePath, accessMode)))
28
36
  return results.flatMap((result) => (result.status === 'fulfilled' ? [result.value] : []))
29
37
  }
30
38
 
@@ -1,4 +1,12 @@
1
- const configuredStaticUrl = process.env.VITE_APP_STATIC_URL || ''
1
+ const configuredStaticUrl =
2
+ process.env.VITE_APP_STATIC_URL || process.env.VITE_APP_API_URL || ''
3
+
4
+ /** 将后端数据文件地址切换到当前构建配置的静态资源域名。 */
5
+ function rewriteDataFileOrigin(src: string, staticOrigin: string): string {
6
+ if (!staticOrigin) return src
7
+ const match = src.match(/^https?:\/\/[^/]+(\/data(?:\/|$).*)$/)
8
+ return match ? `${staticOrigin}${match[1]}` : src
9
+ }
2
10
 
3
11
  /** 日期格式化函数。 */
4
12
  export function formatDate(date: Date, format = 'YYYY-MM-DD HH:mm:ss'): string {
@@ -24,10 +32,13 @@ export function formatPrice(price: number): string {
24
32
 
25
33
  /** 格式化后端静态资源地址。 */
26
34
  export function formatSrc(src: string): string {
27
- if (!src || /^https?:\/\//.test(src)) return src
28
35
  const browserOrigin =
29
- typeof window !== 'undefined' && window.location?.origin ? window.location.origin : ''
36
+ process.env.TARO_ENV === 'h5' && typeof window !== 'undefined' && window.location?.origin
37
+ ? window.location.origin
38
+ : ''
30
39
  const staticOrigin = browserOrigin || configuredStaticUrl.replace(/\/$/, '')
40
+ if (!src) return src
41
+ if (/^https?:\/\//.test(src)) return rewriteDataFileOrigin(src, staticOrigin)
31
42
  if (!staticOrigin) return src
32
43
  return src.startsWith('/')
33
44
  ? `${staticOrigin}${src}`
@@ -1,7 +1,9 @@
1
1
  import { Image, Text, View } from '@tarojs/components'
2
2
  import { useLoad } from '@tarojs/taro'
3
+ import { useEffect, useState } from 'react'
3
4
  import defaultLogo from '@liujitcn/kratos-taro-app-core/static/images/logo_icon.png'
4
5
  import { useSettingStore } from '../../../stores'
6
+ import { formatSrc } from '../../../utils'
5
7
  import './index.scss'
6
8
  import { useI18n } from '../../../locales'
7
9
 
@@ -12,7 +14,14 @@ export default function HomePage() {
12
14
  const loadData = useSettingStore((state) => state.loadData)
13
15
  const mainTitle = settings?.get('mainTitle') || t('core.home.main_title')
14
16
  const subTitle = settings?.get('subTitle') || t('core.home.sub_title')
15
- const appLogo = settings?.get('appLogo') || defaultLogo
17
+ const configuredAppLogo = settings?.get('appLogo')
18
+ const configuredLogoUrl = configuredAppLogo ? formatSrc(configuredAppLogo) : defaultLogo
19
+ const [appLogo, setAppLogo] = useState(configuredLogoUrl)
20
+
21
+ useEffect(() => {
22
+ setAppLogo(configuredLogoUrl)
23
+ }, [configuredLogoUrl])
24
+
16
25
  useLoad(() => {
17
26
  void loadData().catch(() => undefined)
18
27
  })
@@ -36,6 +45,7 @@ export default function HomePage() {
36
45
  className='home-logo'
37
46
  src={appLogo}
38
47
  mode='aspectFit'
48
+ onError={() => setAppLogo(defaultLogo)}
39
49
  />
40
50
  <View className='home-hero__copy'>
41
51
  <Text className='home-title'>{mainTitle}</Text>
@@ -7,6 +7,7 @@ import { defMfaService } from '../../../api/base/v1/mfa'
7
7
  import { navigateAppRoute } from '../../../navigation'
8
8
  import { LoginStatus, type LoginRequest, type LoginResponse } from '../../../rpc/base/v1/login'
9
9
  import { useSettingStore, useUserStore } from '../../../stores'
10
+ import { formatSrc } from '../../../utils'
10
11
  import { restoreLoginRedirect } from '../../../utils/navigation'
11
12
  import { encryptPassword, PASSWORD_CRYPTO_SCENE } from '../../../utils/passwordCrypto'
12
13
  import { createWebAuthnCredential, getWebAuthnAssertion } from '../../../utils/webauthn'
@@ -124,7 +125,9 @@ export default function LoginPage() {
124
125
  languageOptions.find((item) => item.language_code === locale)?.native_name || locale
125
126
  const mainTitle = settings?.get('mainTitle') || t('core.home.main_title')
126
127
  const subTitle = settings?.get('subTitle') || t('core.login.default_sub_title')
127
- const appLogo = settings?.get('appLogo') || defaultLogo
128
+ const configuredAppLogo = settings?.get('appLogo')
129
+ const configuredLogoUrl = configuredAppLogo ? formatSrc(configuredAppLogo) : defaultLogo
130
+ const [appLogo, setAppLogo] = useState(configuredLogoUrl)
128
131
  const showTenantCode = settings?.get('showTenantCode') !== 'false'
129
132
  const configuredCaptchaType = settings?.get('captchaType') || ''
130
133
  const isBehaviorCaptcha = behaviorCaptchaTypes.has(captchaType)
@@ -169,6 +172,10 @@ export default function LoginPage() {
169
172
  t,
170
173
  ]) as unknown as Record<string, string | number | boolean>
171
174
 
175
+ useEffect(() => {
176
+ setAppLogo(configuredLogoUrl)
177
+ }, [configuredLogoUrl])
178
+
172
179
  useEffect(() => {
173
180
  void Taro.setNavigationBarTitle({ title: t('common.action.login') })
174
181
  }, [locale, t])
@@ -601,7 +608,7 @@ export default function LoginPage() {
601
608
  </Picker>
602
609
  <View className='login-hero'>
603
610
  <View className='login-logo-shell'>
604
- <Image className='login-logo' src={appLogo} />
611
+ <Image className='login-logo' src={appLogo} onError={() => setAppLogo(defaultLogo)} />
605
612
  </View>
606
613
  <View className='login-hero-copy'>
607
614
  <Text className='login-title'>{mainTitle}</Text>
@@ -754,7 +761,9 @@ export default function LoginPage() {
754
761
  <Checkbox
755
762
  value='remember'
756
763
  checked={rememberMfaDevice}
757
- onChange={(event) => setRememberMfaDevice(event.detail.value.includes('remember'))}
764
+ onChange={(event) =>
765
+ setRememberMfaDevice(event.detail.value.includes('remember'))
766
+ }
758
767
  />
759
768
  <Text>{t('core.login.mfa_remember_device', { days: mfaRememberDays })}</Text>
760
769
  </View>