request.js 4.6 KB

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