微信小程序静默登录的实现代码

1.通过 wx.login获取 登录凭证(code)

wx.login({ success: function (res) { console.log(res.code); } })

2.在此处获得

appid 和 secret :https://developers.weixin.qq.com/sandbox

微信小程序静默登录的实现代码

如图

3.小程序端

:8080/jeecg-boot 这一段是自己的访问路径

//app.js App({ globalData: { appid: '', appsecret: '',// openid: '' } onLaunch: function () { var that =this; // 登录 wx.login({ success: function (res) { console.log(res.code) wx.request({ url: 'http://127.0.0.1:8080/jeecg-boot/hwork/hworkLog/GetOpenIdServlet', data: { appid: that.globalData.appid, secret: that.globalData.appsecret, js_code: res.code, grant_type: 'authorization_code' }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function (res) { console.log(res) //转json var j= JSON.parse(res.data.result) //获取到openid that.globalData.openid = j.openid; } }) } }) } })

4.后台代码

工具类

package org.jeecg.modules.hworkorder.util; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; public class WeChatService { /** * 调用对方接口方法 * @param path 对方或第三方提供的路径 * @param data 向对方或第三方发送的数据,大多数情况下给对方发送JSON数据让对方解析 */ public static String interfaceUtil(String path,String data) { String openId=""; try { URL url = new URL(path); //打开和url之间的连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); PrintWriter out = null; //请求方式 // conn.setRequestMethod("POST"); // //设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); //设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 //最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, //post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。 conn.setDoOutput(true); conn.setDoInput(true); //获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); //发送请求参数即数据 out.print(data); //缓冲数据 out.flush(); //获取URLConnection对象对应的输入流 InputStream is = conn.getInputStream(); //构造一个字符流缓存 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = ""; while ((str = br.readLine()) != null) { openId=str; System.out.println(str); } //关闭流 is.close(); //断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。 //固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。 conn.disconnect(); System.out.println("完整结束"); } catch (Exception e) { e.printStackTrace(); } return openId; } public static String GetOpenID(String appid,String appsecret,String Code) { //临时登录凭证 String URL = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+appsecret+"&js_code="+Code+"&grant_type=authorization_code"; String openId=interfaceUtil(URL, ""); return openId; } }

@RestController @RequestMapping("/hwork/hworkLog") @Slf4j public class hworkLogContrller { @RequestMapping(value = "/GetOpenIdServlet", method = RequestMethod.POST) public Result<String> GetOpenIdServlet(HttpServletRequest request, HttpServletResponse response){ Result<String> result=new Result<String>(); response.setContentType("text/html;charset=utf-8"); /* 设置响应头允许ajax跨域访问 */ response.setHeader("Access-Control-Allow-Origin", "*"); /* 星号表示所有的异域请求都可以接受, */ response.setHeader("Access-Control-Allow-Methods", "GET,POST"); //转成json数据 String appid=request.getParameter("appid"); String secret=request.getParameter("secret"); String js_code=request.getParameter("js_code"); if(appid!=null&&appid!=""&&secret!=null&&secret!=""&&js_code!=null&&js_code!=""){ WeChatService getOpenId=new WeChatService(); String openId=getOpenId.GetOpenID(appid,secret,js_code); result.setResult(openId); result.setMessage("后台收到并返回"); }else{ result.setMessage("参数为空"); result.setSuccess(false); } return result; } }

到这里 就能得到openid了

总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2a1a5c139cc578c0c2a12b88eae9eb43.html