增加了多appKey支持

This commit is contained in:
TS-QD1 2023-04-07 18:48:32 +08:00
parent 3a8ebd6add
commit f1710cccd4
8 changed files with 175 additions and 43 deletions

View File

@ -3,6 +3,9 @@ package ink.wgink.properties.wechat.miniapp;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
@ -23,6 +26,7 @@ public class MiniappProperties {
private String grantType;
private String appKey;
private String appSecret;
private Map<String, String> appKeySecrets;
public Boolean getActive() {
return active == null ? false : active;
@ -72,20 +76,11 @@ public class MiniappProperties {
this.appSecret = appSecret;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"active\":")
.append(active);
sb.append(",\"authorizeUrl\":\"")
.append(authorizeUrl).append('\"');
sb.append(",\"grantType\":\"")
.append(grantType).append('\"');
sb.append(",\"appKey\":\"")
.append(appKey).append('\"');
sb.append(",\"appSecret\":\"")
.append(appSecret).append('\"');
sb.append('}');
return sb.toString();
public Map<String, String> getAppKeySecrets() {
return appKeySecrets;
}
public void setAppKeySecrets(Map<String, String> appKeySecrets) {
this.appKeySecrets = appKeySecrets;
}
}

View File

@ -4,6 +4,7 @@ import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.exceptions.PropertiesException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginVO;
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginMultiVO;
import ink.wgink.login.wechat.service.sign.IMiniappSignService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultData;
@ -49,4 +50,15 @@ public class SignMiniappController {
return new SuccessResultData<>(miniAppSignService.defaultSign(miniAppLoginVO));
}
@ApiOperation(value = "默认登录", notes = "通过jsCode完成用户与微信openid绑定并返回token")
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("default-multi")
@CheckRequestBodyAnnotation
public SuccessResultData<String> defaultSignMulti(@RequestBody MiniappLoginMultiVO miniappLoginMultiVO) throws Exception {
if (!miniAppProperties.getActive()) {
throw new PropertiesException("小程序配置未激活");
}
return new SuccessResultData<>(miniAppSignService.defaultSignMulti(miniappLoginMultiVO));
}
}

View File

@ -0,0 +1,31 @@
package ink.wgink.login.wechat.pojo.vos.sign;
import ink.wgink.annotation.CheckEmptyAnnotation;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: WechatMiniProgramLoginVO
* @Description: 小程序登录VO
* @Author: WangGeng
* @Date: 2020/5/9 18:12
* @Version: 1.0
**/
@ApiModel
public class MiniappLoginMultiVO extends MiniappLoginVO {
@ApiModelProperty(name = "appKey", value = "appKey")
@CheckEmptyAnnotation(name = "appKey")
private String appKey;
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
}

View File

@ -1,5 +1,6 @@
package ink.wgink.login.wechat.service.sign;
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginMultiVO;
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginVO;
import ink.wgink.login.wechat.pojo.vos.update.MiniappUpdatePhoneVO;
@ -26,6 +27,14 @@ public interface IMiniappSignService {
*/
String defaultSign(MiniappLoginVO miniAppLoginVO) throws Exception;
/**
* 默认登录多APPID
*
* @param miniappLoginMultiVO
* @return
*/
String defaultSignMulti(MiniappLoginMultiVO miniappLoginMultiVO) throws Exception;
/**
* 更新电话
*
@ -34,4 +43,6 @@ public interface IMiniappSignService {
* @return
*/
String updatePhone(String token, MiniappUpdatePhoneVO miniappUpdatePhoneVO) throws UnsupportedEncodingException;
}

View File

