保誠-保戶業務員媒合平台
Benson
2021-12-30 0d13a8e22cfd29bcb4360f2f5d6ed0c2e38e1052
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
<template>
  <div>
     <el-avatar
        :size="150"
        :src="imgSrc"
        class="pam-avatar cursor--pointer fix-chrome-click--issue"
    ></el-avatar>
    <el-upload
      class="pam-avatar-uploader"
      action=""
      :auto-upload="false"
      :on-change="handleAvatarSuccess"
      :show-file-list="false">
      <div class="pam-avatar-uploader--title">更換大頭貼照</div>
    </el-upload>
  </div>
</template>
 
<script lang="ts">
  import { MessageBox } from 'element-ui';
  import { MessageBoxData } from 'element-ui/types/message-box';
  import { Vue, Component, Prop, PropSync } from 'nuxt-property-decorator';
  import myConsultantService from '~/shared/services/my-consultant.service';
  import _ from 'lodash';
 
  @Component
  export default class editConsultantAvatar extends Vue {
    @Prop({type:String, default:""})
    agentNo!:string;
 
    @PropSync('photoBase64',{type:String, default:""})
    syncPhotoBase64!:string;
 
    imgSrc:string='';
 
    //////////////////////////////////////////////////////////////////////
 
    mounted() {
      if(this.agentNo) this.initConsultantAvatar()
    }
 
    //////////////////////////////////////////////////////////////////////
 
    initConsultantAvatar():void{
      myConsultantService.getConsultantAvatar(this.agentNo)
      .then(base64=>
       this.splitbase64WithCommon(base64)
      )
    }
    handleAvatarSuccess(file:any, fileList:any) {
      const isFollowUploadRule =_.includes(file.raw.type,'image/');
      isFollowUploadRule ? this.getImgSrc(file) : this.showErrorMsg()
    }
    getImgSrc(file:any):void{
      const blob = file.raw;
      this.blobToBase64(blob).then(base64=>{
        this.splitbase64WithCommon(base64 as string);
      });
    }
 
    //////////////////////////////////////////////////////////////////////
 
    private blobToBase64(blob:File):Promise<string | ArrayBuffer | null> {
      return new Promise((resolve,reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onloadend = () => {
          resolve(reader.result)
        };
      });
    }
 
    private splitbase64WithCommon(base64:string):void{
      const splitBase64=_.split(base64, ','); // 為了把 data:image , base64 解析分開;
      this.syncPhotoBase64 = splitBase64[1];
      this.imgSrc = base64;
    }
 
    private showErrorMsg():Promise<MessageBoxData>{
       return MessageBox({
          message:`<div class="message-header">上傳格式有誤</div>
                    <div class="message-content">請上傳正確圖檔</div>`,
          dangerouslyUseHTMLString: true,
          showClose:false,
          showConfirmButton:true,
          confirmButtonText:'確認',
          customClass:'pam-message-box',
          closeOnClickModal:false,
        });
    }
  }
</script>
 
<style lang="scss" scoped>
 
</style>