request.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // 请求后端服务器工具类
  2. import Vue from 'vue'
  3. import { TOKEN_NAME } from "./cacheConstant";
  4. const baseURL = "http://114.115.167.11:8081/api"
  5. export function request(url, data, method) {
  6. console.log(url,data,method)
  7. // 赋值token
  8. const token = Vue.prototype.$token
  9. console.log()
  10. console.log(!token)
  11. if(token){
  12. var hander = {
  13. 'Authorization': Vue.prototype.$token,
  14. }
  15. }else{
  16. var hander = {}
  17. }
  18. // 默认执行get方法
  19. method = method===undefined ? 'post' : method
  20. wx.showNavigationBarLoading()
  21. return new Promise((resove, reject) => {
  22. uni.request({
  23. url: baseURL + url,
  24. method,
  25. data,
  26. header: hander,
  27. success: function (res) {
  28. const data = res.data
  29. // 对错误进行拦截提示
  30. if (data.code != 200) {
  31. uni.showToast({
  32. title: data.msg,
  33. icon: 'none',
  34. duration: 2000
  35. })
  36. reject()
  37. }
  38. resove(data.data);
  39. },
  40. fail: function (msg) {
  41. console.log('请求失败=======', msg)
  42. uni.showToast({
  43. title: "网络异常,连接服务器失败",
  44. icon: 'none',
  45. duration: 2000
  46. })
  47. reject()
  48. },
  49. complete: function () {
  50. uni.hideNavigationBarLoading();
  51. }
  52. })
  53. })
  54. }