feat: 登录态

This commit is contained in:
eric
2022-11-21 18:13:35 +08:00
parent df6e9e7820
commit 841b480f01
7 changed files with 125 additions and 114 deletions

View File

@@ -6,6 +6,7 @@ import {
addTable,
getTable,
getTableid,
isLogin,
updateTableid,
} from "../../../../utils/index";
import "./index.scss";
@@ -141,59 +142,39 @@ const details = () => {
className={`detailBtn`}
style={{ bottom: `${bottomheight ?? 0}px` }}
onClick={() => {
// 检查登录态,获取openid
Taro.checkSession({
success: function (res: any) {
//session_key 未过期,并且在本生命周期一直有效
console.log("res", res);
// 获取用户openid
},
fail: function (err: any) {
console.log("err", err);
// session_key 已经失效,需要重新执行登录流程
//重新登录- 跳转到我的页面
Toast.fail({
message: '请先登录',
selector: '#vanToast-demo2',
duration:2000,
onClose: () => {
// 跳转到目的页面,打开新页面
Taro.navigateTo({
url: "/pages/my/index",
});
},
})
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);
},
};
// Taro.login({
// success:(res:any)=>{
// console.log('sures--',res)
// // 获取code,获取用户的openid
// },fail:(err:any)=>{
// console.log("err",err)
// }
// })
},
});
// 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})
console.log("postData", postData);
updateTableid({ ...postData });
}
}}
>
{/* style={{width:'375px'}} */}

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 } from "../../utils/index";
import { getTable, isLogin } from "../../utils/index";
const my = (props: any) => {
// const [active, setActive] = react.useState(props?.active ?? 0);
@@ -24,6 +24,12 @@ const my = (props: any) => {
const [userInfo, setuserInfo] = useState({}); //用户信息
const [detailsData, setdetailsData] = useState<any>([]); //活动详情
const [currentIndex, setcurrentIndex] = useState(0);
const [isin,setisin]=useState(false)
useEffect(()=>{
let isin: any = isLogin();
setisin(isin)
},[])
// 云函数,-获取手机号
const getLogin = (e: any) => {
@@ -37,9 +43,7 @@ const my = (props: any) => {
.then((res: any) => {
console.log("res11");
console.log("res", res);
// this.setState({
// context: res.result,
// });
setisin(true)
});
};
@@ -118,7 +122,7 @@ const my = (props: any) => {
src="cloud://cloud1-5g5xrgza12a0dd6d.636c-cloud1-5g5xrgza12a0dd6d-1253665791/images/images-2.jpg"
/>
<View className="loginBtn">
{!isin&& <View className="loginBtn">
<Button
type="primary"
size="mini"
@@ -128,7 +132,7 @@ const my = (props: any) => {
>
</Button>
</View>
</View>}
</View>
{/* <View>

View File

@@ -1,7 +1,7 @@
import { Toast } from "@antmjs/vantui";
import Taro from "@tarojs/taro";
import { getCurrentInstance } from "@tarojs/taro";
// 数据库环境
// const db: any = Taro.cloud.database({
// env: "cloud1-5g5xrgza12a0dd6d",
@@ -9,18 +9,18 @@ import { getCurrentInstance } from "@tarojs/taro";
interface Typeint {
env?: any;
id?:any
id?: any;
tableName: any;
objData?: any;
okFun: any;
}
const params=()=>{
const params = () => {
let $instance: any = getCurrentInstance();
return $instance.router.params;
}
};
// 添加数据
export const addTable: any = ({ env,tableName, objData, okFun }: Typeint) => {
export const addTable: any = ({ env, tableName, objData, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
@@ -36,24 +36,23 @@ export const addTable: any = ({ env,tableName, objData, okFun }: Typeint) => {
});
};
// 获取指定集合数据表
export const getTable: any = ({ env,tableName, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
nowtable?.get({
success: (res: any) => {
okFun(res);
},
});
};
export const getTable: any = ({ env, tableName, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
// 获取指定id数据数据
export const getTableid: any = ({ env,tableName, id, okFun }: Typeint) => {
let nowtable: any = db?.collection(tableName);
nowtable?.get({
success: (res: any) => {
okFun(res);
},
});
};
// 获取指定id数据数据
export const getTableid: any = ({ env, tableName, id, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
@@ -67,39 +66,58 @@ export const getTableid: any = ({ env,tableName, id, okFun }: Typeint) => {
});
};
// 更新数据
export const updateTableid: any = ({ env,tableName,objData, id, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
nowtable.doc(id).update({
// 更新数据
export const updateTableid: any = ({
env,
tableName,
objData,
id,
okFun,
}: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
nowtable.doc(id).update({
// nowtable.doc(id).set({
// data 传入需要局部更新的数据
data: objData,
success: (res: any) => {
okFun(res);
},
})
};
// data 传入需要局部更新的数据
data: objData,
success: (res: any) => {
okFun(res);
},
});
};
// 删除数据
export const deleteTableid: any = ({ env,tableName, id, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
nowtable.doc(id).remove({
success: (res: any) => {
okFun(res);
},
})
};
// 删除数据
export const deleteTableid: any = ({ env, tableName, id, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
nowtable.doc(id).remove({
success: (res: any) => {
okFun(res);
},
});
};
export const isLogin = () => {
// 检查登录态,获取openid
Taro.checkSession({
success: function (res: any) {
//session_key 未过期,并且在本生命周期一直有效
console.log("res", res);
return true;
// 获取用户openid
},
fail: function (err: any) {
console.log("err", err);
// session_key 已经失效,需要重新执行登录流程
return false;
},
});
};