request.js 5.5 KB

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