add-or-update.vue 11.2 KB
Newer Older
葛齐林's avatar
葛齐林 committed
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
<template lang="pug">
    el-dialog.myDialog( width='60%'  :close-on-click-modal='false', :visible.sync='visible' append-to-body :modal-append-to-body='false')
        div(slot='title')
            span.title-bold {{!dataForm.id ? '新增' : '编辑'}}
        el-form(:model='dataForm', :rules='dataRule', ref='dataForm',  label-width='100px')
            el-row(:gutter='20')
                el-col(:span='12')
                    el-form-item(label="所属线路:" prop="lineName")
                        el-input(v-model='dataForm.lineName' disabled)
            el-row(:gutter='20')
                el-col(:span='12')
                    el-form-item(label="站点名称:" prop="stationName")
                        el-input(disabled v-model='dataForm.stationName' placeholder="请输入站点名称"  clearable )
                el-col(:span='12')
                    el-form-item(label="车站编码:" prop="code")
                        el-input(v-model='dataForm.code' placeholder="请输入车站编码"  clearable )
            el-row(:gutter='20')
                el-col(:span='12')
                    el-form-item(label="车站类型:" prop="type")
                        el-select(style='width:100%;' v-model='dataForm.type' placeholder="请选择车站类型" clearable)
                            el-option(v-for="(item,index) in typeList" :key="index" :label="item.name" :value="item.id")
                el-col(:span='12')
                    el-form-item(label="站点简称:" prop="shortName")
                        el-input(v-model='dataForm.shortName' placeholder="请输入站点简称"  clearable )
                el-col(:span='24')
                    el-form-item(label="站点描述:" prop="remark")
                        el-input(v-model='dataForm.remark' type="textarea" :rows=4 resize="none" placeholder="请输入站点描述")
        el-row(style="padding-bottom:15px;" :span="24")
            el-col(:md='23' :sm="5")
                div(style='line-height: 40px;') 站点地图
        el-divider(style='margin-top:0')
        <div class="fileDiv">
          <div class="fileList" v-for="(item,index) in fileList">
          <el-input size="mini" v-model='item.name' clearable maxlength="20"></el-input>
          <img :src="item.readPath" :onerror="logo" v-image-preview>
          <div class="imgButton">
            <el-button type="text" size="mini" :disabled="index == 0" icon="el-icon-back" @click='leftClick(index,item)'></el-button>
            <el-button type="text" size="mini" icon="el-icon-delete" @click='removeClick(item,index)'></el-button>
            <el-button type="text" size="mini" :disabled="index == fileList.length-1" icon="el-icon-right" @click='rightClick(index,item)'></el-button>
          </div>
        </div>
        <div class="shangchuan" @click="fileClick" v-if="fileList.length < 5">
        <span class="el-icon-plus"></span>
        </div>
        <input @change="fileChange($event)" type="file" id="upload_file" style="display: none"/>
        </div>
        span.dialog-footer(slot='footer')
            el-button(type='primary' size='medium' @click='cancel') 取消
            el-button(type='primary' size='medium' @click='dataFormSubmit()' v-prevent-re-click) 保存

</template>

<script>
import { mapState } from 'vuex'
import Vue from 'vue'

