index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. my.showLoading();
  188. const _this = this;
  189. const { valuex } = e.currentTarget.dataset;
  190. const { hisOrderNo, hisPatientId } = this.$page.data.query;
  191. const { preConfirmInfo } = this.data;
  192. this.setData(
  193. {
  194. isUserPersonalAccount: valuex,
  195. isCulculate: true,
  196. },
  197. async () => {
  198. const [err, result] = await revokeSettle({
  199. hisOrderNo,
  200. insuDivId: preConfirmInfo.insuDivId || "",
  201. hisPatientId,
  202. isInsV2: true,
  203. });
  204. if (!err) {
  205. _this.handlePrePayConfirm();
  206. } else {
  207. my.showToast({
  208. type: "fail",
  209. content: "撤销预结算失败",
  210. duration: 2000,
  211. });
  212. setTimeout(() => {
  213. my.hideLoading();
  214. my.navigateBack();
  215. }, 2000);
  216. }
  217. }
  218. );
  219. },
  220. getPayDetail(query) {
  221. let infoList = [];
  222. if (query) {
  223. infoList = [
  224. {
  225. title: "就诊信息",
  226. list: [
  227. // { label: "门诊类别", value: info.deptName },
  228. { label: "门诊科室", value: query.deptName },
  229. { label: "医生姓名", value: query.doctorName },
  230. { label: "处方时间", value: query.prescribeDate },
  231. {
  232. label: "费用总额",
  233. value: parseFloat(query.total / 100, 2) || 0 + "元",
  234. highlight: true,
  235. },
  236. ],
  237. },
  238. {
  239. title: "费用信息",
  240. list: this.parseDetailItems(query.orderItems),
  241. },
  242. ];
  243. this.setData({ prescriptionList: infoList });
  244. }
  245. },
  246. // 查看更多
  247. openForm() {
  248. this.setData({
  249. expand: !this.data.expand,
  250. });
  251. },
  252. medicareExpand() {
  253. this.setData({
  254. medicareExpand: !this.data.medicareExpand,
  255. });
  256. },
  257. handleActionsheet() {
  258. this.setData({
  259. actionsheetVisible: !this.data.actionsheetVisible,
  260. });
  261. },
  262. // 发起支付
  263. async onPay(e) {
  264. const { canPay } = this.data;
  265. if (!canPay) return;
  266. const {
  267. hisPatientId,
  268. age,
  269. sex,
  270. orderId,
  271. hisOrderNo,
  272. amount,
  273. deptName,
  274. doctorName,
  275. doctorCode,
  276. hisClinicCode,
  277. total,
  278. name,
  279. orderInsType,
  280. prescribeDate,
  281. payInsType = "2",
  282. medInsFee,
  283. selfFee,
  284. payName,
  285. patName,
  286. outTradeNo,
  287. } = this.$page.data.query;
  288. const {
  289. preConfirmInfo: { invoiceNo, insuAdmDr, insUploadFeeResp },
  290. isUserPersonalAccount,
  291. } = this.data;
  292. let result = false;
  293. try {
  294. my.showLoading();
  295. // 门诊订单
  296. result = await tradePay(
  297. {
  298. useBalance: false,
  299. useMedicare: true,
  300. outTradeNo,
  301. hisPatientId,
  302. age,
  303. sex,
  304. orderId,
  305. hisOrderNo,
  306. amount,
  307. deptName,
  308. doctorName,
  309. doctorCode,
  310. total,
  311. hisClinicCode,
  312. name,
  313. prescribeDate,
  314. orderInsType,
  315. payInsType,
  316. totalFee: total,
  317. medInsFee,
  318. selfFee,
  319. payName,
  320. patName,
  321. invoiceNo,
  322. insuAdmDr,
  323. insUploadFeeResp,
  324. consumeType: isUserPersonalAccount ? 0 : 2,
  325. },
  326. {
  327. tradeType: "Outpatient",
  328. }
  329. );
  330. } catch (error) {
  331. console.log("error ===>", error);
  332. } finally {
  333. my.hideLoading();
  334. }
  335. if (result) {
  336. console.log("pay result ===>", result);
  337. // 支付成功后返回列表页
  338. // my.navigateBack();
  339. // 支付成功后暂时返回首页,方便通过验收
  340. const len = getCurrentPages().length;
  341. my.navigateBack({
  342. delta: len + 1,
  343. });
  344. }
  345. },
  346. },
  347. })
  348. );