index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import qs from "qs";
  2. import { percent2percent25 } from "../hospital-payment-detail/service";
  3. import { getYbParams, generateRandomFourDigitNumber } from "./service";
  4. import { tradePay } from "../../utils/tradePay";
  5. import {
  6. prePayConfirm,
  7. getPayAuthInfo,
  8. revokeSettle,
  9. } from "../../../../core/utils/ywtService";
  10. import history from "../../utils/history";
  11. import { createSubscribe } from "applet-page-component";
  12. Component(
  13. createSubscribe({
  14. onShow() {
  15. this.init("onShow");
  16. },
  17. })({
  18. props: {},
  19. data: {
  20. authCode: undefined,
  21. canPay: false,
  22. prescriptionList: [],
  23. actionsheetVisible: false,
  24. isUserPersonalAccount: true,
  25. isCulculate: false,
  26. preConfirmInfo: {},
  27. },
  28. didMount() {
  29. this.init("didMount");
  30. },
  31. didUnmount() {
  32. this._paying = false;
  33. },
  34. methods: {
  35. init(type) {
  36. this.getPayDetail(this.$page.data.query);
  37. this.hanldeGetPayAuthInfo(type);
  38. },
  39. async hanldeGetPayAuthInfo(type) {
  40. if (this._paying) return;
  41. const app = getApp();
  42. const {
  43. hisOrderNo: hisOrdNum,
  44. authCode,
  45. reqBizNo,
  46. } = this.$page.data.query;
  47. const _this = this;
  48. const ramdonNum = generateRandomFourDigitNumber();
  49. const userId = app.globalData.ywtUserId || "";
  50. const queryReqBizNo = reqBizNo || `${hisOrdNum}-${userId}-${ramdonNum}`;
  51. const query = Object.assign(this.$page.data.query, {
  52. reqBizNo: queryReqBizNo,
  53. });
  54. const qsResult = `?${qs.stringify(query)}`;
  55. const callUrl = encodeURIComponent(
  56. `antbuilder/industry/hospitalV2/pages/page-no-pull/index${qsResult}`
  57. );
  58. if (type === "onShow") {
  59. my.getAuthCode({
  60. scopes: ["nhsamp", "auth_user"], // 主动授权:auth_user,静默授权:auth_base。或者其它scopes
  61. success: (res) => {
  62. if (res.authCode) {
  63. _this.requestGetPayAuthInfo(
  64. res.authCode,
  65. callUrl,
  66. queryReqBizNo,
  67. type
  68. );
  69. }
  70. },
  71. });
  72. } else {
  73. this.requestGetPayAuthInfo(authCode, callUrl, queryReqBizNo, type);
  74. }
  75. },
  76. async requestGetPayAuthInfo(authCode, callUrl, reqBizNo, lifeType) {
  77. const _this = this;
  78. const [err, result] = await getPayAuthInfo({
  79. authCode,
  80. callUrl,
  81. reqBizNo,
  82. });
  83. if (!err) {
  84. const { payAuthNo, authNo, medicalCardInstId, medicalCardId } =
  85. result;
  86. if (result.authNo) {
  87. this.setData(
  88. {
  89. payAuthNo,
  90. authNo,
  91. medicalCardInstId,
  92. medicalCardId,
  93. isCulculate: true,
  94. },
  95. () => {
  96. _this.handlePrePayConfirm();
  97. }
  98. );
  99. return;
  100. }
  101. if (result.authUrl && lifeType === "didMount") {
  102. // 跳转医保授权页面
  103. my.ap.openURL({
  104. url: result.authUrl,
  105. fail(e) {
  106. console.log("err", e);
  107. my.navigateBack();
  108. },
  109. });
  110. } else {
  111. my.navigateBack();
  112. }
  113. } else {
  114. console.log("err ==>", err);
  115. return;
  116. my.showToast({
  117. type: "fail",
  118. content: "商户未上线",
  119. duration: 2000,
  120. complete() {
  121. my.navigateBack();
  122. },
  123. });
  124. }
  125. },
  126. async handlePrePayConfirm() {
  127. console.log(
  128. "this.$page.data.query handlePrePayConfirm ==>",
  129. this.$page.data.query
  130. );
  131. console.log("this.$page.data handlePrePayConfirm ==>", this.$page.data);
  132. my.showLoading({ mask: true });
  133. const {
  134. hisOrderNo: hisOrdNum,
  135. hisPatientId: patientId,
  136. hisClinicCode: clinicCode,
  137. total: orderSum,
  138. orderInsType,
  139. } = this.$page.data.query;
  140. const {
  141. payAuthNo,
  142. authNo,
  143. medicalCardInstId,
  144. medicalCardId,
  145. isUserPersonalAccount,
  146. } = this.data;
  147. const [err, result] = await prePayConfirm({
  148. patientId,
  149. clinicCode,
  150. hisOrdNum,
  151. orderSum,
  152. payInsType: "2",
  153. orderInsType,
  154. payAuthNo,
  155. authNo,
  156. medicalCardInstId,
  157. medicalCardId,
  158. consumeType: isUserPersonalAccount ? 0 : 2,
  159. });
  160. if (!err) {
  161. // 预结算成功
  162. my.hideLoading();
  163. const ybData = getYbParams(result);
  164. this.setData({
  165. preConfirmInfo: Object.assign(result, ybData),
  166. isCulculate: false,
  167. canPay: true,
  168. });
  169. } else {
  170. my.hideLoading();
  171. console.log("err ==>", err, typeof err, JSON.stringify(err));
  172. my.navigateBack();
  173. }
  174. },
  175. parseDetailItems(detailItems) {
  176. const list = detailItems
  177. ? JSON.parse(decodeURIComponent(percent2percent25(detailItems) || ""))
  178. : [];
  179. const newList = list.map((item) => {
  180. return {
  181. label: `${item.itemName}*${item.itemNum}`,
  182. // subLabel: `${item.unit || ""}`,
  183. value: `${item.amount || 0}元`,
  184. };
  185. });
  186. return newList || [];
  187. },
  188. async handleIsUserPersonalAccount(e) {
  189. const _this = this;
  190. const { valuex } = e.currentTarget.dataset;
  191. const { hisOrderNo, hisPatientId } = this.$page.data.query;
  192. const { preConfirmInfo, isUserPersonalAccount } = this.data;
  193. if (isUserPersonalAccount === valuex) return;
  194. my.showLoading();
  195. this.setData(
  196. {
  197. isUserPersonalAccount: valuex,
  198. isCulculate: true,
  199. },
  200. async () => {
  201. const [err, result] = await revokeSettle({
  202. hisOrderNo,
  203. insuDivId: preConfirmInfo.insuDivId || "",
  204. hisPatientId,
  205. isInsV2: true,
  206. });
  207. if (!err) {
  208. _this.handlePrePayConfirm();
  209. } else {
  210. my.showToast({
  211. type: "fail",
  212. content: "撤销预结算失败",
  213. duration: 2000,
  214. complete() {
  215. my.hideLoading();
  216. my.navigateBack();
  217. },
  218. });
  219. }
  220. }
  221. );
  222. },
  223. getPayDetail(query) {
  224. let infoList = [];
  225. if (query) {
  226. infoList = [
  227. {
  228. title: "就诊信息",
  229. list: [
  230. // { label: "门诊类别", value: info.deptName },
  231. { label: "门诊科室", value: query.deptName },
  232. { label: "医生姓名", value: query.doctorName },
  233. { label: "处方时间", value: query.prescribeDate },
  234. {
  235. label: "费用总额",
  236. value: parseFloat(query.total / 100, 2) || 0 + "元",
  237. highlight: true,
  238. },
  239. ],
  240. },
  241. {
  242. title: "费用信息",
  243. list: this.parseDetailItems(query.orderItems),
  244. },
  245. ];
  246. this.setData({ prescriptionList: infoList });
  247. }
  248. },
  249. // 查看更多
  250. openForm() {
  251. this.setData({
  252. expand: !this.data.expand,
  253. });
  254. },
  255. medicareExpand() {
  256. this.setData({
  257. medicareExpand: !this.data.medicareExpand,
  258. });
  259. },
  260. handleActionsheet() {
  261. this.setData({
  262. actionsheetVisible: !this.data.actionsheetVisible,
  263. });
  264. },
  265. // 发起支付
  266. async onPay(e) {
  267. const { canPay } = this.data;
  268. if (!canPay) return;
  269. this._paying = true;
  270. const {
  271. hisPatientId,
  272. age,
  273. sex,
  274. orderId,
  275. hisOrderNo,
  276. amount,
  277. deptName,
  278. doctorName,
  279. doctorCode,
  280. hisClinicCode,
  281. total,
  282. name,
  283. orderInsType,
  284. prescribeDate,
  285. payInsType = "2",
  286. medInsFee,
  287. selfFee,
  288. payName,
  289. patName,
  290. outTradeNo,
  291. } = this.$page.data.query;
  292. const {
  293. preConfirmInfo: { invoiceNo, insuAdmDr, insUploadFeeResp },
  294. isUserPersonalAccount,
  295. } = this.data;
  296. let result = false;
  297. try {
  298. my.showLoading();
  299. // 门诊订单
  300. result = await tradePay(
  301. {
  302. useBalance: false,
  303. useMedicare: true,
  304. outTradeNo,
  305. hisPatientId,
  306. age,
  307. sex,
  308. orderId,
  309. hisOrderNo,
  310. amount,
  311. deptName,
  312. doctorName,
  313. doctorCode,
  314. total,
  315. hisClinicCode,
  316. name,
  317. prescribeDate,
  318. orderInsType,
  319. payInsType,
  320. totalFee: total,
  321. medInsFee,
  322. selfFee,
  323. payName,
  324. patName,
  325. invoiceNo,
  326. insuAdmDr,
  327. insUploadFeeResp,
  328. consumeType: isUserPersonalAccount ? 0 : 2,
  329. },
  330. {
  331. tradeType: "Outpatient",
  332. }
  333. );
  334. } catch (error) {
  335. console.log("error ===>", error);
  336. } finally {
  337. my.hideLoading();
  338. }
  339. if (result) {
  340. this._paying = false;
  341. console.log("pay result ===>", result);
  342. // 支付成功后返回列表页
  343. // my.navigateBack();
  344. history.replace({
  345. title: "支付成功",
  346. query: {},
  347. pageType: "hospital-payment-detail-yibao-result",
  348. });
  349. } else {
  350. this._paying = false;
  351. }
  352. },
  353. },
  354. })
  355. );