index.ts 10.0 KB
import { createStore } from 'vuex';
import { default as moment } from 'moment';
import { default as _ } from 'lodash';
import * as user_api from '@/api/user_api';
import { nm_live_message, updateLiveDate } from './ws_live/index';
import createPersistedState from 'vuex-persistedstate';

const store = createStore({
  state: {
    // 初始化
    init: false,
    // 列表
    live_path: '', //默认初始化列表路径
    updateListTime: null, // 列表更新时间
    match_list_all_cache: [], // 全部缓存
    match_list_all: [], // 全部
    match_list_fixture: [], // 赛程
    match_list_result: [], // 赛果
    match_list_attention: {}, // 关注
    attention_id_list: [], // 关注id合集
    // 实时
    updateWsTime: null, // ws更新时间
    nm_live_message: {}, // 即时比分
    nm_live_message_cache: {}, // 即时比分缓存
    nm_game_message_cache: {}, // 比赛信息缓存
    live_timer_switch: false, // 定时器开关
    high_light: { score: {}, red: {}, yellow: {} }, // 高亮
    // 时间
    difference_time_switch: false, // 定时器开关
    difference_time: null, // 服务器和客户端时间差
    server_time: moment().valueOf(),  // 服务器时间
    // 设置
    set_config: { switch: true, goal: true, red: true, yellow: true, type: 'all' },
    // 用户|共用信息
    userInfo: {},
    common_data: {
      isPc: false,
      isAndroid: false,
      isIos: false,
      isApp: false,
      isWeChat: false,
      isLocalTest: false,
      switchState: false,
      isQianHai: false,
      appHeight: 0,
    },
    //app调用返回开关
    app_is_back: false,
    //指数 亚盘 欧赔 总进球 角球 我的定制
    my_asia: [],
    my_euro: [],
    my_bs: [],
    my_cr: [],
    // 比赛详情头部数据和专家数据
    match_detail: {},

    // 缓存数据
    game_info_cache: {},
    game_detail_cache: {},
  },
  mutations: {
    // 初始化
    updateInit (state, init) {
      state.init = init;
    },
    // 列表
    updateUpdateListTime (state, updateListTime) {
      state.updateListTime = updateListTime;
    },
    updateMatchListAllCache (state, match_list_all_cache) {
      state.match_list_all_cache = match_list_all_cache;
    },
    updateMatchListAll (state, match_list_all) {
      state.match_list_all = match_list_all;
    },
    updateMatchListFixture (state, match_list_fixture) {
      state.match_list_fixture = match_list_fixture;
    },
    updateMatchListResult (state, match_list_result) {
      state.match_list_result = match_list_result;
    },
    updateMatchListAttention (state: any, match_list_attention) {
      state.match_list_attention = match_list_attention;
      let arr = [];
      for (let i = 0; i < Object.keys(match_list_attention).length; i++) {
        let key = Object.keys(match_list_attention)[i];
        let list = match_list_attention[key];
        for (let y = 0; y < list.length; y++) {
          arr.push(list[y].id);
        }
      }
      state.attention_id_list = arr;
    },
    // 实时
    updateUpdateWsTime (state, updateWsTime) {
      state.updateWsTime = updateWsTime;
    },
    updateLiveTimerSwitch (state, live_timer_switch) {
      state.live_timer_switch = live_timer_switch;
    },
    updateNmLiveMessage (state, nm_live_message) {
      state.nm_live_message = nm_live_message;
    },
    updateNmGameMessageCache (state, nm_game_message_cache) {
      state.nm_game_message_cache = nm_game_message_cache;
    },
    updateNmLiveMessageCache (state, nm_live_message_cache) {
      state.nm_live_message_cache = nm_live_message_cache;
    },
    updateHighLight (state, high_light) {
      state.high_light = high_light;
    },
    // 时间
    updateDifferenceTimeSwitch (state, difference_time_switch) {
      state.difference_time_switch = difference_time_switch;
    },
    updateDifferenceTime (state, difference_time) {
      state.difference_time = difference_time;
    },
    updateServerTime (state, server_time) {
      state.server_time = server_time;
    },
    updateSetConfig (state, set_config) {
      state.set_config = set_config;
    },
    // 用户|共用信息
    get_common_data (state) {
      const ua = window.navigator.userAgent;
      const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua);
      const isAndroid = ua.indexOf('Android') > -1 || ua.indexOf('android') > -1;
      const isIos = !!ua.match(/ipad|iphone|ios/) || (window as any).ios?.navBarHeight;
      const isPc = !isMobile;
      const isApp = ua.indexOf('app_ydniu') > 0 || ua.search('app_ydniu') > 0 || /(app_ydniu)/i.test(ua);
      const isWeChat = /MicroMessenger/i.test(ua);
      const isLocalTest = location.origin.indexOf('http://localhost') > -1 || location.origin.indexOf('192.168') > -1 || location.origin.indexOf('https://app-test.ydniu.com') > -1;
      const isQianHai = location.origin.indexOf('m.ydn.com') > -1 || isLocalTest;
      // const switchState = location.origin.indexOf('app.ydniu.com') > -1 || isLocalTest;
      const switchState = true;
      let appHeight = 0;
      if (isApp) {
        appHeight = 30;
        if (isAndroid && (window as any).Android?.getStatusBarHeight) {
          appHeight = (window as any).Android.getStatusBarHeight() || 30;
        }
        if (isIos && (window as any).ios?.navBarHeight) {
          appHeight = (window as any).ios?.navBarHeight || 30;
        }
        //一定牛中目前吴良建没有提供此方法,下面默认给35
        if(!(typeof appHeight === 'number') || (typeof appHeight === 'number' && appHeight <= 30)){
          appHeight = 35;
        }
      }
      state.common_data = {
        isAndroid: isAndroid,
        isIos: isIos,
        isPc: isPc,
        isApp: isApp,
        isWeChat: isWeChat,
        switchState: switchState,
        isLocalTest: isLocalTest,
        isQianHai: isQianHai,
        appHeight: appHeight
      };
    },
    updateAppHeight (state, appHeight) {
      state.common_data.appHeight = appHeight;
    },
    updateUserInfo (state, user) {
      state.userInfo = user;
    },
    updateLivePath (state, path) {
      state.live_path = path;
    },
    updateMyAsia (state, asia) {
      state.my_asia = asia;
    },
    updateMyEuro (state, euro) {
      state.my_euro = euro;
    },
    updateMyBs (state, bs) {
      state.my_bs = bs;
    },
    updateMyCr (state, cr) {
      state.my_cr = cr;
    },
    updateAppIsBack (state, is_back) {
      state.app_is_back = is_back;
    },
    updateMatchDetail (state, match_detail) {
      state.match_detail = match_detail;
    },
    updateGameInfoCache (state, game_info_cache) {
      state.game_info_cache = game_info_cache;
    },
    updateGameDetailCache (state, game_detail_cache) {
      state.game_detail_cache = game_detail_cache;
    }
  },
  actions: {
    // 发起异步请求获取用户信息
    async fetchUserInfo (context) {
      const result: any = await user_api.user_login_state({});
      if (result.res_code && result.res_code == 1) {
        const user_data: any = await user_api.get_logined_virtual_account();
        if (user_data.Error != "未登录" && user_data.Result != null) {
          context.commit('updateUserInfo', user_data.Result);
        } else {
          context.commit('updateUserInfo', {});
          context.commit('updateMatchListAttention', {});
        }
      } else {
        context.commit('updateUserInfo', {});
        context.commit('updateMatchListAttention', {});
      }
    },
    // 发起异步请求即时比分数据
    async asyncNmLiveMessage (context: any) {
      try {
        await updateLiveDate(context);
        nm_live_message(context);
      } catch (error) {
        console.log(error);
      }
    },
    // 异步处理高亮数据
    async asyncHighLight (context: any) {
      let timer: any = setInterval(() => {
        if (!context.state.live_timer_switch) {
          clearInterval(timer);
          timer = null;
          return;
        }
        let is_update = false;
        let highLight = _.clone(context.state.high_light);
        let typeArr = ['score', 'red', 'yellow'];
        for (let index = 0; index < typeArr.length; index++) {
          let type = typeArr[index];
          for (let i = 0; i < Object.keys(highLight[type]).length; i++) {
            let key = Object.keys(highLight[type])[i];
            let item = highLight[type][key];
            if (moment(item.time).add(5, 'seconds').valueOf() <= context.state.server_time) {
              is_update = true;
              delete highLight[type][key];
            }
          }
        }
        if (is_update) context.commit('updateHighLight', highLight);
      }, 1000);
    },
    // 异步处理服务器时间
    async asyncServerTime (context: any) {
      (window as any).difference_timer = setInterval(() => {
        if (!context.state.difference_time_switch) {
          clearInterval((window as any).difference_timer);
          (window as any).difference_timer = null;
          return;
        }
        let server_time = moment().valueOf();
        if (context.state.difference_time) server_time += context.state.difference_time;
        context.commit('updateServerTime', server_time);
      }, 1000);
    }
  },
  modules: {},
  plugins: [
    createPersistedState({
      storage: window.localStorage, // 使用localStorage存储状态
      reducer (val: any) {
        return {
          init: val.init,
          updateListTime: val.updateListTime,
          match_list_all_cache: val.match_list_all_cache,
          // updateWsTime: val.updateWsTime,
          nm_live_message_cache: val.nm_live_message_cache,
          match_list_attention: val.match_list_attention,
          attention_id_list: val.attention_id_list,
          set_config: val.set_config,
          my_asia: val.my_asia,
          my_euro: val.my_euro,
          my_bs: val.my_bs,
          my_cr: val.my_cr
        };
      }
    }),
    createPersistedState({
      storage: window.sessionStorage,
      key: `1000hi_live_session`,
      reducer: state => ({
        match_detail: state.match_detail,
        game_info_cache: state.game_info_cache,
        game_detail_cache: state.game_detail_cache,
      })
    }),
  ],
});

export default store;