app-image-upload.vue 11.7 KB
Newer Older
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
<template>
  <div class='app-picture-upload'>
    <ul class='el-upload-list el-upload-list--picture-card'>
<!-- 绘制缩略图 Start -->
        <li v-for="(file,index) in files" :key="index" class='el-upload-list__item is-success'>
          <el-image :src="file.url" class='el-upload-list__item-thumbnail' style='min-height:100px;min-width:100px;'>
              <div slot='error' class='image-slot'>
                  <i class='el-icon-picture-outline'></i>
              </div>
          </el-image>
          <a class='el-upload-list__item-name'>
              <i class='el-icon-document'></i> {{file.name}}
          </a>
          <i class='el-icon-close'></i>
          <label class='el-upload-list__item-status-label'>
              <i class='el-icon-upload-success el-icon-check'></i>
          </label>
          <span class='el-upload-list__item-actions'>
              <span class='el-upload-list__item-preview'>
                  <i class='el-icon-zoom-in' @click="onPreview(file)"></i>
              </span>
              <span class='el-upload-list__item-download'>
                  <i class='el-icon-download' @click="onDownload(file)"></i>
              </span>
              <span :style="{ 'display': disabled? 'none' : 'inline-block' }" class='el-upload-list__item-delete'>
                  <i class='el-icon-delete' @click="onRemove(file, files)"></i>
              </span>
          </span>
        </li>
            <!-- 绘制缩略图 end -->
    </ul>
    <!-- 文件上传 -->
    <el-upload 
      v-if = "multiple || files.length === 0" 
      :class = "{'el-upload-disabled':disabled}"
      :disabled = "disabled"
      :action = "uploadUrl"
      :headers = "{ 'srfappdata': appData }"
      :show-file-list = "false"
      list-type =  "picture-card"
      :file-list =  "files"
      :before-upload = "beforeUpload"
      :on-success = "onSuccess"
      :before-remove = "onRemove"
      :on-error = "onError"
      :on-preview = "onDownload"
      >
      <i class="el-icon-plus"></i>
    </el-upload>
    <!-- 预览 -->
    <modal v-model="dialogVisible" footer-hide class-name='app-image-upload-model'>
52
      <el-image :src="dialogImageUrl">
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
          <div slot='error' class='image-slot'>
              <i class='el-icon-picture-outline'></i>
          </div>
      </el-image>
    </modal>
  </div>
</template>
<script lang = 'ts'>
import { Vue, Component, Prop, Watch, Provide } from 'vue-property-decorator';
import { Environment } from '@/environments/environment';
import { Subject, Unsubscribable } from 'rxjs';

@Component({})
export default class AppImageUpload extends Vue {

    /**
     * 表单状态
     *
     * @type {Subject<any>}
     * @memberof AppImageUpload
     */
    @Prop() public formState?: Subject<any>

    /**
     * 是否忽略表单项书香值变化
     *
     * @type {boolean}
     * @memberof AppImageUpload
     */
    @Prop() public ignorefieldvaluechange?: boolean;

    /**
     * 表单状态事件
     *
     * @private
     * @type {(Unsubscribable | undefined)}
     * @memberof AppImageUpload
     */
    private formStateEvent: Unsubscribable | undefined;

    /**
     * 表单数据
     *
     * @type {string}
     * @memberof AppImageUpload
     */
    @Prop() public data!: string;

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    /**
     * 视图参数
     *
     * @type {*}
     * @memberof AppFormDRUIPart
     */
    @Prop() public viewparams!: any;

    /**
     * 视图上下文
     *
     * @type {*}
     * @memberof AppAutocomplete
     */
    @Prop() public context!: any;

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    /**
     * 初始化值
     *
     * @type {*}
     * @memberof AppImageUpload
     */
    @Prop() public value?: any;

    /**
     * 数据值变化
     *
     * @param {*} newval
     * @param {*} val
     * @returns
     * @memberof AppImageUpload
     */
    @Watch('value')
    onValueChange(newval: any, val: any) {
        if (this.ignorefieldvaluechange) {
            return;
        }
138
        this.getParams();
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
        this.setFiles(newval)
        this.dataProcess();
    }

