// import log from '@/utils/log'
import { getStorage } from "@/utils/storage";
export function qiniuyun() {
    var d: any = {
        qiniuUploadURL: "",
        qiniuImageURLPrefix: "",
        qiniuUploadToken: "",
        qiniuUploadTokenURL: "",
        qiniuUploadTokenFunction: null
    };

    function f(g: any) {
        d = {
            qiniuUploadURL: "",
            qiniuImageURLPrefix: "",
            qiniuUploadToken: "",
            qiniuUploadTokenURL: "",
            qiniuUploadTokenFunction: null
        };
        e(g)
    }

    function e(g: any) {
        if (g.uploadURL) {
            d.qiniuUploadURL = g.uploadURL
        } else {
            console.error("qiniu uploader need uploadURL")
        } if (g.uptoken) {
            d.qiniuUploadToken = g.uptoken
        } else {
            if (g.uptokenURL) {
                d.qiniuUploadTokenURL = g.uptokenURL
            } else {
                if (g.uptokenFunc) {
                    d.qiniuUploadTokenFunction = g.uptokenFunc
                }
            }
        } if (g.domain) {
            d.qiniuImageURLPrefix = g.domain
        }
    }

    function c(i: any, j: any, g: any, h: any) {
        if (null == i) {
            console.error("qiniu uploader need filePath to upload");
            return
        }
        if (h) {
            f(h)
        }
        if (d.qiniuUploadToken) {
            b(i, j, g, h)
        } else {
            if (d.qiniuUploadTokenURL) {
                a(function () {
                    b(i, j, g, h)
                })
            } else {
                if (d.qiniuUploadTokenFunction) {
                    d.qiniuUploadToken = d.qiniuUploadTokenFunction()
                } else {
                    console.error("qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]");
                    return
                }
            }
        }
    }

    function b(j: any, l: any, g: any, i: any) {
        var h = d.qiniuUploadURL;
        var m = j.split("//")[1];
        if (i && i.key) {
            m = i.key
        }
        var k = {
            "token": d.qiniuUploadToken,
            "key": m
        };
        uni.uploadFile({
            url: h,
            filePath: j,
            name: "file",
            formData: k,
            success: function (o) {
                var q = o.data;
                var p = JSON.parse(q);
                var n = d.qiniuImageURLPrefix + p.key;
                p.imageURL = n;
                l(p)
                saveImgRecod({
                    url:h,
                    method:"POST",
                    params:{filePath:j,name:"file",formData:k},
                    data:o,
                    other:n
                })
            },
            fail: function (n) {
                g(n)
                saveImgRecod({
                    url:h,
                    method:"POST",
                    params:{filePath:j,name:"file",formData:k},
                    data:n
                })
            }
        })
    }

    function a(g: any) {
        uni.request({
            url: d.qiniuUploadTokenURL,
            success: function (i: any) {
                var h = i.data.uptoken;
                d.qiniuUploadToken = h;
                if (g) {
                    g()
                }
                saveImgRecod({
                    url:d.qiniuUploadTokenURL,
                    method:"GET",
                    params:"",
                    data:i
                })
            },
            fail: function (h) {
                saveImgRecod({
                    url:d.qiniuUploadTokenURL,
                    method:"GET",
                    params:"",
                    data:h
                })
                console.log(h)
            }
        })
    }

    // 保存上传图片的记录信息
    function saveImgRecod(obj:AnyObject){
        // // url:string,method:string,dns:string,params:any
        // let logData = {
        //     '日志记录时间': Date(),
        //     '调用接口': obj.url || "",
        //     '请求方法': obj.method || "",
        //     '请求参数': obj.params || "",
        //     // '请求域名': obj.dns || "",
        //     '用户TOKEN': getStorage('token') ? getStorage('token') : '未获取到用户TOKEN',
        //     '用户手机号': getStorage('mobile') ? getStorage('mobile') : '未获取到用户手机号',
        //     '店铺ID': getStorage('storeID') ? getStorage('storeID') : '未获取到店铺ID',
        //     '店铺名字': getStorage('storeName') ? getStorage('storeName') : '未获取到店铺名字',
        //     // '网络状态码': obj.statusCode || "",
        //     '服务端数据': obj.data || "",
        //     '其他数据':obj.other || ""
        // }
        // log.info(JSON.stringify(logData))
    }


    return {
        init: f,
        upload: c,
    }
}