request.js 4.5 KB

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