request.js 5.1 KB

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