    /**
     * 所属表单项名称
     *
     * @type {string}
     * @memberof AppImageUpload
     */
    @Prop() public name!: string;

    /**
     * 是否禁用
     *
     * @type {boolean}
     * @memberof AppImageUpload
     */
    @Prop() public disabled?: boolean;

    /**
     * 上传参数
     *
     * @type {string}
     * @memberof AppImageUpload
     */
    @Prop() public uploadparams?: string;

    /**
     * 下载参数
     *
     * @type {string}
     * @memberof AppImageUpload
     */
    @Prop() public exportparams?: string;

    /**
     * 自定义参数
     *
     * @type {*}
     * @memberof AppImageUpload
     */
    @Prop() public customparams?: any;

    /**
     * 上传文件路径
     *
     * @memberof AppImageUpload
     */
    public uploadUrl = Environment.BaseUrl + Environment.UploadFile;

    /**
     * 下载文件路径
     *
     * @memberof AppImageUpload
     */
    public downloadUrl = Environment.BaseUrl + Environment.ExportFile;

    /**
     * 文件列表
     *
     * @memberof AppImageUpload
     */
    @Provide() public files = [];

    /**
205
     * 上传params
206 207 208 209
     *
     * @type {Array<any>}
     * @memberof AppImageUpload
     */
210
    public upload_params: Array<any> = [];
211 212

    /**
213
     * 导出params
214 215 216 217
     *
     * @type {Array<any>}
     * @memberof AppImageUpload
     */
218
    public export_params: Array<any> = [];
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

    /**
     * 自定义数组
     *
     * @type {Array<any>}
     * @memberof AppImageUpload
     */
    public custom_arr: Array<any> = [];

    /**
     * 应用参数
     *
     * @type {*}
     * @memberof AppImageUpload
     */
    public appData: any="";

    /**
     * 设置files
     *
     * @private
     * @memberof AppImageUpload
     */
    private setFiles(value:any): void {
        let _files = JSON.parse(value);
        if (value && Object.prototype.toString.call(_files)=='[object Array]') {
            this.files = _files;
        } else {
            this.files = [];
        }
    }

    /**
     * 数据处理
     *
     * @private
     * @memberof AppImageUpload
     */
    private dataProcess(): void {

        let _url = `${Environment.BaseUrl}${Environment.UploadFile}`;
260 261 262 263 264 265 266 267 268
        if (this.upload_params.length > 0 ) {
            _url +='?';
            this.upload_params.forEach((item:any,i:any)=>{
                _url += `${Object.keys(item)[0]}=${Object.values(item)[0]}`;
                if(i<this.upload_params.length-1){
                    _url += '&';
                }
            })
            
269
        }
270
        
271
        this.uploadUrl = _url;
272
        
273 274
        this.files.forEach((file: any) => {
            let url = `${this.downloadUrl}/${file.id}`;
275 276 277 278 279 280 281 282
            if (this.export_params.length > 0) {
                url +='?';
                this.export_params.forEach((item:any,i:any)=>{
                    url += `${Object.keys(item)[0]}=${Object.values(item)[0]}`;
                    if(i<this.export_params.length-1){
                        url += '&';
                    }
                })
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            }
            file.url = url;
        });
    }

    /**
     * vue 生命周期
     *
     * @memberof AppImageUpload
     */
    public created() {
        if (this.formState) {
            this.formStateEvent = this.formState.subscribe(($event: any) => {
                // 表单加载完成
                if (Object.is($event.type, 'load')) {
298
                    this.getParams();
299 300 301 302 303 304 305 306 307 308 309 310 311 312
                    this.setFiles(this.value);
                    this.dataProcess();
                }
            });
        }
    }

