tiefen_space_h5/api/public.js

265 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { get } from "@/utils/storeInfo";
import requireAPI from "@/utils/requireAPI";
import { Toast } from "antd-mobile";
import { JSEncrypt } from "jsencrypt";
//关注和取关功能
export const handleLogout = async () => {
const account = get("account");
try {
const data = await requireAPI("POST", `/api/login/logout`, {
body: {
mid: account.mid,
},
});
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
} catch (error) {
console.error(error);
}
};
//关注和取关功能
export const handleFollow = async (isFollowed, followedID, callback) => {
const account = get("account");
let body = {
[!isFollowed ? "account_relations" : "sentences"]: [
{ sub_mid: account.mid, obj_mid: followedID, predicate: 0 },
{ sub_mid: followedID, obj_mid: account.mid, predicate: 1 },
],
};
try {
const data = await requireAPI("POST", `/api/account_relation/${
!isFollowed ? "create" : "delete"
}`, {
body,
});
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
} else {
callback && callback(!isFollowed);
}
} catch (error) {
console.error(error);
}
};
//点赞和取消点赞功能
export const thumbsUp = async (id, times, callback, isZone) => {
try {
const body = isZone
? {
zone_moment_id: id,
times: times == 1 ? -1 : 1,
}
: {
moment_id: id,
times: times == 1 ? -1 : 1,
};
console.log("body", body);
const data = await requireAPI("POST", `/api/${
isZone ? "zone_moment" : "moment"
}/thumbs_up`, {
body,
});
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
} else {
callback(times == 1 ? -1 : 1);
}
} catch (error) {
console.error(error);
}
};
//点赞和取消点赞功能
export const zoneThumbsUp = async (id, times = 1, callback) => {
console.log("times", times);
try {
const body = {
moment_id: id,
times: times == 1 ? -1 : 1,
};
const data = await requireAPI("POST", `/api/zone/thumbs_up`, {
body,
});
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
} else {
callback(times == 1 ? -1 : 1);
}
} catch (error) {
console.error(error);
}
};
// 查看关系
export async function checkRelation(subMid, objMid, predicate) {
try {
const data =
await requireAPI("POST", `/api/account_relation/list_by_sentence`, {
body: {
sub_mid: subMid,
obj_mid: objMid,
predicate: predicate,
},
});
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
return data.data.is_account_relation_existed;
} catch (error) {
console.error(error);
}
}
// 获取用户信息
export async function getUserInfo() {
try {
const data = await requireAPI("POST", `/api/account/list_by_mid`, null, true);
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
return data.data.account;
} catch (error) {
console.error(error);
}
}
// 创建订单
export const createOrder = async (type = "alipay_h5") => {
if (!selectedPrice.id && !customCoin.selected) {
Toast.show({
content: "请选择充值档位",
});
return;
}
if (customCoin.selected && customCoin.num < 10) {
Toast.show({
content: "最低充值1元哦",
});
return;
}
const base = webviewBaseRequest();
const body = {
...base,
product_id: customCoin.selected ? "h5_custom_coin" : selectedPrice.id,
custom_coins: customCoin.selected ? customCoin.num : 0,
pay_type: type,
redirect_url: type === "yeepay_wxpay_h5" ? window.location.href : "",
from: "app",
};
//如果是微信jsapi支付直接跳转到中间页
if (type === "wxpay_jsapi") {
router.push(`/pay/${encodeURIComponent(JSON.stringify(body))}`);
return;
}
setIsLoading(true);
const signature = generateSignature(body);
try {
const response = await fetch(
`/api/vas/create_order?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}
);
const data = await response.json();
if (data.ret === -1) {
Toast.show({
content: data.msg,
});
return;
}
switch (type) {
case "yeepay_alipay_h5":
router.push(`${data.data.yeepay_alipay_h5_param_str}`);
break;
case "yeepay_wxpay_h5":
router.push(`${data.data.yeepay_wxpay_h5_param_str}`);
break;
case "alipay_h5":
router.push(`${data.data.alipay_h5_param_str}`);
break;
case "wxpay_h5":
router.push(
`https://shop.tiefen.fun/pay/wxpay_h5/${encodeURIComponent(
data.data.wxpay_h5_param_str
)}`
);
break;
default:
router.push(`${data.data.alipay_h5_param_str}`);
break;
}
} catch (error) {
console.error(error);
} finally {
setIsLoading(false);
}
};
//点击获取验证码
export const handleVerification = async (mobilePhone="",regionCode) => {
console.log("mobilePhone",mobilePhone.toString())
//手机号校验
if (!mobilePhone.toString().match(/^1[3456789]\d{9}$/)) {
Toast.show({
icon: "fail",
content: "手机号码格式错误",
position: "top",
});
return;
}
//对手机号进行RSA加密
const encrypt = new JSEncrypt();
encrypt.setPublicKey(process.env.EXPO_PUBLIC_RSA_KEY);
const mobile_phone = encrypt.encrypt(mobilePhone);
//发送短信验证码
try {
await requireAPI("POST", `/api/veri_code/send`, {
body: {
mobile_phone: mobile_phone,
region_code: regionCode,
},
});
} catch (error) {
console.error(error);
}
};