123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import Vue from 'vue'
- import Vuex from 'vuex'
- Vue.use(Vuex)
- import {code2Session, logout, checkLogin} from '@/api/login.js'
- import {getLookUpDatas} from '@/api/data.js'
- let lifeData = {};
- try{
-
- lifeData = uni.getStorageSync('lifeData');
- }catch(e){
-
- }
- let saveStateKeys = ['vuex_carBrandList'];
- const saveLifeData = function(key, value){
-
- if(saveStateKeys.indexOf(key) != -1) {
-
- let tmp = uni.getStorageSync('lifeData');
-
- tmp = tmp ? tmp : {};
- tmp[key] = value;
-
- uni.setStorageSync('lifeData', tmp);
- }
- }
- const store = new Vuex.Store({
- state: {
-
- forcedLogin: false,
- hasLogin: false,
- isOpenLocation: false,
-
-
- vuex_allLookUp: [],
- vuex_paymentTypeList: [],
- vuex_userInfo: null,
- vuex_openid: ""
- },
- getters:{
- getOpeid(state){
- return state.vuex_openid
- }
- },
- mutations: {
- login(state, data) {
- console.log(data)
- state.hasLogin = true;
- uni.setStorageSync('token', data.token);
- uni.setStorageSync('userInfo', data.customer);
- state.vuex_userInfo = data.customer
- },
- logout(state) {
- state.hasLogin = false;
- uni.removeStorageSync('token')
- uni.removeStorageSync('userInfo')
- state.vuex_userInfo = null
- },
- openId(state,id) {
- state.vuex_openid = id
- uni.setStorageSync('openid', id);
- },
- $uStore(state, payload) {
-
- let nameArr = payload.name.split('.');
- let saveKey = '';
- let len = nameArr.length;
- if(nameArr.length >= 2) {
- let obj = state[nameArr[0]];
- for(let i = 1; i < len - 1; i ++) {
- obj = obj[nameArr[i]];
- }
- obj[nameArr[len - 1]] = payload.value;
- saveKey = nameArr[0];
- } else {
-
- state[payload.name] = payload.value;
- saveKey = payload.name;
- }
-
- saveLifeData(saveKey, state[saveKey])
- },
- isOpenLocation(state,isOpenLocation){
- state.isOpenLocation = isOpenLocation
- },
- setHasLogin(state,isLogin){
- state.hasLogin = isLogin;
- state.vuex_userInfo = uni.getStorageSync('userInfo')
- }
- },
- actions: {
- wxLogin(context,provider){
- uni.login({
- provider: provider,
- success(result) {
- console.log(result)
- if (result.code) {
- code2Session({
- code: result.code
- }).then(res => {
- console.log(res)
- if (res.status === '200') {
- context.commit('openId',res.data)
- };
- });
- }
- }
- });
- },
-
- userLogout(context,data){
- logout().then(res => {
- console.log(res,'out')
- if(res.status == 200){
- context.commit('logout')
- uni.navigateTo({
- url: '/pages/login/login'
- });
- }
- })
- },
-
- checkLogin(context,callback){
- console.log(callback)
- checkLogin().then(res => {
- if(res.status == '200'){
- context.commit('setHasLogin',true)
- callback&&callback()
- }else{
- context.commit('logout')
- }
- })
- },
-
- checkIsOpenLocation(context,data) {
- uni.authorize({
- scope: 'scope.userLocation',
- success:()=> {
-
- const systemInfo = uni.getSystemInfoSync()
- if(systemInfo.locationAuthorized) {
- context.commit('isOpenLocation', true)
- } else {
- context.commit('isOpenLocation', false)
- }
- },
- fail() {
- uni.getSetting({
- success(res) {
- if(!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userLocationBackground']){
- context.commit('isOpenLocation', false)
- } else {
- context.commit('isOpenLocation', true)
- }
- },
- fail() {
- context.commit('isOpenLocation', false)
- }
- })
-
- }
- })
- },
- }
- })
- export default store
|