api.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // api接口请求封装
  2. var useApi = (url, data = {}, method = "get") => {
  3. var that=this;
  4. var requrestTask=wx.request
  5. wx.showLoading({
  6. title: '数据加载中...',
  7. })
  8. return new Promise((resolve, reject) => {
  9. requrestTask ({
  10. url: `${url}`, // 云端正式服
  11. data,
  12. method,
  13. header: {
  14. 'Authorization': 'Bearer '+wx.getStorageSync('token') || '',
  15. 'content-type': 'application/json',
  16. },
  17. dataType: 'json', // 添加这个配置
  18. success(res) {
  19. resolve(res.data)
  20. },
  21. fail(err) {
  22. // wx.hideLoading()
  23. wx.showToast({
  24. title: '加载失败',
  25. icon: 'none'
  26. })
  27. reject(err)
  28. },
  29. complete() {
  30. wx.hideLoading(333)
  31. wx.hideLoading({ //因为showToast、hideLoading不能同时使用
  32. fail() {}
  33. })
  34. }
  35. }).onHeadersReceived(res=>{
  36. if(res && res.header && res.header.Authorization) {
  37. console.log("获取requestTask",res)
  38. wx.setStorageSync("token", res.header.Authorization);
  39. }
  40. })
  41. })
  42. }
  43. module.exports = {
  44. useApi,
  45. getBanner(data) {
  46. console.log(1);
  47. return useApi('', data)
  48. },
  49. }