钉钉小程序免登获取用户信息流程

一、背景

登录钉钉后访问钉钉企业内部的一个小程序,在访问小程序时候不需要登录,且将当前用户信息获取到

二、解决方案

钉钉小程序免登获取用户信息流程_第1张图片

三、代码

1、获取钉钉小程序的授权码
//获取授权码
    getAuthCode() {
      my.getAuthCode({
        scopes: 'auth_user',

        success: (authCodeRes) => {

          console.log("authcode============", authCodeRes.authCode)
          this.httpReq('登录中...', 'http://localhost:8081/beauty/login', 'post', { requestAuthCode: authCodeRes.authCode }).then(res => { this.saveStorage(res.data.data.userId, res.data.data.name) }),
            my.alert({
              content: authCodeRes.authCode,
            });
        },
      });

    }

authCodeRes.authCode即为授权码

2、获取token

Java代码

public static String getToken() throws ApiException {
        String token = tokenCache.get(TOKEN_KEY);
        if (StringUtils.isBlank(token)) {
            logger.info("缓存中token已失效,重新获取。");
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
//            request.setAppkey(appKey);
//            request.setAppsecret(appSecret);
            request.setAppkey("xxxxx");
            request.setAppsecret("yyyyy");
            request.setHttpMethod("GET");

            OapiGettokenResponse response = client.execute(request);
            if (response.getErrcode() != 0) {
                throw new ApiException(response.getErrorCode(), response.getErrmsg());
            }
            token = response.getAccessToken();
            tokenCache.put(TOKEN_KEY, token);
        }
        logger.info("token============================>",token);
        return token;

    }
3、根据token和authcode获取userId

java代码

public static DingUserInfo getUserInfoByCode(String requestAuthCode) throws ApiException {
        logger.info("requestAuthCode====================>",requestAuthCode);
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
        OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
        request.setCode(requestAuthCode);
        request.setHttpMethod("GET");
        OapiUserGetuserinfoResponse response = client.execute(request, getToken());
        if (response.getErrcode() != 0) {
            throw new ApiException(response.getErrorCode(), response.getErrmsg());
        }
        String userId = response.getUserid();
        logger.info("userId:" + userId);
        DingUserInfo dingUserInfo = new DingUserInfo();
        dingUserInfo.setUserId(userId);   dingUserInfo.setUserName(JSONObject.parseObject(response.getBody()).get("name").toString());
        
        return dingUserInfo;
    }
4、将userInfo存入到缓存
saveStorage(userId, name) {
      dd.setStorage({
        key: 'userInfo',
        data: {
          userId: userId,
          name: name,

        },
        success: function () {
          dd.alert({ content: '写入成功' });
        }
      });
    },
5、取出userInfo的缓存信息
//获取用户缓存信息
    getStorage: function (key) {
      dd.getStorage({
        key: key,
        success: (res) => {
          this.data.userId = res.data.userId
        },
        fail: function (res) {
          dd.alert({ content: res.errorMessage });
        }
      });

    },

你可能感兴趣的:(支付宝小程序,前端,java学习)