index.js 2.93 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,
        },
        route: '',
        path: '',
        needLogin: false,
        isLogin: false
      },
      window: {
        clientHeight: 0,
        clientWidth: 0,
        supportsPassive: false,
        supportWebp: false
      },
      historys: [],
      direction: 'forword',
      homePage: true
    },
    mutations: {
      [Types.SET_SERVER_INFO](state, {context}) {
        state.context.title = context.title;
        state.context.route = context.route;
        state.context.path = context.path;
      },
      [Types.SET_ENV](state, {env}) {
        state.context.env = Object.assign({}, env);
      },
      [Types.SET_LOGIN_INFO](state, {uid}) {
        state.context.isLogin = uid > 1;
      },
      [Types.SET_TITLE](state, {title}) {
        state.context.title = title;
      },
      [Types.ROUTE_CHANGE](state, {to}) {
        let topath;

        try {
          topath = decodeURIComponent(to.fullPath);
        } catch (error) {
          topath = to.fullPath;
        }

        if (state.historys.length >= 2 && state.historys[state.historys.length - 2].path === topath) {
          state.historys.pop();
          state.direction = 'back';
        } else {
          state.historys.push({
            name: to.name,
            path: topath
          });
          state.direction = 'forword';
        }
        state.homePage = state.historys.length <= 1;
      },
      [Types.SET_NEED_LOGIN](state, {needLogin}) {
        state.context.needLogin = needLogin;
      },
      [Types.SET_WINDOW_SIZE](state, {clientWidth, clientHeight}) {
        state.window.clientWidth = clientWidth;
        state.window.clientHeight = clientHeight;
      },
      [Types.SET_SUPPORT_PASSIVE](state, {supportsPassive}) {
        state.window.supportsPassive = supportsPassive;
      },
      [Types.SET_SUPPORT_WEBP](state, {supportWebp}) {
        state.window.supportWebp = supportWebp;
      }
    },
    getters: {
      getLogin(state) {
        return state.context.isLogin;
      }
    },
    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);
        });
      }
    }
  };
}