request.js 4.8 KB

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