/**
 * @name 登陆注册逻辑
 * @description 登陆注册文件代码
 */
import config from '../config/index';

// 响应字段处理对象
const responseFuns = {
    0(res) {
        // 登陆成功
        const { accessToken, userInfo, roles = [] } = res.data.data;
        uni.setStorageSync('isLogin', true);
        uni.setStorageSync('token', accessToken);
        uni.setStorageSync('userInfoData', userInfo);
        uni.setStorageSync('userRoles', roles[0]);
        return true;
    },
    401() {
        login();
    },
    403(_res) {
        uni.showToast({
            icon: 'none',
            title: res.data.msg
        });
    },
    500() {
        // 500 登陆失败
        uni.setStorageSync('isLogin', false);
    },
    1002000008() {
        // 1002000008 手机号未注册，登陆失败跳转到重新注册授权页面
        uni.showToast({
            icon: 'none',
            title: '该手机号未注册，正在跳转至授权登陆...',
            mask: true,
            duration: 2000
        });
        setTimeout(() => {
            uni.navigateTo({
                url: '/pages/phone/index'
            });
        }, 2000);
    },
    1002003003() {
        // 1002003003 用户不存在，跳转注册
        uni.showToast({
            icon: 'none',
            title: '用户不存在，正在跳转至授权登陆...',
            mask: true,
            duration: 2000
        });
        setTimeout(() => {
            uni.navigateTo({
                url: '/pages/phone/index'
            });
        }, 2000);
    },
    default(_res) {
        uni.showToast({
            icon: 'none',
            title: _res.data.msg
        });
    }
};

// 登陆
const login = async function () {
    // 获取code到后端换取token
    const { code } = await uni.login();
    const loginParam = {
        url: `${config.env.baseUrl}${config.env.loginPath}`,
        method: 'POST',
        data: {
            code
        }
    };

    // 调用后台等里接口
    try {
        const res = await uni.request(loginParam);
        if (res.statusCode === 200) {
            if (typeof responseFuns[res.data.code] === 'function') {
                return responseFuns[res.data.code](res);
            }
            return responseFuns['default'](res);
        } else {
            uni.setStorageSync('isLogin', false);
            uni.showToast({
                title: '登录请求失败'
            });
        }
        return false;
    } catch (err) {
        console.log('CatchClause', err);
        console.log('CatchClause', err);
        uni.setStorageSync('isLogin', false);
        uni.showToast({
            title: '登录请求出错'
        });
        return false;
    }
};

/**
 * 注册
 * @param {Object} data
 */
const register = async function (data = {}) {
    const registerParam = {
        url: `${config.env.baseUrl}${config.env.loginPath}`,
        method: 'POST',
        data
    };
    try {
        const res = await uni.request(registerParam);
        if (res.statusCode === 200) {
            if (typeof responseFuns[res.data.code] === 'function') {
                return responseFuns[res.data.code](res);
            }
        } else {
            uni.setStorageSync('isLogin', false);
            uni.showToast({
                title: '登录请求失败'
            });
        }
        return false;
    } catch (err) {
        console.log('CatchClause', err);
        console.log('CatchClause', err);
        uni.setStorageSync('isLogin', false);
        uni.showToast({
            icon: 'error',
            title: '登录/注册失败出错了!'
        });
        return false;
    }
};
export default {
    data() {
        return {};
    },
    login,
    register,
    responseFuns
};
