request.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //测试地址
  2. /* const baseURL = 'http://api.dms.66km.com.cn/'
  3. const burl2='http://api.dms.66km.com.cn/' */
  4. const baseURL = 'http://192.168.0.132:20189/'
  5. const burl2='http://192.168.0.132:20189/'
  6. //66正式
  7. /* const baseURL = 'https://apidms.66km.com/'
  8. const burl2='https://apidms.66km.com/' */
  9. //欧洲维修
  10. /* const baseURL = 'http://edms.eurorepar.cn/'
  11. const burl2='http://edms.eurorepar.cn/ ' */
  12. //安徒生
  13. /* const baseURL = 'http://dms-api.lhrhy.cn/'
  14. const burl2='http://dms-api.lhrhy.cn/' */
  15. /* const baseURL = 'http://store-api.qdbtl.cn/'
  16. const burl2='http://store-api.qdbtl.cn/' */
  17. /* const baseURL = 'https://store-api.qdbtl.cn/'
  18. const burl2='https://store-api.qdbtl.cnm/' */
  19. /* const baseURL = '//store-api.qdbtl.cn/'
  20. const burl2='//store-api.qdbtl.cnm/' */
  21. // import md5 from './MD5.js'
  22. const fetchPost = (url = '', params = {}, header = {
  23. }) => {
  24. return new Promise((resolve, reject) => {
  25. header['Content-Type']='application/x-www-form-urlencoded';
  26. header['code'] = code
  27. params = {
  28. ...params,
  29. _t: new Date().getTime()
  30. }
  31. uni.request({
  32. method: 'POST',
  33. url: baseURL + url,
  34. data: params,
  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==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. const fetchGet = (url = '', params = {}, header = {
  63. }) => {
  64. return new Promise((resolve, reject) => {
  65. header['Content-Type']='application/x-www-form-urlencoded';
  66. header['code'] = code
  67. params = {
  68. ...params,
  69. _t: new Date().getTime()
  70. }
  71. uni.request({
  72. method: 'GET',
  73. url: baseURL + url,
  74. data: params,
  75. header: header,
  76. dataType: 'json',
  77. }).then((response) => {
  78. setTimeout(function() {
  79. uni.hideLoading();
  80. }, 200);
  81. let [error, res] = response;
  82. resolve(res.data);
  83. }).catch(error => {
  84. let [err, res] = error;
  85. reject(err)
  86. })
  87. });
  88. }
  89. const http = (url = '', date = {}, type = 'POST', header = {
  90. }) => {
  91. return new Promise((resolve, reject) => {
  92. header={};
  93. //header['token']='98A9FED8814B457288166BDF623F33D1';
  94. //header['uid']='795957B6-8F4B-4E7F-96FE-1F26E5BA21A1';
  95. header['Content-Type']='application/x-www-form-urlencoded';
  96. console.log(baseURL + url)
  97. uni.request({
  98. method: type,
  99. url: baseURL + url,
  100. data: date,
  101. header: header,
  102. dataType: 'json',
  103. }).then((response) => {
  104. /* setTimeout(function() {
  105. uni.hideLoading();
  106. }, 200); */
  107. let [error, res] = response;
  108. if(res.data.code==401||res.data.code==403){
  109. uni.showToast({
  110. title: res.data.msg,
  111. icon:'none',
  112. duration: 3000,
  113. });
  114. uni.clearStorageSync()
  115. /* uni.navigateTo({
  116. url:'../login/login'
  117. }) */
  118. }else if(res.data.code==409){
  119. // getToken(url,date,type,header)
  120. }
  121. resolve(res.data);
  122. }).catch(error => {
  123. let [err, res] = error;
  124. reject(err)
  125. })
  126. });
  127. }
  128. function getToken(url,date,type,header){
  129. var header={};
  130. //var userId=uni.getStorageSync("devData").userId;
  131. //var devData=uni.getStorageSync("devData")
  132. header['Content-Type']='application/x-www-form-urlencoded';
  133. uni.request({
  134. method: "POST",
  135. url: baseURL + '/sys/getTokenByUserID',
  136. data: {
  137. userId:userId
  138. },
  139. header: header,
  140. dataType: 'json',
  141. }).then((response) => {
  142. let [error, res] = response;
  143. var accessToken=res.data.data.accessToken.accessToken;
  144. devData.accessToken=accessToken;
  145. uni.setStorage({
  146. key: 'devData',
  147. data:devData,
  148. success: function () {
  149. refresh(url,date,type,header) //刷新接口
  150. }
  151. });
  152. // console.log(accessToken)
  153. }).catch(error => {
  154. })
  155. }
  156. function refresh(url,date,type,header){
  157. uni.request({
  158. method: type,
  159. url: baseURL + url,
  160. data: date,
  161. header: header,
  162. dataType: 'json',
  163. }).then((response) => {
  164. setTimeout(function() {
  165. uni.hideLoading();
  166. }, 200);
  167. let [error, res] = response;
  168. if(res.data.code==403){
  169. uni.showToast({
  170. title: res.data.msg,
  171. icon:'none',
  172. duration: 3000,
  173. });
  174. uni.navigateTo({
  175. url:'../login/login'
  176. })
  177. }
  178. resolve(res.data);
  179. }).catch(error => {
  180. let [err, res] = error;
  181. reject(err)
  182. })
  183. }
  184. export default {
  185. http,baseURL,fetchGet,fetchPost
  186. }