@coze-arch/cli 0.0.1-alpha.77c1b0 → 0.0.1-alpha.77c276

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.
Files changed (164) hide show
  1. package/lib/__templates__/expo/.coze +7 -2
  2. package/lib/__templates__/expo/.cozeproj/scripts/{deploy_build.sh → dev_build.sh} +25 -25
  3. package/lib/__templates__/expo/.cozeproj/scripts/{deploy_run.sh → dev_run.sh} +45 -63
  4. package/lib/__templates__/expo/.cozeproj/scripts/prod_build.sh +47 -0
  5. package/lib/__templates__/expo/.cozeproj/scripts/prod_run.sh +34 -0
  6. package/lib/__templates__/expo/_gitignore +1 -1
  7. package/lib/__templates__/expo/_npmrc +3 -5
  8. package/lib/__templates__/expo/client/app.config.ts +71 -0
  9. package/lib/__templates__/expo/client/metro.config.js +51 -0
  10. package/lib/__templates__/expo/client/package.json +92 -0
  11. package/lib/__templates__/expo/client/scripts/install-missing-deps.js +36 -12
  12. package/lib/__templates__/expo/client/src/app/index.ts +1 -0
  13. package/lib/__templates__/expo/client/{constants → src/constants}/theme.ts +10 -0
  14. package/lib/__templates__/expo/client/src/contexts/AuthContext.tsx +49 -0
  15. package/lib/__templates__/expo/client/{hooks → src/hooks}/useTheme.ts +1 -1
  16. package/lib/__templates__/expo/client/{screens → src/screens}/home/index.tsx +1 -5
  17. package/lib/__templates__/expo/client/src/screens/home/styles.ts +60 -0
  18. package/lib/__templates__/expo/client/{utils → src/utils}/index.ts +1 -2
  19. package/lib/__templates__/expo/client/tsconfig.json +24 -0
  20. package/lib/__templates__/expo/package.json +27 -13
  21. package/lib/__templates__/expo/pnpm-lock.yaml +426 -514
  22. package/lib/__templates__/expo/pnpm-workspace.yaml +3 -0
  23. package/lib/__templates__/expo/server/package.json +17 -0
  24. package/lib/__templates__/expo/{src → server/src}/index.ts +2 -2
  25. package/lib/__templates__/expo/template.config.js +1 -1
  26. package/lib/__templates__/expo/tsconfig.json +1 -24
  27. package/lib/__templates__/nextjs/.coze +3 -3
  28. package/lib/__templates__/nextjs/README.md +341 -19
  29. package/lib/__templates__/nextjs/_npmrc +1 -1
  30. package/lib/__templates__/nextjs/components.json +21 -0
  31. package/lib/__templates__/nextjs/package.json +54 -1
  32. package/lib/__templates__/nextjs/pnpm-lock.yaml +7947 -1523
  33. package/lib/__templates__/nextjs/scripts/dev.sh +9 -27
  34. package/lib/__templates__/nextjs/src/app/globals.css +114 -11
  35. package/lib/__templates__/nextjs/src/app/layout.tsx +18 -18
  36. package/lib/__templates__/nextjs/src/components/ui/accordion.tsx +66 -0
  37. package/lib/__templates__/nextjs/src/components/ui/alert-dialog.tsx +157 -0
  38. package/lib/__templates__/nextjs/src/components/ui/alert.tsx +66 -0
  39. package/lib/__templates__/nextjs/src/components/ui/aspect-ratio.tsx +11 -0
  40. package/lib/__templates__/nextjs/src/components/ui/avatar.tsx +53 -0
  41. package/lib/__templates__/nextjs/src/components/ui/badge.tsx +46 -0
  42. package/lib/__templates__/nextjs/src/components/ui/breadcrumb.tsx +109 -0
  43. package/lib/__templates__/nextjs/src/components/ui/button-group.tsx +83 -0
  44. package/lib/__templates__/nextjs/src/components/ui/button.tsx +62 -0
  45. package/lib/__templates__/nextjs/src/components/ui/calendar.tsx +220 -0
  46. package/lib/__templates__/nextjs/src/components/ui/card.tsx +92 -0
  47. package/lib/__templates__/nextjs/src/components/ui/carousel.tsx +241 -0
  48. package/lib/__templates__/nextjs/src/components/ui/chart.tsx +357 -0
  49. package/lib/__templates__/nextjs/src/components/ui/checkbox.tsx +32 -0
  50. package/lib/__templates__/nextjs/src/components/ui/collapsible.tsx +33 -0
  51. package/lib/__templates__/nextjs/src/components/ui/command.tsx +184 -0
  52. package/lib/__templates__/nextjs/src/components/ui/context-menu.tsx +252 -0
  53. package/lib/__templates__/nextjs/src/components/ui/dialog.tsx +143 -0
  54. package/lib/__templates__/nextjs/src/components/ui/drawer.tsx +135 -0
  55. package/lib/__templates__/nextjs/src/components/ui/dropdown-menu.tsx +257 -0
  56. package/lib/__templates__/nextjs/src/components/ui/empty.tsx +104 -0
  57. package/lib/__templates__/nextjs/src/components/ui/field.tsx +248 -0
  58. package/lib/__templates__/nextjs/src/components/ui/form.tsx +167 -0
  59. package/lib/__templates__/nextjs/src/components/ui/hover-card.tsx +44 -0
  60. package/lib/__templates__/nextjs/src/components/ui/input-group.tsx +170 -0
  61. package/lib/__templates__/nextjs/src/components/ui/input-otp.tsx +77 -0
  62. package/lib/__templates__/nextjs/src/components/ui/input.tsx +21 -0
  63. package/lib/__templates__/nextjs/src/components/ui/item.tsx +193 -0
  64. package/lib/__templates__/nextjs/src/components/ui/kbd.tsx +28 -0
  65. package/lib/__templates__/nextjs/src/components/ui/label.tsx +24 -0
  66. package/lib/__templates__/nextjs/src/components/ui/menubar.tsx +276 -0
  67. package/lib/__templates__/nextjs/src/components/ui/navigation-menu.tsx +168 -0
  68. package/lib/__templates__/nextjs/src/components/ui/pagination.tsx +127 -0
  69. package/lib/__templates__/nextjs/src/components/ui/popover.tsx +48 -0
  70. package/lib/__templates__/nextjs/src/components/ui/progress.tsx +31 -0
  71. package/lib/__templates__/nextjs/src/components/ui/radio-group.tsx +45 -0
  72. package/lib/__templates__/nextjs/src/components/ui/resizable.tsx +56 -0
  73. package/lib/__templates__/nextjs/src/components/ui/scroll-area.tsx +58 -0
  74. package/lib/__templates__/nextjs/src/components/ui/select.tsx +190 -0
  75. package/lib/__templates__/nextjs/src/components/ui/separator.tsx +28 -0
  76. package/lib/__templates__/nextjs/src/components/ui/sheet.tsx +139 -0
  77. package/lib/__templates__/nextjs/src/components/ui/sidebar.tsx +726 -0
  78. package/lib/__templates__/nextjs/src/components/ui/skeleton.tsx +13 -0
  79. package/lib/__templates__/nextjs/src/components/ui/slider.tsx +63 -0
  80. package/lib/__templates__/nextjs/src/components/ui/sonner.tsx +40 -0
  81. package/lib/__templates__/nextjs/src/components/ui/spinner.tsx +16 -0
  82. package/lib/__templates__/nextjs/src/components/ui/switch.tsx +31 -0
  83. package/lib/__templates__/nextjs/src/components/ui/table.tsx +116 -0
  84. package/lib/__templates__/nextjs/src/components/ui/tabs.tsx +66 -0
  85. package/lib/__templates__/nextjs/src/components/ui/textarea.tsx +18 -0
  86. package/lib/__templates__/nextjs/src/components/ui/toggle-group.tsx +83 -0
  87. package/lib/__templates__/nextjs/src/components/ui/toggle.tsx +47 -0
  88. package/lib/__templates__/nextjs/src/components/ui/tooltip.tsx +61 -0
  89. package/lib/__templates__/nextjs/src/hooks/use-mobile.ts +19 -0
  90. package/lib/__templates__/nextjs/src/lib/utils.ts +6 -0
  91. package/lib/__templates__/nextjs/template.config.js +2 -2
  92. package/lib/__templates__/templates.json +6 -37
  93. package/lib/__templates__/vite/.coze +3 -3
  94. package/lib/__templates__/vite/README.md +204 -26
  95. package/lib/__templates__/vite/_npmrc +1 -1
  96. package/lib/__templates__/vite/package.json +1 -1
  97. package/lib/__templates__/vite/pnpm-lock.yaml +120 -120
  98. package/lib/__templates__/vite/scripts/dev.sh +7 -26
  99. package/lib/__templates__/vite/template.config.js +11 -2
  100. package/lib/__templates__/vite/vite.config.ts +3 -3
  101. package/lib/cli.js +426 -249
  102. package/package.json +7 -4
  103. package/lib/__templates__/expo/app.json +0 -63
  104. package/lib/__templates__/expo/babel.config.js +0 -9
  105. package/lib/__templates__/expo/client/app/(tabs)/_layout.tsx +0 -43
  106. package/lib/__templates__/expo/client/app/(tabs)/home.tsx +0 -1
  107. package/lib/__templates__/expo/client/app/(tabs)/index.tsx +0 -7
  108. package/lib/__templates__/expo/client/app/+not-found.tsx +0 -79
  109. package/lib/__templates__/expo/client/contexts/AuthContext.tsx +0 -142
  110. package/lib/__templates__/expo/client/index.js +0 -12
  111. package/lib/__templates__/expo/client/screens/home/styles.ts +0 -332
  112. package/lib/__templates__/expo/metro.config.js +0 -53
  113. package/lib/__templates__/react-rsbuild/.coze +0 -11
  114. package/lib/__templates__/react-rsbuild/.vscode/settings.json +0 -121
  115. package/lib/__templates__/react-rsbuild/README.md +0 -61
  116. package/lib/__templates__/react-rsbuild/_gitignore +0 -97
  117. package/lib/__templates__/react-rsbuild/_npmrc +0 -22
  118. package/lib/__templates__/react-rsbuild/package.json +0 -31
  119. package/lib/__templates__/react-rsbuild/pnpm-lock.yaml +0 -997
  120. package/lib/__templates__/react-rsbuild/rsbuild.config.ts +0 -13
  121. package/lib/__templates__/react-rsbuild/scripts/build.sh +0 -14
  122. package/lib/__templates__/react-rsbuild/scripts/dev.sh +0 -51
  123. package/lib/__templates__/react-rsbuild/scripts/start.sh +0 -15
  124. package/lib/__templates__/react-rsbuild/src/App.tsx +0 -60
  125. package/lib/__templates__/react-rsbuild/src/index.css +0 -21
  126. package/lib/__templates__/react-rsbuild/src/index.html +0 -12
  127. package/lib/__templates__/react-rsbuild/src/index.tsx +0 -16
  128. package/lib/__templates__/react-rsbuild/tailwind.config.js +0 -9
  129. package/lib/__templates__/react-rsbuild/template.config.js +0 -54
  130. package/lib/__templates__/react-rsbuild/tsconfig.json +0 -17
  131. package/lib/__templates__/rsbuild/.coze +0 -11
  132. package/lib/__templates__/rsbuild/.vscode/settings.json +0 -7
  133. package/lib/__templates__/rsbuild/README.md +0 -61
  134. package/lib/__templates__/rsbuild/_gitignore +0 -97
  135. package/lib/__templates__/rsbuild/_npmrc +0 -22
  136. package/lib/__templates__/rsbuild/package.json +0 -24
  137. package/lib/__templates__/rsbuild/pnpm-lock.yaml +0 -888
  138. package/lib/__templates__/rsbuild/rsbuild.config.ts +0 -12
  139. package/lib/__templates__/rsbuild/scripts/build.sh +0 -14
  140. package/lib/__templates__/rsbuild/scripts/dev.sh +0 -51
  141. package/lib/__templates__/rsbuild/scripts/start.sh +0 -15
  142. package/lib/__templates__/rsbuild/src/index.css +0 -21
  143. package/lib/__templates__/rsbuild/src/index.html +0 -12
  144. package/lib/__templates__/rsbuild/src/index.ts +0 -5
  145. package/lib/__templates__/rsbuild/src/main.ts +0 -65
  146. package/lib/__templates__/rsbuild/tailwind.config.js +0 -9
  147. package/lib/__templates__/rsbuild/template.config.js +0 -56
  148. package/lib/__templates__/rsbuild/tsconfig.json +0 -16
  149. /package/lib/__templates__/expo/{eslint-formatter-simple.mjs → client/eslint-formatter-simple.mjs} +0 -0
  150. /package/lib/__templates__/expo/{eslint.config.mjs → client/eslint.config.mjs} +0 -0
  151. /package/lib/__templates__/expo/client/{app → src/app}/_layout.tsx +0 -0
  152. /package/lib/__templates__/expo/client/{assets → src/assets}/fonts/SpaceMono-Regular.ttf +0 -0
  153. /package/lib/__templates__/expo/client/{assets → src/assets}/images/adaptive-icon.png +0 -0
  154. /package/lib/__templates__/expo/client/{assets → src/assets}/images/default-avatar.png +0 -0
  155. /package/lib/__templates__/expo/client/{assets → src/assets}/images/favicon.png +0 -0
  156. /package/lib/__templates__/expo/client/{assets → src/assets}/images/icon.png +0 -0
  157. /package/lib/__templates__/expo/client/{assets → src/assets}/images/partial-react-logo.png +0 -0
  158. /package/lib/__templates__/expo/client/{assets → src/assets}/images/react-logo.png +0 -0
  159. /package/lib/__templates__/expo/client/{assets → src/assets}/images/react-logo@2x.png +0 -0
  160. /package/lib/__templates__/expo/client/{assets → src/assets}/images/react-logo@3x.png +0 -0
  161. /package/lib/__templates__/expo/client/{assets → src/assets}/images/splash-icon.png +0 -0
  162. /package/lib/__templates__/expo/client/{components → src/components}/Screen.tsx +0 -0
  163. /package/lib/__templates__/expo/client/{components → src/components}/SmartDateInput.tsx +0 -0
  164. /package/lib/__templates__/expo/client/{hooks → src/hooks}/useColorScheme.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coze-arch/cli",
