scheme.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. 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. console.log(item, "item", type, url);
  72. // 跳转小程序
  73. if (type === "miniprogram") {
  74. // 如果没有跳转链接,直接显示小程序码或提示错误
  75. if (!url) {
  76. if (shortImageUrl) {
  77. this.setData({
  78. showQrCodePopup: true,
  79. qrCodeImageUrl: shortImageUrl,
  80. });
  81. } else {
  82. getTickleContext.call(this).showWarnMessage("缺少小程序跳转参数");
  83. }
  84. return;
  85. }
  86. // 解析 miniprogram:// 格式的 URL
  87. let parsedAppId = "";
  88. let parsedPath = "";
  89. const isMiniprogramUrl = url.startsWith("miniprogram://");
  90. if (isMiniprogramUrl) {
  91. // 解析 miniprogram://?appid=xxx&path=xxx 格式
  92. try {
  93. // 提取查询参数字符串(去掉 miniprogram://? 前缀)
  94. const queryString = url.replace(/^miniprogram:\/\/(\?)?/, "");
  95. // 解析查询参数
  96. const params: Record<string, string> = {};
  97. queryString.split("&").forEach((param) => {
  98. const [key, value] = param.split("=");
  99. if (key && value) {
  100. params[key] = decodeURIComponent(value);
  101. }
  102. });
  103. parsedAppId = params.appid || "";
  104. parsedPath = params.path || "";
  105. } catch (error) {
  106. console.error("解析 miniprogram:// URL 失败:", error);
  107. }
  108. }
  109. // 统一的错误处理函数:显示小程序码或错误提示
  110. const showErrorOrQrCode = (errMsg?: string) => {
  111. if (shortImageUrl) {
  112. this.setData({
  113. showQrCodePopup: true,
  114. qrCodeImageUrl: shortImageUrl,
  115. });
  116. } else {
  117. getTickleContext
  118. .call(this)
  119. .showWarnMessage(errMsg || "跳转小程序失败");
  120. }
  121. };
  122. // 统一的跳转函数:使用 appId + path 方式跳转
  123. const navigateWithAppIdAndPath = (appId: string, path: string) => {
  124. wx.navigateToMiniProgram({
  125. appId: appId,
  126. path: path,
  127. envVersion: "release",
  128. fail: (pathErr) => {
  129. if (
  130. pathErr.errMsg.includes("navigateToMiniProgram:fail cancel")
  131. ) {
  132. console.log("用户取消appid小程序跳转");
  133. } else {
  134. showErrorOrQrCode(pathErr.errMsg);
  135. }
  136. },
  137. });
  138. };
  139. // 如果是 miniprogram:// 格式,直接使用 appId + path 方式跳转
  140. if (isMiniprogramUrl) {
  141. if (parsedAppId && parsedPath) {
  142. navigateWithAppIdAndPath(parsedAppId, parsedPath);
  143. }
  144. } else {
  145. // 短链接格式,优先使用短链接方式跳转
  146. wx.navigateToMiniProgram({
  147. shortLink: url,
  148. envVersion: "release",
  149. fail: (err) => {
  150. if (err.errMsg.includes("navigateToMiniProgram:fail cancel")) {
  151. console.log("用户取消短链跳转");
  152. } else {
  153. // 短链接失败,尝试使用 appId + path 方式跳转
  154. if (parsedAppId && parsedPath) {
  155. navigateWithAppIdAndPath(parsedAppId, parsedPath);
  156. } else {
  157. // 没有 appId 和 path,直接显示弹窗或错误提示
  158. showErrorOrQrCode(err.errMsg || "跳转小程序失败");
  159. }
  160. }
  161. },
  162. });
  163. }
  164. } else if (type === "url") {
  165. // h5链接
  166. if (!url) {
  167. getTickleContext.call(this).showWarnMessage("无有效链接");
  168. return;
  169. }
  170. wx.navigateTo({
  171. url: "/module/article/pages/science-info/science-info",
  172. success: (res) => {
  173. res.eventChannel?.emit?.("load", {
  174. title: item.title || "详情",
  175. url,
  176. });
  177. },
  178. fail: (err) => {
  179. getTickleContext
  180. .call(this)
  181. .showWarnMessage(err.errMsg || "打开页面失败");
  182. },
  183. });
  184. return;
  185. } else {
  186. // 无法识别
  187. getTickleContext.call(this).showWarnMessage("未识别的跳转类型");
  188. }
  189. },
  190. // 关闭小程序码弹窗
  191. closeQrCodePopup(e?: any) {
  192. // 如果 visible-change 事件触发且 visible 为 false,才关闭
  193. if (e === undefined || !e.detail?.visible) {
  194. this.setData({
  195. showQrCodePopup: false,
  196. qrCodeImageUrl: "",
  197. });
  198. }
  199. },
  200. },
  201. });