const QR = require('../../miniprogram_npm/qrcode-base64/index.js');
import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel();
import {
    base64src
} from './base64src';
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        title: {
            type: String
        },
        codeName: {
            type: String
        },
        tactivitysignupid: {
            type: String
        }
    },
    lifetimes: {
        attached: function () {
            if (this.data.title == '我的身份码') {
                let codeText = this.data.tactivitysignupid;
                this.qrCode(codeText)
            } else {
                _Http.basic({
                    "accesstoken": wx.getStorageSync('userData').token,
                    "classname": "customer.tagents.tagents",
                    "method": "query_enterpriseAgentsMain",
                    "content": {}
                }).then(res => {
                    if (res.msg != '成功') return wx.showToast({
                        title: res.msg,
                        icon: "none"
                    });
                    let codeText = '';
                    if (this.data.title == '商户二维码') {
                        codeText = 'https://www.buwanjia.com/qrcode?type=shop&fbrand=' + res.data[0].fbrand + '&tagentsid=' + res.data[0].tagentsid;
                    } else {
                        codeText = 'https://www.buwanjia.com/qrcode?type=partner&fbrand=' + res.data[0].fbrand + '&tagentsid=' + res.data[0].tagentsid;
                    };
                    this.qrCode(codeText)
                });
            }
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },
    /**
     * 组件的初始数据
     */
    data: {
        qrcodeURL: "",
    },

    /**
     * 组件的方法列表
     */
    methods: {
        qrCode(codeText) {
            var imgData = QR.drawImg(codeText, {
                typeNumber: 4,
                errorCorrectLevel: 'M',
                size: 500
            })
            base64src(imgData, res => {
                this.setData({
                    qrcodeURL: res
                });
            });
        },
        /* 预览大图 */
        preViewImage() {
            const url = [this.data.qrcodeURL];
            wx.previewImage({
                urls: url,
            })
        },
        /* 下载二维码 */
        download() {
            const that = this;
            wx.authorize({
                /* 这个就是保存相册的 */
                scope: 'scope.writePhotosAlbum',
                success() {
                    /* 保存图片方法 */
                    that.img();
                },
                complete(res) {
                    /* 这里判断一下如果没有授权重新打开设置选项 */
                    wx.getSetting({
                        success(res) {
                            if (!res.authSetting['scope.writePhotosAlbum']) {
                                /* 打开设置的方法 */
                                that.opensit();
                            }
                        }
                    });
                }
            });
        },
        /* 授权提示 ，这里就是重复提示用户去授权*/
        opensit() {
            wx.showModal({
                content: '由于您还没有允许保存图片到您相册里,这无法进行分享操作点击确定去允许授权',
                success: function (res) {
                    if (res.confirm) {
                        /* 这个就是打开设置的API*/
                        wx.openSetting({
                            success(res) {
                                console.log(res.authSetting);
                            }
                        });
                    } else if (res.cancel) {
                        wx.showModal({
                            cancelText: '依然取消',
                            confirmText: '重新授权',
                            content: '很遗憾你点击了取消，这将无法进行分享操作，请慎重考虑',
                            success: function (res) {
                                if (res.confirm) {
                                    wx.openSetting({
                                        success(res) {
                                            console.log(res.authSetting);
                                        }
                                    });
                                } else if (res.cancel) {
                                    console.log('用户不授权');
                                }
                            }
                        });
                    }
                }
            });
        },
        /* 特别注意要先获取图片信息在进行保存，不让保存不了 */
        img() {
            const that = this;
            /* 保存图片到相册 */
            wx.saveImageToPhotosAlbum({
                filePath: that.data.qrcodeURL,
                success: function () {
                    console.log('save success');
                    wx.showModal({
                        title: '保存成功',
                        content: '图片已成功保存到相册，快去分享到您的圈子吧',
                        showCancel: false
                    });
                },
                complete(res) {
                    console.log(res);
                }
            });
        }
    }
})