欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

springboot中怎么獲取用戶openid

這篇文章將為大家詳細講解有關(guān)springboot中怎么獲取用戶openid,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)公司是專業(yè)的鶴壁網(wǎng)站建設(shè)公司,鶴壁接單;提供成都網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行鶴壁網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

openid可以標(biāo)識一個用戶,session_key會變,所以來獲取一下openid。

openid不能在微信小程序中直接獲取,需要后臺發(fā)送請求到微信的接口,然后微信返回一個json格式的字符串到后臺,后臺處理之后,再返回到微信小程序。

發(fā)布的小程序需要https的域名,而測試的時候可以使用http。

小程序在app.js中,修改login()中的內(nèi)容:

// 登錄  wx.login({   success: res => {    // 發(fā)送 res.code 到后臺換取 openId, sessionKey, unionId    if (res.code) {     wx.request({      url: 'http://localhost:84/user/login',      method: 'POST',      data: {       code: res.code      },      header: {       'content-type': 'application/x-www-form-urlencoded'      },      success(res) {       console.log("openid:"+res.data.openid);       if (res.data.openid != "" || res.data.openid!=null){        // 登錄成功        wx.setStorageSync("openid", res.data.openid);//將用戶id保存到緩存中        wx.setStorageSync("session_key", res.data.session_key);//將session_key保存到緩存中       }else{        // 登錄失敗        // TODO 跳轉(zhuǎn)到錯誤頁面,要求用戶重試        return false;       }      }     })    } else {     console.log('獲取用戶登錄態(tài)失?。?#39; + res.errMsg)    }   }  })

這里請求的http://localhost:84/user/login

后臺的處理類:

package com.ft.feathertrade.handler;import com.fasterxml.jackson.databind.ObjectMapper;import com.ft.feathertrade.entity.OpenIdJson;import com.ft.feathertrade.utils.HttpUtil;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.io.IOException;@RestControllerpublic class LoginHandler {  private String appID = "";  private String appSecret = "";  @PostMapping("/user/login")  public String userLogin(@RequestParam("code") String code) throws IOException {    String result = "";    try{//請求微信服務(wù)器,用code換取openid。HttpUtil是工具類,后面會給出實現(xiàn),Configure類是小程序配置信息,后面會給出代碼      result = HttpUtil.doGet(          "https://api.weixin.qq.com/sns/jscode2session?appid="              + this.appID + "&secret="              + this.appSecret + "&js_code="              + code              + "&grant_type=authorization_code", null);    }    catch (Exception e) {      e.printStackTrace();    }    ObjectMapper mapper = new ObjectMapper();    OpenIdJson openIdJson = mapper.readValue(result,OpenIdJson.class);    System.out.println(result.toString());    System.out.println(openIdJson.getOpenid());    return result;  }}

HttpUtil工具類:

package com.ft.feathertrade.utils;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.HashMap;import java.util.Map.Entry;import java.util.Set;import org.apache.commons.httpclient.HttpStatus;//此類需要添加maven依賴或jar包/** 將此依賴添加到pom.xml中 <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> **/public class HttpUtil {  public static String doGet(String urlPath, HashMap<String, Object> params)      throws Exception {    StringBuilder sb = new StringBuilder(urlPath);    if (params != null && !params.isEmpty()) { // 說明有參數(shù)      sb.append("?");      Set<Entry<String, Object>> set = params.entrySet();      for (Entry<String, Object> entry : set) { // 遍歷map里面的參數(shù)        String key = entry.getKey();        String value = "";        if (null != entry.getValue()) {          value = entry.getValue().toString();          // 轉(zhuǎn)碼          value = URLEncoder.encode(value, "UTF-8");        }        sb.append(key).append("=").append(value).append("&");      }      sb.deleteCharAt(sb.length() - 1); // 刪除最后一個&    }    // System.out.println(sb.toString());    URL url = new URL(sb.toString());    HttpURLConnection conn = (HttpURLConnection) url.openConnection();    conn.setConnectTimeout(5000); // 5s超時    conn.setRequestMethod("GET");    if (conn.getResponseCode() == HttpStatus.SC_OK) {// HttpStatus.SC_OK ==      // 200      BufferedReader reader = new BufferedReader(new InputStreamReader(          conn.getInputStream()));      StringBuilder sbs = new StringBuilder();      String line;      while ((line = reader.readLine()) != null) {        sbs.append(line);      }      return sbs.toString();    }    return null;  }}

OpenIdJson的實體類:

package com.ft.feathertrade.entity;public class OpenIdJson {  private String openid;  private String session_key;  public String getOpenid() {    return openid;  }  public void setOpenid(String openid) {    this.openid = openid;  }  public String getSession_key() {    return session_key;  }  public void setSession_key(String session_key) {    this.session_key = session_key;  }}

關(guān)于springboot中怎么獲取用戶openid就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)站名稱:springboot中怎么獲取用戶openid
轉(zhuǎn)載源于:http://chinadenli.net/article46/giijeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈手機網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作微信公眾號、微信小程序Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)