better-staridc-MNBT
1<template>
2 <div class="td-page">
3 <div class="td-page-head">
4 <div>
5 <h3 class="td-page-title"><i class="mdi mdi-upload"></i>导入程序</h3>
6 <p class="td-page-subtitle">上传打包导出后的程序包(zip 文件)快速导入</p>
7 </div>
8 <t-button theme="default" variant="outline" @click="goBack">
9 <i class="mdi mdi-arrow-left"></i> 返回列表
10 </t-button>
11 </div>
12
13 <div class="td-set-card">
14 <div class="td-set-card-hd">
15 <div class="td-set-icon"><i class="mdi mdi-package-variant-closed"></i></div>
16 <div>
17 <h4>程序包导入</h4>
18 <p>选择打包导出后的 zip 程序包上传</p>
19 </div>
20 </div>
21 <div class="td-set-card-bd">
22 <div class="td-form">
23 <div class="td-form-row">
24 <label>程序包文件 <span class="req">*</span></label>
25 <div class="file-row">
26 <label class="file-pick">
27 <i class="mdi mdi-file-upload-outline"></i>
28 <span>{{ form.filecx ? form.filecx.name : '选择 .zip 文件…' }}</span>
29 <input type="file" accept=".zip" @change="onPickFile" />
30 </label>
31 <span class="td-text-mute td-text-xs" v-if="form.filecx">
32 {{ fmtSize(form.filecx.size) }}
33 </span>
34 </div>
35 <div class="td-form-hint">仅支持打包导出后的 zip 程序包</div>
36 </div>
37
38 <div class="td-form-switch">
39 <div class="td-form-switch-txt">
40 <strong>导入后自动上架</strong>
41 <span>关闭后保存为下架状态,可在程序列表中再开启</span>
42 </div>
43 <t-switch v-model="form.kg" />
44 </div>
45
46 <div v-if="uploading" class="td-form-row">
47 <label>上传进度</label>
48 <t-progress :percentage="progress" :label="true" />
49 <div class="td-form-hint">正在上传,请勿关闭页面</div>
50 </div>
51
52 <div class="td-form-note">
53 <b>提示:</b> 选择打包导出后的程序包(zip 文件)。导入后将自动写入数据库,可在程序列表中查看与编辑。
54 </div>
55
56 <div class="td-form-actions">
57 <t-button theme="primary" :loading="uploading" :disabled="!form.filecx" @click="submit">
58 <i class="mdi mdi-upload"></i> 开始上传
59 </t-button>
60 <t-button theme="default" variant="outline" @click="goBack">取消</t-button>
61 </div>
62 </div>
63 </div>
64 </div>
65 </div>
66</template>
67
68<script setup>
69import { reactive, ref } from 'vue'
70import { useRouter } from 'vue-router'
71import { MessagePlugin } from 'tdesign-vue-next'
72import http, { parseResult } from '@/shared/api/http'
73
74const router = useRouter()
75
76const boot = window.__TD_BOOT__ || {}
77const ajaxUrl = boot.ajaxBase || './ajax.php'
78
79const uploading = ref(false)
80const progress = ref(0)
81
82const form = reactive({
83 filecx: null,
84 kg: true,
85})
86
87function onPickFile(e) {
88 const f = e.target.files && e.target.files[0]
89 if (f) form.filecx = f
90}
91
92function goBack() {
93 router.push('/program')
94}
95
96function fmtSize(bytes) {
97 if (!bytes) return '0 B'
98 const units = ['B', 'KB', 'MB', 'GB']
99 const pow = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1)
100 return (bytes / Math.pow(1024, pow)).toFixed(2) + ' ' + units[pow]
101}
102
103async function submit() {
104 if (!form.filecx) return MessagePlugin.warning('请选择程序包文件')
105
106 const fd = new FormData()
107 fd.append('gn', 'cxfiledru')
108 fd.append('file', form.filecx)
109 fd.append('sxj', form.kg ? 'true' : 'false')
110
111 uploading.value = true
112 progress.value = 0
113 try {
114 const res = await http.post(ajaxUrl, fd, {
115 onUploadProgress: (e) => {
116 if (e.total) {
117 progress.value = Math.min(99, Math.round((e.loaded / e.total) * 100))
118 }
119 },
120 })
121 progress.value = 100
122 const r = parseResult(res.data)
123 if (r.ok) {
124 MessagePlugin.success('导入成功')
125 router.push('/program')
126 }
127 } finally {
128 uploading.value = false
129 }
130}
131</script>
132
133<style scoped>
134.req {
135 color: var(--td-error);
136 margin-left: 2px;
137}
138.file-row {
139 display: flex;
140 align-items: center;
141 gap: 12px;
142 flex-wrap: wrap;
143}
144.file-pick {
145 display: inline-flex;
146 align-items: center;
147 gap: 6px;
148 padding: 7px 14px;
149 background: var(--td-surface);
150 border: 1px dashed var(--td-border);
151 border-radius: var(--td-radius);
152 cursor: pointer;
153 font-size: 13px;
154 color: var(--td-text-secondary);
155 transition: border-color 0.15s ease, color 0.15s ease;
156}
157.file-pick:hover {
158 border-color: var(--td-brand);
159 color: var(--td-brand);
160}
161.file-pick input[type='file'] {
162 position: absolute;
163 width: 0;
164 height: 0;
165 opacity: 0;
166 overflow: hidden;
167}
168</style>