request.js 5.2 KB

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