detail.vue 15.1 KB
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
<template>
  <view class="main">
    <view class="toptitle">
      <view class="toptitle_left" @click="ht">
        <u-icon name="arrow-left" color="#fff" size="20"></u-icon>
      </view>
      <view class="toptitle_title">设备详情</view>
    </view>

    <view class="box">
      <view class="top">
        <text class="name">{{ detail.sbmc || '-' }}</text>
      </view>
      <view class="row">
        <text class="label">生产厂家</text>
        <text class="value">{{ detail.sccj || '-' }}</text>
      </view>
      <view class="row">
        <text class="label">购入时间</text>
        <text class="value">{{ formatDate(detail.gmsj) }}</text>
      </view>
      <view class="row">
        <text class="label">设备状态</text>
        <view class="value"><text :class="['status-pill', statusClass(detail.sbzt)]">{{ detail.sbzt || '未知状态' }}</text></view>
      </view>
      <view class="divider" />
      <view class="row">
        <text class="label">维护历史</text>
        <text class="value">{{ formatDate(detail.whls) }}</text>
      </view>
      <view class="row">
        <text class="label">维护周期</text>
        <text class="value">{{ detail.whzq || '-' }}</text>
      </view>
      <view class="row">
        <text class="label">维护计划</text>
        <text class="value">{{ formatDate(detail.whjh) || '-' }}</text>
      </view>
      <view class="row">
        <text class="label">备件信息</text>
        <view class="value multiline">{{ stringify(detail.bjxx1) }}</view>
      </view>
      <view class="row" style="display: flex;flex-direction: column;align-items: flex-start;">
        <text class="label">历史故障</text>
        <view class="value" style="margin-top: 5px;" v-html="detail.lsgz"></view>
      </view>
    </view>

    <view class="box">
      <view class="top">
        <text class="name">设备图片</text>
      </view>
      <view class="row" style="flex-direction: column; align-items: flex-start;">
        <view style="width: 100%;">
          <view v-if="images.length" style="display: flex; flex-wrap: wrap; gap: 10px;">
            <image v-for="(img, idx) in images" :key="idx" :src="fullUrl(img.url || img)" mode="widthFix" style="width: 30%; border-radius: 6px;" @click="openImagePreview(idx)" />
          </view>
          <text v-else class="value">-</text>
        </view>
      </view>
    </view>

    <view class="box">
      <view class="top">
        <text class="name">设备资料</text>
      </view>
      <view v-if="files.length" class="row" style="flex-direction: column; align-items: stretch;">
        <view v-for="(file, idx) in files" :key="idx" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
          <view style="display: flex; align-items: center;">
            <image v-if="!isVideoType((file.type || '').toLowerCase())" :src="fileIcon((file.type || '').toLowerCase())" mode="" style="width: 28px; height: 28px; margin-right: 10px;"></image>
            <u-icon v-else name="play-circle-fill" color="#0052D8" size="28" style="margin-right: 10px;"></u-icon>
            <text class="value">{{ file.name || file.title || basename(file.url || file) }}</text>
          </view>
          <view style="display: flex; align-items: center;">
               <u-button size="small" type="primary" text="查看" @click="previewFile(file)"></u-button>
          </view>
        </view>
      </view>
      <view v-else class="row">
        <text class="value">-</text>
      </view>
    </view>
  </view>

</template>

