Blame view

pages-sub/daily/maintain-manage/add-record.vue 9.89 KB
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
1
  <template>
df24e712   刘淇   养护计划 添加 待优化
2
    <view class="page-container">
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
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
      <view class="inspect-form-content commonPageLRpadding">
        <up-form
            label-position="left"
            :model="inspectForm"
            ref="inspectFormRef"
            labelWidth="140rpx"
        >
          <!-- 1. 巡查描述(文本域) -->
          <up-form-item
              prop="content"
              class="form-item"
          >
            <up-textarea
                v-model="inspectForm.remark"
                placeholder="请输入巡查描述"
                maxlength="200"
                count
            ></up-textarea>
          </up-form-item>
  
          <!-- 2. 上传图片 -->
          <up-form-item
              label="上传图片"
              prop="images"
              required
              border-bottom
              class="form-item"
          >
            <up-upload
                :file-list="imagesList"
                @after-read="(event) => uploadImgs(event)"
                @delete="(event) => deleteImg(event)"
                @on-exceed="handleExceed"
                multiple
                :max-count="3"
                upload-text="选择图片"
                del-color="#ff4d4f"
                class="upload-wrap"
            ></up-upload>
          </up-form-item>
  
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
44
          <!-- 3. 完成进度(滑块) -->
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
45
46
47
          <up-form-item
              label="完成进度"
              prop="progress"
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
48
  
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
49
50
51
52
53
              class="form-item"
          >
            <view class="progress-wrap">
              <up-slider
                  v-model="inspectForm.progress"
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
54
55
56
57
58
59
60
61
                  :min="initProgress"
                  :max="100"
                  active-color="#1989fa"
                  inactive-color="#e5e5e5"
                  block-size="24"
                  :showValue="true"
                  class="progress-slider"
                  @changing="handleProgressChange"
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
62
              ></up-slider>
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
63
64
            </view>
          </up-form-item>
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        </up-form>
      </view>
  
      <!-- 底部提交按钮 -->
      <view class="fixed-bottom-btn-wrap">
        <up-button
            type="primary"
            text="提交"
            @click="submitInspect"
            :style="{ width: '100%', height: '88rpx', fontSize: '32rpx', borderRadius: 0 }"
        ></up-button>
      </view>
    </view>
  </template>
  
  <script setup lang="ts">
  import { ref } from 'vue'
  import type { UniFormRef } from '@/uni_modules/uview-plus/types'
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
83
84
85
86
  const inspectFormRef = ref<UniFormRef>(null)
  </script>
  
  <script lang="ts">
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
87
88
  import { uploadImages } from '@/common/utils/upload'
  import { maintainCreate } from "@/api/maintain-manage/maintain-manage"
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
89
90
91
92
  
  export default {
    data() {
      return {
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
93
        imagesList: [],
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
94
        initProgress: 0,
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
95
        inspectForm: {
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
96
97
          remark: '',
          progress: 0
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
98
        },
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
99
        paramsOptins: {},
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
100
101
102
103
104
105
106
        inspectFormRules: {
          images: [
            {
              required: true,
              message: '请上传图片',
              trigger: 'change',
              validator: (rule, value, callback) => {
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
107
108
                const hasSuccessImg = this.imagesList.some(item => item.status === 'success')
                const imgCount = this.imagesList.filter(item => item.status === 'success').length
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
109
110
111
112
113
114
115
116
117
118
                if (!hasSuccessImg || imgCount < 1) {
                  callback(new Error('最少需要上传1张图片'))
                } else if (imgCount > 3) {
                  callback(new Error('最多只能上传3张图片'))
                } else {
                  callback()
                }
              }
            }
          ],
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
          // progress: [
          //   {
          //
          //     required: true,
          //     message: '请设置完成进度',
          //     trigger: ['change'],
          //     validator: (rule, value, callback) => {
          //       // 第一步:校验是否为空/0
          //       if (!value && value !== 0) {
          //         callback(new Error('请设置完成进度'))
          //       }
          //       // 第二步:校验是否大于初始进度
          //       else if (value <= this.initProgress) {
          //         callback(new Error(`完成进度必须大于${this.initProgress}%`))
          //       }
          //       // 校验通过
          //       else {
          //         callback()
          //       }
          //     }
          //   }
          // ]
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
141
142
143
144
145
146
        }
      }
    },
    onLoad(option) {
      console.log('页面参数:', option)
      this.paramsOptins = option
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
147
148
149
150
      // 初始化初始进度
      this.initProgress = option.finishPercent ? Number(option.finishPercent)+1 : 0
      // 关键修复:初始进度值设为 初始进度+1,避免刚进入就触发校验失败
      this.inspectForm.progress = this.initProgress
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
151
152
153
      console.log('初始进度值:', this.initProgress)
    },
    onReady() {
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
154
155
156
157
158
      this.$refs.inspectFormRef.setRules(this.inspectFormRules)
      console.log('巡查表单规则初始化完成')
    },
    methods: {
      /**
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
159
       * 进度滑块变化处理 - 核心优化:实时触发校验 + 状态更新
df24e712   刘淇   养护计划 添加 待优化
160
       */
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
      handleProgressChange(value) {
        // // 1. 强制修正非法值(兜底)
        // console.log(value)
        // if (value <= this.initProgress) {
        //   console.log('123')
        //   this.inspectForm.progress = this.initProgress + 1
        //   uni.showToast({
        //     title: `进度不能低于${this.initProgress}%`,
        //     icon: 'none',
        //     duration: 1500
        //   })
        // }
        //
        // // 2. 关键:手动触发progress字段的校验,实时更新提示状态
        // this.$nextTick(async () => {
        //   try {
        //     // 触发单个字段校验
        //     await this.$refs.inspectFormRef.validateField('progress')
        //   } catch (err) {
        //     // 校验失败时uView会自动显示提示,此处无需额外处理
        //     console.log('进度校验失败:', err)
        //   }
        // })
df24e712   刘淇   养护计划 添加 待优化
184
185
186
      },
  
      /**
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
187
188
189
190
191
       * 删除图片
       */
      deleteImg(event) {
        console.log('删除图片事件:', event)
        this.imagesList.splice(event.index, 1)
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
192
193
194
195
196
197
198
199
200
201
202
        this.$refs.inspectFormRef.validateField('images')
        uni.showToast({ title: '图片删除成功', icon: 'success' })
      },
  
      /**
       * 上传图片
       */
      async uploadImgs(event) {
        console.log('上传图片事件:', event)
        const fileList = Array.isArray(event.file) ? event.file : [event.file]
        const targetImgList = this.imagesList
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
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
        const filePaths = fileList.map(item => item.url)
        const tempItems = fileList.map(item => ({
          ...item,
          status: 'uploading',
          message: '上传中'
        }))
        const startIndex = targetImgList.length
        targetImgList.push(...tempItems)
  
        try {
          const uploadResultUrls = await uploadImages({
            filePaths: filePaths,
            ignoreError: true
          })
          console.log('上传成功的URL列表:', uploadResultUrls)
  
          uploadResultUrls.forEach((url, index) => {
            if (targetImgList[startIndex + index]) {
              targetImgList.splice(startIndex + index, 1, {
                ...fileList[index],
                status: 'success',
                message: '',
                url: url
              })
            }
          })
  
          if (uploadResultUrls.length < fileList.length) {
            const failCount = fileList.length - uploadResultUrls.length
            for (let i = uploadResultUrls.length; i < fileList.length; i++) {
              if (targetImgList[startIndex + i]) {
                targetImgList.splice(startIndex + i, 1, {
                  ...fileList[i],
                  status: 'failed',
                  message: '上传失败'
                })
              }
            }
            uni.showToast({ title: `成功上传${uploadResultUrls.length}张,失败${failCount}张`, icon: 'none' })
          } else {
            uni.showToast({ title: `成功上传${fileList.length}张图片`, icon: 'success' })
          }
  
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
246
247
248
249
250
251
252
253
254
255
256
257
258
          this.$refs.inspectFormRef.validateField('images')
        } catch (err) {
          console.error('图片上传失败:', err)
          for (let i = 0; i < fileList.length; i++) {
            if (targetImgList[startIndex + i]) {
              targetImgList.splice(startIndex + i, 1, {
                ...fileList[i],
                status: 'failed',
                message: '上传失败'
              })
            }
          }
          uni.showToast({ title: '图片上传失败,请重试', icon: 'none' })
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
          this.$refs.inspectFormRef.validateField('images')
        }
      },
  
      /**
       * 处理图片超出数量限制
       */
      handleExceed() {
        uni.showToast({ title: '最多只能上传3张图片', icon: 'none' })
      },
  
      /**
       * 提取图片URL数组
       */
      getImgUrlList(imgList) {
        return imgList.filter(item => item.status === 'success').map(item => item.url)
      },
  
      /**
       * 提交巡查表单
       */
      async submitInspect() {
        console.log('当前完成进度:', this.inspectForm.progress)
        try {
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
283
284
285
          await this.$refs.inspectFormRef.validate()
          console.log('图片列表:', this.imagesList)
  
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
286
          const submitData = {
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
287
            totalFinishPercent: this.inspectForm.progress,
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
288
289
290
            "planNo": this.paramsOptins.planNo,
            "imgHost": "1",
            "beginImg": this.getImgUrlList(this.imagesList),
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
291
292
            "commonUserList": [],
            "remark": this.inspectForm.remark
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
293
294
          }
  
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
295
          uni.showLoading({ title: '提交中...' })
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
296
          await maintainCreate(submitData)
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
297
          uni.hideLoading()
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
298
  
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
299
300
301
302
303
          uni.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1000
          })
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
304
  
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
305
306
          setTimeout(() => {
            uni.redirectTo({
df24e712   刘淇   养护计划 添加 待优化
307
              url: '/pages-sub/daily/maintain-manage/index'
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
308
309
310
311
            })
          }, 1000)
  
        } catch (error) {
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
312
          uni.hideLoading()
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
313
          if (!Array.isArray(error)) {
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
314
315
316
317
318
319
320
321
322
323
324
325
326
327
            console.error('巡查表单提交失败:', error)
            uni.showToast({
              title: '提交失败,请重试',
              icon: 'none',
              duration: 2000
            })
          }
        }
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
328
329
330
331
332
333
  .inspect-form-content {
    background: #fff;
    padding: 20rpx;
    margin-bottom: 20rpx;
  }
  
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
334
  .progress-wrap {
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
335
    width: 83%;
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
336
  }
fb13622a   刘淇   养护计划列表 样式 差去一个卡片的头部
337
  </style>