溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序如何實現(xiàn)靜默登錄

發(fā)布時間:2021-05-28 10:30:42 來源:億速云 閱讀:435 作者:小新 欄目:web開發(fā)

小編給大家分享一下微信小程序如何實現(xiàn)靜默登錄,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1.通過 wx.login獲取 登錄憑證(code)

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

2.在此處獲得

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

微信小程序如何實現(xiàn)靜默登錄

如圖

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)
   //轉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 向對方或第三方發(fā)送的數(shù)據(jù),大多數(shù)情況下給對方發(fā)送JSON數(shù)據(jù)讓對方解析
  */
 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讀入,此外發(fā)送post請求必須設置這兩個
   //最常用的Http請求無非是get和post,get請求可以獲取靜態(tài)頁面,也可以把參數(shù)放在URL字串后面,傳遞給servlet,
   //post與get的 不同之處在于post的參數(shù)不是放在URL字串里面,而是放在http請求的正文內。
   conn.setDoOutput(true);
   conn.setDoInput(true);
   //獲取URLConnection對象對應的輸出流
   out = new PrintWriter(conn.getOutputStream());
   //發(fā)送請求參數(shù)即數(shù)據(jù)
   out.print(data);
   //緩沖數(shù)據(jù)
   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,鏈接會增多,直到收發(fā)不出信息。寫上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數(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("后臺收到并返回");
  }else{
   result.setMessage("參數(shù)為空");
   result.setSuccess(false);
  }
  return result;
 }
}

到這里 就能得到openid了

以上是“微信小程序如何實現(xiàn)靜默登錄”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI