request.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. var confirmflag = config.confirmflag
  9. // 获取账号密码
  10. var newObj=JSON.parse(JSON.stringify(uni.getStorageSync('account')))
  11. var username=newObj.username;
  12. var password=newObj.password;
  13. var captchaEnabled=newObj.captchaEnabled;
  14. // 自动登录
  15. const request = config => {
  16. // 是否需要设置 token
  17. const isToken = (config.headers || {}).isToken === false
  18. config.header = config.header || {}
  19. if (getToken() && !isToken) {
  20. config.header['Authorization'] = 'Bearer ' + getToken()
  21. }
  22. // get请求映射params参数
  23. if (config.params) {
  24. let url = config.url + '?' + tansParams(config.params)
  25. url = url.slice(0, -1)
  26. config.url = url
  27. }
  28. return new Promise((resolve, reject) => {
  29. uni.request({
  30. method: config.method || 'get',
  31. timeout: config.timeout || timeout,
  32. url: config.baseUrl || baseUrl + config.url,
  33. data: config.data,
  34. header: config.header,
  35. dataType: 'json'
  36. }).then(response => {
  37. let [error, res] = response
  38. if (error) {
  39. toast('后端接口连接异常')
  40. reject('后端接口连接异常')
  41. return
  42. }
  43. const code = res.data.code || 200
  44. const msg = errorCode[code] || res.data.msg || errorCode['default']
  45. if (code === 401) {
  46. uni.showLoading({
  47. title: '加载中'
  48. });
  49. // console.log(confirmflag,85)
  50. if(confirmflag){
  51. // 利用 return 终止函数继续运行
  52. return false;
  53. }
  54. // console.log('按钮点击函数执行');
  55. // 执行函数
  56. confirmflag = true;
  57. setTimeout(()=>{
  58. // 获取store值
  59. var autologin=store.state.user.autologin;
  60. if(username&&password&&!captchaEnabled&&autologin){
  61. // 自动登录
  62. var loginForm={
  63. username:username,
  64. password:password,
  65. strfrom:'request'
  66. }
  67. store.dispatch('Login', loginForm).then((res) => {
  68. setTimeout(function(){
  69. store.dispatch('GetInfo').then(res => {
  70. uni.hideLoading()
  71. // 刷新当前页
  72. var routes = getCurrentPages()
  73. if(routes.length){
  74. var route ='/'+routes[routes.length - 1].route;
  75. var fullPath =routes[routes.length - 1].options;
  76. // console.log(fullPath,23)
  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. // console.log(route,852)
  96. uni.redirectTo({
  97. url:route
  98. })
  99. }else{
  100. var userType=res.user.userType;
  101. var newArr=userType.split(',')
  102. var num=0,b=0;
  103. for(var i=0;i<newArr.length;i++){
  104. if(newArr[i]=='发改委'){
  105. num=1
  106. break;
  107. }
  108. if(newArr[i]=='招商'){
  109. b=1
  110. break;
  111. }
  112. }
  113. if(num==1){
  114. if(b==1){
  115. this.$tab.reLaunch('/pages/index')
  116. }else{
  117. this.$tab.reLaunch('/pages/indexs')
  118. }
  119. }else{
  120. this.$tab.reLaunch('/pages/index')
  121. }
  122. }
  123. })
  124. },500)
  125. })
  126. }else{
  127. uni.hideLoading()
  128. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  129. if (res.confirm) {
  130. store.dispatch('LogOut').then(res => {
  131. uni.reLaunch({ url: '/pages/login' })
  132. })
  133. }
  134. })
  135. }
  136. // 改变 confirmflag
  137. confirmflag = false;
  138. }, 2000);
  139. reject('无效的会话,或者会话已过期,请重新登录。')
  140. } else if (code === 500) {
  141. toast(msg)
  142. reject('500')
  143. } else if (code !== 200) {
  144. toast(msg)
  145. reject(code)
  146. }
  147. resolve(res.data)
  148. })
  149. .catch(error => {
  150. let { message } = error
  151. if (message === 'Network Error') {
  152. message = '后端接口连接异常'
  153. } else if (message.includes('timeout')) {
  154. message = '系统接口请求超时'
  155. } else if (message.includes('Request failed with status code')) {
  156. message = '系统接口' + message.substr(message.length - 3) + '异常'
  157. }
  158. toast(message)
  159. reject(error)
  160. })
  161. })
  162. }
  163. export default request