1.获取微信登录凭证
wx.login({success: (res) => {this.setData({wxCode: res.code,})}
2.登录凭证传回后端获取openid,前端直接调用接口获取openid正式上线代码审核过不去。
wx.request({ url:'http://localhost:8087/controll/getOpenID/'+this.data.wxCode, method: 'GET',success: (res) => {if(res.statusCode == "200"){this.setData({wxOpenId: res.data.openid})}else{wx.showToast({title: res.statusCode,icon: 'none',duration: 2000//持续的时间})}}})
3.后端代码(直接返回的openid中含有secret,可发布,但是会提醒你存在安全漏洞,所以对openid进行截取拼接)
@CrossOrigin@GetMapping("/getOpenID/{code}")public String getAppID(@PathVariable String code) {String result= HttpUtil.get("https://api./sns/jscode2session?appid="+ "AppID" + "&secret=" + "AppSecret"+ "&js_code=" + code + "&grant_type=authorization_code");// return result.openid;result="{\""+result.substring(result.indexOf("openid"));return result;}
3.获取AppSecret和AppID
登录微信公众平台
开发——>开发管理——>开发设置
AppSecret重置后记住