request.js 5.3 KB

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