CustomDialog.vue 15.6 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
<template>
  <div>
    <ht-table
      ref="htTable"
      :data="tableData"
      :page-result="pageResult"
      :selectable="selectable"
      :nopagination="isShowPagination"
      :no-header="true"
      :show-export="false"
      :show-custom-column="false"
      @load="loadData"
      @row-click="orgRowClick"
    >
      <template
        v-if="customDialog.conditionfield.length > 0 && isShowSearch"
        #search
      >
        <ht-table-search-panel :divide="3">
          <ht-table-search-field
            v-for="(item, $index) in customDialog.conditionfield"
            :key="$index"
            :type="item.type"
            :label="item.comment"
            :value-format="item.type == 'date' ? 'yyyy-MM-dd HH:mm:ss' : ''"
            :prop="item.field"
          />
        </ht-table-search-panel>
      </template>
      <template #default>
        <ht-table-column
          align="center"
          :hidden="customDialog.selectNum != 1"
          width="33"
        >
          <template #default="{ row, index }">
            <el-radio
              v-model="textRadio"
              :label="row[customDialog.displayfield[0].field] + index"
            />
          </template>
        </ht-table-column>
        <ht-table-column type="index" width="50" align="center" label="序号" />
        <ht-table-column
          v-for="(item, $index) in customDialog.displayfield"
          :key="$index"
          :prop="item.field.toLowerCase()"
          :label="item.comment"
          :sortable="true"
          :show-overflow-tooltip="true"
        ></ht-table-column>
      </template>
    </ht-table>
  </div>
</template>

