import $toast from "../../../utils/tool/toast";

export default {

    data(){
        return {
            vValue:false,
            poster:''
        }
    },

    methods:{
        preservation(){

            if (this.preservationStatus) return;
            this.preservationStatus = true;

            this.getImageInfo().then((path)=>{
                uni.saveImageToPhotosAlbum({
                    filePath: path,
                    success:(res)=> {
                        this.preservationStatus = false;
                        return $toast.success('保存成功');
                    },
                    fail:(fail)=>{
                        this.preservationStatus = false;
                        return $toast.success('保存失败');
                    }
                });
            }).catch(()=>{
                this.preservationStatus = false;
                return $toast.success('保存失败');
            })

        },

        getImageInfo(){
            return new Promise((resolve, reject)=>{

                if (this.posterPath) {
                    return resolve(this.posterPath);
                } else {
                    uni.getImageInfo({
                        src: this.poster,
                        success:(res)=>{
                            this.posterPath = res.path;
                            resolve(this.posterPath);
                        },
                        fail:(fail)=>{
                            reject();
                        }
                    });
                }


            })
        },

        open(){
            if (this.vValue ||  this.loaing) return;

            if (this.poster) {
                this.vValue = true;
            } else {
                this.$request({
                    url:'index/getInviteImg',
                    token:true,
                    failMessage:true,
                    next:({status})=> this.loaing = status,
                    loading:'加载中',
                    cache:true
                }).then((data)=>{
                    this.poster = data.data;
                    this.vValue = true;
                    this.getImageInfo();
                });
            }
        },

        close(){
            if (this.vValue) {
                this.vValue = false;
            }
        }

    }

}