//authorize.js
import api from './caimeiApi.js'

//判断是否登陆 通过检测sessionkey是否过期 1未过期 0已过期
const checkSessionKey=()=>{	
    return new Promise((resolve,reject) => {
        const user = uni.getStorageSync('user')// 用户缓存信息
        if(user){
            uni.checkSession({
			    success() {
                    resolve(1) //状态未过期
                }
			    ,fail() {
                    resolve(0) //状态已过期
                }
            })
        }else{
            resolve(2) //未存贮
        }
    })
}

// 登录授权
const login = (params)=> {
    return new Promise((resolve,reject) => {
        authDo(params).then(res=>{
            if ( res.statusCode && res.statusCode != 200 ) {
                reject('网络错误，请检查一下网络');return
            }
            if (res.data.code != 0) {
			    reject('登录失败');return
            }
            let user = res.data.data.user
            uni.setStorageSync('user', user)//储存用户信息到本地
            resolve(user)
        })
    })
}

// 查询用户信息 write by self
const authDo = function(params) {
    return new Promise(function (resolve, reject) {
        uni.request({
            url: api.requestUrl +'/login/doLogin',
            data: params,
            method:'POST',
            header: {
	        		'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res){ resolve(res) },
            fail:function(){ reject('保存用户信息失败') }
        })
    })
}

// 获取服务商信息 uni-app是聚合开发 所以先获得是什么小程序 原生的不用这个
const getProvider = () => {
    return new Promise((resolve, reject) => {
        uni.getProvider({
            service: 'oauth', //服务类型  登录授权
            success: function(res) {resolve(res.provider[0])},
            fail:function() { reject('获取服务商失败') }
        })
    })
}


// 获取code
const getCode = provider => {
    return new Promise((resolve, reject) => {
        if (!provider) { reject('获取缺少provider参数') }
        uni.login({
            provider: provider,
            success: function(loginRes) {
                if (loginRes && loginRes.code) { 
                    resolve(loginRes.code) 
                } else {
                    reject('获取code失败') 
                    uni.navigateTo({
                        url:'/pages/tabBar/home/index'
                    })
                }
            },
            fail:function(error){ 
                console.log('获取code失败',)
                reject('获取code失败')
            }
        })
    })
}

// 是否开启了获取用户名授权 当用户一开始拒绝了 需再次提醒授权
const getSetting = function() {
    return new Promise((resolve,reject) => {
        uni.getSetting({
            success:function(res) {
                let authSetting=res.authSetting
                if(authSetting['scope.userInfo']){resolve(1);return}//授权成功
                if(authSetting['scope.userInfo']===false){resolve(0);return}//拒绝授权
                resolve(2) //2未操作
            },
            fail:function(error) { 
                console.log(error)
                reject('获取用户授权失败') 
            }
        })
    })
}

// 获取用户信息
const getUserInfo = (provider)=>{
    return new Promise( (resolve,reject)=>{
        if (!provider) { reject('获取缺少provider参数');return }
        uni.getUserInfo({
            provider: provider,
            success: (detail) => {
                if(detail.iv != ''){
                    resolve(detail)
                }else{
                    reject(0) //用户点击拒绝授权
                }
            }
            ,fail: (error) => {
                console.log('获取用户信息失败',error)
                reject(0) //如果用户拒绝过授权 直接走fail
            }
        })
    })
}
// 获取用户登录状态
const checkLogin = () => {
    return new Promise((resolve, reject) => {
        uni.checkSession({
            success() {
                console.log('登录成功')
                resolve(1)
            },
            fail(error) {
                console.log('获取用户信息失败',error)
                reject(0)
            }
        })
    })
}

// util.js
const getUserProfile = () =>{
    wx.getUserProfile({
        desc: '用于完善个人资料',
        success: function(res) {
            var userInfo = res.userInfo
            // console.log('userInfo==>', userInfo)
            wx.setStorageSync('storage_info', 1)//本地标记
            //下面将userInfo存入服务器中的用户个人资料
            //...
		  },
        fail() {
            console.log('用户拒绝授权')
        }
    })
}
  

export default {
    getProvider,
    getSetting,
    checkSessionKey,
    getCode,
    login,
    getUserInfo,
    checkLogin,
    getUserProfile
}
