index.js 8.1 KB

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