微信小程序靜默登錄的實(shí)現(xiàn)代碼
1.通過 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)求無非是get和post,get請(qǐng)求可以獲取靜態(tài)頁面,也可以把參數(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)文章
uniapp實(shí)現(xiàn)tabs切換(可滑動(dòng))效果實(shí)例
這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)tabs切換(可滑動(dòng))效果的相關(guān)資料,tabs切換無論是在app端還是小程序或者H5頁面都是很常見的功能,文中通過實(shí)例代碼介紹的很詳細(xì),需要的朋友可以參考下2023-07-07
Microsfot .NET Framework4.0框架 安裝失敗的解決方法
今天在安裝.NET Framework 4.0,安裝了半天結(jié)果提示未安裝成功,提示原因是服務(wù)未啟動(dòng)了,error code: (0x80070643), "安裝時(shí)發(fā)生嚴(yán)重錯(cuò)誤 "2013-08-08
基于JS實(shí)現(xiàn)9種不同的面包屑和分布式多步驟導(dǎo)航效果
本文是小編給大家分享的基于js實(shí)現(xiàn)的9種不同風(fēng)格的面包屑和分布式多步驟導(dǎo)航效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-02-02
微信小程序使用uni-app和springboot實(shí)現(xiàn)一鍵登錄功能(JWT鑒權(quán))
微信一鍵登錄是指用戶在使用小程序時(shí),可以通過微信賬號(hào)進(jìn)行快速登錄,而無需額外的注冊(cè)和密碼設(shè)置,這篇文章主要給大家介紹了關(guān)于微信小程序使用uni-app和springboot實(shí)現(xiàn)一鍵登錄功能的相關(guān)資料,需要的朋友可以參考下2023-11-11
如何使用js正則表達(dá)式驗(yàn)證文件夾名是否符合規(guī)范
眾所周知正則表達(dá)式非常強(qiáng)大,下面這篇文章主要給大家介紹了關(guān)于如何使用js正則表達(dá)式驗(yàn)證文件夾名是否符合規(guī)范的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Bootstrap導(dǎo)航中表單簡單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Bootstrap導(dǎo)航中表單簡單實(shí)現(xiàn)代碼,含路徑導(dǎo)航的具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