@ -8,6 +8,7 @@ import ink.wgink.exceptions.WechatUserInfoException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.base.service.BaseAppSignService;
import ink.wgink.login.wechat.pojo.bos.miniapp.MiniappPhoneDataBO;
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginMultiVO;
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginVO;
import ink.wgink.login.wechat.pojo.vos.update.MiniappUpdatePhoneVO;
import ink.wgink.login.wechat.service.sign.IMiniappSignService;
@ -92,6 +93,49 @@ public class MiniappSignServiceImpl extends BaseAppSignService implements IMinia
return getMiniappToken(userPO);
}
@Override
public String defaultSignMulti(MiniappLoginMultiVO miniappLoginMultiVO) throws Exception {
if (miniappLoginMultiVO == null) {
throw new PropertiesException("未有相关配置");
}
String appid = miniappLoginMultiVO.getAppKey();
String secret = miniappProperties.getAppKeySecrets().get(miniappLoginMultiVO.getAppKey());
if (StringUtils.isBlank(secret)) {
throw new PropertiesException("配置有误");
}
Map<String, Object> params = new HashMap<>(2);
params.put("appid", appid);
params.put("secret", secret);
params.put("js_code", miniappLoginMultiVO.getJsCode());
params.put("grant_type", miniappProperties.getGrantType());
StringBuilder url = new StringBuilder(miniappProperties.getAuthorizeUrl());
url.append("?appid={appid}&secret={secret}&js_code={js_code}&grant_type={grant_type}");
RestTemplate restTemplate = new RestTemplate();
String resultJson = restTemplate.getForObject(url.toString(), String.class, params);
MiniappUserInfoBO miniappUserInfoBO = JSONObject.parseObject(resultJson, MiniappUserInfoBO.class);
if (miniappUserInfoBO == null) {
throw new WechatUserInfoException("获取微信用户信息失败");
}
if (miniappUserInfoBO.getErrcode() != null && miniappUserInfoBO.getErrcode() != 0) {
throw new WechatAccessTokenForUserException(String.format("获取用户信息失败,错误码:%s错误信息%s", miniappUserInfoBO.getErrcode(), miniappUserInfoBO.getErrmsg()));
}
LOG.debug("绑定用户");
String userId = miniappUserService.createAndReturnUserId(appid, miniappUserInfoBO.getOpenid());
UserPO userPO = userService.getPO(userId);
if (userPO == null) {
throw new SearchException("用户不存在");
}
// 加入管理队列
MiniappManager.getInstance().addUser(userId, miniappUserInfoBO);
if (miniappUserCreateHandleService != null) {
// 自定义新增处理
miniappUserCreateHandleService.handle(userPO, miniappUserInfoBO.getOpenid());
}
return getMiniappToken(userPO);
}
@Override
public String updatePhone(String token, MiniappUpdatePhoneVO miniappUpdatePhoneVO) throws UnsupportedEncodingException {
String userId = getAppTokenUser(token).getId();

View File

@ -116,4 +116,12 @@ public interface IMiniappUserService extends IMiniappUserBaseService {
*/
List<MiniappUserPO> listPOByUserCodes(List<String> userCodes);
/**
* 通过openId获取用户
*
* @param openId
* @return
*/
List<MiniappUserPO> listPOByOpenId(String openId);
}

View File

@ -51,33 +51,47 @@ public class MiniappUserServiceImpl extends DefaultBaseService implements IMinia
@Override
public String createAndReturnUserId(String appKey, String openId) {
MiniappUserPO miniAppUserPO = getPO(appKey, openId);
if (miniAppUserPO != null) {
LOG.debug("已经绑定小程序用户");
return miniAppUserPO.getUserId();
List<MiniappUserPO> miniAppUserPOS = listPOByOpenId(openId);
LOG.debug("判断是否绑定过系统用户");
String userId;
if (miniAppUserPOS.size() == 0) {
LOG.debug("没有绑定过系统用户,新增");
// 随机字符串
String randomUserName = WStringUtil.randomSubStr(openId, 6);
UserVO userVO = new UserVO();
userVO.setUserUsername(MINIAPP_RANDOM_USER_PREFIX + randomUserName);
userVO.setUserName("微信M" + randomUserName);
userVO.setUserPassword(defaultPassword);
userVO.setUserState(0);
// 公共用户
userVO.setUserType(3);
userId = userService.saveAndReturnId(userVO, true);
} else {
LOG.debug("绑定过系统用户");
userId = miniAppUserPOS.get(0).getUserId();
}
// 随机字符串
String randomUserName = WStringUtil.randomSubStr(openId, 6);
UserVO userVO = new UserVO();
userVO.setUserUsername(MINIAPP_RANDOM_USER_PREFIX + randomUserName);
userVO.setUserName("微信M" + randomUserName);
userVO.setUserPassword(defaultPassword);
userVO.setUserState(0);
// 公共用户
userVO.setUserType(3);
String userId = userService.saveAndReturnId(userVO, true);
LOG.debug("判断是否绑定过小程序用户");
boolean isBindMiniappUser = false;
for (MiniappUserPO miniappUserPO : miniAppUserPOS) {
if (StringUtils.equals(miniappUserPO.getAppId(), appKey)) {
isBindMiniappUser = true;
break;
}
}
if (!isBindMiniappUser) {
LOG.debug("没有绑定过小程序用户appId{},绑定", appKey);
Map<String, Object> params = getHashMap(6);
params.put("appId", appKey);
params.put("openId", openId);
params.put("userId", userId);
params.put("isInitAccount", 1);
params.put("gmtCreate", DateUtil.getTime());
miniappUserDao.save(params);
Map<String, Object> params = getHashMap(6);
params.put("appId", appKey);
params.put("openId", openId);
params.put("userId", userId);
params.put("isInitAccount", 1);
params.put("gmtCreate", DateUtil.getTime());
miniappUserDao.save(params);
Long id = Long.parseLong(params.get("id").toString());
String userCode = ShareCodeUtil.gen(id);
updateUserCode(userCode, openId);
Long id = Long.parseLong(params.get("id").toString());
String userCode = ShareCodeUtil.gen(id);
updateUserCode(userCode, appKey, openId);
}
return userId;
}
@ -184,7 +198,9 @@ public class MiniappUserServiceImpl extends DefaultBaseService implements IMinia
@Override
public List<MiniappUserPO> listPO(String userId) {
return null;
Map<String, Object> params = getHashMap(2);
params.put("userId", userId);
return listPO(params);
}
@Override
@ -228,10 +244,17 @@ public class MiniappUserServiceImpl extends DefaultBaseService implements IMinia
return listPO(params);
}
private void updateUserCode(String userCode, String openId) {
@Override
public List<MiniappUserPO> listPOByOpenId(String openId) {
Map<String, Object> params = getHashMap(2);
params.put("openId", openId);
return listPO(params);
}
private void updateUserCode(String userCode, String appKey, String openId) {
Map<String, Object> params = getHashMap(6);
params.put("userCode", userCode);
params.put("appId", miniappProperties.getAppKey());
params.put("appId", appKey);
params.put("openId", openId);
miniappUserDao.updateUserCode(params);
}

View File

@ -124,6 +124,14 @@
#{userIds[${index}]}
</foreach>
</if>
<if test="userId != null and userId != ''">
AND
user_id = #{userId}
</if>
<if test="openId != null and openId != ''">
AND
open_id = #{openId}
</if>
<if test="userCodes != null and userCodes.size > 0">
AND
user_code IN