request.js 4.9 KB

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