request.js 3.8 KB

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