Blame view

pages-sub/daily/patrol-manage/add-patrol-record.vue 9.61 KB
a2702f6d   刘淇   巡查计划
1
  <template>
37c26bd3   刘淇   巡查计划
2
    <view class="u-page">
420cb443   刘淇   快速工单列表
3
4
5
6
7
8
9
      <!-- 核心:将所有 up-form-item 包裹在同一个 up-form 内 -->
      <view class="inspect-form-content commonPageLRpadding">
        <up-form
            label-position="left"
            :model="inspectForm"
            ref="inspectFormRef"
            labelWidth="140rpx"
a2702f6d   刘淇   巡查计划
10
        >
420cb443   刘淇   快速工单列表
11
12
13
14
          <!-- 1. 巡查描述(文本域) -->
          <up-form-item
              prop="content"
              class="form-item"
fa775c6b   刘淇   养护计划调整
15
              required
420cb443   刘淇   快速工单列表
16
17
          >
            <up-textarea
fa775c6b   刘淇   养护计划调整
18
                v-model.trim="inspectForm.content"
420cb443   刘淇   快速工单列表
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
                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>
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
44
            <!--          <view class="tips">(最少1张,最多3张)</view>-->
420cb443   刘淇   快速工单列表
45
46
47
48
49
50
51
52
          </up-form-item>
  
          <!-- 3. 转为工单(单选框) -->
          <up-form-item
              label="转为工单"
              prop="isWorkOrder"
              required
              class="form-item"
37c26bd3   刘淇   巡查计划
53
          >
420cb443   刘淇   快速工单列表
54
55
56
57
58
            <up-radio-group
                v-model="inspectForm.isWorkOrder"
                active-color="#1989fa"
                direction="row"
                class="radio-group"
37c26bd3   刘淇   巡查计划
59
            >
420cb443   刘淇   快速工单列表
60
61
62
63
64
65
66
67
68
69
70
71
72
73
              <up-radio
                  :custom-style="{marginRight: '40rpx'}"
                  v-for="(item, index) in radioList"
                  :key="index"
                  :label="item.label"
                  :name="item.value"
              >
                {{ item.label }}
              </up-radio>
            </up-radio-group>
          </up-form-item>
  
        </up-form>
      </view>
a2702f6d   刘淇   巡查计划
74
75
  
      <!-- 底部提交按钮 -->
993d98fa   刘淇   工作台
76
      <view class="fixed-bottom-btn-wrap">
37c26bd3   刘淇   巡查计划
77
        <up-button
a2702f6d   刘淇   巡查计划
78
            type="primary"
420cb443   刘淇   快速工单列表
79
80
            text="提交"
            @click="submitInspect"
a2702f6d   刘淇   巡查计划
81
            :style="{ width: '100%', height: '88rpx', fontSize: '32rpx', borderRadius: 0 }"
420cb443   刘淇   快速工单列表
82
        ></up-button>
a2702f6d   刘淇   巡查计划
83
84
85
86
      </view>
    </view>
  </template>
  
420cb443   刘淇   快速工单列表
87
88
89
90
91
92
93
94
95
  <script setup lang="ts">
  import { ref } from 'vue'
  import type { UniFormRef } from '@/uni_modules/uview-plus/types'
  // 定义ref供选项式API使用
  const inspectFormRef = ref<UniFormRef>(null)
  </script>
  
  <script lang="ts">
  import { uploadImages } from '@/common/utils/upload';
5cb33b90   刘淇   完成巡查计划 详情
96
97
  import { inspectionCreate } from  "@/api/patrol-manage/patrol-plan";
  
