index.js 8.1 KB

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