3
- "version": "0.0.1-alpha.77c1b0",
3
+ "version": "0.0.1-alpha.77c276",
4
4
  "private": false,
5
5
  "description": "coze coding devtools cli",
6
6
  "license": "MIT",
@@ -19,12 +19,12 @@
19
19
  "scripts": {
20
20
  "prebuild": "tsx scripts/prebuild.ts",
21
21
  "build": "tsx scripts/build.ts",
22
- "generate-templates": "tsx scripts/generate-templates-config.ts",
23
22
  "lint": "eslint ./ --cache",
23
+ "postpublish": "bash scripts/sync-npmmirror.sh",
24
24
  "test": "vitest --run --passWithNoTests",
25
25
  "test:all": "bash scripts/test-coverage.sh",
26
26
  "test:cov": "vitest --run --passWithNoTests --coverage",
27
- "test:e2e": "vitest run --config vitest.e2e.config.ts",
27
+ "test:e2e": "bash scripts/e2e.sh",
28
28
  "test:perf": "vitest bench --run --config vitest.perf.config.ts",
29
29
  "test:perf:compare": "bash scripts/compare-perf.sh",
30
30
  "test:perf:save": "bash scripts/run-perf-with-output.sh"
@@ -37,6 +37,7 @@
37
37
  "commander": "~12.1.0",
38
38
  "ejs": "^3.1.10",
39
39
  "js-yaml": "^4.1.0",
40
+ "minimist": "^1.2.5",
40
41
  "shelljs": "^0.10.0"
41
42
  },
