request.js 5.2 KB

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