request.js 4.3 KB

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