request.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import axios from 'axios'
  2. import qs from 'qs'
  3. import { actionUrl } from '../helper'
  4. import _ from 'lodash'
  5. export const request = function ({ type, data, dataType,contentType, url, success, error, complete }) {
  6. type = (type || 'get').toUpperCase()
  7. let config = {
  8. method: type || 'get',
  9. url: url,
  10. headers: { 'content-type': contentType || "application/x-www-form-urlencoded" },
  11. responseType: dataType || 'json'
  12. }
  13. if (_.indexOf(['POST', 'PUT', 'PATCH'], type) === -1) {
  14. config.params = data
  15. } else {
  16. config.data = qs.stringify(data)
  17. }
  18. axios(config).then((response) => {
  19. complete && complete()
  20. success && success(response.data)
  21. }).catch((e) => {
  22. complete && complete()
  23. error && error(e)
  24. })
  25. }
  26. export const requestSuccessHandle = function (vue, response, tipSuccess, tipError, success, error) {
  27. if (response.status === 'success') {
  28. if (tipSuccess) {
  29. vue.$Notice.success({
  30. title: '操作提示',
  31. desc: response.info,
  32. duration: 3
  33. })
  34. }
  35. success && success(response)
  36. } else {
  37. if (tipError) {
  38. if (response.status == 'error' && response.info == '未登录系统') {
  39. window.location.href = '/'
  40. }
  41. console.log(response)
  42. vue.$Notice.error({
  43. title: '错误提示',
  44. desc: response.info,
  45. duration: 4
  46. })
  47. }
  48. error && error(response)
  49. }
  50. }
  51. class ActionRequest {
  52. _type = 'get'
  53. _action = ''
  54. _data = {}
  55. _tipSuccess = false
  56. _tipError = true
  57. _dataType = 'json'
  58. _contentType = 'application/x-www-form-urlencoded'
  59. // _contentType = 'multipart/form-data;boundary=WebAppBoundary'
  60. _success = null
  61. _error = null
  62. _complete = null
  63. constructor (vue) {
  64. this.vue = vue
  65. }
  66. dataType (dataType) {
  67. this._dataType = dataType
  68. return this
  69. }
  70. contentType (contentType) {
  71. this._contentType = contentType
  72. return this
  73. }
  74. action (action) {
  75. this._action = action
  76. return this
  77. }
  78. data (data) {
  79. this._data = data
  80. return this
  81. }
  82. showSuccessTip () {
  83. this._tipSuccess = true
  84. return this
  85. }
  86. hideErrorTip () {
  87. this._tipError = false
  88. return this
  89. }
  90. success (success) {
  91. this._success = success
  92. return this
  93. }
  94. error (error) {
  95. this._error = error
  96. return this
  97. }
  98. complete (complete) {
  99. this._complete = complete
  100. return this
  101. }
  102. get () {
  103. return this.execute('get')
  104. }
  105. post () {
  106. return this.execute('post')
  107. }
  108. execute (type) {
  109. this._type = type
  110. request({
  111. type: this._type,
  112. dataType: this._dataType,
  113. contentType: this._contentType,
  114. data: this._data,
  115. url: actionUrl(this._action),
  116. success: (response) => {
  117. requestSuccessHandle(
  118. this.vue,
  119. response,
  120. this._tipSuccess,
  121. this._tipError,
  122. this['_success'],
  123. this['_error']
  124. )
  125. },
  126. error: (res) => {
  127. console.log(res)
  128. this.$router.push('/login')
  129. this.vue.$Notice.error({ title: '对不起您请求的数据不存在或者返回异常', duration: 5 })
  130. },
  131. complete: () => {
  132. this.vue.$Loading.finish()
  133. this['_complete'] && this['_complete']()
  134. }
  135. })
  136. }
  137. }
  138. export const vueRequest = {
  139. install: function (Vue) {
  140. Vue.prototype.$request = function (action) {
  141. let actionRequest = new ActionRequest(this)
  142. actionRequest.action(action)
  143. return actionRequest
  144. }
  145. }
  146. }