42
43
  "devDependencies": {
@@ -50,6 +51,7 @@
50
51
  "@types/ejs": "^3.1.5",
51
52
  "@types/iarna__toml": "^2.0.5",
52
53
  "@types/js-yaml": "^4.0.9",
54
+ "@types/minimist": "^1.2.5",
53
55
  "@types/node": "^24",
54
56
  "@types/shelljs": "^0.10.0",
55
57
  "@vitest/coverage-v8": "~4.0.16",
@@ -60,7 +62,8 @@
60
62
  "vitest": "~4.0.16"
61
63
  },
62
64
  "publishConfig": {
63
- "access": "public"
65
+ "access": "public",
66
+ "registry": "https://registry.npmjs.org"
64
67
  },
65
68
  "cozePublishConfig": {
66
69
  "bin": {
@@ -1,63 +0,0 @@
1
- {
2
- "expo": {
3
- "name": "${app_name}",
4
- "slug": "${slug}",
5
- "version": "1.0.0",
6
- "orientation": "portrait",
7
- "icon": "./client/assets/images/icon.png",
8
- "scheme": "myapp",
9
- "userInterfaceStyle": "automatic",
10
- "newArchEnabled": true,
11
- "ios": {
12
- "supportsTablet": true
13
- },
14
- "android": {
15
- "adaptiveIcon": {
16
- "foregroundImage": "./client/assets/images/adaptive-icon.png",
17
- "backgroundColor": "#ffffff"
18
- }
19
- },
20
- "web": {
21
- "bundler": "metro",
22
- "output": "single",
23
- "favicon": "./client/assets/images/favicon.png"
24
- },
25
- "plugins": [
26
- "expo-router",
27
- [
28
- "expo-splash-screen",
29
- {
30
- "image": "./client/assets/images/splash-icon.png",
31
- "imageWidth": 200,
32
- "resizeMode": "contain",
33
- "backgroundColor": "#ffffff"
34
- }
35
- ],
36
- [
37
- "expo-image-picker",
38
- {
39
- "photosPermission": "允许${app_name}访问您的相册,以便您上传或保存图片。",
40
- "cameraPermission": "允许${app_name}使用您的相机,以便您直接拍摄照片上传。",
41
- "microphonePermission": "允许${app_name}访问您的麦克风,以便您拍摄带有声音的视频。"
42
- }
43
- ],
44
- [
45
- "expo-location",
46
- {
47
- "locationWhenInUsePermission": "${app_name}需要访问您的位置以提供周边服务及导航功能。"
48
- }
49
- ],
50
- [
51
- "expo-camera",
52
- {
53
- "cameraPermission": "${app_name}需要访问相机以拍摄照片和视频。",
54
- "microphonePermission": "${app_name}需要访问麦克风以录制视频声音。",
55
- "recordAudioAndroid": true
56
- }
57
- ]
58
- ],
59
- "experiments": {
60
- "typedRoutes": true
61
- }
62
- }
63
- }
@@ -1,9 +0,0 @@
1
- module.exports = function (api) {
2
- api.cache(true);
3
- return {
4
- presets: ["babel-preset-expo"],
5
- plugins: [
6
- "react-native-reanimated/plugin",
7
- ],
8
- };
9
- };
@@ -1,43 +0,0 @@
1
- import { Tabs } from 'expo-router';
2
- import { FontAwesome6 } from '@expo/vector-icons';
3
- import { useSafeAreaInsets } from 'react-native-safe-area-context';
4
- import { useTheme } from '@/hooks/useTheme';
5
-
6
- export default function TabLayout() {
7
- const insets = useSafeAreaInsets();
8
- const { theme } = useTheme();
9
- const tabBarHeight = 56 + Math.max(insets.bottom, 8);
10
-
11
- return (
12
- <Tabs
13
- backBehavior='history'
14
- screenOptions={{
15
- headerShown: false,
16
- tabBarActiveTintColor: theme.tabIconSelected,
17
- tabBarInactiveTintColor: theme.tabIconDefault,
18
- tabBarStyle: {
19
- backgroundColor: theme.backgroundRoot,
20
- borderTopColor: theme.backgroundSecondary,
21
- borderTopWidth: 1,
22
- height: tabBarHeight,
23
- paddingBottom: Math.max(insets.bottom, 8),
24
- paddingTop: 6,
25
- },
26
- }}
27
- >
28
- <Tabs.Screen
29
- name='index'
30
- options={{ href: null }}
31
- />
32
- <Tabs.Screen
33
- name='home'
34
- options={{
35
- title: '首页',
36
- tabBarIcon: ({ color }) => (
37
- <FontAwesome6 name='house' size={20} color={color} />
38
- ),
39
- }}
40
- />
41
- </Tabs>
42
- );
43
- }
@@ -1 +0,0 @@
1
- export { default } from "@/screens/home";
@@ -1,7 +0,0 @@
1
- import { Redirect } from 'expo-router';
2
-
3
- const Index = () => {
4
- return <Redirect href="./home" relativeToDirectory />;
5
- };
6
-
7
- export default Index;
@@ -1,79 +0,0 @@
1
- import { useRouter } from 'expo-router';
2
- import { StyleSheet, View, Text, Pressable, Platform } from 'react-native';
3
-
4
- export default function NotFoundScreen() {
5
- const router = useRouter();
6
-
7
- const goBack = () => {
8
- if (router.canGoBack()) {
9
- router.back();
10
- }
11
- };
12
-
13
- return (
14
- <View style={styles.container}>
15
- <Text style={styles.heading}>404</Text>
16
- <Text style={styles.textStyle}>页面未找到</Text>
17
- <Text style={styles.subTextStyle}>
18
- 抱歉!您访问的页面不存在,当前页面功能待完善。
19
- </Text>
20
- <Pressable onPress={goBack} style={styles.button}>
21
- <Text style={styles.buttonText}>返回上一页</Text>
22
- </Pressable>
23
- </View>
24
- );
25
- }
26
-
27
- const styles = StyleSheet.create({
28
- container: {
29
- flex: 1,
30
- alignItems: 'center',
31
- justifyContent: 'center',
32
- backgroundColor: '#f3f5ff',
33
- padding: 16,
34
- },
35
- heading: {
36
- fontSize: 120,
37
- fontWeight: '900',
38
- color: '#4a5568',
39
- textShadowColor: 'rgba(0, 0, 0, 0.05)',
40
- textShadowOffset: { width: 0, height: 4 },
41
- textShadowRadius: 8,
42
- },
43
- textStyle: {
44
- fontSize: 24,
45
- fontWeight: '500',
46
- marginTop: -16,
47
- marginBottom: 16,
48
- color: '#4a5568',
49
- },
50
- subTextStyle: {
51
- fontSize: 16,
52
- color: '#718096',
53
- marginBottom: 32,
54
- maxWidth: 400,
55
- textAlign: 'center',
56
- },
57
- button: {
58
- paddingVertical: 12,
59
- paddingHorizontal: 24,
60
- backgroundColor: '#6366f1',
61
- borderRadius: 9999,
62
- ...Platform.select({
63
- ios: {
64
- shadowColor: 'rgba(0, 0, 0, 0.1)',
65
- shadowOffset: { width: 0, height: 10 },
66
- shadowOpacity: 1,
67
- shadowRadius: 15,
68
- },
69
- android: {
70
- elevation: 5,
71
- },
72
- }),
73
- },
74
- buttonText: {
75
- fontSize: 16,
76
- fontWeight: '600',
77
- color: '#ffffff',
78
- },
79
- });
@@ -1,142 +0,0 @@
1
- // @ts-nocheck
2
- /**
3
- * 通用认证上下文
4
- *
5
- * 基于固定的 API 接口实现,可复用到其他项目
6
- * 其他项目使用时,只需修改 @api 的导入路径指向项目的 api 模块
7
- */
8
- import React, {
9
- createContext,
10
- useContext,
11
- useState,
12
- useEffect,
13
- ReactNode,
14
- } from "react";
15
- import AsyncStorage from "@react-native-async-storage/async-storage";
16
- //import { UserOut, UsersService, AuthenticationService } from "@api";
17
-
18
- interface UserOut {
19
-
20
- }
21
-
22
- interface AuthContextType {
23
- user: UserOut | null;
24
- token: string | null;
25
- isAuthenticated: boolean;
26
- isLoading: boolean;
27
- login: (token: string) => Promise<void>;
28
- logout: () => Promise<void>;
29
- updateUser: (userData: Partial<UserOut>) => void;
30
- }
31
-
32
- const AuthContext = createContext<AuthContextType | undefined>(undefined);
33
-
34
- export const AuthProvider: React.FC<{ children: ReactNode }> = ({
35
- children,
36
- }) => {
37
- const [user, setUser] = useState<UserOut | null>(null);
38
- const [token, setToken] = useState<string | null>(null);
39
- const [isLoading, setIsLoading] = useState(true);
40
-
41
- useEffect(() => {
42
- loadAuthData();
43
- }, []);
44
-
45
- const loadAuthData = async () => {
46
- try {
47
- const results = await AsyncStorage.multiGet(["access_token", "user_data"]);
48
- const storedToken = results?.[0]?.[1] ?? null;
49
- const storedUser = results?.[1]?.[1] ?? null;
50
-
51
- if (!storedToken) {
52
- setToken(null);
53
- setUser(null);
54
- await AsyncStorage.multiRemove(["access_token", "user_data"]);
55
- return;
56
- }
57
-
58
- if (storedToken && storedUser) {
59
- setToken(storedToken);
60
- setUser(JSON.parse(storedUser));
61
- } else if (storedToken && !storedUser) {
62
- // 若仅有 token,主动拉取当前用户信息
63
- setToken(storedToken);
64
- try {
65
- // const me = await UsersService.getCurrentUserApiV1UsersMeGet();
66
- //if (me?.success && me.data) {
67
- // setUser(me.data);
68
- // await AsyncStorage.setItem("user_data", JSON.stringify(me.data));
69
- //}
70
- } catch (e) {
71
- // 拉取失败则保持未登录状态
72
- console.error("Failed to fetch current user with stored token:", e);
73
- }
74
- }
75
- } catch (error) {
76
- console.error("Failed to load auth data:", error);
77
- } finally {
78
- setIsLoading(false);
79
- }
80
- };
81
-
82
- const login = async (newToken: string) => {
83
- try {
84
- setToken(newToken);
85
- // 统一写入 access_token,供 OpenAPI 读取并自动携带
86
- await AsyncStorage.setItem("access_token", newToken);
87
- // 登录后拉取当前用户并缓存
88
- try {
89
- const me = await UsersService.getCurrentUserApiV1UsersMeGet();
90
- if (me?.success && me.data) {
91
- setUser(me.data);
92
- await AsyncStorage.setItem("user_data", JSON.stringify(me.data));
93
- }
94
- } catch (e) {
95
- console.error("Fetch current user after login failed:", e);
96
- }
97
- } catch (error) {
98
- console.error("Login failed:", error);
99
- throw error;
100
- }
101
- };
102
-
103
- const logout = async () => {
104
- try {
105
- // await AuthenticationService.logoutApiV1AuthLogoutPost(true);
106
- } catch(error) {
107
- console.warn('Logout failed:', error);
108
- }
109
- // remove token
110
- setToken(null);
111
- setUser(null);
112
- await AsyncStorage.multiRemove(["auth_token", "access_token", "user_data"]);
113
- };
114
-
115
- const updateUser = (userData: Partial<UserOut>) => {
116
- if (user) {
117
- const updatedUser = { ...user, ...userData };
118
- setUser(updatedUser);
119
- AsyncStorage.setItem("user_data", JSON.stringify(updatedUser));
120
- }
121
- };
122
-
123
- const value: AuthContextType = {
124
- user,
125
- token,
126
- isAuthenticated: !!token && !!user,
127
- isLoading,
128
- login,
129
- logout,
130
- updateUser,
131
- };
132
-
133
- return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
134
- };
135
-
136
- export const useAuth = (): AuthContextType => {
137
- const context = useContext(AuthContext);
138
- if (context === undefined) {
139
- throw new Error("useAuth must be used within an AuthProvider");
140
- }
141
- return context;
142
- };
@@ -1,12 +0,0 @@
1
- import '@expo/metro-runtime';
2
- import { registerRootComponent } from 'expo';
3
- import { ExpoRoot } from 'expo-router';
4
-
5
- // 显式定义 App 组件
6
- export function App() {
7
- // eslint-disable-next-line no-undef
8
- const ctx = require.context('./app');
9
- return <ExpoRoot context={ctx} />;
10
- }
11
-
12
- registerRootComponent(App);