<script>
  import request from '@/utils/request'
  import { parseExp } from '@/utils/parse.js'
  import { getValueByPath } from '@/utils/value.js'

  export default {
    name: 'CustomDialogShowDetail',
    props: {
      //自定义对话框的别名
      alias: {
        type: String,
        required: true,
      },
      //自定义对话框的配置
      customDialog: {
        type: Object,
        required: true,
      },
      isShowPagination: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        customDialogPostParam: [], //为REST接口且请求类型是POST,条件字段是固定值的参数
        customDialogGetUrl: '', //为REST接口且请求类型是GET,条件字段是固定值的请求地址
        selectionRadio: [], //ht-table列表单选选择的数据
        textRadio: '', //ht-table列表数据的单选按钮
        selectable: true, //表示ht-table列表第一列是否显示复选框
        isShowSearch: false, //是否显示高级搜索
        tableData: [], //ht-table列表数据
        pageResult: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      }
    },
    methods: {
      //显示对话框
      initData() {
        this.customDialogPostParam = [] //清空为REST接口且请求类型是POST,条件字段是固定值的参数
        this.customDialogGetUrl = '' //清空为REST接口且请求类型是GET,条件字段是固定值的请求地址
        this.selectionRadio = [] //清空ht-table列表单选选择的数据
        this.textRadio = '' //清空ht-table列表数据单选按钮的选中状态
        this.isShowSearch = false //不显示高级搜索
        //是否单选  1:单选;-1:多选
        if (this.customDialog.selectNum == 1) {
          this.selectable = false
        } else {
          this.selectable = true
        }
        //判断是否有条件查询,且数据来源是数据源或者数据来源是REST接口且请求类型是POST
        if (
          this.customDialog.conditionfield.length > 0 &&
          (this.customDialog.dsType == 'dataSource' ||
            (this.customDialog.dsType != 'dataSource' &&
              this.customDialog.requestType == 'POST'))
        ) {
          for (
            let i = this.customDialog.conditionfield.length - 1;
            i >= 0;
            i--
          ) {
            //判断条件字段是否是参数传入并控制器的类型为单行文本框  (controllerType有值就代表控制器的类型为单行文本框,defaultType:1:参数传入,2:固定值 )
            if (
              this.customDialog.conditionfield[i].controllerType &&
              this.customDialog.conditionfield[i].defaultType == '1'
            ) {
              this.isShowSearch = true //显示高级搜索
              //如果条件字段为data类型,则控制器的类型为日期类型
              if (this.customDialog.conditionfield[i].dbType == 'date') {
                this.customDialog.conditionfield[i].type = 'date'
              } else {
                this.customDialog.conditionfield[i].type = 'text'
              }
            } else {
              if (
                this.customDialog.dsType != 'dataSource' &&
                this.customDialog.requestType == 'POST'
              ) {
                this.customDialogPostParam.push(
                  this.customDialog.conditionfield[i]
                ) //为REST接口且请求类型是POST,条件字段是固定值的参数
              }
              //如果是条件字段固定值就清除
              this.customDialog.conditionfield.splice(i, 1)
            }
          }
        }
        //判断是否有条件查询,且数据来源是REST接口且请求类型是GET
        if (
          this.customDialog.conditionfield.length > 0 &&
          this.customDialog.dsType != 'dataSource' &&
          this.customDialog.requestType == 'GET'
        ) {
          for (
            let i = this.customDialog.conditionfield.length - 1;
            i >= 0;
            i--
          ) {
            //判断条件字段是否是固定值(defaultType:1:参数传入,2:固定值 )
            if (this.customDialog.conditionfield[i].defaultType == '2') {
              //判断请求地址是否带有 ?
              if (this.customDialog.url.indexOf('?') == -1) {
                //无
                this.customDialog.url =
                  this.customDialog.url +
                  '?' +
                  this.customDialog.conditionfield[i].comment +
                  '=' +
                  this.customDialog.conditionfield[i].defaultValue
              } else {
                //有
                this.customDialog.url =
                  this.customDialog.url +
                  '&' +
                  this.customDialog.conditionfield[i].comment +
                  '=' +
                  this.customDialog.conditionfield[i].defaultValue
              }
              //如果是条件字段固定值就清除
              this.customDialog.conditionfield.splice(i, 1)
              this.customDialogGetUrl = this.customDialog.url
              //判断条件字段是否是参数传入并控制器的类型为单行文本框(controllerType有值就代表控制器的类型为单行文本框,defaultType:1:参数传入,2:固定值 )
            } else if (
              this.customDialog.conditionfield[i].controllerType &&
              this.customDialog.conditionfield[i].defaultType == '1'
            ) {
              this.isShowSearch = true //显示高级搜索
            }
          }
        }
      },
      //加载数据
      async loadData(param, cb) {
        //为REST接口且请求类型是POST,条件字段是固定值的参数
        if (
          this.customDialog.dsType != 'dataSource' &&
          this.customDialog.requestType == 'POST' &&
          this.customDialogPostParam.length > 0
        ) {
          for (let i = 0; i < this.customDialogPostParam.length; i++) {
            //构建查询条件
            let operation = this.getOperation(
              this.customDialogPostParam[i].condition
            )
            let obj = {
              group: 'main',
              operation: operation,
              property: this.customDialogPostParam[i].comment,
              relation: 'AND',
              value: this.customDialogPostParam[i].defaultValue,
            }
            if (param.querys && param.querys.length > 0) {
              param.querys.push(obj)
            } else {
              param.querys = []
              param.querys.push(obj)
            }
          }
        }
        // post restful接口,并且配置了post参数。则以post参数作为请求参数
        if (
          this.customDialog.dsType != 'dataSource' &&
          this.customDialog.requestType == 'POST' &&
          this.customDialog.dataParam &&
          this.customDialog.dataParam.constructor == String
        ) {
          // 构建上下文数据对象
          let ctx = {}
          param &&
            param.querys &&
            param.querys.forEach((element) => {
              ctx[element.property] = element.value
            })
          const exp = parseExp(this.customDialog.dataParam, ctx)
          try {
            param = JSON.parse(exp)
          } catch (e) {
            this.$message.error(`POST参数不是有效的JSON格式${query.dataParam}`)
          }
        }
        //判断是否有条件查询,且数据来源是REST接口且请求类型是GET,并且判断参数传入是否填写了查询条件
        if (
          this.customDialog.conditionfield.length > 0 &&
          this.customDialog.dsType != 'dataSource' &&
          this.customDialog.requestType == 'GET' &&
          param.querys &&
          param.querys.length > 0
        ) {
          for (
            let i = this.customDialog.conditionfield.length - 1;
            i >= 0;
            i--
          ) {
            //判断条件字段是否是参数传入并控制器的类型为单行文本框(controllerType有值就代表控制器的类型为单行文本框,defaultType:1:参数传入,2:固定值 )
            if (
              this.customDialog.conditionfield[i].controllerType &&
              this.customDialog.conditionfield[i].defaultType == '1'
            ) {
              //参数传入填写了查询条件
              for (let j = 0; j < param.querys.length; j++) {
                //判断为REST接口且请求类型是GET,条件字段是固定值的请求地址是否为空
                if (this.customDialogGetUrl != '') {
                  this.customDialog.url = this.customDialogGetUrl //赋值为REST接口且请求类型是GET,条件字段是固定值的请求地址
                } else {
                  this.customDialogGetUrl = this.customDialog.url
                }
                //判断请求地址是否带有 ?
                if (this.customDialog.url.indexOf('?') == -1) {
                  //无
                  this.customDialog.url =
                    this.customDialog.url +
                    '?' +
                    param.querys[j].property +
                    '=' +
                    param.querys[j].value
                } else {
                  //有
                  this.customDialog.url =
                    this.customDialog.url +
                    '&' +
                    param.querys[j].property +
                    '=' +
                    param.querys[j].value
                }
              }
            }
          }
        } else {
          //判断为REST接口且请求类型是GET,条件字段是固定值的请求地址是否为空
          if (this.customDialogGetUrl != '') {
            this.customDialog.url = this.customDialogGetUrl //赋值为REST接口且请求类型是GET,条件字段是固定值的请求地址
          }
        }
        let queryUrl =
          this.customDialog.dsType == 'dataSource'
            ? `${window.context.form}/form/customDialog/v1/getListData?alias=` +
              this.alias +
              '&mapParam='
            : this.customDialog.url
        let requestMethod = 'POST'
        //如果自定义对话框列表查询数据不是数据源则请求方法为restful配置的请求方式
        if (this.customDialog.dsType != 'dataSource') {
          requestMethod = this.customDialog.requestType
        }
        //查询数据
        let response = await request({
          url: queryUrl,
          method: requestMethod,
          data: param,
        }).finally(() => cb && cb())
        //如果数据来源是REST接口,且请求类型是GET
        if (
          this.customDialog.dsType != 'dataSource' &&
          this.customDialog.requestType == 'GET'
        ) {
          this.tableData = response.rows
          this.nopagination = true //隐藏分页组件
        }
        //如果数据来源是REST接口且请求类型是POST
        if (
          this.customDialog.dsType != 'dataSource' &&
          this.customDialog.requestType == 'POST'
        ) {
          this.tableData = getValueByPath(
            response.rows,
            this.customDialog.listKey
          )
          //如果有分页
          if (getValueByPath(response.rows, this.customDialog.pageKey)) {
            this.pageResult = {
              page: getValueByPath(response.rows, this.customDialog.pageKey),
              pageSize: getValueByPath(
                response.rows,
                this.customDialog.pageSizeKey
              ),
              total: getValueByPath(response.rows, this.customDialog.totalKey),
            }
            this.nopagination = false //显示分页组件
          } else {
            this.nopagination = true //隐藏分页组件
          }
        }
        //如果数据来源是数据源
        if (this.customDialog.dsType == 'dataSource') {
          if (response && response.rows) {
            this.tableData = response.rows
            //如果有分页
            if (response.page) {
              this.pageResult = {
                page: response.page,
                pageSize: response.pageSize,
                total: response.total,
              }
              this.nopagination = false //显示分页组件
            } else {
              this.nopagination = true //隐藏分页组件
            }
          } else {
            this.tableData = []
            this.nopagination = true //隐藏分页组件
          }
        }
      },
      //获取运算符号
      getOperation(old) {
        if (!old) {
          return ''
        } else if (old == 'EQ') {
          return 'EQUAL'
        } else if (old == 'EIC') {
          return 'EQUAL_IGNORE_CASE'
        } else if (old == 'LT') {
          return 'LESS'
        } else if (old == 'GT') {
          return 'GREAT'
        } else if (old == 'LE') {
          return 'LESS_EQUAL'
        } else if (old == 'GE') {
          return 'GREAT_EQUAL'
        } else if (old == 'NE') {
          return 'NOT_EQUAL'
        } else if (old == 'LK') {
          return 'LIKE'
        } else if (old == 'LFK') {
          return 'LEFT_LIKE'
        } else if (old == 'RHK') {
          return 'RIGHT_LIKE'
        } else if (old == 'ISNULL') {
          return 'IS_NULL'
        } else if (old == 'NOTNULL') {
          return 'NOTNULL'
        } else if (old == 'IN') {
          return 'IN'
        } else if (old == 'BETWEEN') {
          return 'BETWEEN'
        } else {
          return 'LIKE'
        }
      },
      //点击列表某一条数据时触发
      orgRowClick(row, column, event) {
        //是否单选  1:单选;-1:多选
        if (this.customDialog.selectNum === 1) {
          //选中单选按钮
          this.textRadio =
            event.currentTarget.children[0].children[0].children[0].__vue__.$refs.radio.value
          this.selectionRadio = [row]
          this.$emit('orgRowClick', row)
        } else {
          //点击列表数据选中复选框
          this.$refs.htTable.$refs.htTable.toggleRowSelection(row)
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-main {
    padding-top: 0px;
  }
</style>