athena之___封装vue-resource数据请求方式

 

src/assets/scripts/base/dataService.js :

/**
 * vue 中的ajax 数据服务:vue-resource
 */
import Vue from 'vue'
import store from 'store'
import vueResource from 'vue-resource'
import MockData from 'assets/mock/index'
import moment from 'moment'
import {dataServiceConfig, loginUserKey, backendMenuId, submitToken, routeKey} from 'src/config/index.js'
import {dotData, alert, replaceNullDeep} from './functions.js' //
import login from './login'
Vue.use(vueResource)
Vue.http.options.emulateHTTP = true
Vue.http.options.timeout = 0

let devMode = process.env.NODE_ENV !== 'production'

// 更新token
Vue.http.interceptors.push((request, next) => {
  const token = dotData(store.get(loginUserKey), 'token')
  if (devMode) {
    if (MockData.has(request.url.replace('/api', ''))) {
      request.url = '/mock' + MockData.get(request.url.replace('/api', ''))
    }
  }
  // 访问token
  if (token) {
    Vue.http.headers.common['token'] = token
  } else {
    Vue.http.headers.common['token'] = ''
  }
  // 菜单编号
  const menuId = store.get(backendMenuId)
  if (menuId) {
    Vue.http.headers.common['menu-id'] = String(menuId)
  }
  const startTime = moment().valueOf()
  // continue to next interceptor
  next((response) => {
    if (process.env.NODE_ENV === 'testing' && token) {
      const endTime = moment().valueOf()
      const menuUtil = require('assets/scripts/business/menu')
      // 这些接口要跳过统计 因为访问这些接口都是没有授权的时候
      const breakPathUrl = [
        '/api/ms-user-info/user/userLogin', // 没有授权
        '/api/ms-user-info/user/userMobile', // 没有授权
        '/api/ms-user-info/user/backPws', // 没有授权
        '/api/ms-user-info/user/menu-user-id', // 不能统计, 这里会产生递归
        '/api/ms-access-log/CallOnInterface/log-record', // 统计的统计接口,不用统计
        '/api/token/resubmit-token' // 获取token不统计
      ]
      let url = request.url
      const urlIndexOf = String(request.url).indexOf('?')
      if (urlIndexOf >= 0) {
        url = url.substring(0, urlIndexOf)
      }
      if (breakPathUrl.indexOf(url) < 0 && response.status === 200) {
        menuUtil.getMenuDataPathMap().then(map => {
          const lableName = dotData(store.get(routeKey), 'name')
          const matchPath = dotData(store.get(routeKey), 'path')
          const menuName = dotData(map.get(matchPath), 'text')
          const util = require('assets/scripts/base/util')
          util.httpSuccessGet('/ms-access-log/CallOnInterface/log-record', {
            menuName, lableName, menuUrl: matchPath, afterPort: url, visitStarttime: startTime, visitEndtime: endTime
          })
        })
      }
    }
    const tokenNew = response.headers.get('token-new')
    let data = store.get(loginUserKey)
    if (tokenNew && data) {
      data.token = tokenNew
      store.set(loginUserKey, data)
    }
  })
})
let statusMap = new Map()
export default {
  createUrl (url) {
    if (/^http:\/\//.test(url)) {
      return url
    }
    if (/^prefix:/.test(url)) {
      return url.replace('prefix:', '')
    }
    if (url.charAt(0) !== '/') {
      url = '/' + url
    }
    return dataServiceConfig.urlPrefix + url
  },
  get (url, options = {}) {
    url = this.createUrl(url)
    options.params = window.$.extend(true, {t: new Date().getTime()}, options.params)
    return Vue.http.get(url, options)
  },
  getWithParams (url, params, options = {}) {
    options.params = window.$.extend(true, params, options.params)
    return this.get(url, options)
  },
  head (url, options = {}) {
    url = this.createUrl(url)
    return Vue.http.head(url, options)
  },
  delete (url, options = {}) {
    url = this.createUrl(url)
    return Vue.http.delete(url, options)
  },
  jsonp (url, options = {}) {
    url = this.createUrl(url)
    return Vue.http.jsonp(url, options)
  },
  post (url, body = {}, options = {}) {
    url = this.createUrl(url)
    options.params = window.$.extend(true, {t: new Date().getTime()}, options.params)
    return Vue.http.post(url, body, options)
  },
  put (url, body = {}, options = {}) {
    url = this.createUrl(url)
    return Vue.http.put(url, body, options)
  },
  patch (url, body = {}, options = {}) {
    url = this.createUrl(url)
    return Vue.http.patch(url, body, options)
  },
  resource (url, params = {}, actions = {}, options = {}) {
    url = this.createUrl(url)
    return Vue.resource(url, params, actions, options)
  },
  handleError (response) {
    if (response.status === 200) {
      response.json().then(responseJson => {
        alert(responseJson.message || '系统报错')
      })
    } else if (response.status === 401) {
      login.logout(true)
    } else if (response.status === 402) {
      const newToken = response.headers.get('resubmit-token')
      if (newToken) {
        store.set(submitToken, newToken)
      }
      alert('请勿重复提交')
    } else if (response.status === 503) {
      window.location.reload()
    } else if (response.status === 0) {
      if (window.navigator.onLine) {
        alert('网络连接异常')
      } else {
        alert('访问失败:请检查网络')
      }
    } else {
      // 此处处理一下 同一个状态码在4秒内只包一次错误
      if (!statusMap.has(response.status)) {
        alert('系统报错 status:' + response.status)
        statusMap.set(response.status, true)
        setTimeout(_ => {
          statusMap.delete(response.status)
        }, 4000)
      }
      throw response
    }
  },
  // 过滤一下json
  handleJson (json) {
    return replaceNullDeep(json)
  }
}

