import http from '../plugins/http';
export default {
    computed: {
        isLogin() {
            return !!this.$store.state.userInfo;
        },
        authStatus() {
            let status = this.$store.state.userInfo?.authStatus;

            return this.AuthStatus.has(status) ? this.AuthStatus.get(status) : '未认证';
        }
    },
    data() {
        return {
            AuthStatus: new Map([
                ['NOT_AUTH', '未认证'],
                ['PENDING', '认证中'],
                ['SUCCESS', '已认证'],
                ['FAIL', '认证失败']
            ])
        };
    },
    methods: {
        updateUser(info, sucess = true) {
            if (info) {
                return this.$http
                    .post(
                        '/user/save',
                        {
                            ...this.$store.state.userInfo,
                            ...info
                        },
                        {
                            body: 'json'
                        }
                    )
                    .then(() => {
                        return this.$store.dispatch('getUserInfo');
                    })
                    .then(() => {
                        if (sucess) {
                            this.$toast.success('更新成功');
                        }
                        return Promise.resolve();
                    })
                    .catch(e => {
                        if (e) {
                            this.$toast(e.error);
                        }
                        return Promise.reject();
                    });
            }
        },
        updateFile(e, size = 2000) {
            const formData = new FormData();
            formData.append('file', e.file, e.file.name);
            formData.append('compress', true);
            formData.append('width', size);
            formData.append('height', size);
            return http.axios.post('/upload/file', formData).then(res => {
                return Promise.resolve(res.data);
            });
        },
        getImg(imgs = '', type = '', size = 800) {
            if (!imgs) {
                imgs = '';
            }
            if (!(imgs instanceof Array)) {
                imgs = imgs.split(',');
            }

            imgs = imgs.filter(item => {
                return !!item;
            });
            if (imgs.length > 0) {
                let img = type ? imgs[0][type] : imgs[0];
                // console.log(img);
                return img + (/\.gif$/i.test(img) ? '' : `?x-oss-process=image/resize,h_${size},m_lfit`);
            } else {
                return '';
            }
        },
        accAdd(arg1, arg2) {
            var r1, r2, m;
            try {
                r1 = arg1.toString().split('.')[1].length;
            } catch (e) {
                r1 = 0;
            }
            try {
                r2 = arg2.toString().split('.')[1].length;
            } catch (e) {
                r2 = 0;
            }
            m = Math.pow(10, Math.max(r1, r2));
            return (arg1 * m + arg2 * m) / m;
        },
        checkBank() {
            return this.$http
                .get('/user/myBankCard')
                .then(res => {
                    if (res.length > 0) {
                        return Promise.resolve();
                    } else {
                        return Promise.reject();
                    }
                })
                .catch(() => {
                    return this.$dialog
                        .confirm({
                            title: '银行卡未绑定',
                            message: '您的账户还未绑定银行卡',
                            confirmButtonText: '前往绑定'
                        })
                        .then(() => {
                            this.$router.push('/mineBanks');
                            return Promise.reject();
                        });
                });
        },
        checkAuth() {
            if (this.authStatus === '已认证') {
                return Promise.resolve();
            } else {
                this.$dialog
                    .confirm({
                        title: '实名认证',
                        message: '您的账户还通过实名认证，认证后可进行添加银行卡',
                        confirmButtonText: '前往认证'
                    })
                    .then(() => {
                        if (this.authStatus === '认证中' || this.authStatus === '认证失败') {
                            this.$router.push('/waiting');
                        } else if (this.authStatus === '未认证') {
                            this.$router.push('/Authentication');
                        }
                    });
                return Promise.reject();
            }
        },
        checkLogin() {
            if (this.isLogin) {
                return Promise.resolve();
            } else {
                this.$dialog
                    .confirm({
                        title: '提示',
                        message: '用户未登录,是否立即登录',
                        confirmButtonText: '立即登录'
                    })
                    .then(() => {
                        this.$router.push('/login');
                    });
                return Promise.reject();
            }
        },
        getLabelName(val = '', list = []) {
            let info = list.find(item => {
                return item.value === val;
            });

            return info ? info.label : '';
        },
        scrollRefreash() {
            this.$toast.clear();
        },
        wait() {
            this.$toast('敬请期待');
        }
    }
};
