feat: 报名逻辑

This commit is contained in:
eric
2022-11-21 22:03:42 +08:00
parent 841b480f01
commit 78adbef9b4
20 changed files with 384 additions and 96 deletions

View File

@@ -7,6 +7,7 @@ import {
getTable,
getTableid,
isLogin,
phoneLogin,
updateTableid,
} from "../../../../utils/index";
import "./index.scss";
@@ -18,6 +19,8 @@ const details = () => {
const [bottomheight, setbottomheight] = useState(0);
let $instance: any = getCurrentInstance();
const { id } = $instance?.router?.params ?? {}; //活动id
const [isCheckin, setisCheckin] = useState(false); // 是否报名
const [openId, setopenId] = useState("");
useEffect(() => {
// 设置标题
@@ -26,6 +29,25 @@ const details = () => {
});
}, []);
useEffect(() => {
// (dataSource?.useridArr ?? []).length > 0 &&
Taro.cloud.callFunction({
name: "getOpenid",
complete: async (res: any) => {
console.log("callFunction test result: ", res);
let newopenId: any = res?.result?.event?.userInfo?.openId;
setopenId(newopenId);
console.log("dataSource ", dataSource);
// 检查是否已报名
if ((dataSource?.useridArr ?? []).includes(newopenId)) {
setisCheckin(true);
} else {
setisCheckin(false);
}
},
});
}, [dataSource]);
useEffect(() => {
const { screenHeight, safeArea = {} } = Taro.getSystemInfoSync();
// @ts-ignore
@@ -73,6 +95,18 @@ const details = () => {
},
];
const btnContent = () => {
if (isCheckin) {
return "取消报名";
} else {
return btnStatus.map((item: any) => {
let status: any = dataSource?.Status;
if (item?.value === status) {
return item?.label;
}
});
}
};
return (
<View className={`detailContent`}>
<View>
@@ -141,55 +175,98 @@ const details = () => {
<View
className={`detailBtn`}
style={{ bottom: `${bottomheight ?? 0}px` }}
onClick={() => {
let isin: any = isLogin();
if (!isin) {
//重新登录- 跳转到我的页面
Toast.fail({
message: "请先登录",
selector: "#vanToast-demo2",
duration: 2000,
onClose: () => {
// 跳转到目的页面,打开新页面
Taro.navigateTo({
url: "/pages/my/index",
});
},
});
} else {
console.log("id", id);
// 立即报名
let postData = {
// env: "any",
id: id,
tableName: "details",
objData: {
// ..._.cloneDeep(dataSource),
useridArr: ["openid"],
},
okFun: (res: any) => {
console.log("res", res);
},
};
console.log("postData", postData);
updateTableid({ ...postData });
}
}}
>
{/* style={{width:'375px'}} */}
<Button
type="primary"
block
disabled={!["1"].includes(dataSource?.Status)}
onClick={() => {
if (isCheckin) {
// 取消报名
updateTableid({
tableName: "details",
id: id,
objData: {
// ..._.cloneDeep(dataSource),
useridArr: [
...(dataSource?.useridArr ?? []).filter(
(value, index, arr) => {
return value !== openId;
}
),
],
},
okFun: (res: any) => {
// console.log("res333", res);
// 报名成功
Toast.success({
message: "取消成功",
selector: "#vanToast-demo2",
onClose: () => {
// 返回首页 [上一页]
Taro.navigateBack({
// url: "/pages/home/index",
delta: 1,
});
},
});
},
});
return false;
}
let isin: any = phoneLogin();
if (!isin) {
//重新登录- 跳转到我的页面
Toast.fail({
message: "请先登录",
selector: "#vanToast-demo2",
duration: 2000,
onClose: () => {
// 跳转到目的页面,打开新页面
Taro.navigateTo({
url: "/pages/my/index",
});
},
});
} else {
console.log("id", id);
// 立即报名
let postData = {
// env: "any",
id: id,
tableName: "details",
objData: {
// ..._.cloneDeep(dataSource),
useridArr: [
..._.cloneDeep(dataSource?.useridArr ?? []),
openId,
],
},
okFun: (res: any) => {
console.log("res333", res);
// 报名成功
Toast.success({
message: "报名成功",
selector: "#vanToast-demo2",
onClose: () => {
// 返回首页 [上一页]
Taro.navigateBack({
// url: "/pages/home/index",
delta: 1,
});
},
});
},
};
console.log("postData", postData);
updateTableid({ ...postData });
}
}}
>
{/* 立即参加 */}
{btnStatus.map((item: any) => {
let status: any = dataSource?.Status;
if (item?.value === status) {
return item?.label;
}
})}
{btnContent()}
</Button>
<Toast id="vanToast-demo2" />
</View>

View File

@@ -16,7 +16,7 @@ import "./index.module.scss";
import { Tab, Tabs } from "@antmjs/vantui";
import Taro from "@tarojs/taro";
import ActiveCard from "../components/ActiveCard";
import { getTable, isLogin } from "../../utils/index";
import { addTable, getOpenid, getTable, getTableid, isLogin, phoneLogin, updateTableid } from "../../utils/index";
const my = (props: any) => {
// const [active, setActive] = react.useState(props?.active ?? 0);
@@ -24,12 +24,22 @@ const my = (props: any) => {
const [userInfo, setuserInfo] = useState({}); //用户信息
const [detailsData, setdetailsData] = useState<any>([]); //活动详情
const [currentIndex, setcurrentIndex] = useState(0);
const [isin,setisin]=useState(false)
const [isin, setisin] = useState(true); //0初始 1未登录 2有手机号
useEffect(()=>{
let isin: any = isLogin();
setisin(isin)
},[])
useEffect(() => {
getok();
// let isin: any = isLogin();
// setisin(isin);
// getOpenid();
}, []);
const getok = async () => {
let isOk: any = await phoneLogin();
console.log("isok", isOk);
setisin(isOk)
// setisin(isOk?'2':'1');
};
// 云函数,-获取手机号
const getLogin = (e: any) => {
@@ -41,9 +51,33 @@ const my = (props: any) => {
},
})
.then((res: any) => {
console.log("res11");
console.log("res", res);
setisin(true)
console.log("res222", res);
let phoneNumber = res?.result?.event?.weRunData?.data?.phoneNumber;
let tencentcloud_uin=res?.result?.context?.tencentcloud_uin
let openId:any=res?.result?.event?.userInfo?.openId
let postData={
tableName:'userInfo',
keyValue: { openId: openId },
okFun:((res:any)=>{
console.log('q',res)
updateTableid({
id:res?.data?.[0]?._id,
tableName:"userInfo",
objData:{
tencentcloud_uin:tencentcloud_uin, //微信号
authInfo:{
authPhonenum:phoneNumber //手机号
}
},
okFun:(res:any)=>{
getok()
}
})
})
}
getTable({...postData})
// setisin(true);
});
};
@@ -64,7 +98,7 @@ const my = (props: any) => {
definePageConfig({
navigationBarTitleText: "我的",
});
getDetails();
// getDetails();
}, []);
const CardCom = () => {
@@ -122,17 +156,19 @@ const my = (props: any) => {
src="cloud://cloud1-5g5xrgza12a0dd6d.636c-cloud1-5g5xrgza12a0dd6d-1253665791/images/images-2.jpg"
/>
{!isin&& <View className="loginBtn">
<Button
type="primary"
size="mini"
open-type="getPhoneNumber"
lang="zh_CN"
onGetPhoneNumber={onGetPhoneNumber}
>
</Button>
</View>}
{!isin && (
<View className="loginBtn">
<Button
type="primary"
size="mini"
open-type="getPhoneNumber"
lang="zh_CN"
onGetPhoneNumber={onGetPhoneNumber}
>
</Button>
</View>
)}
</View>
{/* <View>

View File

@@ -10,6 +10,7 @@ import { getCurrentInstance } from "@tarojs/taro";
interface Typeint {
env?: any;
id?: any;
keyValue?: any;
tableName: any;
objData?: any;
okFun: any;
@@ -37,14 +38,14 @@ export const addTable: any = ({ env, tableName, objData, okFun }: Typeint) => {
};
// 获取指定集合数据表
export const getTable: any = ({ env, tableName, okFun }: Typeint) => {
export const getTable: any = ({ env, tableName, keyValue, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
nowtable?.get({
nowtable?.where({ ...(keyValue ?? {}) }).get({
success: (res: any) => {
okFun(res);
},
@@ -52,18 +53,27 @@ export const getTable: any = ({ env, tableName, okFun }: Typeint) => {
};
// 获取指定id数据数据
export const getTableid: any = ({ env, tableName, id, okFun }: Typeint) => {
export const getTableid: any = ({
env,
tableName,
id,
keyValue,
okFun,
}: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
nowtable?.doc(id).get({
success: (res: any) => {
okFun(res);
},
});
nowtable
?.doc(id)
// .where({ ...(keyValue ?? {}) })
.get({
success: (res: any) => {
okFun(res);
},
});
};
// 更新数据
@@ -104,20 +114,92 @@ export const deleteTableid: any = ({ env, tableName, id, okFun }: Typeint) => {
};
export const isLogin = () => {
let isin = false;
// 检查登录态,获取openid
Taro.checkSession({
success: function (res: any) {
//session_key 未过期,并且在本生命周期一直有效
console.log("res", res);
return true;
console.log("res----", res);
isin = true;
// 获取用户openid
},
fail: function (err: any) {
isin = false;
console.log("err", err);
// session_key 已经失效,需要重新执行登录流程
return false;
Taro.login({
success: function (res: any) {
if (res?.code) {
isin = true;
} else {
isin = false;
}
},
});
},
});
console.log("isin---", isin);
return isin;
};
// 获取用户openid
export const getOpenid = async () => {
Taro.cloud.callFunction({
name: "getOpenid",
complete: async (res: any) => {
console.log("callFunction test result: ", res);
let openid = res?.result?.event?.userInfo?.openId;
},
});
};
export const phoneLogin = async () => {
return new Promise((resolve, reject) => { // 数据库环境
const db: any = Taro.cloud.database({ });
const _ = db.command;
let openId = "";
Taro.cloud.callFunction({
name: "getOpenid",
complete: async (res: any) => {
console.log("callFunction test result: ", res);
openId = res?.result?.event?.userInfo?.openId;
console.log("openId--", openId);
// 判断数据库userinfo是否存在openid
getTable({
tableName: "userInfo",
// 指定条件, 有openid相等的数据
keyValue: { openId: _.eq(openId) },
// keyValue: { _openId: openId },
okFun: (res: any) => {
console.log("res", res);
let data = res?.data ?? [];
if (data?.length === 0) {
//保存openid
addTable({
tableName: "userInfo",
objData: {
openId: openId,
},
okFun: (res: any) => {},
});
} else {
//查看是否存在手机号
if (data?.[0]?.authInfo?.authPhonenum) {
resolve(true)
return true;
} else {
resolve(false)
return false;
}
}
},
});
},
});})
// 不存在-未登录
};