export default {
    data() {
        return {
            timeout: undefined
        };
    },
    onLoad() {
        const app = getApp();
        if (this.loginMethods) {
            if (app.globalData.initialize) {
                this.loginMethods();
            } else {
                app.globalData.initializeCallback = () => {
                    this.loginMethods();
                };
            }
        }
    },
    computed: {
        isLogin() {
            return !!this.$store.state.userInfo;
        }
    },
    methods: {
        debounce(fn, wait) {
            if (this.timeout) clearTimeout(this.timeout);
            this.timeout = setTimeout(() => {
                fn();
            }, wait);
        },
        navigateTo(url) {
            wx.navigateTo({
                url: url
            });
        },
        // navigateTo(url, checkLogin = true) {
        //     if (checkLogin) {
        //         this.checkLogin().then(() => {
        //             if (url) {
        //                 wx.navigateTo({
        //                     url: url
        //                 });
        //             }
        //         });
        //     } else {
        //         if (url) {
        //             wx.navigateTo({
        //                 url: url
        //             });
        //         }
        //     }
        // },
        checkLogin() {
            if (!this.isLogin) {
                wx.navigateTo({
                    url: '/pages/authorized'
                });
                return Promise.reject('未登录');
            }
            return Promise.resolve();
        },
        navigateBack(num = 1) {
            wx.navigateBack({
                delta: num
            });
        },
        preview(img, urls = []) {
            if (!img) {
                return;
            }
            if (urls.length === 0) {
                urls = [img];
            }

            wx.previewImage({
                current: img, // 当前显示图片的http链接
                urls: urls // 需要预览的图片http链接列表
            });
        },
        choosePhoto() {
            return new Promise((resolve, reject) => {
                wx.chooseImage({
                    count: 1, // 默认9
                    sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                    success: res => {
                        const src = res.tempFilePaths[0];
                        this.$http
                            .uploadFile(src)
                            .then(res => {
                                resolve(res);
                            })
                            .catch(e => {
                                reject(e);
                            });
                    }
                });
            });
        },
        phoneCall(phone = '18366668888') {
            wx.makePhoneCall({
                phoneNumber: phone
            });
        },
        goHome() {
            wx.switchTab({
                url: '/pages/Home'
            });
        },
        getNumStr(num) {
            if (num < 10) {
                return '0' + num;
            } else {
                return num;
            }
        },
        toast(title, icon = 'none', duration = 1500) {
            wx.showToast({
                icon: icon,
                title: title,
                mask: true,
                duration: duration
            });
        },
        showLoading() {
            wx.showLoading({
                title: '加载中...'
            });
        },
        hideLoading() {
            wx.hideLoading();
        },
        wait() {
            this.toast('敬请期待');
        },
        getLogo(img) {
            return img ? img + '?x-oss-process=image/circle,r_300/format,png' : '/native/imgs/defaultLogo.png';
        },
        goInLive(roomId) {
            if (roomId) {
                wx.navigateTo({
                    url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}`
                });
            }
        }
    }
};
