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

小程序登錄之支付寶授權(quán)的實(shí)現(xiàn)示例

 更新時(shí)間:2019年12月13日 10:46:41   作者:子欽加油  
這篇文章主要介紹了小程序登錄之支付寶授權(quán)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

眾所周知,微信小程序是可以通過(guò)微信本身授權(quán)后再登錄,平臺(tái)可以拿到微信用的的賬號(hào)相關(guān)信息,然后保存到數(shù)據(jù)庫(kù)中,那么同理在支付寶小程序開(kāi)發(fā)過(guò)程中,登錄功能的設(shè)計(jì)也可以如此

上圖是官方提供的時(shí)序圖,具體看一下流程:

在小程序端獲取 auth_code,目的是獲取用戶(hù)授權(quán)碼

把第一步獲取的授權(quán)碼 auth_code 傳到咱們自己的后臺(tái),也就是說(shuō)后臺(tái)需要編寫(xiě)一個(gè)接口,方便小程序端的傳入

var me = this;
  my.getAuthCode({
   scopes: 'auth_user', // 主動(dòng)授權(quán)(彈框):auth_user,靜默授權(quán)(不彈框):auth_base
   success: (res) => {
    if (res.authCode) {
     // console.log(app.serverUrl + '/login/' + res.authCode);
     // 調(diào)用自己的服務(wù)端接口,讓服務(wù)端進(jìn)行后端的授權(quán)認(rèn)證
     my.httpRequest({
      url: app.serverUrl + '/login/' + res.authCode,
      method: 'POST',
      header:{
       'content-type': 'application/json'
      },
      dataType: 'json',
      success: (res) => {
       // 授權(quán)成功并且服務(wù)器端登錄成功
       console.log(res);
       me.setData({
        userInfo: res.data.data
       });
      }
     });
    }
   },
  });

后臺(tái)拿到這個(gè) auth_code 之后,需要調(diào)用支付寶的授權(quán)平臺(tái),從而獲取用戶(hù)的唯一 token 以及 支付寶的userid,都是唯一的,調(diào)用的接口為 [alipay.system.oauth.token]

獲取到userid后,判斷一下這個(gè)userid是否在我們自己的數(shù)據(jù)庫(kù)中存在,如果存在,直接獲取信息,并且直接返回用戶(hù)對(duì)象到前臺(tái);如果不存在,則需要從支付寶授權(quán)平臺(tái)再一次去獲取支付寶用戶(hù)的信息。

​調(diào)用 [alipay.user.info.share],獲取用戶(hù)信息,這個(gè)用戶(hù)對(duì)象里包含了大量的用戶(hù)真實(shí)信息,具體參考如下

@Autowired
  private UserService userService;

  @ApiOperation(value = "統(tǒng)一登錄接口", notes = "支付寶小程序喚起登錄后調(diào)用", httpMethod = "POST")
  @PostMapping("/login/{authCode}")
  public IMoocJSONResult items(
      @ApiParam(name = "authCode", 
      value = "授權(quán)碼", 
      required = true, 
      example = "授權(quán)碼") @PathVariable String authCode) throws Exception {

    // 1. 服務(wù)端獲取access_token、user_id
    AlipaySystemOauthTokenResponse response = getAccessToken(authCode);
    if (response.isSuccess()) {
      System.out.println("獲取access_token - 調(diào)用成功");
      /**
       * 獲取到用戶(hù)信息后保存到數(shù)據(jù)
       * 1. 如果數(shù)據(jù)庫(kù)不存在對(duì)用的 alipayUserId, 則注冊(cè)
       * 2. 如果存在,則獲取數(shù)據(jù)庫(kù)中的信息再返回
       */
      String accessToken = response.getAccessToken();
      String alipayUserId = response.getUserId();
      System.out.println("accessToken:" + accessToken);
      System.out.println("alipayUserId:" + alipayUserId);
      
      // 2. 查詢(xún)?cè)撚脩?hù)是否存在
      Users userInfo = userService.queryUserIsExist(alipayUserId);
      if (userInfo != null) {
        // 如果用戶(hù)存在,直接返回給前端,表示登錄成功
        return IMoocJSONResult.ok(userInfo);
      } else {
        // 如果用戶(hù)不存在,則通過(guò)支付寶api獲取用戶(hù)的信息后,再注冊(cè)用戶(hù)到自己平臺(tái)數(shù)據(jù)庫(kù)
        // 獲取會(huì)員信息
        AlipayUserInfoShareResponse aliUserInfo = getAliUserInfo(accessToken);
        if (aliUserInfo != null) {
           Users newUser = new Users();
           newUser.setAlipayUserId(alipayUserId);
           newUser.setNickname(aliUserInfo.getNickName());
           newUser.setRegistTime(new Date());
           newUser.setIsCertified(aliUserInfo.getIsCertified().equals("T") ? 1 : 0);
           newUser.setFaceImage(aliUserInfo.getAvatar());
           userService.createUser(newUser);
           return IMoocJSONResult.ok(newUser);
        }
      }
    } else {
      System.out.println("獲取access_token - 調(diào)用失敗");
    }
    return IMoocJSONResult.ok();
  }
  
  // 服務(wù)端獲取access_token、user_id
  private AlipaySystemOauthTokenResponse getAccessToken(String authCode) throws Exception {
    AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", 
        APPID,          // 1. 填入appid
        PRIVATE_KEY,      // 2. 填入私鑰 
        "json", 
        "GBK", 
        ALIPAY_PUBLIC_KEY,     // 3. 填入公鑰
        "RSA2");
    AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
    request.setGrantType("authorization_code");
    request.setCode(authCode);    // 4. 填入前端傳入的授權(quán)碼authCode
    request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");  // 0. 不用管
    AlipaySystemOauthTokenResponse response = alipayClient.execute(request);

    return response;
  }
    
  // 獲取支付寶用戶(hù)信息
  private AlipayUserInfoShareResponse getAliUserInfo (String accessToken) throws Exception {
    AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
        APPID,          // 1. 填入appid
        PRIVATE_KEY,      // 2. 填入私鑰 
        "json", 
        "GBK", 
        ALIPAY_PUBLIC_KEY,     // 3. 填入公鑰
        "RSA2");
    AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
    AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken);
    if(response.isSuccess()){
      System.out.println("獲取會(huì)員信息 - 調(diào)用成功");
      return response;
    }

    return null;
  }

拿到的支付寶用戶(hù)信息如圖:

最終頁(yè)面的展示效果為:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論