request.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = 10000
  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.password){
  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. strfrom:'request'
  68. }
  69. store.dispatch('Login', loginForm).then((res) => {
  70. setTimeout(function(){
  71. store.dispatch('GetInfo').then(res => {
  72. uni.hideLoading()
  73. // 刷新当前页
  74. var routes = getCurrentPages()
  75. // console.log(routes,1)
  76. if(routes.length){
  77. var route ='/'+routes[routes.length - 1].route;
  78. var fullPath =routes[routes.length - 1].options;
  79. // console.log(fullPath,23)
  80. var options={}
  81. if(JSON.stringify(fullPath)!='{}'){
  82. if(fullPath.data){
  83. options=JSON.parse(decodeURIComponent(fullPath.data))
  84. route=route+'?data='+encodeURIComponent(JSON.stringify(options))
  85. }else{
  86. //正常循环
  87. var keystr='?'
  88. Object.keys(fullPath).some((key,idx) => {
  89. if(idx==0){
  90. keystr+=key+'='+fullPath[key]
  91. }else{
  92. keystr+='&'+key+'='+fullPath[key]
  93. }
  94. })
  95. route=route+keystr
  96. }
  97. }
  98. uni.redirectTo({
  99. url:route
  100. })
  101. }else{
  102. this.$tab.reLaunch('/pages/index/index')
  103. }
  104. })
  105. },500)
  106. })
  107. }else{
  108. uni.hideLoading()
  109. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  110. if (res.confirm) {
  111. store.dispatch('LogOut').then(res => {
  112. uni.reLaunch({ url: '/pages/login' })
  113. })
  114. }
  115. })
  116. }
  117. confirmflag = false;
  118. },2000)
  119. reject('无效的会话,或者会话已过期,请重新登录。')
  120. } else if (code === 500) {
  121. toast(msg)
  122. reject('500')
  123. } else if (code !== 200) {
  124. toast(msg)
  125. reject(code)
  126. }
  127. resolve(res.data)
  128. })
  129. .catch(error => {
  130. let { message } = error
  131. if (message === 'Network Error') {
  132. message = '后端接口连接异常'
  133. } else if (message.includes('timeout')) {
  134. message = '系统接口请求超时'
  135. } else if (message.includes('Request failed with status code')) {
  136. message = '系统接口' + message.substr(message.length - 3) + '异常'
  137. }
  138. toast(message)
  139. reject(error)
  140. })
  141. })
  142. }
  143. export default request