he wei
2024-12-23 6a4a27022809f9647c781d8135ac8afc3fb7c393
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<template>
  <div class="main">
    <div class="upload" @click="selectFile" @dragover="dragover" @drop="drop">
      <div ref="txt" class="upload__text">
        将文件拖到此处,或点击此处选择文件
      </div>
    </div>
    <div class="upload__tip" slot="tip">
      只能解析.fbx .fbxc .mcp .mch .bres .alm .bcp .chr 文件
    </div>
  </div>
</template>
 
<script>
import axios from "@/assets/js/axios";
 
export default {
  name: "",
 
  data() {
    let reselect = !!this.$route.query.reselect;
    return {
      URL: "",
      reselect,
    };
  },
  components: {},
  methods: {
    change(file) {
      // let $el = this.$refs.input;
      // console.log($el.files[0].path);
      if (!file || !file.raw) {
        return false;
      }
      console.log(file);
      this.loadFile(file.raw);
    },
    loadFile(url) {
      // TODO 文件类型判断
      let arr = url.split(".");
      let type = arr.length > 1 ? arr[arr.length - 1].toLowerCase() : "";
      // 支持的类型
      let allowList = ["fbx", "fbxc", "mcp", "mch", "bres", "alm", "bcp", "chr"];
 
      if (!allowList.some((v) => v == type)) {
        this.$layer.msg("文件类型错误");
        return false;
      }
      let suffixes = "";
      switch (type) {
        case "fbx":
        case "fbxc":
          suffixes = "fbx";
          break;
        case "mcp":
        case "mch":
          suffixes = "mcp";
          break;
        case "bres":
          suffixes = "res";
          break;
        case "alm":
          suffixes = "alm";
          break;
        // 放电
        case "bcp":
          suffixes = "bcp";
          break;
        // 充电
        case "chr":
          suffixes = "chr";
          break;
      }
      let loading = this.$layer.loading(1);
      axios({
        url: "readFboFile",
        method: "GET",
        params: {
          filePath: url,
        },
      }).then((res) => {
        let { code, data } = res.data;
        // console.log(res, res.code, "=========res");
        this.$layer.close(loading);
        if (code) {
          data.filePath = url;
          // 解析成功 跳转到结果页面
          this.$router.push({
            path: `/result-${suffixes}`,
            query: {
              data,
            },
          });
        }
      });
    },
    dragover(e) {
      e.preventDefault();
      return true;
    },
    drop(e) {
      // console.log(e);
      //必须要阻止拖拽的默认事件
      e.preventDefault();
      e.stopPropagation();
 
      //获得拖拽的文件集合
      var files = e.dataTransfer.files;
      // console.log(files);
      if (!files) {
        return false;
      }
      let url = files[0] ? files[0].path : "";
 
      if (url) {
        this.loadFile(url);
      }
    },
    initEvents() {
      console.log("initEvents");
      window.api.receive(
        "selected-file",
        (path, data) => {
          console.log(path, data, "selected-file");
          if (data && data == "selectFile") {
            let url = path.filePaths[0];
            if (url) {
              this.loadFile(path.filePaths[0]);
            }
          }
        },
        true
      );
    },
    selectFile() {
      // console.log("openfile");
      window.api.send("open-file-dialog", "selectFile");
    },
  },
 
  mounted() {
    this.initEvents();
 
    if (this.reselect) {
      this.$refs.txt.click();
    }
  },
};
</script>
 
<style lang="less" scoped>
.main {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.upload {
  /* flex: 1; */
  width: 80%;
  height: 80%;
  display: flex;
  flex-direction: column;
  text-align: center;
  background: #fff;
  border-radius: 8px;
  justify-content: center;
}
.upload__tip {
  color: #fff;
}
// >>> .el-upload {
//   width: 100%;
//   height: 100%;
// }
// >>> .el-upload-dragger {
//   width: 100%;
//   height: 100%;
//   display: flex;
//   flex-direction: column;
//   justify-content: center;
// }
// >>> .el-upload-list {
//   display: none;
// }
// >>> .el-upload__tip {
//   color: #fff;
// }
</style>