import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex) // vue的插件机制
let userInfo = uni.getStorageSync('userInfo')
// Vuex.Store 构造器选项
const store = new Vuex.Store({
    // 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
    state: {
        active: 4,
        token: uni.getStorageSync('token') || '',
        // 用户信息
        userInfo: userInfo && JSON.parse(userInfo) || {}
    },
    getters: {
        // 获取用户信息
        getActive(state) {
            return state.active
        },
        getToken(state) {
            return state.token || uni.getStorageSync('token')
        }
    },
    mutations: {
        // 修改用户信息
        
        changeUseinf(state, payload) {
            payload().then(res => {
                console.log('wxLoginRes', res, state);
                state.userInfo = res
            })
            // state.userInfo = payload
        },
        changeActive(state, payload) {
            state.active = payload
        },
        wxLogininfo(state, payload) {
            console.log('wxLogin', state, payload)
            uni.login({
                provider: 'weixin', //使用微信登录
                success: function ({ code }) {
                    console.log('loginRes', code);
                    payload({
                        code: code
                    }).then(res => {
                        console.log('wxLoginRes', res, code);
                        uni.$u.toast("登录成功");
                        state.token = res.token;
                        state.userInfo = res.user;
                        uni.setStorageSync('token', res.token);
                        uni.setStorageSync('userInfo', JSON.stringify(res.user));
                        uni.$u.route("/pages/home/webview",
                            {
                            url: 'https://sit-nanhuacs.4dage.com/web/index.html#/home',
                            data: 20
                        });
                    })
                }
            });
        }
    }
})

export default store
