index.js 8.1 KB

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