request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { encrypt, decrypt } from '@/utils/jsencrypt'
  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=decrypt(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. if(confirmflag){
  59. // 利用 return 终止函数继续运行
  60. return false;
  61. }
  62. confirmflag = true;
  63. setTimeout(function(){
  64. var autologin=store.state.user.autologin;
  65. if(username&&password&&!captchaEnabled&&autologin){
  66. // 自动登录
  67. var loginForm={
  68. username:username,
  69. password:password,
  70. strfrom:'request'
  71. }
  72. store.dispatch('Login', loginForm).then((res) => {
  73. setTimeout(function(){
  74. store.dispatch('GetInfo').then(res => {
  75. uni.hideLoading()
  76. // 刷新当前页
  77. var routes = getCurrentPages()
  78. // console.log(routes,1)
  79. if(routes.length){
  80. var route ='/'+routes[routes.length - 1].route;
  81. var fullPath =routes[routes.length - 1].options;
  82. // console.log(fullPath,23)
  83. var options={}
  84. if(JSON.stringify(fullPath)!='{}'){
  85. if(fullPath.data){
  86. options=JSON.parse(decodeURIComponent(fullPath.data))
  87. route=route+'?data='+encodeURIComponent(JSON.stringify(options))
  88. }else{
  89. //正常循环
  90. var keystr='?'
  91. Object.keys(fullPath).some((key,idx) => {
  92. if(idx==0){
  93. keystr+=key+'='+fullPath[key]
  94. }else{
  95. keystr+='&'+key+'='+fullPath[key]
  96. }
  97. })
  98. route=route+keystr
  99. }
  100. }
  101. uni.redirectTo({
  102. url:route
  103. })
  104. }else{
  105. // var roles=this.$store.state.user.roles;
  106. // if(roles.indexOf('user')!=-1){
  107. // this.$tab.reLaunch('/pages/index/home')
  108. // }else{
  109. // this.$tab.reLaunch('/pages/index/index')
  110. // }
  111. }
  112. })
  113. },500)
  114. })
  115. }else{
  116. uni.hideLoading()
  117. store.dispatch('LogOut').then(res => {
  118. uni.reLaunch({ url: '/pages/login' })
  119. })
  120. }
  121. confirmflag = false;
  122. },2000)
  123. reject('无效的会话,或者会话已过期,请重新登录。')
  124. } else if (code === 500) {
  125. toast(msg)
  126. reject('500')
  127. } else if (code !== 200) {
  128. toast(msg)
  129. reject(code)
  130. }
  131. resolve(res.data)
  132. })
  133. .catch(error => {
  134. if(config.lhide){
  135. }else{
  136. store.commit("switch_loading",false);
  137. }
  138. uni.hideLoading()
  139. let { message } = error
  140. if (message === 'Network Error') {
  141. message = '后端接口连接异常'
  142. } else if (message.includes('timeout')) {
  143. message = '系统接口请求超时'
  144. } else if (message.includes('Request failed with status code')) {
  145. message = '系统接口' + message.substr(message.length - 3) + '异常'
  146. }
  147. toast(message)
  148. reject(error)
  149. })
  150. })
  151. }
  152. export default request