index.js 8.32 KB
import * as Types from './types';
import cookie from 'yoho-cookie';
import { merge } from 'lodash';

export default function(mergeState = {}) {
  return {
    state: merge(
      {
        context: {
          title: '',
          env: {
            isApp: true,
            isiOS: false,
            isAndroid: false,
            isYohoBuy: false,
            isWechat: false,
            isQQ: false,
            isWeibo: false,
            isChat: false,
          },
          route: '',
          path: '',
          needLogin: false,
          isLogin: false,
          userHeadIco: '',
          userName: '',
          enableQiugou: null, // 是否启用求购,默认不启用, null: 初始状态标识,需要请求api, false: 不启用, true: 启用,
          query: {}
        },
        window: {
          statusBarStatus: false,
          statusBarHeight: 0,
          actionBarHeight: 0,
          statusBarColor: 'black',
          clientHeight: 0,
          clientWidth: 0,
          supportsPassive: false,
          supportWebp: false,
        },
        historys: [],
        direction: 'forword',
        homePage: true,
        user: {},
        config: {}
      },
      mergeState,
    ),
    mutations: {
      [Types.SET_SERVER_INFO](state, { context }) {
        state.context.title = context.title;
        state.context.route = context.route;
        state.context.path = context.path;
        state.context.env = Object.assign({}, context.env);
      },
      [Types.SET_ENV](state, { env }) {
        state.context.env = Object.assign({}, env);
      },
      [Types.SET_LOGIN_INFO](state, { uid }) {
        state.context.isLogin = uid > 1;
      },
      [Types.SET_USER](
        state,
        { appUid, appSessionKey, appVersion, appClientType },
      ) {
        state.user.appUid = appUid;
        state.user.appSessionKey = appSessionKey;
        state.user.appVersion = appVersion;
        state.user.appClientType = appClientType;
      },
      [Types.SET_TITLE](state, { title }) {
        state.context.title = title;
      },
      [Types.ROUTE_CHANGE](state, { to }) {
        let topath;

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

        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;
        state.user = {};
      },
      [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;
      },
      [Types.SET_STATUS_BAR_STATUS](state, { status }) {
        state.window.statusBarStatus = !!status;
      },
      [Types.SET_STATUS_BAR_HEIGHT](state, { height }) {
        state.window.statusBarHeight = height;
      },
      [Types.SET_ACTION_BAR_HEIGHT](state, { height }) {
        state.window.actionBarHeight = height;
      },
      [Types.SET_STATUS_BAR_COLOR](state, { color }) {
        if (
          state.context.env.isYohoBuy &&
          state.context.env.isiOS &&
          state.window.statusBarStatus
        ) {
          state.window.statusBarColor = color || 'black';
        }
      },
      [Types.FETCH_USER_INFO_SUCCESS](state, { head_ico, nickname }) {
        state.context.userHeadIco = head_ico;
        state.context.userName = nickname;
      },
      [Types.FETCH_USER_INFO_FAILD](state) {
        state.context.userHeadIco = '';
      },
      [Types.SET_USER_SELLER_INFO](state, sellerInfo) {
        state.user.sellerInfo = sellerInfo || {};
      },
      [Types.UPDATE_SYSTEM_CONFIG_QIUGOU](state, { enabled }) {
        state.context.enableQiugou = enabled;
      },
      [Types.FETCH_XIANYU_CONFIG](state, data) {
        state.config = data;
      },
      [Types.SET_QUERY](state, data) {
        state.context.query = data;
      }
    },
    getters: {
      getLogin(state) {
        return state.context.isLogin;
      },
      isQiugouEnabled(state) {
        return state.context.enableQiugou;
      },
    },
    actions: {
      async fetchUserProfile({ commit }) {
        const result = await this.$api.get('/api/grass/userProfile');

        if (result && result.code === 200) {
          if (!result.data) {
            result.data = {};
          }
          commit(Types.FETCH_USER_INFO_SUCCESS, result.data);
          return result.data;
        } else {
          commit(Types.FETCH_USER_INFO_FAILD);
          return {};
        }
      },
      userRealCertification(
        { commit },
        { certName, certNo, frontImageUrl, backImageUrl },
      ) {
        return this.$api.post('/api/ufo/user/certification', {
          certName,
          certNo,
          frontImageUrl,
          backImageUrl,
        });
      },
      sendBindSms({ commit }, { mobile, bindCode }) {
        return this.$api.post('/passport/bind/sms/send', {
          mobile,
          bindCode,
        });
      },
      submitThirdBind({ commit }, { mobile, code, bindCode }) {
        return this.$api.post('/passport/bind/third', {
          mobile,
          code,
          bindCode,
        });
      },
      reportError(params, { error }) {
        this.$reportError(error);
      },
      reportYas(
        params,
        {
          params: { appop, param },
          asyncindx = false,
        },
      ) {
        try {
          // console.log(appop)
          // console.log(param)
          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;
              }

              if (!param.SRC_ID) {
                const srcId = cookie.get('SRC_ID') || '';

                param.SRC_ID = srcId;
              }

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

              if (window._yasEvents) {
                window._yasEvents.forEach(e => {
                  window._yas.sendAppLogs(
                    {
                      appop: e.appop,
                      param: e.param ? JSON.stringify(e.param) : '{}',
                    },
                    asyncindx,
                  );
                });

                window._yasEvents = null;
              }
            } else {
              window._yasEvents = window._yasEvents ? window._yasEvents.push({ appop, param }) : [{ appop, param }];
            }
          }, 300);
        } catch (e) {
          // pass
        }
      },
      setNeedLogin({ commit }, { needLogin }) {
        commit(Types.SET_NEED_LOGIN, { needLogin });
      },
      async getSysConfigQiugou({ commit, state }) {
        // /ufo-gateway/?method=ufo.resource.getConfigTypeContent&code=bid_switch_h5
        // {"alg":"SALT_MD5","code":200,"data":"1","md5":"c13a1ace2c5883be18643551bd17ebcd","message":"操作成功"}
        // H5是否打开求购 1代表打开
        let isEnabled = !!state.context.enableQiugou;

        if (state.context.enableQiugou === null) {
          const resp = await this.$api.post('/api/sys/config', {
            code: 'bid_switch_h5',
          });

          if (resp.code === 200) {
            isEnabled = resp.data === '1';
          }

          commit(Types.UPDATE_SYSTEM_CONFIG_QIUGOU, { enabled: isEnabled });
        }

        return isEnabled;
      },

      async getZkConfig({ commit }) {
        const res = await this.$api.post('/config');

        if (res.code === 200) {
          commit(Types.FETCH_XIANYU_CONFIG, res.data);
        }
      }
    },
  };
}