index.js 2.55 KB
import * as Types from './types';
import cookie from 'yoho-cookie';

export default function() {
  return {
    state: {
      context: {
        title: '',
        env: {
          isApp: true,
          isiOS: false,
          isAndroid: false,
          isYohoBuy: false,
          channel: 'men',
          fs: true,
        },
        route: '',
        path: '',
        needLogin: false
      },
      window: {
        clientHeight: 0,
        clientWidth: 0,
        supportsPassive: false
      },
      historys: [],
      direction: 'forword',
    },
    mutations: {
      [Types.SET_ENV](state, {context}) {
        state.context.title = context.title;
        state.context.env = context.env;
        state.context.route = context.route;
        state.context.path = context.path;
      },
      [Types.SET_TITLE](state, {title}) {
        state.context.title = title;
      },
      [Types.ROUTE_CHANGE](state, {to, from}) {
        if (!state.historys.length) {
          state.historys.push({
            name: from.name,
            path: from.fullPath
          });
        }
        const routeIndex = state.historys.findIndex(route => route.path === to.fullPath);

        if (routeIndex >= 0) {
          state.historys = state.historys.slice(0, routeIndex + 1);
          state.direction = 'back';
        } else {
          state.historys.push({
            name: to.name,
            path: to.fullPath
          });
          state.direction = 'forword';
        }
      },
      [Types.SET_NEED_LOGIN](state, {needLogin}) {
        state.context.needLogin = needLogin;
      },
      [Types.SET_WINDOW_SIZE](state, data) {
        state.window = Object.assign(state.window, data);
      },
      [Types.SET_SUPPORT_PASSIVE](state, {supportsPassive}) {
        state.window.supportsPassive = supportsPassive;
        console.log(state.window.supportsPassive)
      }
    },
    actions: {
      reportYas(params, {params: {appop, param}, asyncindx = false}) {
        document.addEventListener('deviceready', () => {
          setTimeout(() => {
            if (window._yas && window._yas.sendAppLogs) {
              param = param || {};

              if (!param.C_ID) {
                const channel = {
                  men: 1,
                  women: 2
                }[cookie.get('_Channel') || 'men'];

                param.C_ID = channel;
              }
              window._yas.sendAppLogs({
                appop,
                param: param ? JSON.stringify(param) : '{}'
              }, asyncindx);
            }
          }, 300);
        });
      }
    }
  };
}