Skip to content

Commit

Permalink
fix: optimize storage key definition
Browse files Browse the repository at this point in the history
  • Loading branch information
klxiaoniu committed Jan 20, 2025
1 parent 0f75555 commit 0e4e414
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
6 changes: 4 additions & 2 deletions app/(guest)/unified-auth-login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ThemedView } from '@/components/ThemedView';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Text } from '@/components/ui/text';
import { YMT_ACCESS_TOKEN_KEY, YMT_USERNAME } from '@/lib/constants';
import YMTLogin from '@/lib/ymt-login';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Stack, router } from 'expo-router';
Expand Down Expand Up @@ -58,12 +59,13 @@ const UnifiedLoginPage: React.FC = () => {

try {
// 调用统一身份认证登录逻辑
// !! 注意:此处调用的是一码通登录。后续如有其他需求再进行改进 !!
const { name, accessToken } = await ymtLogin.current!.login(account, accountPassword);

// 存储所需的信息
await AsyncStorage.multiSet([
['accessToken', accessToken],
['name', name],
[YMT_ACCESS_TOKEN_KEY, accessToken],
[YMT_USERNAME, name],
]);

console.log('登录成功:', name, accessToken);
Expand Down
16 changes: 9 additions & 7 deletions app/(root)/(tabs)/qrcode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { Text } from '@/components/ui/text';
import { YMT_ACCESS_TOKEN_KEY, YMT_USERNAME } from '@/lib/constants';
import YMTLogin, { IdentifyRespData, PayCodeRespData } from '@/lib/ymt-login';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { router, useFocusEffect } from 'expo-router';
Expand Down Expand Up @@ -39,8 +40,8 @@ export default function YiMaTongPage() {
// 读取本地数据
async function getLocalData() {
try {
const storedAccessToken = await AsyncStorage.getItem('accessToken');
const storedName = await AsyncStorage.getItem('name');
const storedAccessToken = await AsyncStorage.getItem(YMT_ACCESS_TOKEN_KEY);
const storedName = await AsyncStorage.getItem(YMT_USERNAME);

setAccessToken(storedAccessToken);
setName(storedName);
Expand All @@ -49,7 +50,8 @@ export default function YiMaTongPage() {
refresh();
} catch (error) {
console.error('读取本地数据失败:', error);
await AsyncStorage.removeItem('accessToken');
await AsyncStorage.removeItem(YMT_ACCESS_TOKEN_KEY);
await AsyncStorage.removeItem(YMT_USERNAME);
}
}
getLocalData();
Expand All @@ -70,8 +72,8 @@ export default function YiMaTongPage() {

async function logout() {
Alert.alert(
'确认登出',
'您确定要登出吗?',
'确认退出',
'您确定要退出吗?',
[
{
text: '取消',
Expand All @@ -80,7 +82,7 @@ export default function YiMaTongPage() {
{
text: '确定',
onPress: async () => {
ymtLogin.logout();
await AsyncStorage.removeItem(YMT_ACCESS_TOKEN_KEY);
setAccessToken(null);
},
},
Expand All @@ -107,7 +109,7 @@ export default function YiMaTongPage() {

<CardFooter className="flex-row gap-4">
<Button onPress={logout} className="width-full flex-5">
<Text>登出</Text>
<Text>退出</Text>
</Button>
<Button onPress={refresh} className="width-full flex-1">
<Text>刷新</Text>
Expand Down
17 changes: 12 additions & 5 deletions lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
export const JWCH_ID_KEY = 'id';
export const JWCH_COOKIES_KEY = 'cookies';
export const JWCH_USER_ID_KEY = 'user_id';
export const JWCH_USER_PASSWORD_KEY = 'user_password';
export const JWCH_USER_INFO_KEY = 'user_info';
// 服务端Token
export const ACCESS_TOKEN_KEY = 'access_token';
export const REFRESH_TOKEN_KEY = 'refresh_token';

// 本科生教务系统
export const JWCH_ID_KEY = 'jwch_id';
export const JWCH_COOKIES_KEY = 'jwch_cookies';
export const JWCH_USER_ID_KEY = 'jwch_user_id';
export const JWCH_USER_PASSWORD_KEY = 'jwch_user_password';
export const JWCH_USER_INFO_KEY = 'jwch_user_info';

// 一码通
export const YMT_ACCESS_TOKEN_KEY = 'ymt_access_token';
export const YMT_USERNAME = 'ymt_username'; // 姓名

export const NAV_THEME = {
light: {
background: 'hsl(0 0% 100%)', // background
Expand Down
6 changes: 0 additions & 6 deletions lib/ymt-login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import AsyncStorage from '@react-native-async-storage/async-storage';

const YMT_URLS = {
LOGIN: 'https://oss.fzu.edu.cn/api/qr/login/getAccessToken',
PAY_CODE: 'https://oss.fzu.edu.cn/api/qr/deal/getQrCode',
Expand Down Expand Up @@ -124,10 +122,6 @@ class YMTLogin {

return identifyCodeData.data;
}
// 退出登录
async logout() {
await AsyncStorage.removeItem('accessToken');
}
}

export default YMTLogin;

0 comments on commit 0e4e414

Please sign in to comment.