AnalysisPulseComponent.vue 10 KB

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