scheme.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. },
  24. dataset: null,
  25. schemeId: "",
  26. healthIndex: { data: [], loading: false, message: "" },
  27. showQrCodePopup: false,
  28. qrCodeImageUrl: "",
  29. },
  30. observers: {},
  31. methods: {
  32. async getHealthScheme(id: string) {
  33. wx.showLoading({ title: "加载中" });
  34. try {
  35. const dataset = await healthSchemeMethod(id);
  36. this.setData({ dataset });
  37. } catch (error) {
  38. console.log(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. goBuy(this: any, e: any) {
  50. const item = e.currentTarget.dataset.item || {};
  51. const type: string = (item.buyType || "").toLowerCase();
  52. const url: string = item.buyUrl || "";
  53. const shortImageUrl: string = item.shortImageUrl || "";
  54. console.log(item, "item", type, url);
  55. // 跳转小程序
  56. if (type === "miniprogram") {
  57. // 如果没有跳转链接,直接显示小程序码或提示错误
  58. if (!url) {
  59. if (shortImageUrl) {
  60. this.setData({
  61. showQrCodePopup: true,
  62. qrCodeImageUrl: shortImageUrl,
  63. });
  64. } else {
  65. getTickleContext.call(this).showWarnMessage("缺少小程序跳转参数");
  66. }
  67. return;
  68. }
  69. // 解析 miniprogram:// 格式的 URL
  70. let parsedAppId = "";
  71. let parsedPath = "";
  72. const isMiniprogramUrl = url.startsWith("miniprogram://");
  73. if (isMiniprogramUrl) {
  74. // 解析 miniprogram://?appid=xxx&path=xxx 格式
  75. try {
  76. // 提取查询参数字符串(去掉 miniprogram://? 前缀)
  77. const queryString = url.replace(/^miniprogram:\/\/(\?)?/, "");
  78. // 解析查询参数
  79. const params: Record<string, string> = {};
  80. queryString.split("&").forEach((param) => {
  81. const [key, value] = param.split("=");
  82. if (key && value) {
  83. params[key] = decodeURIComponent(value);
  84. }
  85. });
  86. parsedAppId = params.appid || "";
  87. parsedPath = params.path || "";
  88. } catch (error) {
  89. console.error("解析 miniprogram:// URL 失败:", error);
  90. }
  91. }
  92. // 统一的错误处理函数:显示小程序码或错误提示
  93. const showErrorOrQrCode = (errMsg?: string) => {
  94. if (shortImageUrl) {
  95. this.setData({
  96. showQrCodePopup: true,
  97. qrCodeImageUrl: shortImageUrl,
  98. });
  99. } else {
  100. getTickleContext
  101. .call(this)
  102. .showWarnMessage(errMsg || "跳转小程序失败");
  103. }
  104. };
  105. // 统一的跳转函数:使用 appId + path 方式跳转
  106. const navigateWithAppIdAndPath = (appId: string, path: string) => {
  107. wx.navigateToMiniProgram({
  108. appId: appId,
  109. path: path,
  110. envVersion: "release",
  111. fail: (pathErr) => {
  112. if (
  113. pathErr.errMsg.includes("navigateToMiniProgram:fail cancel")
  114. ) {
  115. console.log("用户取消appid小程序跳转");
  116. } else {
  117. showErrorOrQrCode(pathErr.errMsg);
  118. }
  119. },
  120. });
  121. };
  122. // 如果是 miniprogram:// 格式,直接使用 appId + path 方式跳转
  123. if (isMiniprogramUrl) {
  124. if (parsedAppId && parsedPath) {
  125. navigateWithAppIdAndPath(parsedAppId, parsedPath);
  126. }
  127. } else {
  128. // 短链接格式,优先使用短链接方式跳转
  129. wx.navigateToMiniProgram({
  130. shortLink: url,
  131. envVersion: "release",
  132. fail: (err) => {
  133. if (err.errMsg.includes("navigateToMiniProgram:fail cancel")) {
  134. console.log("用户取消短链跳转");
  135. } else {
  136. // 短链接失败,尝试使用 appId + path 方式跳转
  137. if (parsedAppId && parsedPath) {
  138. navigateWithAppIdAndPath(parsedAppId, parsedPath);
  139. } else {
  140. // 没有 appId 和 path,直接显示弹窗或错误提示
  141. showErrorOrQrCode(err.errMsg || "跳转小程序失败");
  142. }
  143. }
  144. },
  145. });
  146. }
  147. } else if (type === "url") {
  148. // h5链接
  149. if (!url) {
  150. getTickleContext.call(this).showWarnMessage("无有效链接");
  151. return;
  152. }
  153. wx.navigateTo({
  154. url: "/module/article/pages/science-info/science-info",
  155. success: (res) => {
  156. res.eventChannel?.emit?.("load", {
  157. title: item.title || "详情",
  158. url,
  159. });
  160. },
  161. fail: (err) => {
  162. getTickleContext
  163. .call(this)
  164. .showWarnMessage(err.errMsg || "打开页面失败");
  165. },
  166. });
  167. return;
  168. } else {
  169. // 无法识别
  170. getTickleContext.call(this).showWarnMessage("未识别的跳转类型");
  171. }
  172. },
  173. // 关闭小程序码弹窗
  174. closeQrCodePopup(e?: any) {
  175. // 如果 visible-change 事件触发且 visible 为 false,才关闭
  176. if (e === undefined || !e.detail?.visible) {
  177. this.setData({
  178. showQrCodePopup: false,
  179. qrCodeImageUrl: "",
  180. });
  181. }
  182. },
  183. },
  184. });