api.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. //测试专用,非必要不要打开
  10. // data.user_id=738;
  11. // data.test="future";
  12. requrestTask({
  13. url: `${url}`, // 云端正式服
  14. data,
  15. method,
  16. header: {
  17. 'Authorization': 'Bearer ' + wx.getStorageSync('token') || '',
  18. 'content-type': 'application/json',
  19. },
  20. dataType: 'json', // 添加这个配置
  21. success(res) {
  22. wx.hideLoading({ //因为showToast、hideLoading不能同时使用
  23. fail() { }
  24. })
  25. resolve(res.data)
  26. },
  27. fail(err) {
  28. wx.hideLoading()
  29. wx.showToast({
  30. title: '加载失败',
  31. icon: 'none'
  32. })
  33. reject(err)
  34. },
  35. complete() {
  36. wx.hideLoading({ //因为showToast、hideLoading不能同时使用
  37. fail() { }
  38. })
  39. }
  40. }).onHeadersReceived(res => {
  41. if (res && res.header && res.header.Authorization) {
  42. console.log("获取requestTask", res)
  43. wx.setStorageSync("token", res.header.Authorization);
  44. }
  45. })
  46. })
  47. }
  48. module.exports = {
  49. useApi,
  50. getBanner(data) {
  51. console.log(1);
  52. return useApi('', data)
  53. },
  54. }