AnalysisPulseComponent.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <script setup lang="ts">
  2. import type { SimplePulseModel } from '../../@types/pulse';
  3. import { createReusableTemplate, tryOnMounted } from '@vueuse/core';
  4. import { useRequest } from 'alova/client';
  5. import { getPulseAgentMethod } from '@/request/api/pulse.api';
  6. import { getURLSearchParamsByUrl } from '@/tools';
  7. import HandLeft from '@/assets/images/pulse-hand-left.png?url';
  8. import HandRight from '@/assets/images/pulse-hand-right.png?url';
  9. import { useVisitor } from '@/stores';
  10. import { Toast } from '@/platform';
  11. type Props = Partial<SimplePulseModel> & { title?: string; results?: string; disabled?: boolean; simple?: boolean };
  12. const { title = '分析', disabled = false, simple = false, results, ..._report } = defineProps<Props>();
  13. const slots = defineSlots();
  14. const Visitor = useVisitor();
  15. const report = computed(() => (unref(results) ? Object.assign(_report, { results }) : Visitor.pulseReport));
  16. const parseUrl = (value?: string) => {
  17. const [url, query = ''] = (value ?? report.value?.url)?.split('report.html') ?? [];
  18. const params = getURLSearchParamsByUrl(query);
  19. if (url && params.has('appId') && params.has('access_session')) {
  20. const replace /* 绘制 3D 跨域 */ = () => [
  21. `https://wx.hzliuzhi.com/mz/hybrid/`,
  22. ].includes(url) ? 'https://hybrid.reborn-tech.com/' : url;
  23. return {
  24. appId: params.get('appId')!!,
  25. token: params.get('access_session')!!,
  26. url: `${replace()}report.html#/`,
  27. };
  28. }
  29. };
  30. const stringifyUrl = (panel: 'left' | 'right', simple = false, overall = true) => {
  31. const fragment = overall ? [`overall-${panel}`] : [];
  32. if (!simple) fragment.push(`${panel}-hand`);
  33. if (!fragment.length) return '';
  34. const value = fragment.map((value) => `${value.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())}=true`).join('&');
  35. return `${agent.value!.url}single?appId=${agent.value!.appId}&access_session=${agent.value!.token}&mid=${report.value?.measureId}&${value}`;
  36. };
  37. const { data: agent, loading, send: getPulseAgent } = useRequest(() => getPulseAgentMethod(), {
  38. initialData: parseUrl(report.value?.url),
  39. immediate: false,
  40. });
  41. const card = useTemplateRef('card');
  42. const panelConfig = reactive({
  43. height: 0,
  44. anchors: [0],
  45. });
  46. const offset = computed(() => panelConfig.anchors[panelConfig.anchors.length - 1] - panelConfig.height);
  47. const getHeightAndScrollTop = () => {
  48. const el = card.value;
  49. if (!el) return;
  50. el.scrollIntoView({ behavior: 'instant', block: 'start' });
  51. const rect = el.getBoundingClientRect();
  52. const maxHeight = window.innerHeight - rect.top;
  53. const height = maxHeight - rect.height;
  54. panelConfig.anchors = [0, height, maxHeight];
  55. panelConfig.height = height;
  56. };
  57. const preview = reactive({
  58. clickMainPanelPreviewable: true,
  59. leftUrl: void 0 as string | void,
  60. rightUrl: void 0 as string | void,
  61. leftPanelUrl: void 0 as string | void,
  62. rightPanelUrl: void 0 as string | void,
  63. showLeftPanel: false,
  64. showRightPanel: false,
  65. });
  66. const load = async (panel: 'left' | 'right' | boolean = true, simple = false) => {
  67. if (loading.value) return;
  68. if (!agent.value) {
  69. try {
  70. if (__FORBID_AUTO_PROCESS_PULSE_AGENCY__) throw { _: `禁止从设备中获取代理地址` };
  71. const { promise, ...resolvers } = Promise.withResolvers<string>();
  72. await window.bridge.postMessage('pulse:url:get', report.value?.measureId, resolvers);
  73. agent.value = parseUrl(await promise);
  74. } catch (e: any) {
  75. if (e._) console.info(`log: ${e._}`);
  76. agent.value = await getPulseAgent().catch(() => void 0);
  77. }
  78. }
  79. if (!agent.value) return;
  80. if (panel === false) {
  81. preview.leftUrl ??= stringifyUrl('left', simple);
  82. preview.rightUrl ??= stringifyUrl('right', simple);
  83. } else {
  84. if (!slots.exception) {
  85. preview.leftPanelUrl = void 0;
  86. preview.rightPanelUrl = void 0;
  87. }
  88. await nextTick();
  89. if (panel === true || panel === 'left') {
  90. preview.leftPanelUrl ??= stringifyUrl('left', simple);
  91. preview.showLeftPanel = true;
  92. } else {
  93. preview.showLeftPanel = false;
  94. }
  95. if (panel === true || panel === 'right') {
  96. preview.rightPanelUrl ??= stringifyUrl('right', simple);
  97. preview.showRightPanel = true;
  98. } else {
  99. preview.showRightPanel = false;
  100. }
  101. }
  102. };
  103. const panelWrapperRef = useTemplateRef('panel-wrapper-ref');
  104. const open = async (panel?: 'left' | 'right', scroll = true) => {
  105. if (disabled) return;
  106. toast = Toast.loading(500);
  107. await load(panel);
  108. if (scroll) {
  109. const last = panelConfig.height;
  110. getHeightAndScrollTop();
  111. if (last === panelConfig.height) {
  112. const el = panelWrapperRef.value?.$el?.querySelector(`.van-floating-panel__content`);
  113. el.scroll({ top: 0, behavior: 'smooth' });
  114. console.log(panelWrapperRef.value.$el);
  115. }
  116. } else {
  117. const height = window.innerHeight * 0.8;
  118. panelConfig.anchors = [0, height];
  119. panelConfig.height = height;
  120. }
  121. };
  122. let toast;
  123. function iframeLoaded() {
  124. toast?.close();
  125. }
  126. tryOnMounted(() => {
  127. if (!slots.exception) load(false, simple);
  128. preview.clickMainPanelPreviewable = !!slots.exception;
  129. });
  130. const { define: PreviewSlot, reuse: ReusePreview } = createReusableTemplate<{ src: string | void }>();
  131. watchPostEffect(() => {
  132. if (panelConfig.height === 0) {
  133. preview.showLeftPanel = false;
  134. preview.showRightPanel = false;
  135. }
  136. });
  137. </script>
  138. <template>
  139. <van-skeleton class="analysis" :row="5" :loading="report?.results == null" v-if="report?.results !== ''">
  140. <PreviewSlot v-slot="{ src }">
  141. <iframe v-if="src" :src="src" :class="{ simple: !src.includes('Hand=true') }" @load="iframeLoaded()"></iframe>
  142. </PreviewSlot>
  143. <slot>
  144. <div ref="card" class="card m-6 text-lg" @click="panelConfig.height = 0">
  145. <div v-if="title" class="card__title mb-3 text-primary text-2xl font-bold">{{ title }}</div>
  146. <slot name="content" :report="report">
  147. <div class="card__content">
  148. <div v-if="report?.summaryLabel" class="flex justify-evenly">
  149. <div v-if="report?.summaryLabel?.left" class="part flex flex-row-reverse justify-center" @click.stop="preview.clickMainPanelPreviewable && open('left')">
  150. <img style="width: 100px; height: 200px" :src="HandLeft" alt="左手" />
  151. <div class="flex flex-col justify-evenly translate-y-2 h-40 text-xl" :class="{ highlight: preview.showLeftPanel }">
  152. <div>寸:<span style="letter-spacing: 4px">{{ report.summaryLabel.left.cun }}</span></div>
  153. <div>关:<span style="letter-spacing: 4px">{{ report.summaryLabel.left.guan }}</span></div>
  154. <div>尺:<span style="letter-spacing: 4px">{{ report.summaryLabel.left.chi }}</span>
  155. </div>
  156. </div>
  157. <div v-if="!disabled && preview.clickMainPanelPreviewable" class="absolute bottom-2 text-primary text-sm">查看脉象详情</div>
  158. </div>
  159. <div v-if="report?.summaryLabel?.right" class="part flex justify-center" @click.stop="preview.clickMainPanelPreviewable && open('right')">
  160. <img style="width: 100px; height: 200px" :src="HandRight" alt="右手" />
  161. <div class="flex flex-col justify-evenly translate-y-2 h-40 text-xl" :class="{ highlight: preview.showRightPanel }">
  162. <div>寸:<span style="letter-spacing: 4px">{{ report.summaryLabel.right.cun }}</span></div>
  163. <div>关:<span style="letter-spacing: 4px">{{ report.summaryLabel.right.guan }}</span></div>
  164. <div>尺:<span style="letter-spacing: 4px">{{ report.summaryLabel.right.chi }}</span></div>
  165. </div>
  166. <div v-if="!disabled && preview.clickMainPanelPreviewable" class="absolute bottom-2 text-primary text-sm">查看脉象详情</div>
  167. </div>
  168. </div>
  169. <p v-if="report?.results" class="text-2xl text-center" @click.stop="preview.clickMainPanelPreviewable && open()">
  170. 总体脉象:<span class="text-primary-400" style="letter-spacing: 4px">{{ report.results }}</span>
  171. </p>
  172. </div>
  173. </slot>
  174. </div>
  175. <slot name="exception">
  176. <div v-if="agent" class="grid grid-rows-1 grid-cols-1 gap-8 m-6">
  177. <div class="card text-lg" v-if="report?.summaryLabel?.left" @click="panelConfig.height = 0">
  178. <div class="card__title mb-3 text-primary text-2xl font-bold flex justify-between" @click.stop="simple && open('left', false)">
  179. <div>左手脉象: {{ report.summaryLabel.left.summary?.join('、') }}</div>
  180. <van-icon v-if="simple" class="text-xl" name="arrow" />
  181. </div>
  182. <div class="card__content">
  183. <ReusePreview v-if="panelConfig.height < 100" :src="preview.leftUrl"></ReusePreview>
  184. </div>
  185. </div>
  186. <div class="card text-lg" v-if="report?.summaryLabel?.right" @click="panelConfig.height = 0">
  187. <div class="card__title mb-3 text-primary text-2xl font-bold flex justify-between" @click.stop="simple && open('right', false)">
  188. <div>右手脉象: {{ report.summaryLabel.right.summary?.join('、') }}</div>
  189. <van-icon v-if="simple" class="text-xl" name="arrow" />
  190. </div>
  191. <div class="card__content">
  192. <ReusePreview v-if="panelConfig.height < 100" :src="preview.rightUrl"></ReusePreview>
  193. </div>
  194. </div>
  195. </div>
  196. </slot>
  197. </slot>
  198. <van-floating-panel ref="panel-wrapper-ref" class="pulse-info-panel" :content-draggable="false" :lock-scroll="true" :anchors="panelConfig.anchors" v-model:height="panelConfig.height">
  199. <template #header>
  200. <div class="van-floating-panel__header !justify-between">
  201. <div></div>
  202. <div class="van-floating-panel__header-bar"></div>
  203. <van-icon class="pr-2" name="cross" @click="panelConfig.height = 0" />
  204. </div>
  205. </template>
  206. <div class="area" :class="{ last: !preview.showRightPanel }" v-if="preview.showLeftPanel && report?.summaryLabel?.left">
  207. <div class="title">左手脉象: {{ report.summaryLabel.left.summary?.join('、') }}</div>
  208. <ReusePreview :src="preview.leftPanelUrl"></ReusePreview>
  209. </div>
  210. <div class="area" v-if="preview.showRightPanel && report?.summaryLabel?.right">
  211. <div class="title">右手脉象: {{ report.summaryLabel.right.summary?.join('、') }}</div>
  212. <ReusePreview :src="preview.rightPanelUrl"></ReusePreview>
  213. </div>
  214. </van-floating-panel>
  215. </van-skeleton>
  216. </template>
  217. <style scoped lang="scss">
  218. .part {
  219. position: relative;
  220. width: 50%;
  221. }
  222. .highlight {
  223. color: rgb(52 167 107 / var(--tw-text-opacity, 1));
  224. & + div {
  225. opacity: 0.5;
  226. }
  227. }
  228. $h: 1660px;
  229. iframe {
  230. width: 100%;
  231. height: $h;
  232. border: none;
  233. &.simple {
  234. height: 432px;
  235. }
  236. }
  237. .pulse-info-panel {
  238. --van-floating-panel-background: #fff;
  239. .van-icon {
  240. color: var(--van-floating-panel-bar-color);
  241. }
  242. .area {
  243. margin: auto;
  244. padding-left: 12px;
  245. min-width: 320px;
  246. max-width: 1200px;
  247. .title {
  248. padding-left: 9px;
  249. font-family:
  250. Helvetica Neue,
  251. Helvetica,
  252. Roboto,
  253. Tahoma,
  254. Arial,
  255. PingFang SC,
  256. sans-serif;
  257. font-size: 18px;
  258. font-weight: 700;
  259. color: #1f2d3d;
  260. line-height: 36px;
  261. }
  262. &:last-of-type,
  263. &.last {
  264. iframe {
  265. height: calc(#{$h} + v-bind(offset) * 1px);
  266. }
  267. }
  268. }
  269. }
  270. </style>