request.js 4.9 KB

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