gaoluyang
2025-09-20 d85836bf6b1574122830f6db8770e98184edd51c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<template>
  <u-popup 
    v-model="dialogVisitable" 
    mode="center" 
    :round="10"
    :closeable="true"
    @close="cancel"
  >
    <view class="popup-content">
      <view class="popup-header">
        <text class="popup-title">巡检</text>
      </view>
      
      <view class="form-container">
        <u-form :model="form" ref="formRef" :rules="rules">
          <u-form-item label="设备名称" prop="deviceName" labelWidth="80">
            <u-input 
              v-model="form.deviceName" 
              placeholder="请输入设备名称" 
              :maxlength="30" 
              :disabled="true"
            />
          </u-form-item>
          
          <u-form-item label="地点" prop="location" labelWidth="80">
            <u-input 
              v-model="form.location" 
              placeholder="请输入地点" 
              :maxlength="30" 
              :disabled="true"
            />
          </u-form-item>
          
          <u-form-item label="附件" prop="storageBlobDTO" labelWidth="80">
            <u-upload
              :fileList="form.storageBlobDTO"
              @afterRead="afterRead"
              @delete="deleteFile"
              name="files"
              multiple
              :maxCount="10"
              :maxSize="1024 * 1024"
              accept="video/*"
              :previewFullImage="true"
            ></u-upload>
          </u-form-item>
          
          <u-form-item label="巡检人" prop="scannerName" labelWidth="80">
            <u-input 
              v-model="form.scannerName" 
              :disabled="true" 
              placeholder="请输入" 
            />
          </u-form-item>
          
          <u-form-item label="巡检时间" prop="scanTime" labelWidth="80">
            <u-input 
              v-model="form.scanTime" 
              :disabled="true" 
              placeholder="请输入" 
            />
          </u-form-item>
        </u-form>
      </view>
      
      <view class="popup-footer">
        <u-button @click="cancel" :customStyle="{ marginRight: '10px' }">取消</u-button>
        <u-button type="primary" @click="submitForm">保存</u-button>
      </view>
    </view>
  </u-popup>
</template>
 
<script setup>
import { reactive, ref, onMounted } from 'vue'
import { addOrEditQrCodeRecord } from '@/api/inspectionUpload/index.js'
import useUserStore from '@/store/modules/user.ts'
 
const emit = defineEmits(['closeDia'])
 
const dialogVisitable = ref(false)
const formRef = ref(null)
const userStore = useUserStore()
const userInfo = ref({})
 
// 获取当前时间
function getCurrentDateTime() {
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0')
  const day = String(now.getDate()).padStart(2, '0')
  const hours = String(now.getHours()).padStart(2, '0')
  const minutes = String(now.getMinutes()).padStart(2, '0')
  const seconds = String(now.getSeconds()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
 
// 表单数据
const form = reactive({
  deviceName: '',
  location: '',
  scannerName: '',
  scannerId: '',
  scanTime: '',
  storageBlobDTO: [],
  qrCode: {
    id: ''
  }
})
 
// 表单验证规则
const rules = reactive({
  deviceName: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
  location: [{ required: true, message: '请输入地点', trigger: 'blur' }]
})
 
// 获取用户信息
onMounted(async () => {
  try {
    const res = await userStore.getInfo()
    userInfo.value = res.user
    form.scannerName = userInfo.value.nickName
    form.scannerId = userInfo.value.userId
    form.scanTime = getCurrentDateTime()
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
})
 
// 文件上传处理
const afterRead = (event) => {
  const { file } = event
  
  // 上传文件到服务器
  uni.uploadFile({
    url: '/api/upload', // 替换为实际的上传接口
    filePath: file.url,
    name: 'file',
    success: (res) => {
      const data = JSON.parse(res.data)
      if (data.code === 200) {
        const fileItem = {
          url: data.data.url,
          name: file.name,
          status: 'success'
        }
        
        form.storageBlobDTO.push(fileItem)
        
        uni.showToast({
          title: '上传成功',
          icon: 'success'
        })
      } else {
        uni.showToast({
          title: '上传失败',
          icon: 'error'
        })
      }
    },
    fail: () => {
      uni.showToast({
        title: '上传失败',
        icon: 'error'
      })
    }
  })
}
 
// 删除文件
const deleteFile = (event) => {
  const { index } = event
  form.storageBlobDTO.splice(index, 1)
}
 
// 打开弹框
const openDialog = async (row) => {
  dialogVisitable.value = true
  form.deviceName = row.deviceName || ''
  form.location = row.location || ''
  form.qrCodeId = row.qrCodeId || row.id || ''
  form.storageBlobDTO = []
}
 
// 提交表单
const submitForm = async () => {
  try {
    // 表单验证
    const valid = await formRef.value.validate()
    if (!valid) return
    
    form.qrCode.id = form.qrCodeId
    
    await addOrEditQrCodeRecord({ ...form })
    
    uni.showToast({
      title: '提交成功',
      icon: 'success'
    })
    
    cancel()
  } catch (error) {
    console.error('提交失败:', error)
    uni.showToast({
      title: '提交失败',
      icon: 'error'
    })
  }
}
 
// 关闭弹框
const cancel = () => {
  dialogVisitable.value = false
  emit('closeDia')
}
 
defineExpose({ openDialog })
</script>
 
<style scoped lang="scss">
.popup-content {
  width: 90vw;
  max-width: 400px;
  background-color: #fff;
  border-radius: 10px;
  overflow: hidden;
}
 
.popup-header {
  padding: 20px 20px 10px;
  text-align: center;
  border-bottom: 1px solid #f0f0f0;
}
 
.popup-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}
 
.form-container {
  padding: 20px;
  max-height: 60vh;
  overflow-y: auto;
}
 
.popup-footer {
  display: flex;
  justify-content: center;
  padding: 15px 20px;
  border-top: 1px solid #f0f0f0;
  background-color: #fafafa;
}
</style>