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) {
            const formData = new FormData();
            formData.append('file', e.file, e.file.name);
            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) {
            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];
                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();
            }
        },
        getLabelName(val = '', list = []) {
            let info = list.find(item => {
                return item.value === val;
            });

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