/*
 * @Description: 这里输入文件功能
 * @Author: zhoupeng
 * @Date: 2020-04-23 11:06:51
 * @LastEditTime: 2020-04-23 13:25:49
 */
import Config from '../../../../utils/config';
import * as Service from '../../services/index';
import { Toast } from 'antd-mobile';
import router from 'umi/router';

export default {
  namespace: 'mobileCodeModule',
  state: {
    loading: false,
    codeLoading: false,
  },
  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *sendCode({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          codeLoading: true,
        },
      });
      const { rc, msg } = yield call(Service.sendCode, { ...payload });
      if (rc === Config.constant.codeSuccess) {
        Toast.info('发送成功', 1);
      } else {
        Toast.info(msg, 1);
      }
      yield put({
        type: 'updateState',
        payload: {
          codeLoading: false,
        },
      });
      return rc;
    },
    *loginByCode({ payload }, { call, put, select }) {
      yield put({
        type: 'updateState',
        payload: {
          loading: true,
        },
      });
      const { rc, msg } = yield call(Service.loginByMobile, { ...payload });
      yield put({
        type: 'updateState',
        payload: {
          loading: false,
        },
      });
      if (rc === Config.constant.codeSuccess) {
      } else {
        Toast.info(msg, 1);
      }
    },
  },
  subscriptions: {
    // setupHistory({ dispatch, history }) {
    //   history.listen(location => {});
    // },
  },
};
