wx.login({ //wx.login 會返回code
success: res => {
if (res.code) {//如果res.code包含參數則為true 執行下一步
wx.request({//發送 res.code 到后台換取 openId, sessionKey, unionId
url: '接口地址url', //僅為示例,並非真實的接口地址
method: "GET",//方法get / post
data: {
"code": res.code
},
header: {//設置請求頭
'content-type': 'application/json' // 默認值
},
// 小程序包含多種接收方式 sueccess(成功) / fail(失敗)/ complent(成功/失敗)
success: function (res) {//請求成功接收res
if (res.data.openid) {
wx.setStorage({
key: "tokenId",
data: res.data.openid,
})
}
}
})
}
}
})