记得上下班打卡 | git大法好,push需谨慎

Commit d9853f10 authored by jiangxiulong's avatar jiangxiulong

防疫答题-服务号获取用户信息公共接口2个

parent 19d98eb9
package com.liquidnet.service.sweet.dto.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import java.io.Serializable;
/**
* <p>
* WxOAuth2AccessToken vo
* </p>
*
* @author jiangxiulong
* @since 2021-09-28
*/
@Data
@ApiModel
public class WechatTokenInfoVo implements Serializable, Cloneable {
private static final long serialVersionUID = 3404938811111878417L;
@ApiModelProperty(value = "openId")
private String openId;
@ApiModelProperty(value = "unionId")
private String unionId;
private static final WechatTokenInfoVo obj = new WechatTokenInfoVo();
public static WechatTokenInfoVo getNew() {
try {
return (WechatTokenInfoVo) obj.clone();
} catch (CloneNotSupportedException e) {
return new WechatTokenInfoVo();
}
}
public WechatTokenInfoVo copy(WxOAuth2AccessToken source) {
if (null == source) return this;
this.setOpenId(source.getOpenId());
this.setUnionId(source.getUnionId());
return this;
}
}
package com.liquidnet.service.sweet.dto.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import java.io.Serializable;
/**
* <p>
* WxOAuth2UserInfo vo
* </p>
*
* @author jiangxiulong
* @since 2021-09-28
*/
@Data
@ApiModel
public class WechatUserInfoVo implements Serializable, Cloneable {
private static final long serialVersionUID = 5067978046376025813L;
@ApiModelProperty(value = "普通用户的标识,对当前开发者帐号唯一")
private String openid;
@ApiModelProperty(value = "普通用户昵称")
private String nickname;
@ApiModelProperty(value = "普通用户性别,1为男性,2为女性")
private Integer sex;
@ApiModelProperty(value = "普通用户个人资料填写的城市")
private String city;
@ApiModelProperty(value = "普通用户个人资料填写的省份")
private String province;
@ApiModelProperty(value = "国家,如中国为CN")
private String country;
@ApiModelProperty(value = "用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像) 用户没有头像时该项为空")
private String headImgUrl;
@ApiModelProperty(value = "用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的")
private String unionId;
@ApiModelProperty(value = "用户特权信息,json数组,如微信沃卡用户为(chinaunicom)")
private String[] privileges;
private static final WechatUserInfoVo obj = new WechatUserInfoVo();
public static WechatUserInfoVo getNew() {
try {
return (WechatUserInfoVo) obj.clone();
} catch (CloneNotSupportedException e) {
return new WechatUserInfoVo();
}
}
public WechatUserInfoVo copy(WxOAuth2UserInfo source) {
if (null == source) return this;
this.setOpenid(source.getOpenid());
this.setNickname(source.getNickname());
this.setSex(source.getSex());
this.setCity(source.getCity());
this.setProvince(source.getProvince());
this.setCountry(source.getCountry());
this.setHeadImgUrl(source.getHeadImgUrl());
this.setUnionId(source.getUnionId());
this.setPrivileges(source.getPrivileges());
return this;
}
}
...@@ -69,6 +69,12 @@ ...@@ -69,6 +69,12 @@
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>
<artifactId>jedis</artifactId> <artifactId>jedis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.liquidnet</groupId>
<artifactId>liquidnet-service-sweet-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.liquidnet.service.sweet.controller; package com.liquidnet.service.sweet.controller;
import com.liquidnet.service.base.ResponseDto; import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.sweet.dto.vo.WechatTokenInfoVo;
import com.liquidnet.service.sweet.dto.vo.WechatUserInfoVo;
import com.liquidnet.service.sweet.service.impl.SweetWechatLoginServiceImpl; import com.liquidnet.service.sweet.service.impl.SweetWechatLoginServiceImpl;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
...@@ -59,4 +61,24 @@ public class SweetWechatLoginController { ...@@ -59,4 +61,24 @@ public class SweetWechatLoginController {
return sweetLoginService.wxOauth2AccessToken(code, type); return sweetLoginService.wxOauth2AccessToken(code, type);
} }
@ApiOperation(value = "微信公众号登录 获取openId、unionId", notes = "本接口在scope参数为snsapi_base时不再提供unionID字段,只需将scope参数修改为snsapi_userinfo即可重新获得用户unionID")
@GetMapping(value = {"mpAccessTokenInfo"})
@ApiImplicitParams({
@ApiImplicitParam(type = "query", dataType = "String", name = "code", value = "微信code", required = true),
@ApiImplicitParam(type = "query", dataType = "Integer", name = "type", value = "1正在 2摩登", required = true)
})
public ResponseDto<WechatTokenInfoVo> mpWxOauth2AccessTokenInfo(@RequestParam String code, @RequestParam Integer type) {
return sweetLoginService.mpWxOauth2AccessTokenInfo(code, type);
}
@ApiOperation(value = "微信公众号登录 获取用户基本信息", notes = "本接口在用户未关注公众号时,将不返回用户unionID信息,已关注的用户,开发者可使用“获取用户基本信息接口”获取unionID,未关注用户,开发者可使用“微信授权登录接口”并将scope参数设置为snsapi_userinfo,获取用户unionID")
@GetMapping(value = {"mpUserInfo"})
@ApiImplicitParams({
@ApiImplicitParam(type = "query", dataType = "String", name = "code", value = "微信code", required = true),
@ApiImplicitParam(type = "query", dataType = "Integer", name = "type", value = "1正在 2摩登", required = true)
})
public ResponseDto<WechatUserInfoVo> mpUserInfo(@RequestParam String code, @RequestParam Integer type) {
return sweetLoginService.mpUserInfo(code, type);
}
} }
...@@ -9,8 +9,11 @@ import com.liquidnet.commons.lang.util.JsonUtils; ...@@ -9,8 +9,11 @@ import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.ResponseDto; import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.base.SqlMapping; import com.liquidnet.service.base.SqlMapping;
import com.liquidnet.service.base.constant.MQConst; import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.sweet.dto.vo.WechatTokenInfoVo;
import com.liquidnet.service.sweet.dto.vo.WechatUserInfoVo;
import com.liquidnet.service.sweet.utils.QueueUtils; import com.liquidnet.service.sweet.utils.QueueUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken; import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -20,7 +23,6 @@ import org.springframework.stereotype.Service; ...@@ -20,7 +23,6 @@ import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import java.time.LocalDateTime;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Objects; import java.util.Objects;
...@@ -89,7 +91,7 @@ public class SweetWechatLoginServiceImpl { ...@@ -89,7 +91,7 @@ public class SweetWechatLoginServiceImpl {
return ResponseDto.success(userInfo); return ResponseDto.success(userInfo);
} catch (Exception e) { } catch (Exception e) {
log.error("WechatUserInfoError", e); log.error("WechatUserInfoError Exception:{}", e);
return ResponseDto.failure(); return ResponseDto.failure();
} }
} }
...@@ -101,7 +103,7 @@ public class SweetWechatLoginServiceImpl { ...@@ -101,7 +103,7 @@ public class SweetWechatLoginServiceImpl {
params.add("mobile", mobile); params.add("mobile", mobile);
HttpUtil.post(adamUrl.concat("/adam/rsc/reg/mobile"), params); HttpUtil.post(adamUrl.concat("/adam/rsc/reg/mobile"), params);
} catch (Exception e) { } catch (Exception e) {
log.error("registerByMobile失败", e); log.error("registerByMobile失败 Exception:{}", e);
} }
} }
...@@ -110,7 +112,7 @@ public class SweetWechatLoginServiceImpl { ...@@ -110,7 +112,7 @@ public class SweetWechatLoginServiceImpl {
WxMaJscode2SessionResult wxMaJscode2SessionResult = sweetWechatService.sessionInfo(jsCode, type); WxMaJscode2SessionResult wxMaJscode2SessionResult = sweetWechatService.sessionInfo(jsCode, type);
return ResponseDto.success(wxMaJscode2SessionResult.getOpenid()); return ResponseDto.success(wxMaJscode2SessionResult.getOpenid());
} catch (Exception e) { } catch (Exception e) {
log.error("wxaCode2SessionError", e); log.error("wxaCode2SessionError Exception:{}", e);
return ResponseDto.failure(); return ResponseDto.failure();
} }
} }
...@@ -120,8 +122,31 @@ public class SweetWechatLoginServiceImpl { ...@@ -120,8 +122,31 @@ public class SweetWechatLoginServiceImpl {
WxOAuth2AccessToken wxOAuth2AccessToken = sweetWechatService.wxOauth2AccessToken(code, type); WxOAuth2AccessToken wxOAuth2AccessToken = sweetWechatService.wxOauth2AccessToken(code, type);
return ResponseDto.success(wxOAuth2AccessToken.getOpenId()); return ResponseDto.success(wxOAuth2AccessToken.getOpenId());
} catch (Exception e) { } catch (Exception e) {
log.error("wxaCode2SessionError", e); log.error("wxaCode2SessionError Exception:{}", e);
return ResponseDto.failure(); return ResponseDto.failure();
} }
} }
public ResponseDto<WechatTokenInfoVo> mpWxOauth2AccessTokenInfo(String code, Integer type) {
try {
WxOAuth2AccessToken wxOAuth2AccessToken = sweetWechatService.wxOauth2AccessToken(code, type);
WechatTokenInfoVo userInfoVo = WechatTokenInfoVo.getNew().copy(wxOAuth2AccessToken);
return ResponseDto.success(userInfoVo);
} catch (Exception e) {
log.error("wxaCode2SessionError Exception:{}", e);
return ResponseDto.failure();
}
}
public ResponseDto<WechatUserInfoVo> mpUserInfo(String code, Integer type) {
try {
WxOAuth2UserInfo wxOAuth2UserInfo = sweetWechatService.mpUserInfo(code, type);
WechatUserInfoVo tokenInfoVo = WechatUserInfoVo.getNew().copy(wxOAuth2UserInfo);
return ResponseDto.success(tokenInfoVo);
} catch (Exception e) {
log.error("wxaCode2SessionError Exception:{}", e);
return ResponseDto.failure();
}
}
} }
...@@ -7,6 +7,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; ...@@ -7,6 +7,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.liquidnet.service.sweet.config.WechatMaConfigure; import com.liquidnet.service.sweet.config.WechatMaConfigure;
import com.liquidnet.service.sweet.config.WechatMpConfigure; import com.liquidnet.service.sweet.config.WechatMpConfigure;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken; import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
...@@ -22,6 +23,9 @@ public class SweetWechatService { ...@@ -22,6 +23,9 @@ public class SweetWechatService {
@Autowired @Autowired
WechatMpConfigure wechatMpConfigure; WechatMpConfigure wechatMpConfigure;
/**
* 小程序--------------
*/
public WxMaJscode2SessionResult sessionInfo(String code, Integer anum) throws WxErrorException { public WxMaJscode2SessionResult sessionInfo(String code, Integer anum) throws WxErrorException {
WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum); WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum);
log.info("isAccessTokenExpired:[{}] ", wxMaService.getWxMaConfig().isAccessTokenExpired()); log.info("isAccessTokenExpired:[{}] ", wxMaService.getWxMaConfig().isAccessTokenExpired());
...@@ -39,12 +43,19 @@ public class SweetWechatService { ...@@ -39,12 +43,19 @@ public class SweetWechatService {
return wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv); return wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
} }
/**
* 服务号--------------
*/
public WxOAuth2AccessToken wxOauth2AccessToken(String code, Integer type) throws WxErrorException { public WxOAuth2AccessToken wxOauth2AccessToken(String code, Integer type) throws WxErrorException {
WxMpService wxMpService = wechatMpConfigure.getWxMpService(type); WxMpService wxMpService = wechatMpConfigure.getWxMpService(type);
log.info("isAccessTokenExpired:[{}] ", wxMpService.getWxMpConfigStorage().isAccessTokenExpired()); log.info("isAccessTokenExpired:[{}] ", wxMpService.getWxMpConfigStorage().isAccessTokenExpired());
log.info("getAccessToken:[{}] ", wxMpService.getWxMpConfigStorage().getAccessToken()); log.info("getAccessToken:[{}] ", wxMpService.getWxMpConfigStorage().getAccessToken());
return wxMpService.getOAuth2Service().getAccessToken(code); return wxMpService.getOAuth2Service().getAccessToken(code);
} }
public WxOAuth2UserInfo mpUserInfo(String code, Integer type) throws WxErrorException {
WxMpService wxMpService = wechatMpConfigure.getWxMpService(type);
WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
return wxMpService.getOAuth2Service().getUserInfo(wxOAuth2AccessToken, "zh_CN");
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment