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.$message.success('更新成功');
                        }
                        return Promise.resolve();
                    })
                    .catch(e => {
                        if (e) {
                            this.$message.error(e.error);
                        }
                        return Promise.reject();
                    });
            }
        },
        // updateFile(e) {
        //     const formData = new FormData();
        //     formData.append('file', e.name);
        //     return http.axios.post('/upload/file', formData).then(res => {
        //         return Promise.resolve(res.data);
        //     });
        // },
        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;
        },
        updateFile(e, size = 2000) {
            console.log(e);
            const formData = new FormData();
            formData.append('file', e, e.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);
            });
        },
        all(link) {
            this.$router.push(link);
        },
        getImg(imgs = '', type = '', size = 300) {
            // console.log(imgs);
            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 '';
            }
        },
        checkLogin() {
            if (this.isLogin) {
                return Promise.resolve();
            } else {
                this.$confirm('用户未登录,是否立即登录', '提示', {
                    confirmButtonText: '立即登录'
                }).then(() => {
                    this.$EventBus.$emit('login');
                });
                return Promise.reject();
            }
        },
        //去认证
        checkAuth() {
            if (this.authStatus === '已认证') {
                return Promise.resolve();
            } else {
                this.$confirm('用户未认证,是否立即认证', '提示', {
                    confirmButtonText: '立即认证'
                }).then(() => {
                    this.$router.push('/authentication');
                });
                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.$message.warning('敬请期待');
        }
    }
};
