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