Coffee_Candy

113 分类: nodejs后端开发,微信小程序开发篇

微信小程序获取openid

微信小程序前端代码
传一个code给后端

   wx.login({
  success:(res:any)=>{
    let code=res.code
    wx.request({
      url:url+"xcxAdmin/getopenid",
      method:"POST",
      data:JSON.stringify({code:code}),
      header:{
        'content-type': 'application/json',
        'Authorization':wx.getStorageSync('token'),
      },
      success(res:any)
      {
        console.log(res.data)
      },
      fail(err:any)
      {
        console.log(err);
      }
    });
  }
})

nodejs后端
grant_type="authorization_code"
里面其他参数填自己的

app.post('/xcxAdmin/getopenid',async(req:any,res:any)=>{
console.log(req.body)
let url = "https://api.weixin.qq.com/sns/jscode2session?grant_type="+grant_type+"&appid="+appid+"&secret="+appsecret+"&js_code="+req.body.code;
const response=await axios.get(url)
console.log(response.data)
res.send({openid:response.data.openid})
})

2024-10-15T02:22:52.png

#none

作者: Coffee_Candy

版权: 除特别声明,均采用BY-NC-SA 4.0许可协议,转载请表明出处

目录Content

评论已关闭