request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { getHashQuery } from '../utils/index.js'
  2. var url='http://'+getHashQuery('url')+'/'
  3. //const baseUrl = url
  4. console.log(getHashQuery('url'))
  5. if(getHashQuery('url')){
  6. var baseUrl =url
  7. }else{
  8. var baseUrl = 'http://api.dms.66km.com.cn/'
  9. }
  10. const burl2=url
  11. import md5 from './MD5.js'
  12. var token=getHashQuery('token')
  13. console.log(token)
  14. var uid=getHashQuery('uid')
  15. console.log(uid)
  16. const http = (url = '', date = {}, type = 'POST', header = {
  17. }) => {
  18. return new Promise((resolve, reject) => {
  19. header={};
  20. if(token){
  21. header['accessToken']=token;
  22. header['uid']=uid;
  23. }else{
  24. header['accessToken']='';
  25. header['uid']='';
  26. }
  27. //header['token']='98A9FED8814B457288166BDF623F33D1';
  28. //header['uid']='795957B6-8F4B-4E7F-96FE-1F26E5BA21A1';
  29. header['Content-Type']='application/x-www-form-urlencoded';
  30. //console.log(header)
  31. uni.request({
  32. method: type,
  33. url: baseUrl + url,
  34. data: date,
  35. header: header,
  36. dataType: 'json',
  37. }).then((response) => {
  38. /* setTimeout(function() {
  39. uni.hideLoading();
  40. }, 200); */
  41. let [error, res] = response;
  42. if(res.data.code==1){
  43. uni.showToast({
  44. title: res.data.msg,
  45. icon:'none',
  46. duration: 3000,
  47. });
  48. // uni.clearStorageSync()
  49. }else if(res.data.code==409){
  50. // getToken(url,date,type,header)
  51. }
  52. resolve(res.data);
  53. }).catch(error => {
  54. let [err, res] = error;
  55. reject(err)
  56. })
  57. });
  58. }
  59. function getToken(url,date,type,header){
  60. var header={};
  61. //var userId=uni.getStorageSync("devData").userId;
  62. //var devData=uni.getStorageSync("devData")
  63. header['Content-Type']='application/x-www-form-urlencoded';
  64. uni.request({
  65. method: "POST",
  66. url: burl2 + '/sys/getTokenByUserID',
  67. data: {
  68. userId:userId
  69. },
  70. header: header,
  71. dataType: 'json',
  72. }).then((response) => {
  73. let [error, res] = response;
  74. var accessToken=res.data.data.accessToken.accessToken;
  75. devData.accessToken=accessToken;
  76. uni.setStorage({
  77. key: 'devData',
  78. data:devData,
  79. success: function () {
  80. refresh(url,date,type,header) //刷新接口
  81. }
  82. });
  83. // console.log(accessToken)
  84. }).catch(error => {
  85. })
  86. }
  87. function refresh(url,date,type,header){
  88. uni.request({
  89. method: type,
  90. url: burl2 + url,
  91. data: date,
  92. header: header,
  93. dataType: 'json',
  94. }).then((response) => {
  95. setTimeout(function() {
  96. uni.hideLoading();
  97. }, 200);
  98. let [error, res] = response;
  99. if(res.data.code==403){
  100. uni.showToast({
  101. title: res.data.msg,
  102. icon:'none',
  103. duration: 3000,
  104. });
  105. uni.navigateTo({
  106. url:'../login/login'
  107. })
  108. }
  109. resolve(res.data);
  110. }).catch(error => {
  111. let [err, res] = error;
  112. reject(err)
  113. })
  114. }
  115. export default {
  116. http,baseUrl
  117. }