u
This commit is contained in:
1 parent
89aaef1d6b
commit
6d54a9e407
192 files changed
+971
-51708
No files matched your search
@@ -6,13 +6,14 @@ import { ChevronRightIcon } from "lucide-react-native";
|
||||
import { ListGroup, PressableFeedback, Dialog, Button } from "heroui-native";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import { withUniwind } from "uniwind";
|
||||
import { useUserStore } from "@/store";
|
||||
import { useUserStore, useFamilyStore } from "@/store";
|
||||
import { useRouter } from "expo-router";
|
||||
const StyledIonicons = withUniwind(Ionicons);
|
||||
|
||||
export default function My() {
|
||||
const router = useRouter();
|
||||
const userStore = useUserStore();
|
||||
const familyStore = useFamilyStore();
|
||||
|
||||
const [openLogout, setOpenLogout] = useState(false);
|
||||
|
||||
@@ -21,6 +22,7 @@ export default function My() {
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
userStore.clear();
|
||||
familyStore.clear();
|
||||
router.replace("/auth/login");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import { useState } from "react";
|
||||
import { FadeIn } from "react-native-reanimated";
|
||||
import { useUserStore } from "@/store";
|
||||
import { useRouter } from "expo-router";
|
||||
import ExpoQQ from "expo-qq";
|
||||
import ExpoWeChat from "expo-wechat";
|
||||
// import ExpoQQ from "expo-qq";
|
||||
// import ExpoWeChat from "expo-wechat";
|
||||
import { useEvent } from "expo";
|
||||
import { toast } from "sonner-native";
|
||||
import { loginQqApi, loginWxApi } from "@/request/api";
|
||||
@@ -17,11 +17,6 @@ export default function Login() {
|
||||
const router = useRouter();
|
||||
const userStore = useUserStore();
|
||||
|
||||
// 登录
|
||||
const onLoginFinished = useEvent(ExpoQQ, "onLoginFinished");
|
||||
const onGetUserInfo = useEvent(ExpoQQ, "onGetUserInfo");
|
||||
const authResult = useEvent(ExpoWeChat, "onAuthResult");
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [isAgreed, setIsAgreed] = useState(true);
|
||||
const [form, setForm] = useState({
|
||||
@@ -30,188 +25,196 @@ export default function Login() {
|
||||
code: "",
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
ExpoQQ.init("102826474", "");
|
||||
ExpoWeChat.registerApp("wxdab3e21a1f7e392f", "");
|
||||
}, []);
|
||||
// 登录
|
||||
// const onLoginFinished = useEvent(ExpoQQ, "onLoginFinished");
|
||||
// const onGetUserInfo = useEvent(ExpoQQ, "onGetUserInfo");
|
||||
// const authResult = useEvent(ExpoWeChat, "onAuthResult");
|
||||
|
||||
// 微信登录
|
||||
async function handleLoginWechat() {
|
||||
if (loading) {
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
// useEffect(() => {
|
||||
// ExpoQQ.init("102826474", "");
|
||||
// ExpoWeChat.registerApp("wxdab3e21a1f7e392f", "");
|
||||
// }, []);
|
||||
|
||||
// 检查微信是否安装
|
||||
const isInstalled = await ExpoWeChat.isWXAppInstalled();
|
||||
if (!isInstalled) {
|
||||
toast.warning(onLoginFinished?.errorMessage || "请先安装微信");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
// // 微信登录
|
||||
// async function handleLoginWechat() {
|
||||
// if (loading) {
|
||||
// return;
|
||||
// }
|
||||
// setLoading(true);
|
||||
|
||||
setForm((val) => ({
|
||||
...val,
|
||||
type: 0,
|
||||
}));
|
||||
// // 检查微信是否安装
|
||||
// const isInstalled = await ExpoWeChat.isWXAppInstalled();
|
||||
// if (!isInstalled) {
|
||||
// toast.warning(onLoginFinished?.errorMessage || "请先安装微信");
|
||||
// setLoading(false);
|
||||
// return;
|
||||
// }
|
||||
|
||||
ExpoWeChat.sendAuthRequest("snsapi_userinfo", "state123");
|
||||
}
|
||||
// setForm((val) => ({
|
||||
// ...val,
|
||||
// type: 0,
|
||||
// }));
|
||||
|
||||
// QQ 登录
|
||||
async function handleLoginQQ() {
|
||||
if (loading) {
|
||||
return;
|
||||
}
|
||||
// ExpoWeChat.sendAuthRequest("snsapi_userinfo", "state123");
|
||||
// }
|
||||
|
||||
setLoading(true);
|
||||
|
||||
setForm((val) => ({
|
||||
...val,
|
||||
type: 1,
|
||||
}));
|
||||
await ExpoQQ.login(["get_user_info"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {"code": 1000, "data": {"avatar": "http://thirdqq.qlogo.cn/ek_qqapp/AQHVialeUibE42oV8RvTTVZ4tyRlPYeJrUibWTwtCPfV68E4rSzn9cw00GkXQ3QDTiawwhjoqQb7/100", "nickname": "Zhang", "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo3OTY0NTY3MTI3ODU5ODl9LCJpYXQiOjE3NzY1ODU2MDR9.ZwmN5_0MGrP9dYpn3quK94HXMG3QYKmAq7wnP5X68dY", "userId": 796456712785989}, "isSuccess": true, "message": "success", "track_id": "djIRo23SZlA160zBTH2DX-kxeuLApKTNyZdxqqauSMk"}
|
||||
*/
|
||||
|
||||
// function handleTest() {
|
||||
// // QQ 登录
|
||||
// async function handleLoginQQ() {
|
||||
// if (loading) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// setLoading(true);
|
||||
|
||||
// setTimeout(() => {
|
||||
// requestAnimationFrame(() => {
|
||||
// userStore.setUserInfo({
|
||||
// id: 779895419121733,
|
||||
// token: "ZwmN5_0MGrP9dYpn3quK94HXMG3QYKmAq7wnP5X68dY",
|
||||
// nickname: "Zhang",
|
||||
// avatar: "http://thirdqq.qlogo.cn/ek_qqapp/AQHVialeUibE42oV8RvTTVZ4tyRlPYeJrUibWTwtCPfV68E4rSzn9cw00GkXQ3QDTiawwhjoqQb7/100",
|
||||
// });
|
||||
// userStore.setAuthExpired(false);
|
||||
// router.replace("/(tabs)/home");
|
||||
// });
|
||||
// setLoading(false);
|
||||
// }, 2000);
|
||||
// setForm((val) => ({
|
||||
// ...val,
|
||||
// type: 1,
|
||||
// }));
|
||||
// await ExpoQQ.login(["get_user_info"]);
|
||||
// }
|
||||
|
||||
useEffect(() => {
|
||||
if (!onLoginFinished) {
|
||||
/**
|
||||
* {"code": 1000, "data": {"avatar": "http://thirdqq.qlogo.cn/ek_qqapp/AQHVialeUibE42oV8RvTTVZ4tyRlPYeJrUibWTwtCPfV68E4rSzn9cw00GkXQ3QDTiawwhjoqQb7/100",
|
||||
* "nickname": "Zhang", "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo3OTY0NTY3MTI3ODU5ODl9LCJpYXQiOjE3NzY1ODU2MDR9.ZwmN5_0MGrP9dYpn3quK94HXMG3QYKmAq7wnP5X68dY", "userId": 796456712785989}, "isSuccess": true, "message": "success", "track_id": "djIRo23SZlA160zBTH2DX-kxeuLApKTNyZdxqqauSMk"}
|
||||
*/
|
||||
|
||||
function handleTest() {
|
||||
if (loading) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!onLoginFinished.success) {
|
||||
toast.warning(onLoginFinished?.errorMessage || "授权失败,请检查手机是否安装 QQ 或确认授权");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
|
||||
const fetchUserInfo = async () => {
|
||||
try {
|
||||
const tokenInfo = await ExpoQQ.getLoginTokenInfo();
|
||||
if (!tokenInfo.openId) {
|
||||
toast.warning(tokenInfo?.errorMessage || "授权失败,请检查手机是否安装 QQ 或确认授权");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setForm((val) => ({
|
||||
...val,
|
||||
openid: String(tokenInfo.openId),
|
||||
}));
|
||||
|
||||
await ExpoQQ.sendGetUserInfoReq();
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchUserInfo();
|
||||
}, [onLoginFinished]);
|
||||
|
||||
// QQ登录
|
||||
useEffect(() => {
|
||||
const handleLogin = async () => {
|
||||
if (!onGetUserInfo) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const loginRes = await loginQqApi({
|
||||
openid: form.openid,
|
||||
nickname: onGetUserInfo.nickname || "",
|
||||
avatar: onGetUserInfo.figureurlQq_2 || onGetUserInfo.figureurl_2,
|
||||
setTimeout(() => {
|
||||
requestAnimationFrame(() => {
|
||||
userStore.setUserInfo({
|
||||
id: 779895419121733,
|
||||
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo3OTY0NTY3MTI3ODU5ODl9LCJpYXQiOjE3NzY1ODU2MDR9.ZwmN5_0MGrP9dYpn3quK94HXMG3QYKmAq7wnP5X68dY",
|
||||
nickname: "Zhang",
|
||||
avatar:
|
||||
"http://thirdqq.qlogo.cn/ek_qqapp/AQHVialeUibE42oV8RvTTVZ4tyRlPYeJrUibWTwtCPfV68E4rSzn9cw00GkXQ3QDTiawwhjoqQb7/100",
|
||||
});
|
||||
userStore.setAuthExpired(false);
|
||||
// router.replace("/(tabs)/home");
|
||||
router.replace("/family/entry");
|
||||
});
|
||||
setLoading(false);
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
console.log(loginRes);
|
||||
// useEffect(() => {
|
||||
// if (!onLoginFinished) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
if (loginRes.isSuccess) {
|
||||
requestAnimationFrame(() => {
|
||||
userStore.setUserInfo({
|
||||
token: loginRes.data.token,
|
||||
id: loginRes.data.userId,
|
||||
nickname: loginRes.data.nickname,
|
||||
avatar: loginRes.data.avatar,
|
||||
});
|
||||
// if (!onLoginFinished.success) {
|
||||
// toast.warning(onLoginFinished?.errorMessage || "授权失败,请检查手机是否安装 QQ 或确认授权");
|
||||
// setLoading(false);
|
||||
// return;
|
||||
// }
|
||||
|
||||
userStore.setAuthExpired(false);
|
||||
router.replace("/(tabs)/home");
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("登录接口出错", err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
// const fetchUserInfo = async () => {
|
||||
// try {
|
||||
// const tokenInfo = await ExpoQQ.getLoginTokenInfo();
|
||||
// if (!tokenInfo.openId) {
|
||||
// toast.warning(tokenInfo?.errorMessage || "授权失败,请检查手机是否安装 QQ 或确认授权");
|
||||
// setLoading(false);
|
||||
// return;
|
||||
// }
|
||||
|
||||
handleLogin();
|
||||
}, [onGetUserInfo]);
|
||||
// setForm((val) => ({
|
||||
// ...val,
|
||||
// openid: String(tokenInfo.openId),
|
||||
// }));
|
||||
|
||||
// 微信登录
|
||||
useEffect(() => {
|
||||
if (!authResult) return;
|
||||
// await ExpoQQ.sendGetUserInfoReq();
|
||||
// } catch (e) {
|
||||
// console.log(e);
|
||||
|
||||
const handleLogin = async () => {
|
||||
if (authResult.errorCode === 0) {
|
||||
try {
|
||||
const loginRes = await loginWxApi({
|
||||
code: authResult.code,
|
||||
});
|
||||
// setLoading(false);
|
||||
// }
|
||||
// };
|
||||
|
||||
if (loginRes.isSuccess) {
|
||||
requestAnimationFrame(() => {
|
||||
userStore.setUserInfo({
|
||||
token: loginRes.data.token,
|
||||
id: loginRes.data.userId,
|
||||
nickname: loginRes.data.nickname,
|
||||
avatar: loginRes.data.avatar,
|
||||
});
|
||||
// fetchUserInfo();
|
||||
// }, [onLoginFinished]);
|
||||
|
||||
userStore.setAuthExpired(false);
|
||||
router.replace("/(tabs)/home");
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("登录接口出错", err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
} else {
|
||||
console.log("授权失败:", authResult.errorMessage);
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
// // QQ登录
|
||||
// useEffect(() => {
|
||||
// const handleLogin = async () => {
|
||||
// if (!onGetUserInfo) {
|
||||
// setLoading(false);
|
||||
// return;
|
||||
// }
|
||||
|
||||
handleLogin();
|
||||
}, [authResult]);
|
||||
// try {
|
||||
// const loginRes = await loginQqApi({
|
||||
// openid: form.openid,
|
||||
// nickname: onGetUserInfo.nickname || "",
|
||||
// avatar: onGetUserInfo.figureurlQq_2 || onGetUserInfo.figureurl_2,
|
||||
// });
|
||||
|
||||
// console.log(loginRes);
|
||||
|
||||
// if (loginRes.isSuccess) {
|
||||
// requestAnimationFrame(() => {
|
||||
// userStore.setUserInfo({
|
||||
// token: loginRes.data.token,
|
||||
// id: loginRes.data.userId,
|
||||
// nickname: loginRes.data.nickname,
|
||||
// avatar: loginRes.data.avatar,
|
||||
// });
|
||||
|
||||
// userStore.setAuthExpired(false);
|
||||
// router.replace("/(tabs)/home");
|
||||
// });
|
||||
// }
|
||||
// } catch (err) {
|
||||
// console.error("登录接口出错", err);
|
||||
// } finally {
|
||||
// setLoading(false);
|
||||
// }
|
||||
// };
|
||||
|
||||
// handleLogin();
|
||||
// }, [onGetUserInfo]);
|
||||
|
||||
// // 微信登录
|
||||
// useEffect(() => {
|
||||
// if (!authResult) return;
|
||||
|
||||
// const handleLogin = async () => {
|
||||
// if (authResult.errorCode === 0) {
|
||||
// try {
|
||||
// const loginRes = await loginWxApi({
|
||||
// code: authResult.code,
|
||||
// });
|
||||
|
||||
// if (loginRes.isSuccess) {
|
||||
// requestAnimationFrame(() => {
|
||||
// userStore.setUserInfo({
|
||||
// token: loginRes.data.token,
|
||||
// id: loginRes.data.userId,
|
||||
// nickname: loginRes.data.nickname,
|
||||
// avatar: loginRes.data.avatar,
|
||||
// });
|
||||
|
||||
// userStore.setAuthExpired(false);
|
||||
// router.replace("/(tabs)/home");
|
||||
// });
|
||||
// }
|
||||
// } catch (err) {
|
||||
// console.error("登录接口出错", err);
|
||||
// } finally {
|
||||
// setLoading(false);
|
||||
// }
|
||||
// } else {
|
||||
// console.log("授权失败:", authResult.errorMessage);
|
||||
// setLoading(false);
|
||||
// }
|
||||
// };
|
||||
|
||||
// handleLogin();
|
||||
// }, [authResult]);
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -231,7 +234,7 @@ export default function Login() {
|
||||
</View>
|
||||
|
||||
<View className="flex flex-col gap-4">
|
||||
<Button onPress={handleLoginWechat}>
|
||||
{/* <Button onPress={handleLoginWechat}>
|
||||
{loading ? (
|
||||
<Spinner entering={FadeIn.delay(50)} color="white" />
|
||||
) : (
|
||||
@@ -243,6 +246,11 @@ export default function Login() {
|
||||
<Button variant="outline" onPress={handleLoginQQ}>
|
||||
{loading ? <Spinner entering={FadeIn.delay(50)} /> : <IconQq />}
|
||||
<Button.Label>QQ</Button.Label>
|
||||
</Button> */}
|
||||
|
||||
<Button variant="outline" onPress={handleTest}>
|
||||
{loading ? <Spinner entering={FadeIn.delay(50)} /> : <IconQq />}
|
||||
<Button.Label>Test</Button.Label>
|
||||
</Button>
|
||||
</View>
|
||||
|
||||
|
||||
@@ -0,0 +1,431 @@
|
||||
import { forwardRef, useState, useRef, useEffect } from "react";
|
||||
import { TouchableOpacity, Text, View } from "react-native";
|
||||
import { Stack } from "expo-router";
|
||||
import EmptyState from "@/components/EmptyStatus";
|
||||
import {
|
||||
Button,
|
||||
Separator,
|
||||
Spinner,
|
||||
Chip,
|
||||
BottomSheet,
|
||||
Label,
|
||||
TextField,
|
||||
Input,
|
||||
InputOTP,
|
||||
type InputOTPRef,
|
||||
PressableFeedback,
|
||||
useBottomSheetAwareHandlers,
|
||||
} from "heroui-native";
|
||||
import { PlusIcon, HousePlusIcon } from "lucide-react-native";
|
||||
import { getUniqueIdApi, loadDataApi, loadDataBySqlApi, saveDataApi } from "@/request/api";
|
||||
import { useUserStore, useFamilyStore } from "@/store";
|
||||
import { toast } from "sonner-native";
|
||||
import { generateCode } from "@/utils/common";
|
||||
import { FadeIn } from "react-native-reanimated";
|
||||
import { Keyboard } from "react-native";
|
||||
import { useRouter } from "expo-router";
|
||||
|
||||
const BottomSheetTextInput = forwardRef<any, React.ComponentProps<typeof Input>>((props, ref) => {
|
||||
const { onFocus, onBlur } = useBottomSheetAwareHandlers();
|
||||
|
||||
return (
|
||||
<Input
|
||||
ref={ref}
|
||||
{...props}
|
||||
onFocus={(e) => {
|
||||
onFocus(e);
|
||||
props.onFocus?.(e);
|
||||
}}
|
||||
onBlur={(e) => {
|
||||
onBlur(e);
|
||||
props.onBlur?.(e);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
const BottomSheetTextInputOTP = forwardRef<any, React.ComponentProps<typeof InputOTP>>(
|
||||
(props, ref) => {
|
||||
const { onFocus, onBlur } = useBottomSheetAwareHandlers();
|
||||
|
||||
return (
|
||||
<InputOTP
|
||||
ref={ref}
|
||||
{...props}
|
||||
onFocus={(e) => {
|
||||
onFocus(e);
|
||||
props.onFocus?.(e);
|
||||
}}
|
||||
onBlur={(e) => {
|
||||
onBlur(e);
|
||||
props.onBlur?.(e);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
export default function EntryFamily() {
|
||||
const router = useRouter();
|
||||
const userStore = useUserStore();
|
||||
const familyStore = useFamilyStore();
|
||||
|
||||
// 加入
|
||||
const [showJoin, setShowJoin] = useState(false);
|
||||
const [joinLoading, setJoinLoading] = useState(false);
|
||||
const [joinForm, setJoinForm] = useState({
|
||||
invite_code: "",
|
||||
});
|
||||
const joinInputRef = useRef<InputOTPRef>(null);
|
||||
|
||||
// 新建
|
||||
const [showCreate, setShowCreate] = useState(false);
|
||||
const [createForm, setCreateForm] = useState({
|
||||
name: "",
|
||||
});
|
||||
const [createLoading, setCreateLoading] = useState(false);
|
||||
const createInputRef = useRef<any>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (showCreate) {
|
||||
requestAnimationFrame(() => {
|
||||
createInputRef.current?.focus();
|
||||
});
|
||||
} else {
|
||||
setCreateForm({
|
||||
name: "",
|
||||
});
|
||||
setCreateLoading(false);
|
||||
Keyboard.dismiss();
|
||||
}
|
||||
}, [showCreate]);
|
||||
|
||||
useEffect(() => {
|
||||
if (showJoin) {
|
||||
requestAnimationFrame(() => {
|
||||
joinInputRef.current?.focus();
|
||||
});
|
||||
} else {
|
||||
setJoinForm({
|
||||
invite_code: "",
|
||||
});
|
||||
setJoinLoading(false);
|
||||
Keyboard.dismiss();
|
||||
}
|
||||
}, [showJoin]);
|
||||
|
||||
async function handleCreate() {
|
||||
if (createLoading) return;
|
||||
|
||||
if (createForm.name === "") {
|
||||
toast.warning("空间名称不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
setCreateLoading(true);
|
||||
|
||||
try {
|
||||
// 检查是否存在该家庭
|
||||
const memberRes = await loadDataApi({
|
||||
view_name: "b_family_member",
|
||||
search_condition: "user_id = ?",
|
||||
search_columns: ["family_id"],
|
||||
order_by: "",
|
||||
args: [userStore.userInfo.id],
|
||||
});
|
||||
|
||||
if (!memberRes.isSuccess) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (memberRes.data.length > 0) {
|
||||
toast.warning("你已经有家庭了");
|
||||
|
||||
familyStore.setCurrentFamily({
|
||||
family_id: memberRes.data[0].family_id,
|
||||
});
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
router.replace("/home");
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let inviteCode = "";
|
||||
let isExist = true;
|
||||
|
||||
// 循环生成直到不存在
|
||||
while (isExist) {
|
||||
inviteCode = generateCode(6);
|
||||
|
||||
const res = await loadDataBySqlApi({
|
||||
sql: `SELECT 1 FROM b_family WHERE invite_code = ? LIMIT 1`,
|
||||
args: [inviteCode],
|
||||
});
|
||||
|
||||
if (!res.isSuccess) {
|
||||
throw new Error("校验邀请码失败");
|
||||
}
|
||||
|
||||
isExist = res.data.length > 0;
|
||||
}
|
||||
|
||||
// 创建
|
||||
const autoRes = await getUniqueIdApi(2);
|
||||
if (!autoRes.isSuccess) {
|
||||
return;
|
||||
}
|
||||
|
||||
const familyId = autoRes.data[0];
|
||||
const familyMemberId = autoRes.data[1];
|
||||
|
||||
// 插入空间
|
||||
const requestData = [];
|
||||
|
||||
requestData.push({
|
||||
table_name: "b_family",
|
||||
key_field: "id",
|
||||
inserts: [
|
||||
{
|
||||
id: familyId,
|
||||
name: createForm.name,
|
||||
invite_code: inviteCode,
|
||||
owner_id: userStore.userInfo.id,
|
||||
},
|
||||
],
|
||||
updates: [],
|
||||
deletes: [],
|
||||
});
|
||||
|
||||
// 插入空间用户
|
||||
requestData.push({
|
||||
table_name: "b_family_member",
|
||||
key_field: "id",
|
||||
inserts: [
|
||||
{
|
||||
id: familyMemberId,
|
||||
family_id: familyId,
|
||||
user_id: userStore.userInfo.id,
|
||||
role: 0,
|
||||
nickname: userStore.userInfo.nickname,
|
||||
},
|
||||
],
|
||||
updates: [],
|
||||
deletes: [],
|
||||
});
|
||||
|
||||
const saveRes = await saveDataApi(requestData);
|
||||
|
||||
if (saveRes.isSuccess) {
|
||||
toast.success("创建成功");
|
||||
setShowCreate(false);
|
||||
|
||||
setCreateForm({
|
||||
name: "",
|
||||
});
|
||||
|
||||
familyStore.setCurrentFamily({
|
||||
family_id: familyId,
|
||||
});
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
router.replace("/home");
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
} finally {
|
||||
setCreateLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleJoin() {
|
||||
if (joinLoading) return;
|
||||
|
||||
const code = joinForm.invite_code.trim();
|
||||
|
||||
if (!code) {
|
||||
toast.warning("邀请码不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!/^[A-Za-z0-9]{6}$/.test(code)) {
|
||||
toast.warning("邀请码必须为6位字母或数字");
|
||||
return;
|
||||
}
|
||||
|
||||
setJoinLoading(true);
|
||||
|
||||
try {
|
||||
const res = await loadDataBySqlApi({
|
||||
sql: ` SELECT
|
||||
s.id,
|
||||
s.name,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM space_member sm
|
||||
WHERE sm.space_id = s.id
|
||||
AND sm.user_id = ?
|
||||
) AS is_joined
|
||||
FROM space s
|
||||
WHERE s.invite_code = ?
|
||||
LIMIT 1;
|
||||
`,
|
||||
args: [userStore.userInfo.id, code],
|
||||
});
|
||||
|
||||
if (!res.isSuccess) return;
|
||||
|
||||
const row = res.data?.[0];
|
||||
|
||||
if (!row) {
|
||||
toast.warning("邀请码无效");
|
||||
return;
|
||||
}
|
||||
|
||||
if (row.is_joined) {
|
||||
toast.warning("你已经在该空间了");
|
||||
return;
|
||||
}
|
||||
|
||||
const autoRes = await getUniqueIdApi(1);
|
||||
if (!autoRes.isSuccess) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置是否是默认空间
|
||||
let isDefault = false;
|
||||
|
||||
const defaultRes = await loadDataApi({
|
||||
view_name: "v_user_spaces",
|
||||
search_condition: `v_user_id = ?`,
|
||||
search_columns: ["v_is_default"],
|
||||
order_by: "",
|
||||
args: [userStore.userInfo.id],
|
||||
});
|
||||
|
||||
if (!defaultRes.isSuccess) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (defaultRes.data.length == 0) {
|
||||
isDefault = true;
|
||||
}
|
||||
|
||||
// 加入
|
||||
const requestData = [];
|
||||
|
||||
requestData.push({
|
||||
table_name: "space_member",
|
||||
key_field: "id",
|
||||
inserts: [
|
||||
{
|
||||
id: autoRes.data[0],
|
||||
space_id: row.id,
|
||||
user_id: userStore.userInfo.id,
|
||||
role: 1,
|
||||
is_default: isDefault ? 1 : 0,
|
||||
sort_number: defaultRes.data.length,
|
||||
member_name: userStore.userInfo.nickname,
|
||||
},
|
||||
],
|
||||
updates: [],
|
||||
deletes: [],
|
||||
});
|
||||
|
||||
const saveRes = await saveDataApi(requestData);
|
||||
|
||||
if (saveRes.isSuccess) {
|
||||
toast.success("加入成功");
|
||||
setShowJoin(false);
|
||||
|
||||
setJoinForm({
|
||||
invite_code: "",
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
toast.error("加入失败,请稍后重试");
|
||||
} finally {
|
||||
setJoinLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
animation: "fade",
|
||||
}}
|
||||
/>
|
||||
|
||||
<View className="flex-1 px-6 justify-center">
|
||||
<EmptyState title="请 创建 / 加入 家庭" style={{ flex: 0 }} />
|
||||
|
||||
<View className="gap-4 mt-6">
|
||||
<Button
|
||||
onPress={() => {
|
||||
setShowCreate(true);
|
||||
}}
|
||||
>
|
||||
<PlusIcon color="#fff" size={20} />
|
||||
<Button.Label>创建家庭</Button.Label>
|
||||
</Button>
|
||||
|
||||
<Button variant="outline">
|
||||
<HousePlusIcon size={20} />
|
||||
<Button.Label>加入家庭</Button.Label>
|
||||
</Button>
|
||||
|
||||
<View className="flex-row items-center my-4">
|
||||
<View className="flex-1 h-px bg-muted/30" />
|
||||
<Text className="text-xs text-muted px-3">或</Text>
|
||||
<View className="flex-1 h-px bg-muted/30" />
|
||||
</View>
|
||||
|
||||
<TouchableOpacity onPress={() => {}} className="items-center py-2">
|
||||
<Text className="text-sm text-muted">重新登录</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
<BottomSheet
|
||||
isOpen={showCreate}
|
||||
onOpenChange={(val) => {
|
||||
setShowCreate(val);
|
||||
}}
|
||||
>
|
||||
<BottomSheet.Portal>
|
||||
<BottomSheet.Overlay />
|
||||
<BottomSheet.Content
|
||||
animation="disabled"
|
||||
enableBlurKeyboardOnGesture
|
||||
keyboardBlurBehavior="restore"
|
||||
>
|
||||
<View>
|
||||
<TextField>
|
||||
<Label>家庭名称</Label>
|
||||
<BottomSheetTextInput
|
||||
ref={createInputRef}
|
||||
placeholder="请输入家庭名称"
|
||||
value={createForm.name}
|
||||
onChangeText={(val) =>
|
||||
setCreateForm({
|
||||
name: val,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</TextField>
|
||||
|
||||
<Button className="mt-3" onPress={handleCreate}>
|
||||
{createLoading && <Spinner entering={FadeIn.delay(50)} color="white" />}
|
||||
<Button.Label>创建</Button.Label>
|
||||
</Button>
|
||||
</View>
|
||||
</BottomSheet.Content>
|
||||
</BottomSheet.Portal>
|
||||
</BottomSheet>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,18 +1,26 @@
|
||||
import { useEffect } from "react";
|
||||
import { useRouter } from "expo-router";
|
||||
import { useUserStore } from "@/store";
|
||||
import { useUserStore, useFamilyStore } from "@/store";
|
||||
import * as SplashScreen from "expo-splash-screen";
|
||||
|
||||
export default function Index() {
|
||||
const router = useRouter();
|
||||
const userStore = useUserStore();
|
||||
|
||||
useEffect(() => {
|
||||
const run = async () => {
|
||||
if (!userStore.isLogged()) {
|
||||
const { isLogged } = useUserStore.getState();
|
||||
const { inFamily } = useFamilyStore.getState();
|
||||
|
||||
console.log(useFamilyStore.getState().currentFamily);
|
||||
|
||||
if (!isLogged()) {
|
||||
requestAnimationFrame(() => {
|
||||
router.replace("/auth/login");
|
||||
});
|
||||
} else if (!inFamily()) {
|
||||
requestAnimationFrame(() => {
|
||||
router.replace("/family/entry");
|
||||
});
|
||||
} else {
|
||||
requestAnimationFrame(() => {
|
||||
router.replace("/(tabs)/home");
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { ScrollView, Text, Image } from "react-native";
|
||||
import { View, Text, Image, StyleProp, ViewStyle } from "react-native";
|
||||
|
||||
interface EmptyStateProps {
|
||||
type?: "empty" | "error" | "404" | "network" | "loading";
|
||||
title?: string;
|
||||
description?: string;
|
||||
footer?: React.ReactNode;
|
||||
|
||||
className?: string;
|
||||
style?: StyleProp<ViewStyle>;
|
||||
}
|
||||
|
||||
const images = {
|
||||
@@ -28,31 +31,32 @@ export default function EmptyState({
|
||||
title,
|
||||
description,
|
||||
footer,
|
||||
}: EmptyStateProps) {
|
||||
className,
|
||||
style,
|
||||
}: EmptyStateProps & { style?: StyleProp<ViewStyle> }) {
|
||||
const finalTitle = title ?? defaultTitles[type];
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
contentContainerStyle={{
|
||||
flexGrow: 1,
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
padding: 16,
|
||||
}}
|
||||
showsVerticalScrollIndicator={false}
|
||||
<View
|
||||
className={className}
|
||||
style={[
|
||||
{
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
padding: 16,
|
||||
},
|
||||
style,
|
||||
]}
|
||||
>
|
||||
{/* image */}
|
||||
<Image source={images[type]} style={{ width: 224, height: 224, resizeMode: "contain" }} />
|
||||
|
||||
{/* title */}
|
||||
<Text className="text-xl font-semibold text-foreground text-center mt-4">{finalTitle}</Text>
|
||||
|
||||
{/* description */}
|
||||
{description && (
|
||||
<Text className="text-sm text-foreground/60 text-center mt-2 leading-6">{description}</Text>
|
||||
)}
|
||||
|
||||
{footer}
|
||||
</ScrollView>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
|
||||
interface Family {
|
||||
family_id: number;
|
||||
}
|
||||
|
||||
interface FamilyState {
|
||||
currentFamily: Family | null;
|
||||
|
||||
setCurrentFamily: (family: Family | null) => void;
|
||||
|
||||
clear: () => void;
|
||||
|
||||
inFamily: () => boolean;
|
||||
}
|
||||
|
||||
export const useFamilyStore = create<FamilyState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
currentFamily: null,
|
||||
|
||||
setCurrentFamily: (family) =>
|
||||
set({
|
||||
currentFamily: family,
|
||||
}),
|
||||
|
||||
clear: () =>
|
||||
set({
|
||||
currentFamily: null,
|
||||
}),
|
||||
|
||||
inFamily: () => {
|
||||
const currentFamily = get().currentFamily;
|
||||
|
||||
return currentFamily !== null && currentFamily.family_id !== 0;
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: "family-storage",
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
},
|
||||
),
|
||||
);
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./app";
|
||||
export * from "./user";
|
||||
export * from "./space";
|
||||
export * from "./family";
|
||||
@@ -1,6 +1,4 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
|
||||
interface Space {
|
||||
space_id: number;
|
||||
@@ -13,20 +11,21 @@ interface SpaceState {
|
||||
inSpace: () => boolean;
|
||||
}
|
||||
|
||||
export const useSpaceStore = create<SpaceState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
currentSpace: null,
|
||||
setCurrentSpace: (space) => set({ currentSpace: space }),
|
||||
clear: () => set({ currentSpace: null }),
|
||||
inSpace: () => {
|
||||
const currentSpace = get().currentSpace;
|
||||
return currentSpace !== null && currentSpace.space_id !== 0;
|
||||
},
|
||||
export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
currentSpace: null,
|
||||
|
||||
setCurrentSpace: (space) =>
|
||||
set({
|
||||
currentSpace: space,
|
||||
}),
|
||||
{
|
||||
name: "space-storage",
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
clear: () =>
|
||||
set({
|
||||
currentSpace: null,
|
||||
}),
|
||||
|
||||
inSpace: () => {
|
||||
const currentSpace = get().currentSpace;
|
||||
return currentSpace !== null && currentSpace.space_id !== 0;
|
||||
},
|
||||
}));
|
||||
Reference in new issue
Block a user