<!--文件懒加载控件（集成了龙嘉协议获取图片的方式）-->
<template>
    <van-image style="max-width: 100%;" :src="currentSrc" 
        :fit="fit"  :round="Round" :radius="RadiusValue"
        @click="imageClick">
    </van-image>
</template>

<script>
    export default {
        name: "lj-img-vant",
        props: {
            // 龙嘉协议的图片md5值
            md5: {
                Type: String,
                default: ""
            },
            // 普通http路径
            src: {
                Type: String,
                default: ""
            },
            fit:{
                Type:String,
                default:"contain"
                // contain	保持宽高缩放图片，使图片的长边能完全显示出来
                // cover	保持宽高缩放图片，使图片的短边能完全显示出来，裁剪长边
                // fill	拉伸图片，使图片填满元素
                // none	保持图片原有尺寸
                // scale-down	取none或contain中较小的一个
            },
            Round:{
                Type:Boolean,
                default:false
            },
            Radius:{
                Type:String,
                default:'0'
            }
        },
        data() {
            return {
                currentSrc: "",
                RadiusValue: ""
            }
        },
        mounted() {

            if(this.Round){
              this.RadiusValue = '50%';
            }
            else{
                this.RadiusValue = this.Radius;
            }

            let self = this;




            if (this.src) {
                //this.currentSrc = this.src;
                api.imageCache({
                    url: this.src,
                    policy: "cache_only",
                    thumbnail: false
                }, function (ret, err) {
                    if (!err) {
                        self.getBase64(ret.url)
                    }else{
                        alert(JSON.stringify(err));
                    }
                    console.log(ret.status + "\r\n" + ret.url);
                })
            } else if (this.md5) {
                $lj.getCacheDir(function (cacheDir) {
                    let path = cacheDir + "/" + self.md5;
                    var fs = $lj.getFileHelperModule();
                    console.warn(path);
                    fs.exist({
                        path: path
                    }, function (ret, err) {
                        if (ret.exist) {
                            self.getBase64(path);
                        } else {
                            $lj.postLJRequest("GetCacheFile", {filemd5: self.md5}, function (ret) {
                                var cacheDir = $lj.constant.cacheDir + "/";
                                let newPath = cacheDir + "/" + self.md5;
                                if (ret.filedata !== self.md5) {
                                    let oldPath = cacheDir + "/" + ret.filedata;
                                    console.error(`!!!!!!!!  lj-img:renaming ${ret.filedata} to ${self.md5}  !!!`);
                                    fs.rename({
                                        oldPath: oldPath,
                                        newPath: newPath
                                    }, function (ret, err) {
                                        if (ret.status) {
                                            self.getBase64(newPath);
                                        } else {
                                            console.error("图片重命名: " + JSON.stringify(err));
                                        }
                                    });
                                } else {
                                    self.getBase64(newPath);
                                }
                            });
                        }
                    });
                });
            }
        },
        methods: {
            imageClick(item) {
                this.$emit('image-click', item);
            },
            getBase64(path) {
                this.currentSrc = path;
                return;
                //热加载时使用
                console.log(path);
                var trans = $lj.getBase64TranModule();
                let self = this;
                trans.decodeImgToBase64({
                    imgPath: path
                }, function (ret, err) {
                    if (ret.status) {
                        console.log(JSON.stringify(ret));
                        var str = ret.base64Str;
                        self.currentSrc = "data:image/png;base64," + str;
                    } else {
                        alert(JSON.stringify(err));
                    }
                });
            },
        }
    }
</script>