index.vue 8.41 KB
<!-- 物料管理 -->
<template>
  <d2-container>
    <el-form
      :inline="true"
      :model="searchForm"
      label-width="85px"
      slot="header"
    >
      <el-form-item label="物料">
        <el-input
          v-model="searchForm.keyWord"
          placeholder="请输入物料代码/编码/名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="所在地">
        <el-select
          v-model="searchForm.stationCode"
          placeholder="请选择所在地"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in baseData.station"
            :key="index"
            :value="item.confCode"
            :label="item.confValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          :id="tableParams.btnId"
        >查询</el-button>
      </el-form-item>
    </el-form>
    <d2-table-header title="物料列表">
      <span style="margin-right: 10px;">
        <auth-button label="导出"></auth-button>
      </span>
      <auth-button label="导入">
        <el-link href="http://img01.glsx.com.cn/upload/tfimspdf/物料导入模板.xls" type="primary"  class="import-template" icon="el-icon-link">模版下载</el-link>
        <input
          type="file"
          id="fileUpload"
          class="import-input"
          accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          @change="importFile($event)"
        />
      </auth-button>
    </d2-table-header>
    <d2-table
      :searchForm="searchForm"
      :tableParams="tableParams"
      :getListApi="getListApi"
    >
      <auth-button
        slot="auth-button"
        slot-scope="scope"
        :data="scope.scope"
      ></auth-button>
    </d2-table>
    <edit-dialog
      :isVisible="editVisible"
      :editForm="editForm"
      @closeDialog="handleEditClose"
      @confirmDialog="handleEditConfirm"
    ></edit-dialog>
  </d2-container>
</template>

<script>
import axios from 'axios'
import EditDialog from './editDialog'
import { BaseData } from '@/common/constants'
import {
  getListApi,
  editApi,
  removeApi,
  exportApi
} from '@api/deviceManagement'
import notice from '@/common/notice'
import Qs from 'qs'
import sign from '@api/sign'
import { errorCreate } from '@/plugin/axios'
export default {
  name: 'fimsDeviceManagement',
  components: {
    EditDialog
  },
  data() {
    return {
      // 编辑弹窗
      editVisible: false,
      editForm: {
        id: '',
        deviceNum: '',
        deviceCode: '',
        crcCode: '',
        drawNum: '',
        deviceName: '',
        specifications: '',
        stationCode: '',
        stationName: '',
        stockCount: '',
        responsible: ''
      },
      // 搜索
      searchForm: {
        keyWord: '',
        stationCode: ''
      },
      // 表格
      // tableData: [],
      // 表格参数
      tableParams: {
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'deviceList',
        tableOption: true,
        tableColumns: [
          {
            label: '物料代码',
            prop: 'deviceNum',
            width: '170'
          },
          {
            label: '物料编码',
            prop: 'deviceCode',
            width: '170'
          },
          {
            label: 'CRC物料号',
            prop: 'crcCode',
            width: '170'
          },
          {
            label: '图号',
            prop: 'drawNum',
            width: '170'
          },
          {
            label: '物料名称',
            prop: 'deviceName',
            minWidth: '200'
          },
          {
            label: '规格',
            prop: 'specifications',
            width: '140'
          },
          {
            label: '所在地',
            prop: 'stationName',
            width: '120'
          },
          {
            label: '库存',
            prop: 'stockCount',
            width: '100'
          },
          {
            label: '责任人',
            prop: 'responsible',
            width: '120'
          }
        ]
      },
      baseData: {
        station: []
      },
      fileInfo: { fileType: '', fileBase: '' },
      getListApi: getListApi
    }
  },

  mounted() {
    this.loadBaseData()
  },

  methods: {
    async loadBaseData() {
      const data = await this.$store.dispatch(
        'd2admin/base/get',
        BaseData.STATION.KEY
      )
      this.baseData.station = data
    },

    /* 接口请求-start */

    delObjMutiAttr(obj, keys) {
      keys.map(key => delete obj[key])
      return obj
    },

    editDevice(params) {
      params = this.delObjMutiAttr(params, [
        'createTime',
        'keyWord',
        'pageCount',
        'pageSize',
        'updateTime',
        'stationName',
        'stationCode'
      ])
      // console.log(params)
      editApi(params)
        .then(res => {
          this.handleEditClose()
          notice.okTips()
        })
        .catch(() => {
          notice.errorTips()
        })
    },

    // 导入
    importFile(e) {
      const name = e.target.files[0].name
      this.fileInfo.fileType = name.split('.')[1]
      // // 转码base64
      const reader = new FileReader()
      reader.readAsDataURL(e.target.files[0])
      reader.onload = event => {
        const imgFile = event.target.result
        const arrBase64 = imgFile.split(',')
        this.fileInfo.fileBase = arrBase64[1]
        var params = {
          file: this.fileInfo.fileType + '@' + this.fileInfo.fileBase
        }
        const noSignAttr = ['file']
        params.source = '1'
        params.app_key = '48e5e13229b82c1b4e6e8c96151f0637'
        params.timestamp = new Date().getTime()
        params.v = '1.0.0'
        params.format = 'json'
        params.method = 'fims.device.import' // params.method
        // console.log(params)
        params = sign(params, noSignAttr)
        axios
          .post(
            process.env.VUE_APP_API,
            Qs.stringify({
              ...params
            }),
            {
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
              }
            }
          )
          .then(res => {
            const _data = res.data
            const { code, data, message } = _data
            if (+code !== 0) {
              errorCreate(`${message}`)
              return
            }
            const { url, failCount, successCount } = data
            if (url) {
              this.$open(url)
              notice.errorTips(
                `本次导入结果:成功 ${successCount} 条,失败 ${failCount} 条`
              )
            } else {
              notice.okTips('本次导入结果:全部成功!')
            }
            this.$store.commit('d2admin/refreshtable/set', !this.$store.state.d2admin.refreshtable.value)
            document.getElementById('fileUpload').value = null
          })
          .catch(() => {
            notice.errorTips()
            document.getElementById('fileUpload').value = null
          })
      }
    },

    exportFile() {
      exportApi(this.searchForm)
        .then(res => {
          this.$open(res.url)
          notice.okTips()
        })
        .catch(() => {
          notice.errorTips()
        })
    },
    /* 接口请求-end */

    handleEditClose() {
      this.editVisible = false
    },

    handleEditConfirm() {
      var editParams = _.cloneDeep(this.editForm)
      this.editDevice(editParams)
    },

    // 表格-编辑
    handleEdit(row) {
      // console.log(row)
      this.editForm = _.cloneDeep(row)
      this.editVisible = true
    },

    handleDelete(row) {
      this.$confirm('确认删除?', '删除', {
        comfirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          removeApi({
            id: row.id,
            usedStatus: 2
          })
            .then(res => {
              notice.okTips()
              // this.getList()
            })
            .catch(() => {
              notice.errorTips()
            })
        })
        .catch(() => {})
    }
  }
}
</script>
<style scoped>
.import-input {
  opacity: 0;
  position: absolute;
  right: 0;
  top: 0;
  width: 70px;
  height: 36px;
  cursor: pointer;
  font-size: 0;
}
.import-template{
  position:absolute;height:20px;top:10px;right:212px;
}
</style>