index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. my.showLoading({ mask: true });
  78. const _this = this;
  79. const [err, result] = await getPayAuthInfo({
  80. authCode,
  81. callUrl,
  82. reqBizNo,
  83. });
  84. if (!err) {
  85. my.hideLoading();
  86. const { payAuthNo, authNo, medicalCardInstId, medicalCardId } =
  87. result;
  88. if (result.authNo) {
  89. this.setData(
  90. {
  91. payAuthNo,
  92. authNo,
  93. medicalCardInstId,
  94. medicalCardId,
  95. isCulculate: true,
  96. },
  97. () => {
  98. _this.handlePrePayConfirm();
  99. }
  100. );
  101. return;
  102. }
  103. if (result.authUrl && lifeType === "didMount") {
  104. // 跳转医保授权页面
  105. my.ap.openURL({
  106. url: result.authUrl,
  107. fail(e) {
  108. console.log("err", e);
  109. my.navigateBack();
  110. },
  111. });
  112. } else {
  113. my.navigateBack();
  114. }
  115. } else {
  116. console.log("err ==>", err);
  117. my.hideLoading();
  118. my.showToast({
  119. type: "fail",
  120. content: err.info || "未知错误",
  121. duration: 2000,
  122. complete() {
  123. my.navigateBack();
  124. },
  125. });
  126. }
  127. },
  128. async handlePrePayConfirm() {
  129. console.log(
  130. "this.$page.data.query handlePrePayConfirm ==>",
  131. this.$page.data.query
  132. );
  133. console.log("this.$page.data handlePrePayConfirm ==>", this.$page.data);
  134. my.showLoading({ mask: true });
  135. const {
  136. hisOrderNo: hisOrdNum,
  137. hisPatientId: patientId,
  138. hisClinicCode: clinicCode,
  139. total: orderSum,
  140. orderInsType,
  141. } = this.$page.data.query;
  142. const {
  143. payAuthNo,
  144. authNo,
  145. medicalCardInstId,
  146. medicalCardId,
  147. isUserPersonalAccount,
  148. } = this.data;
  149. const [err, result] = await prePayConfirm({
  150. patientId,
  151. clinicCode,
  152. hisOrdNum,
  153. orderSum,
  154. payInsType: "2",
  155. orderInsType,
  156. payAuthNo,
  157. authNo,
  158. medicalCardInstId,
  159. medicalCardId,
  160. consumeType: isUserPersonalAccount ? 0 : 2,
  161. });
  162. if (!err) {
  163. // 预结算成功
  164. my.hideLoading();
  165. const ybData = getYbParams(result);
  166. this.setData({
  167. preConfirmInfo: Object.assign(result, ybData),
  168. isCulculate: false,
  169. canPay: true,
  170. });
  171. } else {
  172. my.hideLoading();
  173. console.log("err ==>", err, typeof err, JSON.stringify(err));
  174. my.navigateBack();
  175. }
  176. },
  177. parseDetailItems(detailItems) {
  178. const list = detailItems
  179. ? JSON.parse(decodeURIComponent(percent2percent25(detailItems) || ""))
  180. : [];
  181. const newList = list.map((item) => {
  182. return {
  183. label: `${item.itemName}*${item.itemNum}`,
  184. // subLabel: `${item.unit || ""}`,
  185. value: `${item.amount || 0}元`,
  186. };
  187. });
  188. return newList || [];
  189. },
  190. async handleIsUserPersonalAccount(e) {
  191. const _this = this;
  192. const { valuex } = e.currentTarget.dataset;
  193. const { hisOrderNo, hisPatientId } = this.$page.data.query;
  194. const { preConfirmInfo, isUserPersonalAccount } = this.data;
  195. if (isUserPersonalAccount === valuex) return;
  196. my.showLoading();
  197. this.setData(
  198. {
  199. isUserPersonalAccount: valuex,
  200. isCulculate: true,
  201. },
  202. async () => {
  203. const [err, result] = await revokeSettle({
  204. hisOrderNo,
  205. insuDivId: preConfirmInfo.insuDivId || "",
  206. hisPatientId,
  207. isInsV2: true,
  208. });
  209. if (!err) {
  210. _this.handlePrePayConfirm();
  211. } else {
  212. my.showToast({
  213. type: "fail",
  214. content: "撤销预结算失败",
  215. duration: 2000,
  216. complete() {
  217. my.hideLoading();
  218. my.navigateBack();
  219. },
  220. });
  221. }
  222. }
  223. );
  224. },
  225. getPayDetail(query) {
  226. let infoList = [];
  227. if (query) {
  228. infoList = [
  229. {
  230. title: "就诊信息",
  231. list: [
  232. // { label: "门诊类别", value: info.deptName },
  233. { label: "门诊科室", value: query.deptName },
  234. { label: "医生姓名", value: query.doctorName },
  235. { label: "处方时间", value: query.prescribeDate },
  236. {
  237. label: "费用总额",
  238. value: parseFloat(query.total / 100, 2) || 0 + "元",
  239. highlight: true,
  240. },
  241. ],
  242. },
  243. {
  244. title: "费用信息",
  245. list: this.parseDetailItems(query.orderItems),
  246. },
  247. ];
  248. this.setData({ prescriptionList: infoList });
  249. }
  250. },
  251. // 查看更多
  252. openForm() {
  253. this.setData({
  254. expand: !this.data.expand,
  255. });
  256. },
  257. medicareExpand() {
  258. this.setData({
  259. medicareExpand: !this.data.medicareExpand,
  260. });
  261. },
  262. handleActionsheet() {
  263. this.setData({
  264. actionsheetVisible: !this.data.actionsheetVisible,
  265. });
  266. },
  267. // 发起支付
  268. async onPay(e) {
  269. const { canPay } = this.data;
  270. if (!canPay) return;
  271. this._paying = true;
  272. const {
  273. hisPatientId,
  274. age,
  275. sex,
  276. orderId,
  277. hisOrderNo,
  278. amount,
  279. deptName,
  280. doctorName,
  281. doctorCode,
  282. hisClinicCode,
  283. total,
  284. name,
  285. orderInsType,
  286. prescribeDate,
  287. payInsType = "2",
  288. medInsFee,
  289. selfFee,
  290. payName,
  291. patName,
  292. outTradeNo,
  293. } = this.$page.data.query;
  294. const {
  295. preConfirmInfo: { invoiceNo, insuAdmDr, insUploadFeeResp },
  296. isUserPersonalAccount,
  297. } = this.data;
  298. let result = false;
  299. try {
  300. my.showLoading();
  301. // 门诊订单
  302. result = await tradePay(
  303. {
  304. useBalance: false,
  305. useMedicare: true,
  306. outTradeNo,
  307. hisPatientId,
  308. age,
  309. sex,
  310. orderId,
  311. hisOrderNo,
  312. amount,
  313. deptName,
  314. doctorName,
  315. doctorCode,
  316. total,
  317. hisClinicCode,
  318. name,
  319. prescribeDate,
  320. orderInsType,
  321. payInsType,
  322. totalFee: total,
  323. medInsFee,
  324. selfFee,
  325. payName,
  326. patName,
  327. invoiceNo,
  328. insuAdmDr,
  329. insUploadFeeResp,
  330. consumeType: isUserPersonalAccount ? 0 : 2,
  331. },
  332. {
  333. tradeType: "Outpatient",
  334. }
  335. );
  336. } catch (error) {
  337. console.log("error ===>", error);
  338. } finally {
  339. my.hideLoading();
  340. }
  341. if (result) {
  342. this._paying = false;
  343. console.log("pay result ===>", result);
  344. // 支付成功后返回列表页
  345. // my.navigateBack();
  346. history.replace({
  347. title: "支付成功",
  348. query: {},
  349. pageType: "hospital-payment-detail-yibao-result",
  350. });
  351. } else {
  352. this._paying = false;
  353. }
  354. },
  355. },
  356. })
  357. );