request.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //测试地址
  2. const baseUrl = 'http://58.56.15.138:20201/'
  3. const burl2='http://58.56.15.138:20201/'
  4. //正式地址
  5. /* const baseUrl = 'https://apiclt.66km.com/'
  6. const burl2='https://apiclt.66km.com/' */
  7. import md5 from './MD5.js'
  8. /* const http = (url = '', date = {}, type = 'POST', header = {
  9. }) => {
  10. return new Promise((resolve, reject) => {
  11. console.log(burl2 + url)
  12. //header['Content-Type']='application/x-www-form-urlencoded';
  13. header['actionFromMiniApp']=1;
  14. uni.request({
  15. method: type,
  16. url: burl2 + url,
  17. data: date,
  18. header: header,
  19. dataType: 'json',
  20. }).then((response) => {
  21. setTimeout(function() {
  22. uni.hideLoading();
  23. }, 200);
  24. let [error, res] = response;
  25. if(res.data.code==403){
  26. uni.showToast({
  27. title: res.data.msg,
  28. icon:'none',
  29. duration: 3000,
  30. });
  31. uni.clearStorageSync()
  32. uni.navigateTo({
  33. url:'../login/login'
  34. })
  35. }else if(res.data.code==409){
  36. getToken(url,date,type,header)
  37. }
  38. resolve(res.data);
  39. }).catch(error => {
  40. let [err, res] = error;
  41. reject(err)
  42. })
  43. });
  44. }
  45. */
  46. const http = (url = '', date = {}, type = 'POST', header = {
  47. }) => {
  48. return new Promise((resolve, reject) => {
  49. console.log(burl2 + url)
  50. header={};
  51. const logodata = uni.getStorageSync("logodata");
  52. header['token']=logodata.token;
  53. header['uid']=logodata.uid;
  54. //header['token']='98A9FED8814B457288166BDF623F33D1';
  55. //header['uid']='39415962-C9D3-466D-A73F-5B5429A8DB2A';
  56. header['Content-Type']='application/x-www-form-urlencoded';
  57. //console.log(header)
  58. uni.request({
  59. method: type,
  60. url: baseUrl + url,
  61. data: date,
  62. header: header,
  63. dataType: 'json',
  64. }).then((response) => {
  65. /* setTimeout(function() {
  66. uni.hideLoading();
  67. }, 200); */
  68. let [error, res] = response;
  69. if(res.data.code==401||res.data.code==403){
  70. uni.showToast({
  71. title: res.data.msg,
  72. icon:'none',
  73. duration: 3000,
  74. });
  75. uni.clearStorageSync()
  76. uni.navigateTo({
  77. url:'../login/login'
  78. })
  79. }else if(res.data.code==409){
  80. // getToken(url,date,type,header)
  81. }
  82. resolve(res.data);
  83. }).catch(error => {
  84. let [err, res] = error;
  85. reject(err)
  86. })
  87. });
  88. }
  89. // 请求图片流
  90. const httpBinary = (url = '', date = {}, type = 'POST', header = {
  91. }) => {
  92. return new Promise((resolve, reject) => {
  93. console.log(burl2 + url)
  94. header={};
  95. const logodata = uni.getStorageSync("logodata");
  96. header['token']=logodata.token;
  97. header['uid']=logodata.uid;
  98. //header['token']='98A9FED8814B457288166BDF623F33D1';
  99. //header['uid']='39415962-C9D3-466D-A73F-5B5429A8DB2A';
  100. header['Content-Type']='application/x-www-form-urlencoded';
  101. //console.log(header)
  102. uni.request({
  103. method: type,
  104. url: baseUrl + url,
  105. data: date,
  106. header: header,
  107. dataType: 'json',
  108. responseType: "arraybuffer",
  109. }).then((response) => {
  110. /* setTimeout(function() {
  111. uni.hideLoading();
  112. }, 200); */
  113. let [error, res] = response;
  114. if(res.data.code==401||res.data.code==403){
  115. uni.showToast({
  116. title: res.data.msg,
  117. icon:'none',
  118. duration: 3000,
  119. });
  120. uni.clearStorageSync()
  121. uni.navigateTo({
  122. url:'../login/login'
  123. })
  124. }else if(res.data.code==409){
  125. // getToken(url,date,type,header)
  126. }
  127. resolve(res.data);
  128. }).catch(error => {
  129. let [err, res] = error;
  130. reject(err)
  131. })
  132. });
  133. }
  134. function getToken(url,date,type,header){
  135. var header={};
  136. //var userId=uni.getStorageSync("devData").userId;
  137. //var devData=uni.getStorageSync("devData")
  138. header['Content-Type']='application/x-www-form-urlencoded';
  139. uni.request({
  140. method: "POST",
  141. url: burl2 + '/sys/getTokenByUserID',
  142. data: {
  143. userId:userId
  144. },
  145. header: header,
  146. dataType: 'json',
  147. }).then((response) => {
  148. let [error, res] = response;
  149. var accessToken=res.data.data.accessToken.accessToken;
  150. devData.accessToken=accessToken;
  151. uni.setStorage({
  152. key: 'devData',
  153. data:devData,
  154. success: function () {
  155. refresh(url,date,type,header) //刷新接口
  156. }
  157. });
  158. // console.log(accessToken)
  159. }).catch(error => {
  160. })
  161. }
  162. function refresh(url,date,type,header){
  163. uni.request({
  164. method: type,
  165. url: burl2 + url,
  166. data: date,
  167. header: header,
  168. dataType: 'json',
  169. }).then((response) => {
  170. setTimeout(function() {
  171. uni.hideLoading();
  172. }, 200);
  173. let [error, res] = response;
  174. if(res.data.code==403){
  175. uni.showToast({
  176. title: res.data.msg,
  177. icon:'none',
  178. duration: 3000,
  179. });
  180. uni.navigateTo({
  181. url:'../login/login'
  182. })
  183. }
  184. resolve(res.data);
  185. }).catch(error => {
  186. let [err, res] = error;
  187. reject(err)
  188. })
  189. }
  190. export default {
  191. http,baseUrl,httpBinary
  192. }