위챗 애플릿 로그인

3051 단어 위챗 애플릿

 

위챗 애플릿 위챗 로그인을 실현하는 전제에서 당신은 위챗 공중 플랫폼 애플릿 계정을 등록해야만 아래의 조작을 할 수 있습니다!(애플릿 로그인이 기업 위챗 로그인보다 간단할 것 같다)


말을 많이 하지 않고 코드를 말하다.


위챗 애플릿에서 로그인 시작


 

--------------------------------------------------------------------------------------------------------------------------


현재 로그인은 button 형식으로 Open-type='getUserInfo'bindgetuser info='doLogin'두 가지 중요한 속성을 부여합니다
 doLogin :function(e){
  console.log(e)
  wx.login({
    success: function(res){
        console.log(res)
        //        
        var code = res.code;
        //            session_key   openID
        wx.request({
          url: 'http://localhost:8080/wxLogin?code='+code,
          method:"post",
          success: function (resule){
            console.log(resule);
          }
        })
    }
  })  
  }

그리고 js에 가서 위챗 애플릿login 방법을 호출해서 백엔드 인터페이스에 접근합니다
 
 

----------------------------------------------java 백엔드 코드---------------------------------------------------------------------------------------------

@PostMapping("/wxLogin")
	public IMoocJSONResult wxLogin(String code) {
        String url = "https://api.weixin.qq.com/sns/jscode2session";
		Map param = new HashMap<>();
		param.put("appid", "          id");
		param.put("secret", "            ");
		param.put("js_code", code);
		param.put("grant_type", "authorization_code");
		String wxResult = this.doGet(url, param);
		System.out.println(wxResult);
        //wxResult      openid    session_key      json  
		WXSessionModel model = JsonUtils.jsonToPojo(wxResult, WXSessionModel.class);
		return IMoocJSONResult.ok(model);
	}




    public static String doGet(String url, Map param) {

		//   Httpclient  
		CloseableHttpClient httpclient = HttpClients.createDefault();

		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			//   uri
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();

			//   http GET  
			HttpGet httpGet = new HttpGet(uri);

			//     
			response = httpclient.execute(httpGet);
			//          200
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}

직접 cv, 이상은 위챗 애플릿이 로그인을 얻는 절차와 실현


 


 
 
 

좋은 웹페이지 즐겨찾기