Newer
Older
smartwell_front / src / views / wellManage / editWell.vue
lyg on 25 Jun 19 KB 测试问题修改
<template>
  <el-dialog :visible.sync="dialogFormVisible" class="editDialog" title="编辑井" append-to-body @close="cancel">
    <el-scrollbar :native="false">
      <el-form ref="dataForm" :label-position="labelPosition" :rules="rules" :model="wellForm" label-width="120px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="点位名称" prop="wellName">
              <el-input v-model.trim="wellForm.wellName" type="text" placeholder="必填" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="1">
            <el-form-item label="点位编号" prop="wellCode">
              <el-input v-model.trim="wellForm.wellCode" :disabled="isEditMode" type="text" placeholder="必填" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="点位类型" prop="wellType">
              <el-select v-model="wellForm.wellType" placeholder="点击选择点位类型" clearable>
                <el-option
                  v-for="item in wellTypeList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="1">
            <el-form-item label="权属单位" prop="deptid">
              <dept-select v-model="wellForm.deptid" size="normal" :need-top="deptShowTop" :dept-show="deptShow" placeholder="选择权属单位" @change="deptChanged" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="井深(m)" prop="deep">
              <el-input v-model.trim="wellForm.deep" type="text" placeholder="需要装液位监测仪的井必填" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="经度" prop="coordinateX">
              <el-input v-model.trim="wellForm.coordinateX" type="text" placeholder="必填" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="1">
            <el-form-item label="纬度" prop="coordinateY">
              <el-input v-model.trim="wellForm.coordinateY" type="text" placeholder="必填" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="路标图片" prop="images">
              <el-upload
                ref="upload"
                :before-upload="handleBeforeUpload"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-exceed="handleExceed"
                :on-change="handleChange"
                :http-request="uploadFile"
                :file-list="imageList"
                :limit="4"
                :class="{hide:hideUpload}"
                multiple
                action="string"
                accept=".jpg,.jpeg,.png "
                list-type="picture-card"
              >
                <i class="el-icon-plus" />
              </el-upload>
              <el-dialog :visible.sync="dialogVisible" title="图片预览" append-to-body>
                <div class="imgBox">
                  <img :src="dialogImageUrl" width="100%" max-height="400px;" alt="" style="margin: 0 auto;">
                </div>
              </el-dialog>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="1">
          <el-form-item label="所在区域">
            <el-col :span="5">
              <el-form-item prop="qu">
                <el-select v-model="wellForm.qu" :disabled="showResponseDept" placeholder="请选择所在区/县" clearable @change="quChanged">
                  <el-option
                    v-for="item in quList"
                    :key="item.id"
                    :label="item.areaName"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="1" class="line">
              区/县
            </el-col>
            <el-col :span="10">
              <el-form-item prop="area">
                <el-select v-model="wellForm.area" :disabled="showResponseDept" placeholder="请选择所在街道/镇" clearable>
                  <el-option
                    v-for="item in jiedaoList"
                    :key="item.id"
                    :label="item.areaName"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="1" class="line">
              街道/镇
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="21">
            <el-form-item label="所在道路" prop="road">
              <el-input v-model.trim="wellForm.road" type="text" placeholder="非必填,建议填写,不填影响统计结果" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="21">
            <el-form-item label="详细地址" prop="position">
              <el-input v-model.trim="wellForm.position" type="text" placeholder="必填">
                <el-button slot="append" size="small" icon="el-icon-refresh" @click="generateAddress">
                  坐标解析
                </el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-show="secondDeptTreeList.length>=1" :gutter="20">
          <el-col :span="13">
            <el-form-item label="井维护机构" prop="responsibleDept">
              <el-select v-if="secondDeptTreeList.length>=1" v-model="wellForm.responsibleDept" :disabled="showResponseDept" placeholder="选择维护组织,不选默认为权属单位" clearable>
                <el-option
                  v-for="item in secondDeptTreeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
              <el-input v-else v-model="wellForm.responsibleDeptName" type="text" placeholder="未知" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row :gutter="20">
        <el-col :span="15" style="margin-left: 120px;">
          <el-button size="medium" @click="cancel">
            取消
          </el-button>
          <el-button type="primary" size="medium" style="margin-left: 10px;" @click="saveData">
            保存
          </el-button>
        </el-col>
      </el-row>
    </el-scrollbar>
  </el-dialog>
