request.js 5.3 KB

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