request.js 3.8 KB

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