http.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const request = function (url = "", options) {
  2. const baseUrl = getApp().globalData.http.baseUrl + "/yos/rest/index" + url;
  3. options.data.accesstoken = wx.getStorageSync('userMsg').token;
  4. return new Promise((resolve, reject) => {
  5. wx.request({
  6. url: baseUrl,
  7. method: options.method,
  8. data: options.method == "GET" ? options.data : JSON.stringify(options.data),
  9. // header这里根据业务情况自行选择需要还是不需要
  10. header: {
  11. "Content-Type": 'application/json;charset=UTF-8'
  12. },
  13. success: (res) => {
  14. if (res.data.code == -1) {
  15. wx.showToast({
  16. title: res.data.msg,
  17. icon: 'none'
  18. })
  19. resolve(res.data)
  20. } else if (res.data.code == 0) {
  21. wx.showToast({
  22. title: res.data.msg,
  23. duration: 3000,
  24. icon: 'none'
  25. })
  26. resolve(res.data)
  27. } else {
  28. resolve(res.data)
  29. }
  30. },
  31. fail: (err) => {
  32. reject(err)
  33. }
  34. })
  35. })
  36. }
  37. module.exports = {
  38. get(url, data) {
  39. return request(url, {
  40. method: "GET",
  41. data
  42. })
  43. },
  44. //封装post方法
  45. post(url, data) {
  46. return request(url, {
  47. method: "POST",
  48. data
  49. })
  50. }
  51. }