欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序靜默登錄的實(shí)現(xiàn)代碼

 更新時(shí)間:2020年01月08日 15:11:08   作者:Hxning.  
這篇文章主要介紹了微信小程序靜默登錄的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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)文章

最新評(píng)論