request.js 4.8 KB

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