</template>

<script>
import { toTreeList } from '@/utils/structure'
import { getDeptTreeList } from '@/api/system/dept'
import { getAreaList, getAreaByDept } from '@/api/system/area'
import { getWellType, updateWell } from '@/api/well/well'
import { Uploadimg } from '@/api/common'
import DeptSelect from '@/components/DeptSelect'
import { mapGetters } from 'vuex'
import AMapContainer from '@/components/Amap/AMapContainer'
import AMapLoader from '@amap/amap-jsapi-loader'
import { convertLnglat, reverseLnglat, toSize } from '@/components/Amap/utils/convert-helper'

export default {
  name: 'EditWell',
  components: { DeptSelect },
  data() {
    const validateDeep = (rule, value, callback) => {
      if (value !== '') {
        if ((/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value) === false) {
          callback(new Error('请填写大于0的数字'))
        } else {
          const deep = parseFloat(value)
          if (deep > 10 || deep < 0) {
            callback(new Error('请填写0到10数值'))
          } else {
            callback()
          }
        }
      } else {
        callback()
      }
    }
    const validateLng = (rule, value, callback) => {
      if (value !== '') {
        if ((/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value) === false) {
          callback(new Error('请填写大于0的数字'))
        } else {
          callback()
        }
      } else {
        callback(new Error('经度不能为空'))
      }
    }
    const validateLat = (rule, value, callback) => {
      if (value !== '') {
        if ((/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value) === false) {
          callback(new Error('请填写大于0的数字'))
        } else {
          callback()
        }
      } else {
        callback(new Error('纬度不能为空'))
      }
    }
    return {
      map: null, // 地图对象
      baseLayer: 'gaode_vec', // 底图图层
      center: [this.$store.getters.lng, this.$store.getters.lat], // 地图中心
      zoom: 12,
      searchResultIcon: require('@/assets/overview/pure-position-icon.png'), // 报警图标
      searchResultSize: [24, 30],
      marker: null,
      locationMarker: '',
      postionlatlon: '',
      dialogFormVisible: false, // 对话框是否显示
      wellForm: {
        id: null, // 井id
        wellCode: '', // 点位编号
        wellName: '', // 点位名称
        wellType: '', // 点位类型
        deptid: '', // 权属单位
        deep: '', // 井深,
        position: '', // 位置描述
        coordinateX: '', // 经度
        coordinateY: '', // 纬度
        photos: '', // 照片路径
        notes: '', // 备注,
        qu: '',
        area: '', // 街道
        road: '', // 道路
        responsibleDept: '', // 维护人员部门
        responsibleDeptName: '' // 维护人员部门
      }, // 表单
      deptProps: {
        parent: 'pid',
        value: 'id',
        label: 'name',
        children: 'children'
      },
      hideUpload: false,
      imageList: [],
      secondDeptTreeList: [], // 二级组织树列表
      wellTypeList: [],
      quList: [],
      jiedaoList: [],
      textMap: {
        update: '编辑',
        create: '新增'
      }, // 表头显示标题
      rules: {
        wellCode: [{ required: true, message: '点位编号不能为空', trigger: ['blur', 'change'] }],
        wellName: [{ required: true, message: '点位名称不能为空', trigger: ['blur', 'change'] }],
        position: [{ required: true, message: '井详细地址不能为空', trigger: ['blur', 'change'] }],
        deep: [{ required: false, trigger: ['blur', 'change'], validator: validateDeep }],
        coordinateX: [{ required: true, trigger: ['blur', 'change'], validator: validateLat }],
        coordinateY: [{ required: true, trigger: ['blur', 'change'], validator: validateLng }],
        deptid: [{ required: true, message: '权属单位必选', trigger: 'change' }],
        wellType: [{ required: true, message: '点位类型必选', trigger: 'change' }]
        // qu: [{ required: true, message: '所在区/县必选', trigger: 'change' }],
        // area: [{ required: true, message: '所在街道/镇必选', trigger: 'change' }]
      },
      deptShowTop: false, // 权属单位下拉是否显示顶级
      dialogImageUrl: '',
      dialogVisible: false,
      labelPosition: 'right',
      responseAssignShow: false,
      isEditMode: true,
      deptShow: true
    }
  },
  computed: {
    ...mapGetters([
      'bodyHeight'
    ]),
    showResponseDept() {
      if (this.wellForm.deptid === '') {
        return true
      } else {
        return false
      }
    },
    qu() { // 获取区
      return this.wellForm.qu
    },
    area() { // 获取街道
      return this.wellForm.area
    },
    lonLat() {
      return this.wellForm.coordinateX + ',' + this.wellForm.coordinateY
    }
  },
  watch: {
    'wellForm.deptid'(val, oldVal) { // 监控权属单位变化
      if (oldVal !== '' && val !== '') {
        this.wellForm.responsibleDept = ''
        this.wellForm.responsibleDeptName = ''
        this.wellForm.area = ''
        this.wellForm.qu = ''
        this.fetchSecondDeptTree()
        this.fetchArea1()
      } else {
        if (val !== '') {
          this.fetchSecondDeptTree()
          this.fetchArea1()
        }
      }
    },
    qu(val) { // 监控区变化
      if (val !== '') {
        this.fetchArea2()
      }
    }
  },
  mounted: function() {
    this.fetchWellType()
    this.imageList = []
  },
  methods: {
    // 逆地址解析
    generateAddress() {
      // 如果deptid为空提示权属单位先选择
      if (!this.wellForm.deptid) {
        this.$message.warning('请先选择权属单位')
        return
      }
      // 1. 转换为高德坐标系
      if (this.wellForm.coordinateX && this.wellForm.coordinateY) {
        convertLnglat([this.wellForm.coordinateX, this.wellForm.coordinateY]).then(lnglat => {
          // 2.逆地址解析
          reverseLnglat(lnglat).then(res => {
            this.wellForm.position = res.formattedAddress
            this.wellForm.qu = res.addressComponent.adcode
            this.wellForm.road = res.addressComponent.street // 道路
          })
        })
      }
    },
    // 初始化对话框
    initDialog: function(dialogFormVisible, row = null) {
      this.imageList = []
      // if (row.photos.length === 0 && this.$refs.upload) this.$refs.upload.clearFiles()
      this.dialogFormVisible = dialogFormVisible
      this.fetchWellType()
      this.wellForm = {
        id: row.id, // 井id
        wellCode: row.wellCode, // 点位编号
        wellName: row.wellName, // 点位名称
        wellType: row.wellType, // 点位类型
        deptid: row.deptid, // 权属单位
        deep: row.deep, // 井深,
        position: row.position, // 位置描述
        coordinateX: row.coordinateX, // 经度
        coordinateY: row.coordinateY, // 纬度
        photos: row.photos, // 照片路径
        notes: row.notes, // 备注,
        qu: row.qu,
        area: row.area, // 街道
        responsibleDept: row.responsibleDept, // 维护人员部门
        responsibleDeptName: row.responsibleDeptName // 维护人员部门
      }
      if (row.deptid && row.deptid !== '') {
        this.fetchArea1(row.deptid)
        this.fetchArea2()
        this.fetchSecondDeptTree()
        this.loading = false
      }
      // 处理图片
      const base_url = this.baseConfig.baseUrl + '/static/'
      const urls = row.photos.split(';')
      for (const url of urls) {
        if (url) {
          this.imageList.push({ name: url, url: base_url + url })
        }
      }
      this.handleChange(null, this.imageList)
    },
    // 加载组织机构树形下拉菜单
    fetchSecondDeptTree: function() {
      const listQuery = {
        pid: this.wellForm.deptid,
        deptType: '04'
      }
      getDeptTreeList(listQuery).then(response => {
        console.log(response.data)
        if (response.data.list) {
          this.secondDeptTreeList = toTreeList(response.data.list, this.wellForm.deptid)
        }
      })
    },
    // 获取点位类型
    fetchWellType() {
      getWellType().then(response => {
        this.wellTypeList = []
        const wellTypes = this.$store.getters.wellTypes
        for (const wellType of response.data) {
          if (wellTypes.indexOf(wellType.value) !== -1) {
            this.wellTypeList.push(wellType)
          }
        }
        if (this.wellTypeList.length <= 1) {
          this.showWellType = false
        }
      })
    },
    // 获取区域1
    fetchArea1(deptid) {
      getAreaByDept(this.wellForm.deptid).then(response => {
        if (response.code === 200) {
          const pid = response.data.area
          getAreaList(pid).then(response => {
            this.quList = response.data
          })
        }
      })
    },
    // 改变权属单位选项时
    deptChanged() {
      console.log('deptChanged')
      this.wellForm.responsibleDept = ''
      this.wellForm.area = ''
      this.wellForm.qu = ''
    },
    // 改变区选项时的操作
    quChanged() {
      this.wellForm.area = ''
    },
    // 获取区域2
    fetchArea2() {
      getAreaList(this.wellForm.qu).then(response => {
        this.jiedaoList = response.data
      })
    },
    // 取消
    cancel() {
      this.dialogFormVisible = false
      this.imageList = []
      this.wellForm.deptid = ''
      this.$emit('watchChild')
      // 清除验证
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 保存数据
    saveData: function() {
      this.wellForm.photos = ''
      if (this.wellForm.responsibleDept === '') {
        if (this.secondDeptTreeList.length > 0) {
          this.$message.warning('必须选择维护机构')
          return
        } else {
          this.wellForm.responsibleDept = this.wellForm.deptid
        }
      }
      for (const image of this.imageList) {
        let index = image.url.indexOf('/static/')
        if (index !== -1) {
          index += 8
        }
        this.wellForm.photos += image.url.substring(index) + ';'
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateWell(this.wellForm).then(response => {
            console.log('修改井')
            if (response.code === 200) {
              this.$message.success('修改成功!')
              this.$emit('watchChild')
              this.dialogFormVisible = false
              this.imageList = []
            }
            // 清除验证
            this.$nextTick(() => {
              this.$refs['dataForm'].clearValidate()
            })
          })
        }
      })
    },
    uploadFile(file) {
      console.log('uploadFile:' + file.file.name)
      const base_url = this.baseConfig.baseUrl + '/static/'
      Uploadimg(file).then(res => {
        if (res.code === 200) {
          this.imageList.push({ name: file.file.name, url: base_url + res.data })
        }
      })
    },
    handleBeforeUpload(file) {
      const isJPG = (file.type === 'image/jpeg') || (file.type === 'image/png')
      let res = true
      const isLt2M = file.size / 1024 / 1024 < 5
      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!')
        res = false
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5MB!')
        res = false
      }
      for (const i in this.fileList) {
        if (this.fileList[i].name === file.name) {
          this.$message.error('该图片已上传过,请选择其他图片')
          res = false
        }
      }
      return res
    },
    // 文件列表移除文件时的钩子
    handleRemove(file, fileList) {
      console.log('handleRemove')
      for (const i in this.imageList) {
        if (this.imageList[i].name === file.name) {
          this.imageList.splice(i, 1)
          this.hideUpload = this.imageList.length >= 4
          return
        }
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(`限制选择 4 张图片,本次选择了 ${files.length} 张图片,共选择了 ${files.length + fileList.length} 张图片`)
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePictureCardPreview(file) {
      console.log('handlePictureCardPreview')
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleChange(file, fileList) {
      console.log('handleChange')
      if (fileList) {
        this.hideUpload = fileList.length >= 4
      } else {
        this.hideUpload = false
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.overview-map-container{
  width: 600px;
  margin: 0 auto 20px;
}
</style>
<style rel="stylesheet/scss" lang="scss">

  .hide .el-upload--picture-card {
    display: none;
  }

  .editDialog {
    .el-dialog {
      width: 900px;
      margin-top: 10vh !important;
      .el-scrollbar {
        height: 410px;
        width: 100%;
      }
      .el-scrollbar__wrap {
        /*overflow: scroll;*/
        overflow-x: auto;
        overflow-y: auto;
      }
      .el-scrollbar__view {
        width: 98%;
      }
    }
  }
 .el-dialog{
    width:700px
  }
  .el-select {
    width: 100%;
  }

  .line {
    width: 50px;
    margin-left: 5px;
  }

  .hide .el-upload-–picture-card {
    display: none;
  }

  .imgBox {
    width: 100%;
    text-align: center;
  }
</style>