api.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. wx.hideLoading({ //因为showToast、hideLoading不能同时使用
  20. fail() { }
  21. })
  22. resolve(res.data)
  23. },
  24. fail(err) {
  25. wx.hideLoading()
  26. wx.showToast({
  27. title: '加载失败',
  28. icon: 'none'
  29. })
  30. reject(err)
  31. },
  32. complete() {
  33. wx.hideLoading({ //因为showToast、hideLoading不能同时使用
  34. fail() { }
  35. })
  36. }
  37. }).onHeadersReceived(res => {
  38. if (res && res.header && res.header.Authorization) {
  39. console.log("获取requestTask", res)
  40. wx.setStorageSync("token", res.header.Authorization);
  41. }
  42. })
  43. })
  44. }
  45. module.exports = {
  46. useApi,
  47. getBanner(data) {
  48. console.log(1);
  49. return useApi('', data)
  50. },
  51. }