scheme.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import I18nBehavior from "../../../../i18n/behavior";
  3. import TickleBehavior, {
  4. getTickleContext,
  5. } from "../../../../core/behavior/tickle.behavior";
  6. // module/health/pages/scheme/scheme.ts
  7. import { healthSchemeMethod } from "../../request";
  8. import { toReportPage } from "../../router";
  9. Component({
  10. behaviors: [I18nBehavior, PageContainerBehavior, TickleBehavior],
  11. lifetimes: {
  12. attached() {
  13. this.getHealthScheme(this.data.id);
  14. },
  15. },
  16. properties: {
  17. id: { type: String, value: "" },
  18. },
  19. data: {
  20. i18n: {
  21. report: { title: '报告' },
  22. scheme: { title: '方案', date: '' },
  23. orderText:{goPay:'去确认'}
  24. },
  25. dataset: null,
  26. schemeId: "",
  27. healthIndex: { data: [], loading: false, message: "" },
  28. showQrCodePopup: false,
  29. qrCodeImageUrl: "",
  30. },
  31. observers: {},
  32. methods: {
  33. async getHealthScheme(id: string) {
  34. wx.showLoading({ title: "加载中" });
  35. try {
  36. const dataset = await healthSchemeMethod(id);
  37. this.setData({ dataset });
  38. } catch (error) {
  39. getTickleContext
  40. .call(this)
  41. .showErrorMessage(error.errMsg || error.message, 0);
  42. }
  43. wx.hideLoading();
  44. },
  45. toReportPage() {
  46. toReportPage(this.data.id);
  47. },
  48. // 跳转到商品选择页面
  49. goToProductPage() {
  50. wx.setStorageSync('healthAnalysisReportId', this.data.id);
  51. if (this.data.id) {
  52. wx.redirectTo({
  53. url: `/module/order/pages/select-goods/select-goods?healthAnalysisReportId=${this.data.id}`,
  54. fail: (err) => {
  55. getTickleContext.call(this).showWarnMessage(err.errMsg || "跳转失败");
  56. },
  57. });
  58. } else {
  59. wx.showToast({
  60. title: "健康评估报告ID不能为空",
  61. icon: "none",
  62. });
  63. }
  64. },
  65. // 去购买
  66. goBuy(this: any, e: any) {
  67. const item = e.currentTarget.dataset.item || {};
  68. const type: string = (item.buyType || "").toLowerCase();
  69. const url: string = item.buyUrl || "";
  70. const shortImageUrl: string = item.shortImageUrl || "";
  71. // 跳转小程序
  72. if (type === "miniprogram") {
  73. // 如果没有跳转链接,直接显示小程序码或提示错误
  74. if (!url) {
  75. if (shortImageUrl) {
  76. this.setData({
  77. showQrCodePopup: true,
  78. qrCodeImageUrl: shortImageUrl,
  79. });
  80. } else {
  81. getTickleContext.call(this).showWarnMessage("缺少小程序跳转参数");
  82. }
  83. return;
  84. }
  85. // 解析 miniprogram:// 格式的 URL
  86. let parsedAppId = "";
  87. let parsedPath = "";
  88. const isMiniprogramUrl = url.startsWith("miniprogram://");
  89. if (isMiniprogramUrl) {
  90. // 解析 miniprogram://?appid=xxx&path=xxx 格式
  91. try {
  92. // 提取查询参数字符串(去掉 miniprogram://? 前缀)
  93. const queryString = url.replace(/^miniprogram:\/\/(\?)?/, "");
  94. // 解析查询参数
  95. const params: Record<string, string> = {};
  96. queryString.split("&").forEach((param) => {
  97. const [key, value] = param.split("=");
  98. if (key && value) {
  99. params[key] = decodeURIComponent(value);
  100. }
  101. });
  102. parsedAppId = params.appid || "";
  103. parsedPath = params.path || "";
  104. } catch (error) {
  105. console.error("解析 miniprogram:// URL 失败:", error);
  106. }
  107. }
  108. // 统一的错误处理函数:显示小程序码或错误提示
  109. const showErrorOrQrCode = (errMsg?: string) => {
  110. if (shortImageUrl) {
  111. this.setData({
  112. showQrCodePopup: true,
  113. qrCodeImageUrl: shortImageUrl,
  114. });
  115. } else {
  116. getTickleContext
  117. .call(this)
  118. .showWarnMessage(errMsg || "跳转小程序失败");
  119. }
  120. };
  121. // 统一的跳转函数:使用 appId + path 方式跳转
  122. const navigateWithAppIdAndPath = (appId: string, path: string) => {
  123. wx.navigateToMiniProgram({
  124. appId: appId,
  125. path: path,
  126. envVersion: "release",
  127. fail: (pathErr) => {
  128. if (
  129. pathErr.errMsg.includes("navigateToMiniProgram:fail cancel")
  130. ) {
  131. console.log("用户取消appid小程序跳转");
  132. } else {
  133. showErrorOrQrCode(pathErr.errMsg);
  134. }
  135. },
  136. });
  137. };
  138. // 如果是 miniprogram:// 格式,直接使用 appId + path 方式跳转
  139. if (isMiniprogramUrl) {
  140. if (parsedAppId && parsedPath) {
  141. navigateWithAppIdAndPath(parsedAppId, parsedPath);
  142. }
  143. } else {
  144. // 短链接格式,优先使用短链接方式跳转
  145. wx.navigateToMiniProgram({
  146. shortLink: url,
  147. envVersion: "release",
  148. fail: (err) => {
  149. if (err.errMsg.includes("navigateToMiniProgram:fail cancel")) {
  150. console.log("用户取消短链跳转");
  151. } else {
  152. // 短链接失败,尝试使用 appId + path 方式跳转
  153. if (parsedAppId && parsedPath) {
  154. navigateWithAppIdAndPath(parsedAppId, parsedPath);
  155. } else {
  156. // 没有 appId 和 path,直接显示弹窗或错误提示
  157. showErrorOrQrCode(err.errMsg || "跳转小程序失败");
  158. }
  159. }
  160. },
  161. });
  162. }
  163. } else if (type === "url") {
  164. // h5链接
  165. if (!url) {
  166. getTickleContext.call(this).showWarnMessage("无有效链接");
  167. return;
  168. }
  169. wx.navigateTo({
  170. url: "/module/article/pages/science-info/science-info",
  171. success: (res) => {
  172. res.eventChannel?.emit?.("load", {
  173. title: item.title || "详情",
  174. url,
  175. });
  176. },
  177. fail: (err) => {
  178. getTickleContext
  179. .call(this)
  180. .showWarnMessage(err.errMsg || "打开页面失败");
  181. },
  182. });
  183. return;
  184. } else {
  185. // 无法识别
  186. getTickleContext.call(this).showWarnMessage("未识别的跳转类型");
  187. }
  188. },
  189. // 关闭小程序码弹窗
  190. closeQrCodePopup(e?: any) {
  191. // 如果 visible-change 事件触发且 visible 为 false,才关闭
  192. if (e === undefined || !e.detail?.visible) {
  193. this.setData({
  194. showQrCodePopup: false,
  195. qrCodeImageUrl: "",
  196. });
  197. }
  198. },
  199. },
  200. });