request.js 4.7 KB

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