保誠-保戶業務員媒合平台
Tomas
2023-12-25 f065760fa7df1f88747395ab4b55349ce8b2faf0
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
<template>
  <div>
 
    <el-upload
      class="pam-avatar-uploader"
      ref="upload"
      action="#"
      :auto-upload="false"
      :on-change="handleAvatarUploaded"
      :show-file-list="false"
      accept="image/png, image/jpeg, image/jpg">
        <el-avatar
          :size="150"
          :src="imgSrc"
          class="pam-avatar cursor--pointer fix-chrome-click--issue"
        ></el-avatar>
            <div class="text--center mt-10">
              <el-button
              >設定相片</el-button>
            </div>
    </el-upload>
      <div
        v-if="showResetAvatarBtn"
        class="pam-avatar-uploader__action-label text--center mt-10 cursor--pointer"
        style="line-height: 1.5"
        >
        <i class="icon-information"></i>
        請按頁面最下方的送出按鈕來更新照片,
        <span class="text--primary cursor--pointer text--underline" @click="resetAvatar">或點此取消</span>
 
      </div>
 
  </div>
</template>
 
<script lang="ts">
  import { Vue, Component, Prop, PropSync } from 'nuxt-property-decorator';
 
  import { MessageBox } from 'element-ui';
  import { MessageBoxData } from 'element-ui/types/message-box';
 
  import myConsultantService from '~/shared/services/my-consultant.service';
 
  @Component
  export default class editConsultantAvatar extends Vue {
 
    @Prop({type:String, default:""})
    agentNo!:string;
 
    @PropSync('photoBase64',{type:String, default:""})
    syncPhotoBase64!:string;
 
    _imgSrc: string = '';
    imgSrc: string='';
 
    //////////////////////////////////////////////////////////////////////
 
    mounted() {
      if(this.agentNo) this.initConsultantAvatar()
    }
 
    private initConsultantAvatar(): void {
      myConsultantService.getConsultantAvatar(this.agentNo)
      .then(base64=>
       this.splitBase64WithCommon(base64)
      )
    }
 
    //////////////////////////////////////////////////////////////////////
 
    resetAvatar(): void {
      this.imgSrc = this._imgSrc;
    }
 
    handleAvatarUploaded(file:any): void {
      const isFollowUploadRule = file.raw.type.includes('image/');
      isFollowUploadRule ? this.getImgSrc(file) : this.showFileUploadErrorMsg()
    }
 
    private 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 = base64.split(','); // 為了把 data:image , base64 解析分開;
      this.syncPhotoBase64 = splitBase64[1];
      // NOTE: 因為目前以 agentNO 取得 avatar 會失敗,
      // 故加上此判斷來防範不預期顯示'取消按鈕'的狀況。 [Tomas, 2022/1/3]
      if (!this._imgSrc) {
        this._imgSrc = base64;
      }
      this.imgSrc = base64;
    }
 
    private showFileUploadErrorMsg():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,
        });
    }
 
    get showResetAvatarBtn(): boolean {
      // NOTE: 因為目前以 agentNO 取得 avatar 會失敗,
      // 故加上此判斷來防範不預期顯示'取消按鈕'的狀況。 [Tomas, 2022/1/3]
      if (!this._imgSrc && !this.imgSrc) return false;
      return this._imgSrc !== this.imgSrc;
    }
  }
</script>
 
<style lang="scss" scoped>
 
</style>