Files
gitlab-instance-0a899031_ci…/src/utils/index.ts
2022-11-27 11:53:15 +08:00

248 lines
5.5 KiB
TypeScript

import { Toast } from "@antmjs/vantui";
import Taro from "@tarojs/taro";
import { getCurrentInstance } from "@tarojs/taro";
import {type} from '../../config'
// 数据库环境
// const db: any = Taro.cloud.database({
// env: "cloud1-5g5xrgza12a0dd6d",
// });
interface Typeint {
env?: any;
id?: any;
keyValue?: any;
tableName: any;
objData?: any;
okFun: any;
}
// 获取params参数
export const getparams = () => {
let $instance: any = getCurrentInstance();
return $instance?.router?.params;
};
// 添加数据
export const addTable: any = ({ env, tableName, objData, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
nowtable?.add({
// data 字段表示需新增的 JSON 数据
data: objData,
success: (res: any) => {
okFun(res);
},
fail:(err:any)=>{
console.log('err',err)
}
});
};
// 获取指定集合数据表 (keyValue指定条件)
export const getTable: any = ({ env, tableName, keyValue={}, okFun }: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
if(Object.keys(keyValue)?.length>0){
nowtable?.where(keyValue).get({
success: (res: any) => {
okFun(res);
},
});
}else{
nowtable?.get({
success: (res: any) => {
okFun(res);
},
});
}
};
// 获取指定id数据数据
export const getTableid: any = ({
env,
tableName,
id,
keyValue={},
okFun,
}: Typeint) => {
// 数据库环境
const db: any = Taro.cloud.database({
env: env,
});
let nowtable: any = db?.collection(tableName);
if(Object.keys(keyValue)?.length>0){
nowtable
?.doc(id)
.where(keyValue)
.get({
success: (res: any) => {
okFun(res);
},
fail:(err:any)=>{
console.log('err',err)
}
});
}else{
nowtable
?.doc(id)?.get({
success: (res: any) => {
okFun(res);
},
fail:(err:any)=>{
console.log('err',err)
}
});
}
};
// 更新数据
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);
},
fail:(err:any)=>{
console.log('err',err)
}
});
};
// 删除数据
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 = () => {
let isin = false;
// 检查登录态,获取openid
Taro.checkSession({
success: function (res: any) {
//session_key 未过期,并且在本生命周期一直有效
console.log("res----", res);
isin = true;
// 获取用户openid
},
fail: function (err: any) {
isin = false;
console.log("err", err);
// session_key 已经失效,需要重新执行登录流程
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?.[0]?.telNumber) {
resolve(true)
return true;
} else {
if(type==='people'){
// resolve(true)
// return true;
resolve(false)
return false;
}else{
resolve(false)
return false;
}
}
}
},
});
},
});})
// 不存在-未登录
};