gaoluyang
13 小时以前 045da5de062c1b43f53bc7b6a4cf125fbd97a3e5
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Upload" @click="openUploadDialog">上传APK</el-button>
      </el-col>
    </el-row>
 
    <el-table v-loading="loading" :data="versionList">
      <el-table-column label="ID" prop="id" align="center" width="80" />
      <el-table-column label="应用名称" prop="name" align="center" min-width="150" />
      <el-table-column label="版本号" prop="version" align="center" width="120" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" prop="updateTime" align="center" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createUser" align="center" width="100" />
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="downloadAttachment(scope.row)">下载</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
 
    <el-dialog title="上传APK" v-model="uploadOpen" width="560px" append-to-body @close="resetUploadForm">
      <el-form ref="uploadRef" :model="uploadForm" :rules="uploadRules" label-width="90px">
        <el-form-item label="应用名称" prop="name">
          <el-input v-model="uploadForm.name" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="uploadForm.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="APK文件" prop="file">
          <el-upload
            :auto-upload="false"
            :show-file-list="true"
            :limit="1"
            accept=".apk"
            :on-change="handleApkChange"
            :on-remove="handleApkRemove"
            :on-exceed="handleApkExceed"
            :before-upload="beforeApkUpload"
          >
            <el-button type="primary">选择APK文件</el-button>
            <template #tip>
              <div class="el-upload__tip">只能上传 APK 文件,且不超过 200MB</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="uploading" @click="submitUpload">确 定</el-button>
          <el-button @click="uploadOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup name="SystemAppVersion">
import { listAppVersion, uploadApk } from "@/api/system/appVersion";
 
const { proxy } = getCurrentInstance();
 
const loading = ref(false);
const versionList = ref([]);
const total = ref(0);
 
const queryParams = reactive({
  current: 1,
  size: 10,
});
 
const uploadOpen = ref(false);
const uploading = ref(false);
const uploadForm = reactive({
  name: "",
  version: "",
  file: null,
});
 
const uploadRules = {
  name: [{ required: true, message: "请输入应用名称", trigger: "blur" }],
  version: [{ required: true, message: "请输入版本号", trigger: "blur" }],
  file: [{ required: true, message: "请上传APK文件", trigger: "change" }],
};
 
function normalizeListResp(res) {
  const data = res?.data || {};
  const records = data.records || res?.rows || [];
  const totalNum = Number(data.total ?? res?.total ?? 0);
  return {
    records: Array.isArray(records) ? records : [],
    total: Number.isNaN(totalNum) ? 0 : totalNum,
  };
}
 
function getList() {
  loading.value = true;
  listAppVersion(queryParams)
    .then(res => {
      const result = normalizeListResp(res);
      versionList.value = result.records;
      total.value = result.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
 
function openUploadDialog() {
  resetUploadForm();
  uploadOpen.value = true;
}
 
function resetUploadForm() {
  uploadForm.name = "";
  uploadForm.version = "";
  uploadForm.file = null;
  proxy.resetForm("uploadRef");
}
 
function beforeApkUpload(file) {
  const isApk = /\.apk$/i.test(file.name);
  if (!isApk) {
    proxy.$modal.msgWarning("只能上传APK文件");
    return false;
  }
  const isLt200M = file.size / 1024 / 1024 < 200;
  if (!isLt200M) {
    proxy.$modal.msgWarning("APK 文件大小不能超过 200MB");
    return false;
  }
  return true;
}
 
function handleApkChange(file) {
  if (!file || !file.raw) return;
  if (!beforeApkUpload(file.raw)) {
    uploadForm.file = null;
    proxy.$refs.uploadRef?.clearValidate("file");
    return;
  }
  uploadForm.file = file.raw;
}
 
function handleApkRemove() {
  uploadForm.file = null;
}
 
function handleApkExceed() {
  proxy.$modal.msgWarning("只能上传一个APK文件");
}
 
function downloadAttachment(row) {
  const filePath =
    (row.commonFileList &&
      row.commonFileList.length > 0 &&
      row.commonFileList[0].url) ||
    row.url;
  if (!filePath) {
    proxy.$modal.msgError("下载链接不存在");
    return;
  }
 
  const link = document.createElement("a");
  const rawName = String(row.name || "").trim();
  const fallbackName = String(filePath).split("/").pop()?.split("?")[0] || "app";
  const baseName = rawName || fallbackName;
  const downloadName = /\.apk$/i.test(baseName) ? baseName : `${baseName}.apk`;
  console.log(downloadName,filePath,'downloadName,filePath');
  link.href = filePath;
  link.download = downloadName;
  link.target = "_blank";
  link.rel = "noopener noreferrer";
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}
 
function submitUpload() {
  proxy.$refs.uploadRef.validate(valid => {
    if (!valid) return;
    const formData = new FormData();
    formData.append("name", uploadForm.name);
    formData.append("version", uploadForm.version);
    formData.append("file", uploadForm.file);
 
    uploading.value = true;
    uploadApk(formData)
      .then(() => {
        proxy.$modal.msgSuccess("上传成功");
        uploadOpen.value = false;
        getList();
      })
      .finally(() => {
        uploading.value = false;
      });
  });
}
 
onMounted(() => {
  getList();
});
</script>