index.js 8.2 KB

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