request.js 4.2 KB

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