420cb443   刘淇   快速工单列表
98
99
100
101
102
103
104
105
  
  export default {
    data() {
      return {
        // 图片列表
        imagesList: [],
        // 单选列表
        radioList: [
fa775c6b   刘淇   养护计划调整
106
107
          { label: '否', value: '1' },
          { label: '是', value: '2' }
420cb443   刘淇   快速工单列表
108
109
110
111
        ],
        // 巡查表单数据
        inspectForm: {
          content: '',        // 巡查描述
fa775c6b   刘淇   养护计划调整
112
          isWorkOrder: '1'    // 是否转为工单 1:否(默认) 2:是
a2702f6d   刘淇   巡查计划
113
        },
5cb33b90   刘淇   完成巡查计划 详情
114
        paramsOptins:{},//接受参数
420cb443   刘淇   快速工单列表
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        // 表单校验规则
        inspectFormRules: {
          images: [
            {
              required: true,
              message: '请上传图片',
              trigger: 'change',
              validator: (rule, value, callback) => {
                // 自定义校验规则:检查是否有成功上传的图片
                const hasSuccessImg = this.imagesList.some(item => item.status === 'success')
                const imgCount = this.imagesList.filter(item => item.status === 'success').length
  
                if (!hasSuccessImg || imgCount < 1) {
                  callback(new Error('最少需要上传1张图片'))
                } else if (imgCount > 3) {
                  callback(new Error('最多只能上传3张图片'))
                } else {
                  callback()
                }
              }
            }
          ],
fa775c6b   刘淇   养护计划调整
137
138
139
140
  
          content: [
            { type: 'string', required: true, message: '请输入工单描述', trigger: ['change'] }
          ],
420cb443   刘淇   快速工单列表
141
142
143
144
          isWorkOrder: [
            { type: 'string', required: true, message: '请选择是否转为工单', trigger: ['change'] }
          ]
        }
a2702f6d   刘淇   巡查计划
145
      }
420cb443   刘淇   快速工单列表
146
    },
5cb33b90   刘淇   完成巡查计划 详情
147
148
149
150
    onLoad(option){
      console.log(option)
      this.paramsOptins = option
    },
420cb443   刘淇   快速工单列表
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    onReady() {
      // 兼容微信小程序,通过setRules设置校验规则
      this.$refs.inspectFormRef.setRules(this.inspectFormRules)
      console.log('巡查表单规则初始化完成')
    },
    methods: {
      /**
       * 删除图片
       */
      deleteImg(event) {
        console.log('删除图片事件:', event)
        this.imagesList.splice(event.index, 1)
        // 删除图片后重新校验图片字段
        this.$refs.inspectFormRef.validateField('images')
        uni.showToast({ title: '图片删除成功', icon: 'success' })
      },
a2702f6d   刘淇   巡查计划
167
  
420cb443   刘淇   快速工单列表
168
169
170
171
172
173
174
      /**
       * 上传图片
       */
      async uploadImgs(event) {
        console.log('上传图片事件:', event)
        const fileList = Array.isArray(event.file) ? event.file : [event.file]
        const targetImgList = this.imagesList
a2702f6d   刘淇   巡查计划
175
  
420cb443   刘淇   快速工单列表
176
177
178
179
180
181
182
183
        const filePaths = fileList.map(item => item.url)
        const tempItems = fileList.map(item => ({
          ...item,
          status: 'uploading',
          message: '上传中'
        }))
        const startIndex = targetImgList.length
        targetImgList.push(...tempItems)
a2702f6d   刘淇   巡查计划
184
  
420cb443   刘淇   快速工单列表
185
186
187
188
189
190
        try {
          const uploadResultUrls = await uploadImages({
            filePaths: filePaths,
            ignoreError: true
          })
          console.log('上传成功的URL列表:', uploadResultUrls)
a2702f6d   刘淇   巡查计划
191
  
420cb443   刘淇   快速工单列表
192
193
194
195
196
197
198
199
200
201
          uploadResultUrls.forEach((url, index) => {
            if (targetImgList[startIndex + index]) {
              targetImgList.splice(startIndex + index, 1, {
                ...fileList[index],
                status: 'success',
                message: '',
                url: url
              })
            }
          })
a2702f6d   刘淇   巡查计划
202
  
420cb443   刘淇   快速工单列表
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
          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' })
          }
a2702f6d   刘淇   巡查计划
218
  
420cb443   刘淇   快速工单列表
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
255
256
257
258
259
260
          // 上传完成后重新校验图片字段
          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' })
  
          // 上传失败后重新校验图片字段
          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.isWorkOrder)
        try {
          // 先执行表单校验
          await this.$refs.inspectFormRef.validate()
5cb33b90   刘淇   完成巡查计划 详情
261
          console.log(this.imagesList)
420cb443   刘淇   快速工单列表
262
263
          // 构造提交数据
          const submitData = {
5cb33b90   刘淇   完成巡查计划 详情
264
265
266
267
268
269
270
271
272
273
274
275
            // content: this.inspectForm.content,
            // images: this.getImgUrlList(this.imagesList),
            // isWorkOrder: this.inspectForm.isWorkOrder // 1=否,2=是
  
  
            "batchNo": this.paramsOptins.batchNo,
            "planNo":this.paramsOptins.planNo,
            "imgHost": "1",
            "imgList": this.getImgUrlList(this.imagesList),
            "inspectionState": this.inspectForm.isWorkOrder,
            "transState": this.inspectForm.isWorkOrder==1?'1':'2',
            "transWorkNo": "default'",
fa775c6b   刘淇   养护计划调整
276
            "remark": this.inspectForm.content.trim()
420cb443   刘淇   快速工单列表
277
278
279
280
281
          }
  
          // 显示加载中
          uni.showLoading({ title: '提交中...' })
  
5cb33b90   刘淇   完成巡查计划 详情
282
          await inspectionCreate(submitData)
a2702f6d   刘淇   巡查计划
283
  
420cb443   刘淇   快速工单列表
284
285
286
287
          uni.hideLoading()
          uni.showToast({
            title: '提交成功',
            icon: 'success',
5cb33b90   刘淇   完成巡查计划 详情
288
            duration: 1000
420cb443   刘淇   快速工单列表
289
          })
a2702f6d   刘淇   巡查计划
290
  
5cb33b90   刘淇   完成巡查计划 详情
291
          // // 延迟跳转(等待提示框显示完成)
420cb443   刘淇   快速工单列表
292
          setTimeout(() => {
5cb33b90   刘淇   完成巡查计划 详情
293
            uni.redirectTo({
b0e2f1a9   刘淇   快速工单和巡查计划样式优化
294
              url: '/pages-sub/daily/patrol-manage/index'
5cb33b90   刘淇   完成巡查计划 详情
295
296
            })
          }, 1000)
37c26bd3   刘淇   巡查计划
297
  
420cb443   刘淇   快速工单列表
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
        } catch (error) {
          // 隐藏加载框
          uni.hideLoading()
  
          // 区分是表单校验失败还是接口调用失败
          if (Array.isArray(error)) {
            // 表单校验失败 - 静默处理(uView会自动提示)
          } else {
            // 接口调用失败
            console.error('巡查表单提交失败:', error)
            uni.showToast({
              title: '提交失败,请重试',
              icon: 'none',
              duration: 2000
            })
          }
        }
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  // 全局页面样式
  .u-page {
  }
  
  // 巡查表单内容容器
  .inspect-form-content {
    background: #fff;
    padding: 20rpx;
  }
37c26bd3   刘淇   巡查计划
330
  
a2702f6d   刘淇   巡查计划
331
  </style>