request.js 4.9 KB

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