request.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //测试地址
  2. // const baseURL = 'http://192.168.0.149:7782/'
  3. /*const burl2='http://api.dms.66km.com.cn/' */
  4. //66正式
  5. /* const baseURL = 'https://apidms.66km.com/'
  6. const burl2='https://apidms.66km.com/' */
  7. //欧洲维修
  8. const baseURL = 'http://zsapi.66km.cn/'
  9. // const burl2='http://edms.eurorepar.cn/ '
  10. // import md5 from './MD5.js'
  11. const fetchPost = (url = '', params = {}, header = {
  12. }) => {
  13. return new Promise((resolve, reject) => {
  14. header['Content-Type']='application/x-www-form-urlencoded';
  15. const openId = uni.getStorageSync("openId");
  16. header['openId']=openId;
  17. uni.request({
  18. method: 'POST',
  19. url: baseURL + url,
  20. data: params,
  21. header: header,
  22. dataType: 'json',
  23. }).then((response) => {
  24. setTimeout(function() {
  25. uni.hideLoading();
  26. }, 200);
  27. let [error, res] = response;
  28. if(res.data.code==1000){
  29. location.href = baseURL + '/weChat/authorize'
  30. }
  31. resolve(res.data);
  32. }).catch(error => {
  33. let [err, res] = error;
  34. reject(err)
  35. })
  36. });
  37. }
  38. const fetchGet = (url = '', params = {}, header = {
  39. }) => {
  40. return new Promise((resolve, reject) => {
  41. header['Content-Type']='application/x-www-form-urlencoded';
  42. header['code'] = code
  43. params = {
  44. ...params,
  45. _t: new Date().getTime()
  46. }
  47. uni.request({
  48. method: 'GET',
  49. url: baseURL + url,
  50. data: params,
  51. header: header,
  52. dataType: 'json',
  53. }).then((response) => {
  54. setTimeout(function() {
  55. uni.hideLoading();
  56. }, 200);
  57. if(res.data.code==1000){
  58. location.href = baseURL + '/weChat/authorize'
  59. }
  60. let [error, res] = response;
  61. resolve(res.data);
  62. }).catch(error => {
  63. let [err, res] = error;
  64. reject(err)
  65. })
  66. });
  67. }
  68. var that=this;
  69. const http = (url = '', date = {}, type = 'POST', header = {
  70. }) => {
  71. return new Promise((resolve, reject) => {
  72. header={};
  73. const openId = uni.getStorageSync("openId");
  74. //console.log(that.$store)
  75. //header['token']='98A9FED8814B457288166BDF623F33D1';
  76. header['openId']=openId;
  77. header['Content-Type']='application/x-www-form-urlencoded';
  78. console.log(baseURL + url)
  79. uni.request({
  80. method: type,
  81. url: baseURL+'search?action=' + url,
  82. data: date,
  83. header: header,
  84. dataType: 'json',
  85. }).then((response) => {
  86. /* setTimeout(function() {
  87. uni.hideLoading();
  88. }, 200); */
  89. let [error, res] = response;
  90. if(res.data.code==1000){
  91. location.href = baseURL + '/weChat/authorize'
  92. }
  93. resolve(res.data);
  94. }).catch(error => {
  95. let [err, res] = error;
  96. reject(err)
  97. })
  98. });
  99. }
  100. function getToken(url,date,type,header){
  101. var header={};
  102. //var userId=uni.getStorageSync("devData").userId;
  103. //var devData=uni.getStorageSync("devData")
  104. header['Content-Type']='application/x-www-form-urlencoded';
  105. uni.request({
  106. method: "POST",
  107. url: baseURL + '/sys/getTokenByUserID',
  108. data: {
  109. userId:userId
  110. },
  111. header: header,
  112. dataType: 'json',
  113. }).then((response) => {
  114. let [error, res] = response;
  115. var accessToken=res.data.data.accessToken.accessToken;
  116. devData.accessToken=accessToken;
  117. uni.setStorage({
  118. key: 'devData',
  119. data:devData,
  120. success: function () {
  121. refresh(url,date,type,header) //刷新接口
  122. }
  123. });
  124. // console.log(accessToken)
  125. }).catch(error => {
  126. })
  127. }
  128. function refresh(url,date,type,header){
  129. uni.request({
  130. method: type,
  131. url: baseURL + url,
  132. data: date,
  133. header: header,
  134. dataType: 'json',
  135. }).then((response) => {
  136. setTimeout(function() {
  137. uni.hideLoading();
  138. }, 200);
  139. let [error, res] = response;
  140. if(res.data.code==403){
  141. uni.showToast({
  142. title: res.data.msg,
  143. icon:'none',
  144. duration: 3000,
  145. });
  146. uni.navigateTo({
  147. url:'../login/login'
  148. })
  149. }
  150. resolve(res.data);
  151. }).catch(error => {
  152. let [err, res] = error;
  153. reject(err)
  154. })
  155. }
  156. export default {
  157. http,baseURL,fetchGet,fetchPost
  158. }