1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import Vue from 'vue'
- import Vuex from 'vuex'
- import {apiWXLogin,apiLoginOut} from '@/api/user.js'
- import uniAsync from '@/utils/uni-async.js'
- Vue.use(Vuex);//vue的插件机制
- const token=uni.getStorageSync('token')||''
- let userInfo={}
- if(uni.getStorageSync('userInfo')){
- userInfo=JSON.parse(uni.getStorageSync('userInfo'))
- }
- //Vuex.Store 构造器选项
- const store = new Vuex.Store({
- state:{//存放状态
- token:token,
- userInfo:userInfo,
- },
- mutations:{
- // 添加token
- addToken(state,data){
- state.token=data
- uni.setStorageSync('token',data)
- },
- // 添加用户信息
- addUserData(state,data){
- state.userInfo=data
- uni.setStorageSync('userInfo',JSON.stringify(data))
- },
- // 清空用户信息和token
- removeUser(state){
- state.token='',
- state.userInfo={}
- uni.reLaunch({
- url:'/pages/login/login'
- })
- }
- },
- actions:{
- // 微信登录
- async WXLogin(context){
-
- let wxLoginRes=await uniAsync.login({
- provider:'weixin'
- })
-
- const res=await apiWXLogin({Code:wxLoginRes.code})
- if(res.code===200){
- context.commit('addToken',res.data.Authorization)
- context.commit('addUserData',res.data.UserInfo)
- }
- },
-
- // 退出登录
- async loginOut(context){
- const res=await apiLoginOut()
- if(res.code===200){
- uni.clearStorageSync()
- context.commit('removeUser')
- }
- }
- }
- })
- export default store
|