獲取微信OpenId
先獲取code
再通過code獲取authtoken,從authtoken中取出openid給前臺
微信端一定不要忘記設定網(wǎng)頁賬號中的授權回調(diào)頁面域名
流程圖如下
主要代碼
頁面js代碼
/* 寫cookie */ function setCookie(name, value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/"; } /* 讀cookie */ function getCookie(name) { var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) { return unescape(arr[2]); } return null; } /* 獲取URL參數(shù) */ function getUrlParams(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null; } /* 獲取openid */ function getOpenId(url) { var openid = getCookie("usropenid"); if (openid == null) { openid = getUrlParams('openid'); alert("openid="+openid); if (openid == null) { window.location.href = "wxcode?url=" + url; } else { setCookie("usropenid", openid); } } }
WxCodeServlet代碼
//訪問微信獲取code @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String state = req.getParameter("url"); //WxOpenIdServlet的地址 String redirect ="http://"+Configure.SITE+"/wxopenid"; redirect = URLEncoder.encode(redirect, "utf-8"); StringBuffer url = new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?appid=") .append(Configure.APP_ID).append("&redirect_uri=").append(redirect) .append("&response_type=code&scope=snsapi_base&state=").append(state).append("#wechat_redirect"); resp.sendRedirect(url.toString()); }
WxOpenIdServlet代碼
//訪問微信獲取openid @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String code = req.getParameter("code"); String state = req.getParameter("state"); Result ret = new Result(); AuthToken token = WXUtil.getAuthToken(code); if(null != token.getOpenid()){ ret.setCode(0); log.info("====openid=="+token.getOpenid()); Map<String,String> map = new HashMap<String,String>(); map.put("openid", token.getOpenid()); map.put("state", state); ret.setData(map); }else{ ret.setCode(-1); ret.setMsg("登錄錯誤"); } String redUrl = state+"?openid="+token.getOpenid(); resp.sendRedirect(redUrl); }
獲取AuthToken(WXUtil.getAuthToken(code))代碼
public static AuthToken getAuthToken(String code){ AuthToken vo = null; try { String uri = "https://api.weixin.qq.com/sns/oauth2/access_token?"; StringBuffer url = new StringBuffer(uri); url.append("appid=").append(Configure.APP_ID); url.append("&secret=").append(Configure.APP_SECRET); url.append("&code=").append(code); url.append("&grant_type=").append("authorization_code"); HttpURLConnection conn = HttpClientUtil.CreatePostHttpConnection(url.toString()); InputStream input = null; if (conn.getResponseCode() == 200) { input = conn.getInputStream(); } else { input = conn.getErrorStream(); } vo = JSON.parseObject(new String(HttpClientUtil.readInputStream(input),"utf-8"),AuthToken.class); } catch (Exception e) { log.error("getAuthToken error", e); } return vo; }
HttpClientUtil類
package com.huatek.shebao.util; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; public class HttpClientUtil { // 設置body體 public static void setBodyParameter(String sb, HttpURLConnection conn) throws IOException { DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.writeBytes(sb); out.flush(); out.close(); } // 添加簽名header public static HttpURLConnection CreatePostHttpConnection(String uri) throws MalformedURLException, IOException, ProtocolException { URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setInstanceFollowRedirects(true); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setRequestProperty("Content-Type","application/json"); conn.setRequestProperty("Accept-Charset", "utf-8"); conn.setRequestProperty("contentType", "utf-8"); return conn; } public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } }
封裝AuthToken的VO類
package com.huatek.shebao.wxpay; public class AuthToken { private String access_token; private Long expires_in; private String refresh_token; private String openid; private String scope; private String unionid; private Long errcode; private String errmsg; public String getAccess_token() { return access_token; } public void setAccess_token(String access_token) { this.access_token = access_token; } public Long getExpires_in() { return expires_in; } public void setExpires_in(Long expires_in) { this.expires_in = expires_in; } public String getRefresh_token() { return refresh_token; } public void setRefresh_token(String refresh_token) { this.refresh_token = refresh_token; } public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } public String getUnionid() { return unionid; } public void setUnionid(String unionid) { this.unionid = unionid; } public Long getErrcode() { return errcode; } public void setErrcode(Long errcode) { this.errcode = errcode; } public String getErrmsg() { return errmsg; } public void setErrmsg(String errmsg) { this.errmsg = errmsg; } }
? ?

熱AI工具

Undress AI Tool
免費脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機

Video Face Swap
使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)

