request.js 3.8 KB

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