微信小程序靜默登錄的實(shí)現(xiàn)代碼
1.通過(guò) wx.login獲取 登錄憑證(code)
wx.login({ success: function (res) { console.log(res.code); } })
2.在此處獲得
appid 和 secret :https://developers.weixin.qq.com/sandbox
如圖
3.小程序端
http://127.0.0.1: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) //轉(zhuǎn)json var j= JSON.parse(res.data.result) //獲取到openid that.globalData.openid = j.openid; } }) } }) } })
4.后臺(tái)代碼
工具類
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 { /** * 調(diào)用對(duì)方接口方法 * @param path 對(duì)方或第三方提供的路徑 * @param data 向?qū)Ψ交虻谌桨l(fā)送的數(shù)據(jù),大多數(shù)情況下給對(duì)方發(fā)送JSON數(shù)據(jù)讓對(duì)方解析 */ 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; //請(qǐng)求方式 // conn.setRequestMethod("POST"); // //設(shè)置通用的請(qǐng)求屬性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); //設(shè)置是否向httpUrlConnection輸出,設(shè)置是否從httpUrlConnection讀入,此外發(fā)送post請(qǐng)求必須設(shè)置這兩個(gè) //最常用的Http請(qǐng)求無(wú)非是get和post,get請(qǐng)求可以獲取靜態(tài)頁(yè)面,也可以把參數(shù)放在URL字串后面,傳遞給servlet, //post與get的 不同之處在于post的參數(shù)不是放在URL字串里面,而是放在http請(qǐng)求的正文內(nèi)。 conn.setDoOutput(true); conn.setDoInput(true); //獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流 out = new PrintWriter(conn.getOutputStream()); //發(fā)送請(qǐng)求參數(shù)即數(shù)據(jù) out.print(data); //緩沖數(shù)據(jù) out.flush(); //獲取URLConnection對(duì)象對(duì)應(yīng)的輸入流 InputStream is = conn.getInputStream(); //構(gòu)造一個(gè)字符流緩存 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = ""; while ((str = br.readLine()) != null) { openId=str; System.out.println(str); } //關(guān)閉流 is.close(); //斷開連接,最好寫上,disconnect是在底層tcp socket鏈接空閑時(shí)才切斷。如果正在被其他線程使用就不切斷。 //固定多線程的話,如果不disconnect,鏈接會(huì)增多,直到收發(fā)不出信息。寫上disconnect后正常一些。 conn.disconnect(); System.out.println("完整結(jié)束"); } catch (Exception e) { e.printStackTrace(); } return openId; } public static String GetOpenID(String appid,String appsecret,String Code) { //臨時(shí)登錄憑證 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"); /* 設(shè)置響應(yīng)頭允許ajax跨域訪問 */ response.setHeader("Access-Control-Allow-Origin", "*"); /* 星號(hào)表示所有的異域請(qǐng)求都可以接受, */ response.setHeader("Access-Control-Allow-Methods", "GET,POST"); //轉(zhuǎn)成json數(shù)據(jù) 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("后臺(tái)收到并返回"); }else{ result.setMessage("參數(shù)為空"); result.setSuccess(false); } return result; } }
到這里 就能得到openid了
總結(jié)
以上所述是小編給大家介紹的微信小程序靜默登入的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
javascript 折半查找字符在數(shù)組中的位置(有序列表)
折半查找字符在數(shù)組中的位置(有序列表),需要的朋友可以參考下。2010-12-12跟我學(xué)習(xí)javascript的undefined與null
跟我學(xué)習(xí)javascript的undefined與null,從定義上理解null和undefined,告訴大家提高undefined性能的方法,感興趣的小伙伴們可以參考一下2015-11-11javascript發(fā)送短信驗(yàn)證碼實(shí)現(xiàn)代碼
我們?cè)谧?cè)賬號(hào),或者是參加活動(dòng)時(shí),都會(huì)向手機(jī)發(fā)送收短信驗(yàn)證碼,短信驗(yàn)證碼到底是如何實(shí)現(xiàn)的,本文為大家揭曉,并為大家分項(xiàng)1javascript發(fā)送短信驗(yàn)證碼實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2015-11-11Js利用console計(jì)算代碼運(yùn)行時(shí)間的方法示例
最近看了一本書,發(fā)現(xiàn)了個(gè)計(jì)算代碼執(zhí)行時(shí)間的方法,感覺還挺有用的,所以這篇文章主要給大家介紹了關(guān)于Javascript利用console計(jì)算代碼運(yùn)行時(shí)間的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-09-09一步一步封裝自己的HtmlHelper組件BootstrapHelper(三)
一步一步封裝自己的HtmlHelper組件:BootstrapHelper,系列文章第三篇,感興趣的小伙伴們可以參考一下2016-09-09使用setTimeout實(shí)現(xiàn)SetInterval原理解析
這篇文章主要為大家介紹了使用setTimeout實(shí)現(xiàn)SetInterval原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10原生js的數(shù)組除重復(fù)簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇原生js的數(shù)組除重復(fù)簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05JS實(shí)現(xiàn)文字鏈接感應(yīng)鼠標(biāo)淡入淡出改變顏色的方法
這篇文章主要介紹了JS實(shí)現(xiàn)文字鏈接感應(yīng)鼠標(biāo)淡入淡出改變顏色的方法,實(shí)例分析了javascript操作鼠標(biāo)事件及css樣式的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02