import store from '@/store/index.js'
import authorize from '@/common/authorize.js'
import ajaxService from '@/services/ajax.service.js'
import UserService from '@/services/user.service'

const newUserService = new UserService(ajaxService)

// 根据微信的code获取用户登录状态:1已登录过 -1未登录过
const wxLoginAuthorize = async function() {
    const wechatCode = await authorize.getCode('weixin') // 根据微信的code获取用户登录状态:1已登录过 -1未登录过
    newUserService.UserWechatAuthorLogin({
        code: wechatCode
    })
        .then(response => {
            const _data = JSON.parse(response.data)
            store.commit('app/updateStatus', _data)
            store.commit('app/login', _data)
            uni.setStorageSync('openId', _data.openId)
        })
        .catch(error => {
            const _data = JSON.parse(error.data)
            uni.setStorageSync('openId', _data.openId)
            store.commit('app/logout', _data)
            store.commit('app/updateStatus', _data)
        })
}
const wxLoginQuick = async function() { // 根据微信的code获取用户登录状态:1已登录过 -1未登录过跳转
    const wechatCode = await authorize.getCode('weixin') // 根据微信的code获取用户登录状态:1已登录过 -1未登录过
    const getUserInfo = await authorize.getUserInfo('weixin')
    const params =
        newUserService.UserWechatAuthorLogin({
            code: wechatCode
        })
            .then(response => {
                const _data = JSON.parse(response.data)
                store.commit('app/updateStatus', _data)
                store.commit('app/login', _data)
                uni.setStorageSync('openId', _data.openId)
            })
            .catch(error => {
                const _data = JSON.parse(response.data)
                uni.setStorageSync('openId', _data.openId)
                store.commit('app/logout', _data)
                store.commit('app/updateStatus', _data)
            })
}
export default {
    wxLoginAuthorize,
    wxLoginQuick
}
