request.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import Vue from 'vue'
  2. const config = {
  3. timeout: 15000,
  4. baseURL: 'http://wdkl.natapp1.cc',
  5. headers: {
  6. 'Content-Type':'application/x-www-form-urlencoded'
  7. }
  8. }
  9. export const Method = {
  10. GET: 'GET',
  11. POST: 'POST',
  12. PUT: 'PUT',
  13. DELETE: 'DELETE'
  14. }
  15. function service(options) {
  16. let url = config.baseURL + '/' + options.url
  17. if (options.url.includes("http")){
  18. url = options.url
  19. }
  20. let headers = config.headers
  21. if (options.config.isJson){
  22. headers['Content-Type']='application/json'
  23. } else {
  24. headers['Content-Type']='application/x-www-form-urlencoded'
  25. }
  26. if (options.config.loading){
  27. uni.showLoading()
  28. }
  29. return new Promise((resolve,reject)=>{
  30. // console.log(options, config);
  31. uni.request({
  32. url: url,
  33. data: options.params,
  34. header: headers,
  35. timeout: config.timeout,
  36. method: options.config.method,
  37. success(res) {
  38. uni.hideLoading()
  39. if (res.statusCode == 200){
  40. resolve(res.data)
  41. } else{
  42. if (url.indexOf("/wx_mobile") === -1) {
  43. Vue.prototype.$message.error(res.data.message)
  44. }
  45. reject(res)
  46. }
  47. },
  48. fail(err){
  49. uni.hideLoading()
  50. console.log(err);
  51. Vue.prototype.$message.error(err.data.message)
  52. reject(err)
  53. }
  54. });
  55. })
  56. }
  57. export default function request(options){
  58. return service(options)
  59. }