index.js 8.0 KB

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