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