将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)})
或者await:
let res = await app.callFunction({name:'login',data:{x:1,y:2}})
console.log(res)
传统后台:nginx+nodejs+koa2+mongo
nginx定义一条路由location:'https://xxx.com/applet/'
koa定义一条路由router:'login'
封装wx.request后,提示 unknown: Unexpected reserved word 'await'. (86:17).