import qs from "qs"; import { getYbParams, generateRandomFourDigitNumber } from "./service"; import { tradePay } from "../../utils/tradePay"; import { prePayConfirm, getPayAuthInfo, } from "../../../../core/utils/ywtService"; import history from "../../utils/history"; import { createSubscribe } from "applet-page-component"; Component( createSubscribe({ onShow() { this.init("onShow"); }, })({ props: {}, data: { authCode: undefined, canPay: false, prescriptionList: [], actionsheetVisible: false, isUserPersonalAccount: true, isCulculate: false, preConfirmInfo: {}, }, didMount() { this.init("didMount"); }, didUnmount() {}, methods: { init(type) { this.getPayDetail(this.$page.data.query); this.hanldeGetPayAuthInfo(type); }, saveSubscribe(ref) { this.subscribe = ref; }, async hanldeGetPayAuthInfo(type) { const app = getApp(); const { hisOrderNo: hisOrdNum, authCode, reqBizNo, resultCode, } = this.$page.data.query; const _this = this; const ramdonNum = generateRandomFourDigitNumber(); const userId = app.globalData.ywtUserId || ""; const queryReqBizNo = reqBizNo || `${hisOrdNum}-${userId}-${ramdonNum}`; const query = Object.assign(this.$page.data.query, { reqBizNo: queryReqBizNo, }); const qsResult = `?${qs.stringify(query)}`; const callUrl = encodeURIComponent( `antbuilder/industry/hospitalV2/pages/page-no-pull/index${qsResult}` ); if (type === "onShow") { // 需要路由传递一个参数,用于判断是否授权,确定授权则下一步;取消授权则返回上一页 console.log( "this.$page.data.query ===>", this.$page.data.query, this.$page.data, resultCode ); // if (resultCode === "SUCCESS") { // my.navigateBack(); // return; // } my.getAuthCode({ scopes: ["nhsamp", "auth_user"], // 主动授权:auth_user,静默授权:auth_base。或者其它scopes success: (res) => { if (res.authCode) { _this.requestGetPayAuthInfo( res.authCode, callUrl, queryReqBizNo, type ); } }, }); } else { this.requestGetPayAuthInfo(authCode, callUrl, queryReqBizNo, type); } }, async requestGetPayAuthInfo(authCode, callUrl, reqBizNo, lifeType) { const _this = this; const [err, result] = await getPayAuthInfo({ authCode, callUrl, reqBizNo, }); if (!err) { const { payAuthNo, authNo, medicalCardInstId, medicalCardId } = result; if (result.authNo) { this.setData( { payAuthNo, authNo, medicalCardInstId, medicalCardId, isCulculate: true, }, () => { _this.handlePrePayConfirm(); } ); return; } if (result.authUrl && lifeType === 'didMount') { // 跳转医保授权页面 my.ap.openURL({ url: result.authUrl, fail() { my.navigateBack(); }, }); }else{ my.navigateBack(); } } }, async handlePrePayConfirm() { my.showLoading({ mask: true }); try { const { hisOrderNo: hisOrdNum, hisPatientId: patientId, hisClinicCode: clinicCode, total: orderSum, orderInsType, } = this.$page.data.query; const { payAuthNo, authNo, medicalCardInstId, medicalCardId } = this.data; const [err, result] = await prePayConfirm({ patientId, clinicCode, hisOrdNum, orderSum, payInsType: "2", orderInsType, payAuthNo, authNo, medicalCardInstId, medicalCardId, }); if (!err) { // 预结算成功 const ybData = getYbParams(result); this.setData({ preConfirmInfo: Object.assign(result, ybData), isCulculate: false, canPay: true, }); } else { my.navigateBack(); throw `${err}`; } } catch (error) { console.log("error ===>", error); } finally { my.hideLoading(); } }, parseDetailItems(detailItems) { const list = detailItems ? JSON.parse(decodeURIComponent(detailItems || "")) : []; const newList = list.map((item) => { return { label: `${item.itemName}*${item.itemNum}`, // subLabel: `${item.unit || ""}`, value: `${item.amount || 0}元`, }; }); return newList || []; }, handleIsUserPersonalAccount(e) { my.showLoading(); const _this = this; const { valuex } = e.currentTarget.dataset; this.setData( { isUserPersonalAccount: valuex, isCulculate: true, }, () => { setTimeout(() => { my.hideLoading(); _this.setData({ isCulculate: false, }); }, 3000); } ); }, getPayDetail(query) { let infoList = []; if (query) { infoList = [ { title: "就诊信息", list: [ // { label: "门诊类别", value: info.deptName }, { label: "门诊科室", value: query.deptName }, { label: "医生姓名", value: query.doctorName }, { label: "处方时间", value: query.prescribeDate }, { label: "费用总额", value: parseFloat(query.total / 100, 2) || 0 + "元", highlight: true, }, ], }, { title: "费用信息", list: this.parseDetailItems(query.orderItems), }, ]; this.setData({ prescriptionList: infoList }); } }, // 查看更多 openForm() { this.setData({ expand: !this.data.expand, }); }, medicareExpand() { this.setData({ medicareExpand: !this.data.medicareExpand, }); }, handleActionsheet() { this.setData({ actionsheetVisible: !this.data.actionsheetVisible, }); }, // 发起支付 async onPay(e) { const { canPay } = this.data; if (!canPay) return; const { hisPatientId, age, sex, orderId, hisOrderNo, amount, deptName, doctorName, doctorCode, hisClinicCode, total, name, orderInsType, prescribeDate, payInsType = "2", consumeType = "0", medInsFee, selfFee, payName, patName, outTradeNo, } = this.$page.data.query; const { preConfirmInfo: { invoiceNo, insuAdmDr, insUploadFeeResp }, } = this.data; console.log("this.$page.data.query ==>", this.$page.data.query); let result = false; console.log("requset data ==>", { useBalance: false, useMedicare: true, outTradeNo, hisPatientId, age, sex, orderId, hisOrderNo, amount, deptName, doctorName, doctorCode, total, hisClinicCode, name, prescribeDate, orderInsType, payInsType, consumeType, totalFee: total, medInsFee, selfFee, payName, patName, invoiceNo, insuAdmDr, insUploadFeeResp, }); try { my.showLoading(); // 门诊订单 result = await tradePay( { useBalance: false, useMedicare: true, outTradeNo, hisPatientId, age, sex, orderId, hisOrderNo, amount, deptName, doctorName, doctorCode, total, hisClinicCode, name, prescribeDate, orderInsType, payInsType, consumeType, totalFee: total, medInsFee, selfFee, payName, patName, invoiceNo, insuAdmDr, insUploadFeeResp, }, { tradeType: "Outpatient", } ); } catch (error) { console.log("error ===>", error); } finally { my.hideLoading(); } if (result) { // 支付成功后返回列表页 my.navigateBack(); } }, }, }) );