request.js 4.7 KB

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