request.js 5.1 KB

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