http.js 1.4 KB

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