request.js 4.1 KB

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