123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import axios from 'axios'
- import qs from 'qs'
- import { actionUrl } from '../helper'
- import _ from 'lodash'
- export const request = function ({ type, data, dataType,contentType, url, success, error, complete }) {
- type = (type || 'get').toUpperCase()
- let config = {
- method: type || 'get',
- url: url,
- headers: { 'content-type': contentType || "application/x-www-form-urlencoded" },
- responseType: dataType || 'json'
- }
- if (_.indexOf(['POST', 'PUT', 'PATCH'], type) === -1) {
- config.params = data
- } else {
- config.data = qs.stringify(data)
- }
- axios(config).then((response) => {
- complete && complete()
- success && success(response.data)
- }).catch((e) => {
- complete && complete()
- error && error(e)
- })
- }
- export const requestSuccessHandle = function (vue, response, tipSuccess, tipError, success, error) {
- if (response.status === 'success') {
- if (tipSuccess) {
- vue.$Notice.success({
- title: '操作提示',
- desc: response.info,
- duration: 3
- })
- }
- success && success(response)
- } else {
- if (tipError) {
- if (response.status == 'error' && response.info == '未登录系统') {
- window.location.href = '/'
- }
- console.log(response)
- vue.$Notice.error({
- title: '错误提示',
- desc: response.info,
- duration: 4
- })
- }
- error && error(response)
- }
- }
- class ActionRequest {
- _type = 'get'
- _action = ''
- _data = {}
- _tipSuccess = false
- _tipError = true
- _dataType = 'json'
- _contentType = 'application/x-www-form-urlencoded'
- // _contentType = 'multipart/form-data;boundary=WebAppBoundary'
- _success = null
- _error = null
- _complete = null
- constructor (vue) {
- this.vue = vue
- }
- dataType (dataType) {
- this._dataType = dataType
- return this
- }
- contentType (contentType) {
- this._contentType = contentType
- return this
- }
- action (action) {
- this._action = action
- return this
- }
- data (data) {
- this._data = data
- return this
- }
- showSuccessTip () {
- this._tipSuccess = true
- return this
- }
- hideErrorTip () {
- this._tipError = false
- return this
- }
- success (success) {
- this._success = success
- return this
- }
- error (error) {
- this._error = error
- return this
- }
- complete (complete) {
- this._complete = complete
- return this
- }
- get () {
- return this.execute('get')
- }
- post () {
- return this.execute('post')
- }
- execute (type) {
- this._type = type
- request({
- type: this._type,
- dataType: this._dataType,
- contentType: this._contentType,
- data: this._data,
- url: actionUrl(this._action),
- success: (response) => {
- requestSuccessHandle(
- this.vue,
- response,
- this._tipSuccess,
- this._tipError,
- this['_success'],
- this['_error']
- )
- },
- error: (res) => {
- console.log(res)
- this.$router.push('/login')
- this.vue.$Notice.error({ title: '对不起您请求的数据不存在或者返回异常', duration: 5 })
- },
- complete: () => {
- this.vue.$Loading.finish()
- this['_complete'] && this['_complete']()
- }
- })
- }
- }
- export const vueRequest = {
- install: function (Vue) {
- Vue.prototype.$request = function (action) {
- let actionRequest = new ActionRequest(this)
- actionRequest.action(action)
- return actionRequest
- }
- }
- }
|