scheme.ts 6.7 KB

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