1. 程式人生 > 其它 >小程式實現請求介面資料

小程式實現請求介面資料

程式碼實現

1、新建檔案請求介面域名

2、封裝介面

  2-1、方法一:新建一個api.js

var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
  return typeof e;
} : function(e) {
  return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof
e; }, _rewx = require("./rewx.js"), app = getApp(),IndexData = function (i) { return (0, _rewx.post)("/applet/index/index", i).then(function (o) { return new Promise(function (e, n) { e(o); }); }); },
orderList= function (i) {
  return (0, _rewx.post)("/applet/order/index", i).then(function (o) {
    return new Promise(function (e, n) {
      e(o);
    });
  });
}
module.exports={ IndexData:IndexData ,orderList:orderList
}

  目錄檔案呼叫

(0, _api.IndexData)().then(function (t) {
   console.log(t)
   that.setData({

   })
});

  2-2、方法二:目錄檔案直接使用(get/post請求及帶引數請求)

 (0, _rewx.post)("/applet/store/getdetail", { store_id: 1 }).then(function (t) {
    console.log(t)
    that.setData({
       
    })
});

(
0, _rewx.get)("/applet/index/index").then(function (t) { console.log(t) that.setData({ }) });

  2-3、方法三:直接請求

  wx.request({
      url: 'https://xxx.com/applet/store/getdetail', 
      data: {
        x: ''
      },//get/post請求引數
      method:"POST",
      header: {
        'content-type': 'application/json' // 預設值
      },
      success: function(res) {
        console.log(res.data)
      }
    })

具體程式碼已經放到了GitHub,有需要的小夥伴可以直接下載

https://github.com/sryhx/smallProduct