request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import store from '@/store'
  2. import config, {
  3. baseUrl,foreignUrl
  4. } from '@/config'
  5. import {
  6. getToken,
  7. getServerUrl
  8. } from '@/utils/auth'
  9. import errorCode from '@/utils/errorCode'
  10. import {
  11. toast,
  12. showConfirm,
  13. tansParams
  14. } from '@/utils/common'
  15. let timeout = 200000
  16. console.log(baseUrl)
  17. const request = config => {
  18. // 是否需要设置 token
  19. const isToken = (config.headers || {}).isToken === false
  20. config.header = config.header || {}
  21. if (getToken() && !isToken) {
  22. // config.header['Authorization'] = 'Bearer ' + getToken()
  23. config.header['UserKey'] = getToken()
  24. config.header['ServerUrl'] = getServerUrl()
  25. } else {
  26. //去除token校验
  27. // uni.reLaunch({
  28. // url: '/pages/login/index'
  29. // })
  30. }
  31. // get请求映射params参数
  32. if (config.params) {
  33. let url = config.url + '?' + tansParams(config.params)
  34. url = url.slice(0, -1)
  35. config.url = url
  36. }
  37. return new Promise((resolve, reject) => {
  38. let requestUrl = uni.getStorageSync("Url")+"/K3Cloud/Kingdee.BOS.WebApi.ServicesStub."
  39. // if (Yycbm == "LT" || Yycbm == "LF" || Yycbm == "LL") {
  40. // requestUrl = baseUrl
  41. // } else {
  42. // requestUrl = foreignUrl
  43. // }
  44. console.log(requestUrl)
  45. uni.request({
  46. method: config.method || 'get',
  47. timeout: config.timeout || timeout,
  48. url: requestUrl + config.url,
  49. data: config.data,
  50. header: config.header,
  51. // dataType: 'json'
  52. }).then(response => {
  53. console.log(response)
  54. let [error, res] = response
  55. // if (error) {
  56. // toast('后端接口连接异常')
  57. // reject('后端接口连接异常')
  58. // return
  59. // }
  60. console.log(res)
  61. // const code = res.data.Code || 200
  62. // const msg = errorCode[code] || res.data.Msg || errorCode['default']
  63. // console.log(code)
  64. if(error !=null){
  65. reject(error.errMsg)
  66. }
  67. console.log(res.data.statusCode)
  68. if(res.statusCode !== 200){
  69. reject("后端接口连接异常")
  70. }
  71. if (typeof res.data === 'object' && res.data !== null && res.data.Result) {
  72. console.log('数据是一个对象');
  73. let responseStatus = res.data.Result.ResponseStatus;
  74. if (responseStatus && responseStatus.MsgCode === 1) {
  75. console.log('会话信息已丢失,请重新登录');
  76. showConfirm(responseStatus.Errors[0].Message).then(res => {
  77. console.log(res)
  78. if (res) {
  79. console.log("aaa")
  80. uni.reLaunch({
  81. url: '/pages/login/index'
  82. })
  83. }
  84. })
  85. // reject(responseStatus.Errors[0].Message)
  86. }
  87. }
  88. // if (msg == 'UNLOGIN') {
  89. // showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  90. // if (res.confirm) {
  91. // console.log(getToken())
  92. // if (getToken()) {
  93. // store.dispatch('LogOut').then(res => {
  94. // uni.reLaunch({
  95. // url: '/pages/login/index'
  96. // })
  97. // })
  98. // } else {
  99. // uni.reLaunch({
  100. // url: '/pages/login/index'
  101. // })
  102. // }
  103. // }
  104. // })
  105. // reject('无效的会话,或者会话已过期,请重新登录。')
  106. // }
  107. resolve(res.data)
  108. })
  109. .catch(error => {
  110. let {
  111. message
  112. } = error
  113. if (message === 'Network Error') {
  114. message = '后端接口连接异常'
  115. } else if (message.includes('timeout')) {
  116. message = '系统接口请求超时'
  117. } else if (message.includes('Request failed with status code')) {
  118. message = '系统接口' + message.substr(message.length - 3) + '异常'
  119. }
  120. toast(message)
  121. reject(error)
  122. })
  123. })
  124. }
  125. export default request