<template>
    <app-layout>

    </app-layout>
</template>

<script>
    import appLayout from "@/components/app-layout"
    export default {
        components:{
            appLayout,
        },
        data() {
            return {
                redirect: ''
            }
        },
        methods: {
            login(){
                let _this = this;
                uni.showLoading({title: '登录中...'})
                uni.login({
                    provider: 'weixin',
                    success: res => {
                        uni.getUserInfo({
                            provider: 'weixin',
                            success: data =>  {
                                let params = {
                                    code: res.code,
                                    encryptedData: data.encryptedData,
                                    iv:data.iv,
                                    signature:data.signature
                                }
                                _this.$u.api.login(params).then(loginRes => {
                                    _this.$u.vuex(_this.$const.USER_TOKEN,loginRes.token)
                                    _this.$u.vuex(_this.$const.USER_DATA,loginRes.user)
                                    uni.hideLoading()
                                    uni.reLaunch({
                                        url: _this.redirect
                                    });
                                })
                            }
                        });
                    }
                });
            },
        },
        onLoad(options) {
            let redirect = options.redirect;
            delete options.redirect;
            let arr = [];
            for (const key in options) {
                arr.push(`${key}=${options[key]}`)
            }
            this.redirect = `/${redirect}?${arr.join("&")}`
            if(!this.redirect){
                this.redirect = '/pages/index/index'
            }
            if(!this.vuex_user_token){
                this.login()
            }
        }
    }
</script>

<style lang="scss" scoped>

</style>