export default {
  computed: {
    ...mapState('d2admin/user', [
      'info'
    ])
  },
  data () {
    return {
      clickFlag: false,
      visible: false,
      dataList: [],
      dataListLoading: false,
      currentRow: {},
      fileList: [],
co_dengxiongwen's avatar
co_dengxiongwen committed
71
      urlPath: window.CONFIG.urlPath,
葛齐林's avatar
葛齐林 committed
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
      imageUrl: '',
      logo: 'this.src="' + require('../../../assets/images/videoImg.png') + '"',
      typeList: [
        { id: '1', name: '正线站点' },
        { id: '2', name: '换乘站点' }
      ],
      dataForm: {},
      dataRule: {
        // stationName: [
        //     { required: true, message: '站点名称不能为空', trigger: 'blur' },
        //     { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        // ],
        shortName: [
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '车站编码不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ],
        type: [{ required: true, message: '车站类型不能为空', trigger: 'blur' }],
        remark: [
          {
            min: 1,
            max: 300,
            message: '长度在 1 到 300 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.visible = true
      this.clickFlag = false
      this.dataForm.id = id
      this.initData()
      if (this.dataForm.id) {
        this.initDataFrom()
      }
    },
    initDataFrom () {
      this.$http({
        url: this.$http.adornUrl(`/liStation/getId/${this.dataForm.id}`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(data => {
        if (data && data.code === 0) {
          this.dataForm = data.bean
          this.fileList = data.bean.mapList
co_dengxiongwen's avatar
co_dengxiongwen committed
122 123 124
          this.fileList.forEach(element => {
            element.readPath = this.urlPath + element.filePath
          })
葛齐林's avatar
葛齐林 committed
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
        }
      })
    },
    initData () {
      this.typeList = [
        { id: '1', name: '正线站点' },
        { id: '2', name: '换乘站点' }
      ]
    },
    // 表单提交
    dataFormSubmit (id) {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          let errorflag = false
          this.fileList.forEach(e => {
            if (!e.name) {
              this.$message.error('请填写地图名称!')
              errorflag = true
              return false
            }
          })
          if (errorflag) {
            return
          }

          this.clickFlag = true

          for (var i in this.fileList) {
            this.fileList[i].orderNum = Number(i) + 1
          }
          this.dataForm.mapList = this.fileList
          this.$http({
            url: this.$http.adornUrl(
              `/liStation/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            // headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            data: this.dataForm
          }).then(data => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshdatalist')
                }
              })
            } else {
              this.clickFlag = false
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    fileClick () {
      document.getElementById('upload_file').click()
    },
    fileChange (el) {
      this.$showLoading()
      if (!el.target.files[0].size) return
      let file = el.target.files[0]

      if (this.beforeAvatarUpload(file)) {
        let fd = new FormData()
        fd.append('type', 'stationMap')
        // fd.append('stationId', this.dataForm.id)
        fd.append('file', file)
        this.$http({
          url: this.$http.adornUrl(`/liStationMap/uploadFile`),
          method: 'post',
          data: fd
        }).then(data => {
          if (data && data.code === 0) {
            this.currentRow = data.bean
co_dengxiongwen's avatar
co_dengxiongwen committed
202
            this.currentRow.readPath = this.urlPath + this.currentRow.filePath
葛齐林's avatar
葛齐林 committed
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
            this.fileList.push(this.currentRow)
            console.log('上传图片后的fileList', this.fileList)
            this.$message.success('上传成功!')
          } else {
            this.$message.error('上传失败!')
          }
        })
      }
      el.target.value = ''
    },
    removeClick (item, index) {
      var ids = [item.id]
      this.$confirm(`确认删除地图?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: true
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/liStationMap/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then((data) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.fileList.splice(index, 1)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {
      })
    },
    leftClick (index, row) {
      let front = JSON.parse(JSON.stringify(this.fileList[index - 1]))

      Vue.set(this.fileList, index - 1, row)
      Vue.set(this.fileList, index, front)
      // console.log(this.fileList)
    },
    rightClick (index, row) {
      let after = JSON.parse(JSON.stringify(this.fileList[index + 1]))

      Vue.set(this.fileList, index + 1, row)
      Vue.set(this.fileList, index, after)
      // console.log(this.fileList)
    },
    //文件上传前的验证
    beforeAvatarUpload (file) {
      const isJPG =
        file.type === 'image/jpg' ||
        file.type === 'image/png' ||
        file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 10

      if (!isJPG) {
        this.$message.error('上传图片只能是 jpg,jpeg,png 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过10MB!')
      }
      return isJPG && isLt2M
    },
    //取消
    cancel() {
      this.$confirm('确认取消?', !this.dataForm.id ? '新增' : '编辑', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.visible = false
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    }
  }
}
</script>
<style lang='scss' scoped>
.fileDiv {
  height: 200px;
  width: 100%;
  border: 1px solid #ccc;
  border-radius: 5px;

  .fileList {
    height: 130px;
    width: 100px;
    margin: 10px 0 0 10px;
    float: left;
    border-radius: 5px;
    overflow: hidden;

    .el-button {
      padding-left: 8px;
      font-size: 14px;
    }

    img {
      height: 100px;
      width: 100px;
    }

    .imgButton {
      position: relative;
      top: -30px;
      background: #ccc;
    }
  }

  .shangchuan {
    line-height: 100px;
    text-align: center;
    font-size: 30px;
    height: 100px;
    width: 100px;
    float: left;
    border: 1px solid #ccc;
    margin-top: 40px;
    margin-left: 10px;
    border-radius: 5px;
  }
}

.el-table__header {
  width: 100% !important;
}

.el-table__body {
  width: 100% !important;
}

.el-divider {
  margin-top: 0px;
}
.myDialog{
  /deep/.el-dialog{
    margin-top:4vh !important;
  }
}
</style>