actions.js 1.71 KB
import * as Types from './types';

function _url({ name }) {
  return `//cdn.yoho.cn/${name}`;
}

function _handleData(data) {
  const result = {
    business_type: data.businessType,
    business_name: data.businessName,
    social_credit_code: data.socialCreditCode,
    cert_name: data.certName,
    cert_no: data.certNo,
    start_time: data.time.start_time,
    expire_time: data.time.noLimit ? '0' : data.time.expire_time,
    license_original_image: data.licenseOriginalImage.map(_url).join(''),
    license_copy_image: data.licenseCopyImage.map(_url).join(''),
    cert_face_image: data.certFaceImage.map(_url).join(''),
    cert_reverse_image: data.certReverseImage.map(_url).join('')
  };

  return result;
}

export default {
  async fetchToken({ commit }) {
    const result = await this.$api.get('/getToken?type=yohocdn');

    commit(Types.FETCH_UFO_UPLOAD_TOKEN_SUCCESS, { token: result.uptoken });
  },

  async saveLicense({ commit }, data) {
    const result = await this.$api.post(
      '/api/ufo/license/save',
      _handleData(data)
    );

    return result;
  },

  async licenseStatus({ commit }) {
    let result = {};

    try {
      result = await this.$api.get('/api/ufo/license/status');
    } catch (e) {
      if (e.code === 401) {
        commit(Types.FETCH_UFO_NEED_LOGIN, true);
        return;
      }
    }

    if (result.code === 200) {
      commit(Types.FETCH_UFO_STATUS_SUCCESS, { status: result.data, message: result.message });
    } else {
      commit(Types.FETCH_UFO_STATUS_SUCCESS, -1);
    }
  },

  async getStoreStatus({ commit }) {
    let result = {};

    try {
      result = await this.$api.get('/api/ufo/store/status');
    } catch (e) {
      return result;
    }

    return result;
  }
};