dataParse.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div class="flex">
  3. <codeEditor class="params flex1" ref="mirrorRef" mode="" :content="content"></codeEditor>
  4. <div class="mock" style="width: 300px;margin-left: 20px;">
  5. <el-input class="input" v-model="inputData" type="textarea" placeholder="请输入入参,以字符串的方式,如果是对象字符串会在执行时自动转换为对象再执行"></el-input>
  6. <el-input class="output" v-model="outputData" type="textarea" readonly placeholder="此处显示执行结果"></el-input>
  7. </div>
  8. </div>
  9. <el-button type="primary" style="margin-top:20px" v-auth="'save'" @click="saveCode">保存脚本</el-button>
  10. <el-button type="primary" style="margin-top:20px" v-auth="'debug'" @click="mock">调试</el-button>
  11. </template>
  12. <script lang="ts" setup>
  13. import { onMounted, ref } from 'vue';
  14. import codeEditor from '/@/components/codeEditor/index.vue'
  15. import { ElMessage } from 'element-plus';
  16. import api from '/@/api/device';
  17. import { useRoute } from 'vue-router';
  18. const route = useRoute();
  19. const emit = defineEmits(['updateScript'])
  20. const props = defineProps({
  21. script: String
  22. })
  23. const inputData = ref('')
  24. const outputData = ref('')
  25. const content = ref('')
  26. const mirrorRef = ref()
  27. onMounted(() => {
  28. content.value = props.script!
  29. })
  30. function saveCode() {
  31. const funStr = mirrorRef.value.getValue()
  32. if (funStr === '') {
  33. return toSave(funStr)
  34. }
  35. try {
  36. eval("(" + funStr + ")")
  37. toSave(funStr)
  38. } catch (error) {
  39. ElMessage.error('语法校验未通过')
  40. }
  41. }
  42. function toSave(data: string) {
  43. api.product.script({
  44. id: route.params.id,
  45. scriptInfo: data
  46. }).then(() => {
  47. ElMessage.success('保存成功')
  48. emit('updateScript', data)
  49. })
  50. }
  51. function mock() {
  52. if (!inputData.value) return ElMessage.error('请输入参数')
  53. const funStr = mirrorRef.value.getValue()
  54. if (funStr === '') {
  55. return ElMessage.error('请先输入可执行脚本')
  56. }
  57. try {
  58. const fun = eval("(" + funStr + ")")
  59. try {
  60. const res = fun(JSON.parse(inputData.value))
  61. outputData.value = typeof res === 'object' ? JSON.stringify(res, null, 2) : res
  62. } catch {
  63. const res = fun(inputData.value)
  64. outputData.value = typeof res === 'object' ? JSON.stringify(res, null, 2) : res
  65. }
  66. } catch (error) {
  67. ElMessage.error('数据解析脚本语法校验未通过或参数类型有误')
  68. }
  69. }
  70. </script>
  71. <style lang="scss" scoped>
  72. ::v-deep(.CodeMirror) {
  73. height: calc(100vh - 360px);
  74. }
  75. .input,
  76. .output {
  77. ::v-deep(.el-textarea__inner) {
  78. height: calc(50vh - 190px);
  79. }
  80. }
  81. .output {
  82. margin-top: 20px;
  83. }
  84. </style>