request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. vue.$Notice.error({
  42. title: '错误提示',
  43. desc: response.info,
  44. duration: 4
  45. })
  46. }
  47. error && error(response)
  48. }
  49. }
  50. class ActionRequest {
  51. _type = 'get'
  52. _action = ''
  53. _data = {}
  54. _tipSuccess = false
  55. _tipError = true
  56. _dataType = 'json'
  57. _contentType = 'application/x-www-form-urlencoded'
  58. _success = null
  59. _error = null
  60. _complete = null
  61. constructor (vue) {
  62. this.vue = vue
  63. }
  64. dataType (dataType) {
  65. this._dataType = dataType
  66. return this
  67. }
  68. contentType (contentType) {
  69. this._contentType = contentType
  70. return this
  71. }
  72. action (action) {
  73. this._action = action
  74. return this
  75. }
  76. data (data) {
  77. this._data = data
  78. return this
  79. }
  80. showSuccessTip () {
  81. this._tipSuccess = true
  82. return this
  83. }
  84. hideErrorTip () {
  85. this._tipError = false
  86. return this
  87. }
  88. success (success) {
  89. this._success = success
  90. return this
  91. }
  92. error (error) {
  93. this._error = error
  94. return this
  95. }
  96. complete (complete) {
  97. this._complete = complete
  98. return this
  99. }
  100. get () {
  101. return this.execute('get')
  102. }
  103. post () {
  104. return this.execute('post')
  105. }
  106. execute (type) {
  107. this._type = type
  108. request({
  109. type: this._type,
  110. dataType: this._dataType,
  111. contentType: this._contentType,
  112. data: this._data,
  113. url: actionUrl(this._action),
  114. success: (response) => {
  115. requestSuccessHandle(
  116. this.vue,
  117. response,
  118. this._tipSuccess,
  119. this._tipError,
  120. this['_success'],
  121. this['_error']
  122. )
  123. },
  124. error: (res) => {
  125. console.log(res)
  126. this.$router.push('/login')
  127. this.vue.$Notice.error({ title: '对不起您请求的数据不存在或者返回异常', duration: 5 })
  128. },
  129. complete: () => {
  130. this.vue.$Loading.finish()
  131. this['_complete'] && this['_complete']()
  132. }
  133. })
  134. }
  135. }
  136. export const vueRequest = {
  137. install: function (Vue) {
  138. Vue.prototype.$request = function (action) {
  139. let actionRequest = new ActionRequest(this)
  140. actionRequest.action(action)
  141. return actionRequest
  142. }
  143. }
  144. }