    /**
     * vue 生命周期
     *
     * @memberof AppImageUpload
     */
    public mounted() {
        this.appData = this.$store.getters.getAppData();
313 314 315 316
        this.getParams();
        this.setFiles(this.value);
        this.dataProcess();
    }
317

318 319 320 321 322 323 324 325
    /**
     *获取上传,导出参数
     *
     *@memberof AppImageUpload
     */
    public getParams(){
        let uploadparams: any = JSON.parse(JSON.stringify(this.uploadparams));
        let exportparams: any = JSON.parse(JSON.stringify(this.exportparams));
326

327 328 329 330 331 332
        let upload_params: Array<string> = [];
        let export_params: Array<string> = [];

        let param:any = this.viewparams;
        let context:any = this.context;
        let _data:any = JSON.parse(this.data);
333

334
        if (this.uploadparams && !Object.is(this.uploadparams, '')) {
335
            upload_params = this.$util.computedNavData(_data,param,context,uploadparams);    
336 337
        }
        if (this.exportparams && !Object.is(this.exportparams, '')) {
338
            export_params = this.$util.computedNavData(_data,param,context,exportparams);
339
        }
340
        
341 342 343
        this.upload_params = [];
        this.export_params = [];

344 345 346 347 348 349 350 351 352
        for (const item in upload_params) {
            this.upload_params.push({
                [item]:upload_params[item]
            })
        }
        for (const item in export_params) {
            this.export_params.push({
                [item]:export_params[item]
            })
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
        }
    }

    /**
     * 组件销毁
     *
     * @memberof AppImageUpload
     */
    public destroyed(): void {
        if (this.formStateEvent) {
            this.formStateEvent.unsubscribe();
        }
    }

    /**
     * 上传之前
     *
     * @param {*} file
     * @memberof AppImageUpload
     */
    public beforeUpload(file: any) {
        // console.log('上传之前');
    }

    /**
     * 上传成功回调
     *
     * @param {*} response
     * @param {*} file
     * @param {*} fileList
     * @returns
     * @memberof AppImageUpload
     */
    public onSuccess(response: any, file: any, fileList: any) {
        if (!response) {
            return;
        }
        const data = { name: response.filename, id: response.fileid };
        let arr: Array<any> = [];
        this.files.forEach((_file: any) => {
            arr.push({ name: _file.name, id: _file.id })
        });
        arr.push(data);

        let value: any = arr.length > 0 ? JSON.stringify(arr) : null;
        this.$emit('formitemvaluechange', { name: this.name, value: value });
    }

    /**
     * 上传失败回调
     *
     * @param {*} error
     * @param {*} file
     * @param {*} fileList
     * @memberof AppImageUpload
     */
    public onError(error: any, file: any, fileList: any) {
410
        this.$Notice.error({ title: (this.$t('components.appImageUpload.uploadFail') as string) });
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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    }

    /**
     * 删除文件
     *
     * @param {*} file
     * @param {*} fileList
     * @memberof AppImageUpload
     */
    public onRemove(file: any, fileList: any) {
        let arr: Array<any> = [];
        fileList.forEach((f: any) => {
            if (f.id != file.id) {
                arr.push({ name: f.name, id: f.id });
            }
        });
        let value: any = arr.length > 0 ? JSON.stringify(arr) : null;
        this.$emit('formitemvaluechange', { name: this.name, value: value });
    }

    /**
     * 下载文件
     *
     * @param {*} file
     * @memberof AppImageUpload
     */
    public onDownload(file: any) {
        window.open(file.url);
    }

    /**
     * 预览图片地址
     *
     * @type {string}
     * @memberof AppImageUpload
     */
    public dialogImageUrl: string = '';

    /**
     * 是否显示预览界面
     *
     * @type {boolean}
     * @memberof AppImageUpload
     */
    public dialogVisible: boolean = false;

    /**
     * 是否支持多个上传
     *
     * @type {boolean}
     * @memberof AppImageUpload
     */
    @Prop({ default: true }) public multiple?: boolean;

    /**
     * 预览
     *
     * @param {*} file
     * @memberof AppImageUpload
     */
    public onPreview(file: any) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
    }
}
</script>
<style lang = "less">
@import './app-image-upload.less';
</style>