158 lines
4.6 KiB
JavaScript
158 lines
4.6 KiB
JavaScript
import { ref } from "vue";
|
|
import { onLoad } from "@dcloudio/uni-app";
|
|
import useUser from "@/hooks/useUser";
|
|
import { updateConsultOrderStatus, refreshOrderPayStatus } from "@/utils/api";
|
|
import ybPlugin from "@/utils/insurance-plugin";
|
|
import { toast, confirm, loading, hideLoading } from "@/utils/widget";
|
|
import { useIMStore } from "@/store/tim";
|
|
import { startConsultOrder } from "@/utils/api";
|
|
|
|
const hisPayStatus = {
|
|
0: "已支付",
|
|
1: "已退费",
|
|
5: "未收费",
|
|
x: "已作废"
|
|
}
|
|
export default function useYbPay(paidCallback,reloadCallback) {
|
|
const { getAuthCode } = useUser();
|
|
const order = ref({});
|
|
|
|
async function pay(data) {
|
|
order.value = { ...data };
|
|
const { status } = await getPayStatus();
|
|
if (status == '5') {
|
|
pluginPay();
|
|
} else if (status == '0') {
|
|
await confirm("订单已支付", { showCancel: false });
|
|
await updateConsultOrderStatus({
|
|
orderId: order.value.orderId,
|
|
payStatus: "success"
|
|
});
|
|
enterRoom();
|
|
} else if (hisPayStatus[status]) {
|
|
await confirm(`订单支付状态为${hisPayStatus[status]}`, { showCancel: false });
|
|
typeof reloadCallback === 'function' && reloadCallback()
|
|
} else {
|
|
await confirm(`获取订单支付状态异常`, { showCancel: false });
|
|
typeof reloadCallback === 'function' && reloadCallback()
|
|
}
|
|
}
|
|
|
|
async function pluginPay() {
|
|
try {
|
|
const authCode = await getAuthCode("nhsamp");
|
|
ybPlugin.toAuthAndPay({
|
|
authCode,
|
|
cardNo: order.value.blhno,
|
|
medOrgOrd: order.value.medorg_order_no
|
|
});
|
|
} catch (e) {
|
|
toast(e);
|
|
}
|
|
}
|
|
|
|
async function onPaid() {
|
|
await confirm("支付成功", { showCancel: false });
|
|
loading("正在获取结果...");
|
|
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
hideLoading();
|
|
const { success, status } = await getPayStatus(false);
|
|
if (success && status === '0') {
|
|
await updateConsultOrderStatus({
|
|
orderId: order.value.orderId,
|
|
payStatus: "success"
|
|
});
|
|
enterRoom();
|
|
return
|
|
}
|
|
if (success && status && status !== '5') {
|
|
await updateConsultOrderStatus({
|
|
orderId: order.value.orderId,
|
|
payStatus: "failed"
|
|
});
|
|
}
|
|
if (hisPayStatus[status]) {
|
|
await confirm(`订单状态结果为${hisPayStatus[status]}`, { showCancel: false });
|
|
typeof reloadCallback === 'function' && reloadCallback()
|
|
} else {
|
|
await confirm(`获取订单状态异常`, { showCancel: false });
|
|
typeof reloadCallback === 'function' && reloadCallback()
|
|
}
|
|
}
|
|
|
|
async function getPayStatus(showLoading = true) {
|
|
const { success, status } = await refreshOrderPayStatus({
|
|
orderId: order.value.orderId,
|
|
patientId: order.value.patientId,
|
|
registerId: order.value.registerId,
|
|
medorgOrderNo: order.value.medorg_order_no,
|
|
}, showLoading);
|
|
return {
|
|
status,
|
|
success,
|
|
};
|
|
}
|
|
|
|
|
|
async function enterRoom() {
|
|
const { sex, age, name, doctorCode } = order.value;
|
|
const userId = order.value.orderId;
|
|
if (!doctorCode) {
|
|
toast("进入视频间错误,医生视频账号不存在 ");
|
|
return;
|
|
}
|
|
if (!userId) {
|
|
toast("进入视频间错误,用户视频账号不存在 ");
|
|
return;
|
|
}
|
|
loading("正在进入视频间, 请稍后等待");
|
|
const imStore = useIMStore();
|
|
try {
|
|
// 登录 IM
|
|
const res = await imStore.login(userId);
|
|
if (!res) {
|
|
hideLoading();
|
|
toast("IM 登录失败");
|
|
return;
|
|
}
|
|
// 等待 SDK 准备好
|
|
await new Promise((resolve) => {
|
|
if (imStore.isSDKReady) {
|
|
resolve();
|
|
} else {
|
|
const timer = setInterval(() => {
|
|
if (imStore.isSDKReady) {
|
|
clearInterval(timer);
|
|
resolve();
|
|
}
|
|
}, 300);
|
|
}
|
|
});
|
|
// SDK 准备好后,更新昵称
|
|
await imStore.updateNickname(`${name}/${sex}/${age}`);
|
|
await startConsultOrder({
|
|
orderId: userId,
|
|
doctorCode,
|
|
});
|
|
const conversationID = `C2C${doctorCode}`;
|
|
await imStore.enterConversation(conversationID);
|
|
// 准备进入会话
|
|
hideLoading();
|
|
// 导航到聊天页面
|
|
uni.redirectTo({
|
|
url: `/pages/chat/index?conversationID=${conversationID}&orderId=${userId}`,
|
|
});
|
|
} catch (error) {
|
|
hideLoading();
|
|
console.error("进入会话失败:", error);
|
|
toast(`进入会话失败: ${error.message || "未知错误"}`);
|
|
}
|
|
}
|
|
|
|
onLoad((e) => {
|
|
ybPlugin.init({ payCallback: paidCallback || onPaid });
|
|
});
|
|
|
|
return pay;
|
|
}
|