request.js 5.0 KB

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