/**
 * Web唐明明
 * 匆匆数载恍如梦，岁月迢迢华发增。
 * 碌碌无为枉半生，一朝惊醒万事空。
 * moduleName: 登录
 */

import {
    router
} from '../router'
import {
    keyAuth
} from '../apis/interfaces/auth'
import store from '../store'
import {
    getImToken
} from '@/apis/interfaces/im.js'
import im from '@/utils/im/index.js'

class userAuth {
    constructor() {
        this.univerfyConfig = {
            fullScreen: true,
            authButton: {
                'title': '一键登录',
                'normalColor': '#34CE98',
                'highlightColor': '#16b17a',
                'disabledColor': '#aae4cc',
            },
            otherLoginButton: {
                'title': '其他手机号码',
                'borderColor': '#34CE98',
                'textColor': '#34CE98'
            },
            privacyTerms: {
                'checkedImage': '/static/icon/checked-icon.png',
                'uncheckedImage': '/static/icon/unchecked-icon.png',
                'textColor': '#999',
                'termsColor': '#34CE98',
                'suffix': '并使用本机号码登录/注册',
                'privacyItems': [{
                    'url': 'https://www.baidu.com',
                    'title': '隐私协议'
                }, {
                    'url': 'https://www.baidu.com',
                    'title': '服务协议'
                }]
            },
            buttons: {
                'iconWidth': '45px',
                'list': [{
                    "provider": '微信登录',
                    "iconPath": '/static/icons/wechat.png',
                }]
            }
        }
    }
    // 预登录
    Login() {
        //#ifdef H5
        router.push({
            name: 'Auth'
        })
        return
        //#endif

        return new Promise((resolve, reject) => {
            uni.showLoading({
                title: '加载中',
                mask: true
            })
            uni.preLogin({
                provider: 'univerify',
                success: res => {
                    this.keyLogin().then(() => {
                        resolve({
                            auth: true
                        })
                    }).catch(errMsg => {
                        reject(errMsg)
                    })
                },
                fail: err => {
                    router.push({
                        name: 'Auth'
                    })
                },
                complete() {
                    uni.hideLoading()
                }
            })
        })
    }
    // 一键登录
    keyLogin() {
        return new Promise((resolve, reject) => {
            uni.login({
                provider: 'univerify',
                univerifyStyle: {
                    ...this.univerfyConfig
                },
                success: authResult => {
                    keyAuth({
                        access_token: authResult.authResult.access_token,
                        openid: authResult.authResult.openid
                    }).then(res => {
                        uni.closeAuthView()
                        store.commit('setToken', res.token_type + ' ' + res
                            .access_token)
                        // 在这里，登录成功，链接IM服务
                        getImToken().then(res => {
                            im.connect(res.token, res.userInfo, () => {})
                        })
                        resolve()
                    }).catch(err => {
                        reject(err)
                    })
                },
                fail: err => {
                    uni.closeAuthView()
                    switch (err.code) {
                        case 30002:
                            router.push({
                                name: 'Auth',
                                params: {
                                    keyPhone: 1
                                }
                            })
                            break
                    }
                }
            })
        })
    }
}

export default userAuth
