index.js 8.5 KB

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