request.js 4.9 KB

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