request.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import store from '@/store'
  2. import config from '@/config'
  3. import { getToken } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. import { toast, showConfirm, tansParams } from '@/utils/common'
  6. let timeout = 10000
  7. const baseUrl = config.baseUrl
  8. // const Clientid = config.Clientid
  9. var confirmflag = config.confirmflag
  10. // 获取账号密码
  11. var newObj=JSON.parse(JSON.stringify(uni.getStorageSync('account')))
  12. var username=newObj.username;
  13. var password=newObj.password;
  14. var tenantId=newObj.tenantId;
  15. var companyName=newObj.companyName;
  16. const request = config => {
  17. // 是否需要设置 token
  18. const isToken = (config.headers || {}).isToken === false
  19. config.header = config.header || {}
  20. if (getToken() && !isToken) {
  21. config.header['Authorization'] = 'Bearer ' + getToken();
  22. // config.header['Clientid'] = Clientid
  23. }
  24. // config.header['Access-Control-Allow-Origin'] = '*'
  25. // get请求映射params参数
  26. if (config.params) {
  27. let url = config.url + '?' + tansParams(config.params)
  28. url = url.slice(0, -1)
  29. config.url = url
  30. }
  31. return new Promise((resolve, reject) => {
  32. uni.request({
  33. method: config.method || 'get',
  34. timeout: config.timeout || timeout,
  35. url: config.baseUrl || baseUrl + config.url,
  36. data: config.data,
  37. header: config.header,
  38. dataType: 'json'
  39. }).then(response => {
  40. let [error, res] = response
  41. if (error) {
  42. toast('后端接口连接异常')
  43. reject('后端接口连接异常')
  44. return
  45. }
  46. const code = res.data.code || 200
  47. const msg = errorCode[code] || res.data.msg || errorCode['default']
  48. if (code === 401) {
  49. uni.showLoading({
  50. title: '加载中'
  51. });
  52. if(confirmflag){
  53. // 利用 return 终止函数继续运行
  54. return false;
  55. }
  56. confirmflag = true;
  57. setTimeout(function(){
  58. var autologin=store.state.user.autologin;
  59. if(username&&password&&tenantId&&companyName&&autologin){
  60. // 自动登录
  61. var loginForm={
  62. username:username,
  63. password:password,
  64. tenantId:tenantId,
  65. companyName:companyName,
  66. strfrom:'request'
  67. }
  68. store.dispatch('Logines', loginForm).then((res) => {
  69. setTimeout(function(){
  70. store.dispatch('GetInfo').then(res => {
  71. uni.hideLoading()
  72. // 刷新当前页
  73. var routes = getCurrentPages()
  74. // console.log(routes,1)
  75. if(routes.length){
  76. var route ='/'+routes[routes.length - 1].route;
  77. var fullPath =routes[routes.length - 1].options;
  78. // console.log(fullPath,23)
  79. var options={}
  80. if(JSON.stringify(fullPath)!='{}'){
  81. if(fullPath.data){
  82. options=JSON.parse(decodeURIComponent(fullPath.data))
  83. route=route+'?data='+encodeURIComponent(JSON.stringify(options))
  84. }else{
  85. //正常循环
  86. var keystr='?'
  87. Object.keys(fullPath).some((key,idx) => {
  88. if(idx==0){
  89. keystr+=key+'='+fullPath[key]
  90. }else{
  91. keystr+='&'+key+'='+fullPath[key]
  92. }
  93. })
  94. route=route+keystr
  95. }
  96. }
  97. uni.redirectTo({
  98. url:route
  99. })
  100. }else{
  101. this.$tab.reLaunch('/pages/index/index')
  102. }
  103. })
  104. },500)
  105. })
  106. }else{
  107. uni.hideLoading()
  108. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  109. if (res.confirm) {
  110. store.dispatch('LogOut').then(res => {
  111. uni.reLaunch({ url: '/pages/login' })
  112. })
  113. }
  114. })
  115. }
  116. confirmflag = false;
  117. },2000)
  118. reject('无效的会话,或者会话已过期,请重新登录。')
  119. } else if (code === 500) {
  120. toast(msg)
  121. reject('500')
  122. } else if (code !== 200) {
  123. toast(msg)
  124. reject(code)
  125. }
  126. resolve(res.data)
  127. })
  128. .catch(error => {
  129. let { message } = error
  130. if (message === 'Network Error') {
  131. message = '后端接口连接异常'
  132. } else if (message.includes('timeout')) {
  133. message = '系统接口请求超时'
  134. } else if (message.includes('Request failed with status code')) {
  135. message = '系统接口' + message.substr(message.length - 3) + '异常'
  136. }
  137. toast(message)
  138. reject(error)
  139. })
  140. })
  141. }
  142. export default request