抖音網(wǎng)頁版的登錄入口是https://www.douyin.com/。登錄步驟包括:1.打開瀏覽器;2.輸入網(wǎng)址https://www.douyin.com/;3.點擊“登錄”按鈕并選擇登錄方式;4.輸入賬號密碼;5.完成登錄。網(wǎng)頁版提供了瀏覽、搜索、互動、上傳視頻和個人主頁管理等功能,具有大屏幕體驗、多任務處理、便捷的賬號管理和數(shù)據(jù)統(tǒng)計等優(yōu)勢。

拷貝漫畫無疑是一個不容錯過的寶藏。在這里,你可以找到各種風格的籃球漫畫,從熱血勵志的競技故事,到輕松幽默的日常喜劇,應有盡有。無論是想重溫經(jīng)典,還是想發(fā)掘新作,拷貝漫畫都能滿足你的需求。通過拷貝漫畫提供的正版在線閱讀入口,你將告別盜版資源的困擾,享受高清流暢的閱讀體驗,更能支持你喜愛的漫畫作者,為正版漫畫的發(fā)展貢獻一份力量。

選擇UC瀏覽器還是QQ瀏覽器取決于你的需求:1.UC瀏覽器適合追求快速加載和豐富娛樂功能的用戶;2.QQ瀏覽器適合需要穩(wěn)定性和與騰訊產(chǎn)品無縫連接的用戶。

結(jié)合 2025 年最新行業(yè)動態(tài)與多維度評測數(shù)據(jù),以下為綜合排名前十的 AI 寫作軟件推薦,涵蓋通用創(chuàng)作、學術研究、商業(yè)營銷等主流場景,同時兼顧中文優(yōu)化與本地化服務:

奈斯漫畫,一個致力于為漫畫愛好者打造的沉浸式閱讀體驗平臺,匯聚了海量國內(nèi)外優(yōu)質(zhì)漫畫資源。它不僅僅是一個漫畫閱讀平臺,更是一個連接漫畫家與讀者、分享漫畫文化的社區(qū)。通過簡潔直觀的界面設計和強大的搜索功能,奈斯漫畫讓你能夠輕松找到心儀的作品,享受流暢舒適的閱讀體驗。告別漫長的等待和繁瑣的操作,即刻進入奈斯漫畫的世界,開啟你的漫畫之旅吧!

蛙漫漫畫,憑借其豐富多元的漫畫資源和便捷流暢的在線閱讀體驗,已成為眾多漫畫愛好者的首選。它就像一個充滿活力的池塘,源源不斷地涌現(xiàn)出新鮮有趣的故事,等待著你去發(fā)現(xiàn)和探索。蛙漫漫畫涵蓋了各種題材,從熱血冒險到甜蜜戀愛,從奇幻科幻到懸疑推理,無論你喜歡哪種類型,都能在這里找到心儀的作品。其簡潔直觀的界面設計,更讓你能夠輕松上手,快速找到想看的漫畫,沉浸在精彩紛呈的漫畫世界中。

在這里,您可以盡情暢游于浩瀚的漫畫海洋,探索各種題材和風格的作品,從熱血激昂的少年漫,到細膩動人的少女漫,從懸疑燒腦的推理漫,到輕松搞笑的日常漫,應有盡有,總有一款能夠觸動您的心弦。我們不僅擁有海量的正版漫畫資源,還不斷引進和更新最新的作品,確保您能夠第一時間閱讀到您喜愛的漫畫。

2025b安最新官網(wǎng)入口地址:https://www.marketwebb.co/zh-CN/join?ref=507720986&type=wenzi;幣安(Binance)交易所是一家全球性的加密貨幣交易所,服務包括北美、歐洲、臺灣、中東、香港、馬來西亞在內(nèi)的180個國家地區(qū),提供超過600種加密貨幣,在全球擁有2.7億注冊用戶。
