以前都是使用LeanCloud为存储,如今用传统API调用时作以下封装php
var HOST = 'http://localhost/lendoo/public/index.php/'; // 网站请求接口,统一为post function post(req) { //发起网络请求 wx.request({ url: HOST + req.uri, data: req.param, header: { "content-type": "application/x-www-form-urlencoded" }, method: 'POST', success: function (res) { req.success(res.data) }, fail: function (res) { console.log(res); } }) } // 导出模块 module.exports = { post: post }
而后前端调用就能够这样作了:前端
var http = require('../../utils/http.js'); ... http.post({ uri: http.orderListUri, param: { third_session: wx.getStorageSync('third_session') }, success: function (data) { that.setData({ orderList: data }); } });
通常对本身写的接口给本身用的时候,method方法或header都是约定好的,因此不用重复书写。网络
1 header: { 2 "content-type": "application/x-www-form-urlencoded" 3 }, 4 method: 'POST'
而fail回调方法也能够统一处理;进一步地,也能够对success回调里的针对code值进一步判断,特定错误码统一处理,好比跳转登陆页面等。session