request.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if(config.lhide){
  33. uni.showLoading({
  34. title:"加载中"
  35. })
  36. }else{
  37. // vues.$loading(true)
  38. store.commit("switch_loading",true);
  39. }
  40. return new Promise((resolve, reject) => {
  41. uni.request({
  42. method: config.method || 'get',
  43. timeout: config.timeout || timeout,
  44. url: config.baseUrl || baseUrl + config.url,
  45. data: config.data,
  46. header: config.header,
  47. dataType: 'json'
  48. }).then(response => {
  49. uni.hideLoading()
  50. // vues.$loading(false)
  51. store.commit("switch_loading",false);
  52. let [error, res] = response
  53. if (error) {
  54. toast('后端接口连接异常')
  55. reject('后端接口连接异常')
  56. return
  57. }
  58. const code = res.data.code || 200
  59. const msg = errorCode[code] || res.data.msg || errorCode['default']
  60. if (code === 401) {
  61. // uni.showLoading({
  62. // title: '加载中'
  63. // });
  64. if(confirmflag){
  65. // 利用 return 终止函数继续运行
  66. return false;
  67. }
  68. confirmflag = true;
  69. setTimeout(function(){
  70. var autologin=store.state.user.autologin;
  71. if(username&&password&&!captchaEnabled&&autologin){
  72. // 自动登录
  73. var loginForm={
  74. username:username,
  75. password:password,
  76. strfrom:'request'
  77. }
  78. store.dispatch('Login', loginForm).then((res) => {
  79. setTimeout(function(){
  80. store.dispatch('GetInfo').then(res => {
  81. uni.hideLoading()
  82. // 刷新当前页
  83. var routes = getCurrentPages()
  84. // console.log(routes,1)
  85. if(routes.length){
  86. var route ='/'+routes[routes.length - 1].route;
  87. var fullPath =routes[routes.length - 1].options;
  88. // console.log(fullPath,23)
  89. var options={}
  90. if(JSON.stringify(fullPath)!='{}'){
  91. if(fullPath.data){
  92. options=JSON.parse(decodeURIComponent(fullPath.data))
  93. route=route+'?data='+encodeURIComponent(JSON.stringify(options))
  94. }else{
  95. //正常循环
  96. var keystr='?'
  97. Object.keys(fullPath).some((key,idx) => {
  98. if(idx==0){
  99. keystr+=key+'='+fullPath[key]
  100. }else{
  101. keystr+='&'+key+'='+fullPath[key]
  102. }
  103. })
  104. route=route+keystr
  105. }
  106. }
  107. uni.redirectTo({
  108. url:route
  109. })
  110. }else{
  111. this.$tab.reLaunch('/pages/index/index')
  112. }
  113. })
  114. },500)
  115. })
  116. }else{
  117. uni.hideLoading()
  118. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  119. if (res.confirm) {
  120. store.dispatch('LogOut').then(res => {
  121. uni.reLaunch({ url: '/pages/login' })
  122. })
  123. }
  124. })
  125. }
  126. confirmflag = false;
  127. },2000)
  128. reject('无效的会话,或者会话已过期,请重新登录。')
  129. } else if (code === 500) {
  130. toast(msg)
  131. reject('500')
  132. } else if (code !== 200) {
  133. toast(msg)
  134. reject(code)
  135. }
  136. resolve(res.data)
  137. })
  138. .catch(error => {
  139. if(config.lhide){
  140. }else{
  141. store.commit("switch_loading",false);
  142. // vues.$loading(false)
  143. }
  144. uni.hideLoading()
  145. let { message } = error
  146. if (message === 'Network Error') {
  147. message = '后端接口连接异常'
  148. } else if (message.includes('timeout')) {
  149. message = '系统接口请求超时'
  150. } else if (message.includes('Request failed with status code')) {
  151. message = '系统接口' + message.substr(message.length - 3) + '异常'
  152. }
  153. toast(message)
  154. reject(error)
  155. })
  156. })
  157. }
  158. export default request