<script>
export default {
  data() {
    return {
      detail: {},
      images: []
      ,
      files: []
    }
  },
  onLoad(options) {
    // 从URL参数获取设备ID
    const deviceId = options.id || options.Id || options.ID
    if (deviceId) {
      this.fetchDeviceDetail(deviceId)
    } else {
      // 兼容旧方式:从缓存读取
      try {
        const raw = uni.getStorageSync('myDeviceDetail')
        if (raw) {
          const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw
          const cachedId = parsed.id || parsed.Id || parsed.ID
          if (cachedId) {
            this.fetchDeviceDetail(cachedId)
          } else {
            // 如果没有ID,使用缓存数据(兼容旧版本)
            this.detail = parsed || {}
            this.images = this.parseImageList(this.detail.sbtp || this.detail.tp)
            this.files = this.parseFileList(this.detail.sbzl || this.detail.zl)
          }
        } else {
          uni.showToast({
            icon: 'error',
            title: '缺少设备ID'
          })
        }
      } catch (e) {
        console.error('读取设备详情失败', e)
        uni.showToast({
          icon: 'error',
          title: '加载失败'
        })
      }
    }
  },
  methods: {
    // 获取设备详情
    fetchDeviceDetail(deviceId) {
      uni.showLoading({
        title: '加载中...'
      })
      this.API.hqwdsbxq({ id: deviceId }).then(res => {
        uni.hideLoading()
        console.log('设备详情数据', res)
        if (res && res.code === 200 && res.data) {
          this.detail = res.data || {}
          console.log('设备详情数据详情', this.detail)
          
          // 设备图片:KhsbInfoOutput 中 sbtp 是 List<FileControlsModel>
          // FileControlsModel: { name, fileId, url }
          if (this.detail.sbtp) {
            if (Array.isArray(this.detail.sbtp)) {
              // 如果已经是数组格式(API返回的对象数组)
              this.images = this.detail.sbtp.map(item => {
                if (typeof item === 'string') {
                  return { url: item }
                }
                // FileControlsModel 格式:{ name, fileId, url }
                const url = item.url || ''
                return {
                  url: url,
                  name: item.name || this.basename(url)
                }
              }).filter(img => img.url)
            } else if (typeof this.detail.sbtp === 'string') {
              // 如果是JSON字符串(兼容旧数据)
              const str = this.detail.sbtp.trim()
              if (str) {
                try {
                  const parsed = JSON.parse(str)
                  if (Array.isArray(parsed)) {
                    this.images = parsed.map(item => {
                      if (typeof item === 'string') {
                        return { url: item }
                      }
                      return {
                        url: item.url || '',
                        name: item.name || this.basename(item.url || '')
                      }
                    }).filter(img => img.url)
                  } else {
                    this.images = [{ url: str }]
                  }
                } catch (e) {
                  // 不是JSON,可能是单个URL字符串
                  this.images = [{ url: str }]
                }
              } else {
                this.images = []
              }
            } else {
              this.images = []
            }
          } else {
            this.images = []
          }
          
          // 设备资料:KhsbInfoOutput 中 sbzl 是 List<FileControlsModel>
          if (this.detail.sbzl) {
            if (Array.isArray(this.detail.sbzl)) {
              // 如果已经是数组格式(API返回的对象数组)
              this.files = this.detail.sbzl.map(item => {
                if (typeof item === 'string') {
                  const url = item
                  return {
                    name: this.basename(url),
                    url: url,
                    type: this.extname(url)
                  }
                }
                // FileControlsModel 格式:{ name, fileId, url }
                const url = item.url || ''
                return {
                  name: item.name || this.basename(url),
                  url: url,
                  type: this.extname(url)
                }
              }).filter(file => file.url)
            } else if (typeof this.detail.sbzl === 'string') {
              // 如果是JSON字符串(兼容旧数据)
              this.files = this.parseFileList(this.detail.sbzl)
            } else {
              this.files = []
            }
          } else {
            this.files = []
          }
          
          console.log('解析后的图片', this.images)
          console.log('解析后的资料', this.files)
        } else {
          uni.showToast({
            icon: 'error',
            title: res?.msg || '获取设备详情失败'
          })
        }
      }).catch(err => {
        uni.hideLoading()
        console.error('获取设备详情失败', err)
        uni.showToast({
          icon: 'error',
          title: '加载失败,请重试'
        })
      })
    },
    ht(){
      uni.navigateBack({ delta: 1 })
    },
    // 解析设备资料列表,统一结构为 {name, url, type}
    parseFileList(v) {
      const arr = []
      if (v === null || v === undefined) return arr
      let list = null
      if (typeof v === 'string') {
        const s = v.trim()
        if (!s) return arr
        try { list = JSON.parse(s) } catch (e) { list = [s] }
      } else if (Array.isArray(v)) {
        list = v
      } else {
        return arr
      }
      for (const it of list) {
        if (typeof it === 'string') {
          const url = it
          arr.push({ name: this.basename(url), url, type: this.extname(url) })
          continue
        }
        const url = it.url || ''
        const name = it.name || it.title || this.basename(url)
        let type = (it.type || '').toLowerCase()
        if (!type) type = this.extname(url)
        arr.push({ name, url, type })
      }
      return arr
    },
    extname(url) {
      if (!url) return ''
      const i = url.lastIndexOf('.')
      if (i === -1) return ''
      return url.substring(i + 1).toLowerCase()
    },
    basename(url) {
      if (!url) return ''
      try {
        const u = url.split('?')[0]
        return u.substring(u.lastIndexOf('/') + 1)
      } catch { return url }
    },
    fileIcon(type) {
      if (!type) return '/static/WORD.png'
      if (type === 'pdf') return '/static/PDF.png'
      if (type === 'doc' || type === 'docx') return '/static/WORD.png'
      if (type === 'xlsx' || type === 'xls') return '/static/WORD.png'
      if (type === 'png' || type === 'jpg' || type === 'jpeg') return '/static/img/picture.png'
      return '/static/WORD.png'
    },
    formatDate(ts) {
      if (!ts) return '-'
      const d = new Date(ts)
      if (isNaN(d.getTime())) return String(ts)
      const y = d.getFullYear()
      const m = String(d.getMonth() + 1).padStart(2, '0')
      const day = String(d.getDate()).padStart(2, '0')
      return `${y}-${m}-${day}`
    },
    statusClass(status) {
      if (status === null || status === undefined) return 'status-unknown'
      const s = String(status).trim().toLowerCase()
      const num = Number(s)
      if (!isNaN(num)) {
        if (num === 1) return 'status-running'
        if (num === 2) return 'status-repair'
        if (num === 3) return 'status-stop'
        if (num === 4) return 'status-scrap'
      }
      if (s.includes('运')) return 'status-running'
      if (s.includes('检') || s.includes('修')) return 'status-repair'
      if (s.includes('停') || s.includes('关')) return 'status-stop'
      if (s.includes('废')) return 'status-scrap'
      if (s.includes('running') || s.includes('run') || s.includes('online')) return 'status-running'
      if (s.includes('repair') || s.includes('maint') || s.includes('maintenance') || s.includes('fix')) return 'status-repair'
      if (s.includes('stop') || s.includes('down') || s.includes('offline')) return 'status-stop'
      if (s.includes('scrap') || s.includes('decomm')) return 'status-scrap'
      return 'status-unknown'
    },
    stringify(v) {
      if (!v) return '-'
      if (Array.isArray(v)) return v.join('、')
      if (typeof v === 'object') return Object.values(v).join('、') || '-'
      return String(v)
    },
    isString(v) { return typeof v === 'string' },
    fullUrl(url) {
      if (!url) return ''
      // 兼容后端返回相对路径
      if (/^https?:\/\//.test(url)) return url
      const base = 'https://hhjj.antissoft.com'
      return base + url
    },
    openImagePreview(idx){
      const urls = (this.images || []).map(img => this.fullUrl(img.url || img)).filter(u => !!u)
      if (!urls.length) return
      uni.previewImage({ current: idx, urls })
    },
    // 解析图片字段:支持 null、空串、JSON 字符串、数组
    parseImageList(v) {
      if (v === null || v === undefined) return []
      if (typeof v === 'string') {
        const s = v.trim()
        if (!s) return []
        try {
          const parsed = JSON.parse(s)
          if (Array.isArray(parsed)) {
            return parsed.map(item => {
              if (typeof item === 'string') {
                return { url: item }
              }
              return {
                url: item.url || '',
                name: item.name || this.basename(item.url || '')
              }
            }).filter(img => img.url)
          }
          return []
        } catch (e) {
          // 不是 JSON,可能是单个 url 字符串
          return [{ url: s }]
        }
      }
      if (Array.isArray(v)) {
        return v.map(item => {
          if (typeof item === 'string') {
            return { url: item }
          }
          return {
            url: item.url || '',
            name: item.name || this.basename(item.url || '')
          }
        }).filter(img => img.url)
      }
      // 非字符串非数组,无法解析
      return []
    },
    // 判断是否为视频类型
    isVideoType(type) {
      const videoTypes = ['mp4', 'mov', 'avi', 'wmv', 'mkv', 'webm', 'mpeg', 'mpg', 'flv', '3gp'];
      return videoTypes.includes((type || '').toLowerCase());
    },
    // 判断是否为图片类型
    isImageType(type) {
      const imageTypes = ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp'];
      return imageTypes.includes((type || '').toLowerCase());
    },
    // 判断是否为PDF类型
    isPdfType(type) {
      return (type || '').toLowerCase() === 'pdf';
    },
    // 统一文件预览方法(在线预览,不支持下载)
    previewFile(file) {
      const url = this.fullUrl(file.url || file)
      const type = (file.type || '').toLowerCase() || url.substring(url.lastIndexOf('.') + 1).toLowerCase()
      
      // 图片预览
      if (this.isImageType(type)) {
        uni.previewImage({ current: 0, urls: [url] })
        return
      }
      
      // PDF预览
      if (this.isPdfType(type)) {
        uni.setStorageSync('wjurl', url)
        uni.navigateTo({ url: '/pages/pdf/pdf' })
        return
      }
      
      // 视频预览
      if (this.isVideoType(type)) {
        uni.setStorageSync('videoUrl', url)
        uni.setStorageSync('videoName', file.name || '视频')
        uni.navigateTo({
          url: '/pages/new/zlgl/video-preview'
        })
        return
      }
      
      // 其他文件类型(Word、Excel等)提示不支持在线预览
      uni.showToast({
        title: '该文件类型暂不支持在线预览',
        icon: 'none',
        duration: 2000
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import './detail.scss';
</style>