import Vue from 'vue'
import { mapState,mapMutations} from 'vuex'
import authorize from '@/common/config/authorize.js'
const appMixins = {
	data() {
	    return {
			authParams: {
			    code: '',
			    encryptedData: '',
			    iv: ''
			}
	    }
	},
    computed: {
        ...mapState(['hasLogin','isWxAuthorize'])
    },
    methods: {
        ...mapMutations(['login','logout','updateNoticeNum']),
        // 授权登录
        async authApplets() {
            try {
                const wechatCode = await authorize.getCode('weixin')
                const getUserInfo = await authorize.getUserInfo('weixin')
                this.authParams.code = wechatCode
                this.authParams.encryptedData = getUserInfo.encryptedData
                this.authParams.iv = getUserInfo.iv
                //授权登录
                const res = await this.UserService.UserLoginAuthApplets(this.authParams)
                this.userId = res.data.userId
                this.$store.commit('updateStatus', res.data)
                this.login(res.data)
                uni.setStorageSync('token', res.data.token)
                uni.setStorageSync('unionId', res.data.unionId)
            } catch (error) {
                this.logout()
                uni.setStorageSync('unionId', error.data.unionId)
                this.$store.commit('updateStatus', error.data)
            }
        },
        appUpdataRefresh(){
            let TIME = (20*60)*1000
            setInterval(()=>{
                this.authApplets()
            },TIME)
        },
        diffTime(t){
            let date = Date.now()
            return (date -t) < 2*60*1000 ? false : true
        },
        async initSetSystemInfo() {
            let self = this
            uni.getSystemInfo({
                success: function(e) {
                    let modelmes = e.model
                    console.log('System',e)
                    if (modelmes.search('iPhone 11') !== -1 || modelmes.search('iPhone 11 Pro Max') !==
						-1 || modelmes.search('iPhone X') != -1) { //XS,XR,XS MAX均可以适配
                        self.$store.dispatch('setVariableFun', true)
                    } else {
                        self.$store.dispatch('setVariableFun', false)
                    }
                    // #ifndef MP
                    Vue.prototype.StatusBar = e.statusBarHeight
                    if (e.platform == 'android') {
                        Vue.prototype.CustomBar = e.statusBarHeight + 50
                        Vue.prototype.platformClass = true
                    } else {
                        Vue.prototype.CustomBar = e.statusBarHeight + 45
                        Vue.prototype.platformClass = false
                    };
                    // #endif
                    // #ifdef MP-WEIXIN || MP-QQ
                    console.log(e.platform)
                    if (e.platform == 'android') {
                        Vue.prototype.platformClass = 'left'
                        self.$store.dispatch('setVariableFun', false)
                    } else {
                        Vue.prototype.platformClass = 'center'
                        self.$store.dispatch('setIsIphoneFun', true)
                    }
                    Vue.prototype.StatusBar = e.statusBarHeight
                    Vue.prototype.fontSizeSetting = e.fontSizeSetting
                    Vue.prototype.screenWidth = e.screenWidth
                    let capsule = wx.getMenuButtonBoundingClientRect()
                    Vue.prototype.capsule = capsule
                    if (capsule) {
                        Vue.prototype.Custom = capsule
                        Vue.prototype.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight
                    } else {
                        Vue.prototype.CustomBar = e.statusBarHeight + 50
                    }
                    // #endif		
                    // #ifdef MP-ALIPAY
                    Vue.prototype.StatusBar = e.statusBarHeight
                    Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight
                    // #endif
                }
            })
        }
    }
}

export default appMixins
