custom-request111.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * 自定义封装http请求
  3. * 在此处可以自定义其他的请求方法
  4. * 直接在main.js中引入挂载即可在页面中使用
  5. * 也可以引入到/modules中,使用$u进行api的挂载
  6. * 建议基于uniapp的request进行封装,请勿使用其他三方非uniapp请求
  7. */
  8. export const VUE_APP_API_URL = ''
  9. export const httpRequest = (opts, data) => {
  10. let httpDefaultOpts = {
  11. url: baseUrl + opts.url,
  12. data: data,
  13. method: opts.method,
  14. header: opts.method == 'GET' ? {
  15. 'X-Requested-With': 'XMLHttpRequest',
  16. "Accept": "application/json",
  17. "Content-Type": "application/json; charset=UTF-8"
  18. } : {
  19. 'X-Requested-With': 'XMLHttpRequest',
  20. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  21. },
  22. dataType: 'json',
  23. }
  24. let promise = new Promise(function(resolve, reject) {
  25. uni.request(httpDefaultOpts).then(
  26. (res) => {
  27. resolve(res[1])
  28. }
  29. ).catch(
  30. (response) => {
  31. reject(response)
  32. }
  33. )
  34. })
  35. return promise
  36. };
  37. export const httpRequestUploadFile = (opts, filePath, data) => {
  38. let token = "";
  39. uni.getStorage({
  40. key: 'token',
  41. success: function(ress) {
  42. token = ress.data
  43. }
  44. });
  45. // token="dd0847da213958adf77e88a2cfb661e5"
  46. let httpDefaultOpts = {
  47. url: baseUrl + opts.url,
  48. header: {
  49. token: token
  50. },
  51. name: "file",
  52. filePath: filePath,
  53. name: 'file',
  54. formData: data,
  55. }
  56. let promise = new Promise(function(resolve, reject) {
  57. uni.uploadFile(httpDefaultOpts).then(
  58. (res) => {
  59. resolve(res[1])
  60. }
  61. ).catch(
  62. (response) => {
  63. reject(response)
  64. }
  65. )
  66. })
  67. return promise
  68. };
  69. //带Token请求
  70. export const httpTokenRequest = (opts, data) => {
  71. let token = "";
  72. uni.getStorage({
  73. key: 'token',
  74. success: function(ress) {
  75. token = ress.data
  76. }
  77. });
  78. // 测试用
  79. token = "dd0847da213958adf77e88a2cfb661e5"
  80. //此token是登录成功后后台返回保存在storage中的
  81. let httpDefaultOpts = {
  82. url: baseUrl + opts.url,
  83. data: data,
  84. method: opts.method,
  85. header: opts.method == 'GET' ? {
  86. 'Token': token,
  87. 'X-Requested-With': 'XMLHttpRequest',
  88. "Accept": "application/json",
  89. "Content-Type": "application/json; charset=UTF-8"
  90. } : {
  91. 'Token': token,
  92. 'X-Requested-With': 'XMLHttpRequest',
  93. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  94. },
  95. dataType: 'json',
  96. }
  97. let promise = new Promise(function(resolve, reject) {
  98. uni.request(httpDefaultOpts).then(
  99. (res) => {
  100. resolve(res[1])
  101. }
  102. ).catch(
  103. (response) => {
  104. reject(response)
  105. }
  106. )
  107. })
  108. return promise
  109. };
  110. //自定义请求
  111. export const httpCustomeRequest = (opts, data) => {
  112. let httpDefaultOpts = {
  113. url: opts.url,
  114. data: data,
  115. method: opts.method,
  116. header: opts.method == 'GET' ? {
  117. 'X-Requested-With': 'XMLHttpRequest',
  118. "Accept": "application/json",
  119. "Content-Type": "application/json; charset=UTF-8"
  120. } : {
  121. 'X-Requested-With': 'XMLHttpRequest',
  122. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  123. },
  124. dataType: 'json',
  125. }
  126. let promise = new Promise(function(resolve, reject) {
  127. uni.request(httpDefaultOpts).then(
  128. (res) => {
  129. resolve(res[1])
  130. }
  131. ).catch(
  132. (response) => {
  133. reject(response)
  134. }
  135. )
  136. })
  137. return promise
  138. };