request.js 4.5 KB

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