api.js 1.4 KB

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