logger.preview.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <script setup lang="ts">
  2. import type { LoggerModel } from '@/request/model/manage.model';
  3. defineOptions({
  4. name: 'LoggerPreview',
  5. inheritAttrs: false,
  6. });
  7. const props = defineProps<
  8. LoggerModel & {
  9. onComplete?: (result?: LoggerModel) => void;
  10. onCancel?: () => void;
  11. }
  12. >();
  13. const BUSINESS_TYPES: Record<number, string> = {
  14. 0: '其它',
  15. 1: '新增',
  16. 2: '修改',
  17. 3: '删除',
  18. 4: '授权',
  19. 5: '导出',
  20. 6: '导入',
  21. 7: '强退',
  22. 8: '生成代码',
  23. 9: '清空数据',
  24. 10: '其它',
  25. };
  26. const OPERATOR_TYPES: Record<number, string> = {
  27. 1: '后台用户',
  28. 2: '手机端用户',
  29. };
  30. function display(v: unknown): string {
  31. if (v === null || v === undefined || v === '') return '—';
  32. return String(v);
  33. }
  34. function prettyJson(text: string | null | undefined): string {
  35. if (text == null || text === '') return '—';
  36. try {
  37. return JSON.stringify(JSON.parse(text), null, 2);
  38. } catch {
  39. return text;
  40. }
  41. }
  42. function mapDict(v: unknown, dict: Record<number, string>): string {
  43. if (v === null || v === undefined || v === '') return '—';
  44. const n = Number(v);
  45. if (Number.isNaN(n)) return String(v);
  46. return dict[n] ?? String(v);
  47. }
  48. const detail = computed(() => {
  49. const raw = props.__raw__;
  50. if (raw && typeof raw === 'object') {
  51. return raw as Record<string, unknown>;
  52. }
  53. return {
  54. operId: props.id,
  55. title: props.title,
  56. operTime: props.date,
  57. operName: props.operator,
  58. } as Record<string, unknown>;
  59. });
  60. const operParamPretty = computed(() => prettyJson(detail.value.operParam as string | undefined));
  61. const jsonResultPretty = computed(() => prettyJson(detail.value.jsonResult as string | undefined));
  62. const statusTagType = computed(() => {
  63. const s = detail.value.status;
  64. if (s === 0) return 'success' as const;
  65. if (s === 1) return 'danger' as const;
  66. return 'default' as const;
  67. });
  68. const statusLabel = computed(() => {
  69. const s = detail.value.status;
  70. if (s === 0) return '成功';
  71. if (s === 1) return '失败';
  72. return display(s);
  73. });
  74. const showError = computed(() => {
  75. const msg = detail.value.errorMsg;
  76. if (msg !== null && msg !== undefined && String(msg).trim() !== '') return true;
  77. return detail.value.status === 1;
  78. });
  79. const collapseActive = ref<string[]>([]);
  80. </script>
  81. <template>
  82. <div class="logger-preview pb-safe">
  83. <van-cell-group inset title="概要">
  84. <van-cell title="模块标题" :value="display(detail.title)" />
  85. <van-cell title="操作 ID" :value="display(detail.operId)" />
  86. <van-cell title="操作人" :value="display(detail.operName)" />
  87. <van-cell title="操作时间" :value="display(detail.operTime)" />
  88. <!-- <van-cell title="业务类型" :value="mapDict(detail.businessType, BUSINESS_TYPES)" />-->
  89. <!-- <van-cell title="操作类别" :value="mapDict(detail.operatorType, OPERATOR_TYPES)" />-->
  90. <!-- <van-cell title="部门" :value="display(detail.deptName)" />-->
  91. <van-cell title="备注" :value="display(detail.remark)" />
  92. <van-cell title="状态与请求">
  93. <template #value>
  94. <div class="tag-row">
  95. <van-tag v-if="detail.requestMethod" plain type="primary">{{ display(detail.requestMethod) }}</van-tag>
  96. <van-tag :type="statusTagType">{{ statusLabel }}</van-tag>
  97. </div>
  98. </template>
  99. </van-cell>
  100. </van-cell-group>
  101. <van-cell-group v-if="showError" inset title="错误" class="mt-3">
  102. <van-cell title="错误信息">
  103. <template #value>
  104. <span class="text-danger">{{ display(detail.errorMsg) }}</span>
  105. </template>
  106. </van-cell>
  107. </van-cell-group>
  108. <van-cell-group inset title="请求信息" class="mt-3">
  109. <van-cell title="请求 URL" :value="display(detail.operUrl)" />
  110. <van-cell title="IP" :value="display(detail.operIp)">
  111. <template #value>
  112. <div class="tag-row">
  113. <van-tag v-if="detail.operLocation" plain>{{ detail.operLocation }}</van-tag>
  114. <span>{{ display(detail.operIp) }}</span>
  115. </div>
  116. </template>
  117. </van-cell>
  118. <van-cell title="调用方法">
  119. <template #value>
  120. <span class="mono-wrap">{{ display(detail.method) }}</span>
  121. </template>
  122. </van-cell>
  123. </van-cell-group>
  124. <van-collapse v-model="collapseActive" class="mt-3 logger-preview__collapse">
  125. <van-collapse-item title="请求参数" name="params">
  126. <pre class="json-block">{{ operParamPretty }}</pre>
  127. </van-collapse-item>
  128. <van-collapse-item title="返回结果" name="result">
  129. <pre class="json-block">{{ jsonResultPretty }}</pre>
  130. </van-collapse-item>
  131. </van-collapse>
  132. <div class="px-4 py-3">
  133. <van-button block type="primary" plain @click="onCancel?.()">关闭</van-button>
  134. </div>
  135. </div>
  136. </template>
  137. <style scoped lang="scss">
  138. .logger-preview {
  139. --van-cell-group-title-color: hsl(var(--primary-color-hover) / 0.5);
  140. --van-cell-text-color: hsl(var(--primary-hover) / 0.5);
  141. --van-cell-value-color: #fff;
  142. --van-cell-value-font-size: 14px;
  143. :deep(.van-cell__value) {
  144. flex: 2;
  145. }
  146. }
  147. .tag-row {
  148. display: flex;
  149. flex-wrap: wrap;
  150. gap: 6px;
  151. justify-content: flex-end;
  152. }
  153. .text-danger {
  154. color: var(--van-danger-color);
  155. text-align: right;
  156. word-break: break-word;
  157. }
  158. .mono-wrap {
  159. display: block;
  160. max-width: 100%;
  161. font-size: 12px;
  162. line-height: 1.45;
  163. text-align: right;
  164. word-break: break-all;
  165. white-space: pre-wrap;
  166. }
  167. .logger-preview__collapse {
  168. margin-left: 16px;
  169. margin-right: 16px;
  170. overflow: hidden;
  171. border-radius: var(--van-radius-lg);
  172. }
  173. .json-block {
  174. margin: 0;
  175. padding: 8px 0 4px;
  176. font-size: 12px;
  177. line-height: 1.5;
  178. word-break: break-all;
  179. white-space: pre-wrap;
  180. color: #fff;
  181. }
  182. .pb-safe {
  183. padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
  184. }
  185. </style>