<!--文件懒加载控件（集成了龙嘉协议获取图片的方式）-->
<template>
<div style="width:100%;height:100%;margin-bottom:5px;">
    <div style="width:100%;height:100%" @click.prevent="photoBrowser">
        <img :src="currentSrc" style="max-width: 100%;max-height: 100%;"/>
    </div>
    <div style="margin-top:5px;"><a @click.prevent="download">图片下载</a></div>
</div>
</template>

<script>
    export default {
        name: "lj-img",
        props: {
            // 龙嘉协议的图片md5值
            md5: {
                Type: String,
                default: ""
            },
            // 普通http路径
            src: {
                Type: String,
                default: ""
            },
            fileType: String,
        },
        data() {
            return {
                currentSrc: ""
            }
        },
        mounted() {
            let self = this;
            if (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  + "." + self.realFileType;
                    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 + "." + self.realFileType;
                                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: {
            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));
                    }
                });
            },
            download(){
                api.saveMediaToAlbum({
                    path: this.currentSrc
                }, function(ret, err) {
                    if (ret && ret.status) {
                        alert('保存成功');
                    } else {
                        alert('保存失败');
                    }
                });
            },
            photoBrowser() {
                var images = [];
                var url = this.currentSrc;
                images.push(url);
                $lj.photoBrowser(images, 0);
            },
        },
        computed: {
            realFileType() {
                let type = this.fileType || (this.fileFj ? this.fileFj.fileType : "");
                if (type) {
                    let dotIndex = type.indexOf('.');
                    if (dotIndex >= 0) {
                        type = type.substring(type.indexOf('.') + 1);
                    }
                }
                return type.replace(/^\s+|\s+$/g,"");;
            },
        }
    }
</script>