数据请求方式集合:src/assets/scripts/base/util.js

httpSuccessGet、httpSuccessGetArr、httpGetTable、httpGetDetail、httpSuccessPost

import dataService from './dataService'
import {unixTimestampFilter, filterDataBaseDictionary} from './filters'
import store from 'store'
举例:

// 获取详情数据
export const httpGetDetail = (url, that, params = {}, dictionary = [], key = 'detailData', dataFunc = (responseJson) => {
  return responseJson
}) => {
  return httpSuccessGet(url, params).then(detailData => {
    let data = dataFunc(detailData)
    dictionary.forEach(v => {
      setDotData(data, v + '@Text', filterDataBaseDictionary(dotData(data, v)))
    })
    that[key] = dataService.handleJson(data)
    return that[key]
  })
}

// 成功post数据
export const httpSuccessPost = (url, body, options = {}, failMessage = '') => {
  let resubmitUrl = lodash.indexOf(interfaceArr, '/api' + url)
  // 嵌入 token 后端防止重复提交
  const resubmitToken = store.get(submitToken)
  if (resubmitToken && resubmitUrl >= 0) {
    options.headers = merge(options.headers || {}, {'resubmit-token': resubmitToken})
  }
  return dataService.post(url, body, options).then(response => {
    const status = response.status
    const newToken = response.headers.get('resubmit-token')
    if (newToken && resubmitUrl >= 0) {
      store.set(submitToken, newToken)
    }
    return response.json().then(responseJson => {
      if (responseJson.success === true) {
        return {success: true, data: dataService.handleJson(responseJson.data)}
      } else {
        if (status === 200) {
          alert(failMessage || responseJson.error_message || '系统返回 success: false')
        }
      }
      return {success: false, error: responseJson.error_message}
    })
  }).catch(response => {
    dataService.handleError(response)
  })
}

// 成功Get数据
export const httpSuccessGet = (url, params = {}, options = {}, failMessage = '') => {
  return dataService.getWithParams(url, params, options).then(response => {
    const status = response.status
    return response.json().then(responseJson => {
      if (responseJson.success === true) {
        return dataService.handleJson(responseJson.data)
      } else {
        if (status === 200) {
          alert(failMessage || responseJson.error_message || '系统返回 success: false')
        }
      }
      return false
    })
  }).catch(response => {
    dataService.handleError(response)
  })
}

// 成功Get数据
export const httpGetResponse = (url, params = {}, options = {}, failMessage = '') => {
  return dataService.getWithParams(url, params, options).then(response => {
    const status = response.status
    return response.json().then(responseJson => {
      if (responseJson.success === true) {
        return dataService.handleJson(responseJson)
      } else {
        if (status === 200) {
          alert(failMessage || responseJson.error_message || '系统返回 success: false')
        }
      }
      return responseJson
    })
  }).catch(response => {
    dataService.handleError(response)
  })
}
 

 

 

 

 

你可能感兴趣的:(数据请求)