index.js 8.3 KB

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