request.js 5.2 KB

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