api.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {
  2. prodUrl,
  3. contentType,
  4. clientId,
  5. clientSecret
  6. } from '@/common/setting'
  7. import {setUuid} from '@/utils/uuid.js'
  8. import {
  9. options
  10. } from '@/http/config.js';
  11. import {
  12. Base64
  13. } from '@/utils/base64.js';
  14. import Request from '@/utils/luch-request/index.js';
  15. const http = new Request(options);
  16. http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  17. //获取宿主App参数内容
  18. let szInfo = uni.getStorageSync('isSzApp');
  19. // 假设有token值需要在头部需要携带
  20. let accessToken = uni.getStorageSync('accessToken');
  21. if (accessToken) {
  22. config.header['Blade-Auth'] = 'bearer ' + accessToken;
  23. }
  24. if(szInfo){
  25. //判断网络环境,公安网添加请求头部
  26. if(!szInfo.extraData.internet){
  27. config.header['appCredential'] = szInfo.extraData.data.appCredential;
  28. config.header['userCredential'] =szInfo.extraData.data.userCredential;
  29. }else{
  30. //互联网输入用户名和密码
  31. config.data.username = szInfo.extraData.xwkjUserName
  32. config.data.password = szInfo.extraData.xwkjPassword
  33. }
  34. //小程序应用id
  35. config.data.appId = szInfo.extraData.appId
  36. //设置动态参数Url,
  37. config.baseURL = szInfo.extraData.data.resourceList[0].resourceAddress
  38. //拼接请求主体
  39. let dataParams ={
  40. messageId:setUuid(),
  41. verson:'1.0',
  42. parameter:{
  43. condition:{
  44. keyValueList:[{
  45. key :"params",
  46. relationOperator:'=',
  47. value:JSON.stringify(config.data)
  48. }],
  49. logicalOperate:'and'
  50. },
  51. dataObjId:szInfo.extraData.data.dataObjId,
  52. fields:"data",
  53. networkCode:szInfo.extraData.data.NetworkCode,
  54. page:{pageNo:1,pageSize:1},
  55. orderBy:[],
  56. regionalismCode:szInfo.extraData.data.resourceList[0].resourceRegionalismCode
  57. },
  58. }
  59. config.data = JSON.stringify(dataParams);
  60. }
  61. //encodeURIComponent
  62. // 安全请求header
  63. config.header['Blade-Requested-With'] = 'BladeHttpRequest';
  64. // 客户端认证参数
  65. config.header['Authorization'] = 'Basic c2FiZXI6c2FiZXJfc2VjcmV0'//'Basic ' + Base64.encode(clientId + ':' + clientSecret);
  66. // #ifndef H5
  67. let url = config.url
  68. if (process.env.NODE_ENV == 'development' && !url.startsWith("http")) {
  69. url = url.substring(url.indexOf('/api') + 4)
  70. config.url = url
  71. }
  72. // #endif
  73. // 额外参数
  74. // config.data = config.data || {};
  75. // config.data.pf = uni.getSystemInfoSync().platform;
  76. // config.data.sys = uni.getSystemInfoSync().system;
  77. // 演示custom 用处
  78. // if (config.custom.auth) {
  79. // config.header.token = 'token'
  80. // }
  81. // if (config.custom.loading) {
  82. // uni.showLoading()
  83. // }
  84. /**
  85. /* 演示
  86. if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
  87. return Promise.reject(config)
  88. }
  89. **/
  90. //console.log(config,'config')
  91. return config
  92. }, config => { // 可使用async await 做异步操作
  93. return Promise.reject(config)
  94. })
  95. http.interceptors.response.use((response) => {
  96. //console.log(response,'response')
  97. // 若有数据返回则通过
  98. if (response.statusCode==200) {
  99. return response.data
  100. }
  101. // 服务端返回的状态码不等于200,则reject()
  102. if (response.statusCode !== 200) {
  103. uni.showToast({
  104. title: response.data.msg,
  105. icon: 'none'
  106. });
  107. return Promise.reject(response);
  108. }
  109. return response.data;
  110. }, (response) => {
  111. /* 对响应错误做点什么 (statusCode !== 200)*/
  112. // uni.showToast({
  113. // title: response.data.msg,
  114. // icon: 'none'
  115. // });
  116. if (response.statusCode == 401) {
  117. const pages = getCurrentPages()
  118. const currentPage = pages[pages.length - 1]
  119. uni.redirectTo({
  120. url: `/pages/login/login-account?redirect=/${currentPage.route}`
  121. })
  122. }
  123. return Promise.reject(response)
  124. })
  125. export default http;