无遮无挡三级动态图,熟女人妻高清一区二区三区,午夜精品一区二区三区 ,色吧成人网,无码爆乳护士让我爽

私域電商軟件及相關增值服務提供商    

免費試用

小程序簡單封裝http訪問網絡庫

2018-06-26|HiShop
導讀:之前都是使用LeanCloud為存儲,現在用傳統(tǒng)API調用時做封裝 ...

之前都是使用LeanCloud為存儲,現在用傳統(tǒng)API調用時做如下封裝

小程序簡單封裝http訪問網絡庫

文檔出處:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html

代碼如下:

  1. var HOST = 'https://localhost/lendoo/public/index.php/';
  2. // 網站請求接口,統(tǒng)一為post
  3.  
  4. function post(req) {
  5. //發(fā)起網絡請求
  6. wx.request({
  7. url: HOST + req.uri,
  8. data: req.param,
  9. header: {
  10. "content-type": "application/x-www-form-urlencoded"
  11. },
  12. method: 'POST',
  13. success: function (res) {
  14. req.success(res.data)
  15. },
  16. fail: function (res) {
  17. console.log(res);
  18. }
  19. })
  20. }
  21. // 導出模塊
  22.  
  23. module.exports = { post: post
  24. }

然后前端調用就可以這樣做了:

  1. var http = require('../../utils/http.js');
  2. ...
  3. http.post({
  4. uri: http.orderListUri,
  5. param: {
  6. third_session: wx.getStorageSync('third_session')
  7. },
  8. success: function (data) {
  9. that.setData({
  10. orderList: data
  11. });
  12. }
  13. });

一般對自己寫的接口給自己用的時候,method方法或header都是約定好的,所以不用重復書寫。

  1. header: {
  2. "content-type": "application/x-www-form-urlencoded"
  3. },
  4. method: 'POST'

而fail回調方法也可以統(tǒng)一處理;進一步地,也可以對success回調里的針對code值進一步判斷,特定錯誤碼統(tǒng)一處理,比如跳轉登錄頁面等。

經過上述處理,是不是變得簡潔了?

電話咨詢 預約演示 0元開店