wx.request包装成云函数格式,以便将来一键迁移。
将wx.request包装成云函数格式:
云函数的调用格式是:wx.cloud.callFunction({name:'login',data:{x:1,y:2}}).then()
仿云函数的调用格式是:app.callFunction({name:'login',data:{x:1,y:2}}).then()
两者一模一样。
在app.js里:
url: 'https://xxx.com/applet/',
callFunction: function(args) {
return new Promise((resolve, reject) => {
wx.request({
url: this.url + args.name,
method: 'POST',
data: {
sessionid: this.getSessionid(),//换取openid
...args.data
},
success: resolve,
fail: reject
})
})
},
page里index.js
app.callFunction({name:'login',data:{x:1,y:2}}).then(res=>{console.log(res)})
传统后台:nginx+nodejs+koa2+mongo
nginx定义一条路由:'https://xxx.com/applet/'
koa定义一条路由:'login'