mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-21 07:39:55 +08:00
新增 微信第三方平台 微信一键登录接口
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package com.jsowell.api.uniapp;
|
package com.jsowell.api.uniapp;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.jsowell.common.annotation.Anonymous;
|
import com.jsowell.common.annotation.Anonymous;
|
||||||
import com.jsowell.common.core.controller.BaseController;
|
import com.jsowell.common.core.controller.BaseController;
|
||||||
import com.jsowell.common.response.RestApiResponse;
|
import com.jsowell.common.response.RestApiResponse;
|
||||||
import com.jsowell.pile.domain.AuthorizationEventResult;
|
import com.jsowell.pile.domain.AuthorizationEventResult;
|
||||||
import com.jsowell.pile.domain.agentDev.CategoryInfo;
|
import com.jsowell.pile.domain.agentDev.CategoryInfo;
|
||||||
|
import com.jsowell.pile.dto.WechatLoginDTO;
|
||||||
import com.jsowell.pile.dto.agentDev.*;
|
import com.jsowell.pile.dto.agentDev.*;
|
||||||
import com.jsowell.pile.vo.agentDev.AuthInfoVO;
|
import com.jsowell.pile.vo.agentDev.AuthInfoVO;
|
||||||
import com.jsowell.service.AgentDevService;
|
import com.jsowell.service.AgentDevService;
|
||||||
@@ -298,4 +300,23 @@ public class AgentDevController extends BaseController {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信一键登录
|
||||||
|
* @param dto
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/wechatLogin")
|
||||||
|
public RestApiResponse<?> wechatLogin(@RequestBody AgentWechatLoginDTO dto) {
|
||||||
|
logger.info("第三方平台微信一键登录 params:{}", JSONObject.toJSONString(dto));
|
||||||
|
RestApiResponse<?> response = null;
|
||||||
|
try {
|
||||||
|
String memberToken = agentDevService.wechatLogin(dto);
|
||||||
|
response = new RestApiResponse<>(ImmutableMap.of("memberToken", memberToken));
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("第三方平台微信一键登录 error, ", e);
|
||||||
|
response = new RestApiResponse<>(e);
|
||||||
|
}
|
||||||
|
logger.info("第三方平台微信一键登录 result:{}", response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ import com.jsowell.common.util.wxplatform.WXXmlToMapUtil;
|
|||||||
import com.jsowell.pile.domain.agentDev.AuditItem;
|
import com.jsowell.pile.domain.agentDev.AuditItem;
|
||||||
import com.jsowell.pile.domain.agentDev.CategoryInfo;
|
import com.jsowell.pile.domain.agentDev.CategoryInfo;
|
||||||
import com.jsowell.pile.domain.agentDev.UserInfoSetting;
|
import com.jsowell.pile.domain.agentDev.UserInfoSetting;
|
||||||
|
import com.jsowell.pile.dto.WechatLoginDTO;
|
||||||
import com.jsowell.pile.dto.agentDev.*;
|
import com.jsowell.pile.dto.agentDev.*;
|
||||||
|
import com.jsowell.pile.service.IPileMerchantInfoService;
|
||||||
import com.jsowell.pile.vo.agentDev.AuthInfoVO;
|
import com.jsowell.pile.vo.agentDev.AuthInfoVO;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.collections.MapUtils;
|
import org.apache.commons.collections.MapUtils;
|
||||||
@@ -40,9 +42,15 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public class AgentDevService {
|
public class AgentDevService {
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MemberService memberService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisCache redisCache;
|
private RedisCache redisCache;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IPileMerchantInfoService pileMerchantInfoService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 第三方平台 appid
|
* 第三方平台 appid
|
||||||
*/
|
*/
|
||||||
@@ -572,11 +580,11 @@ public class AgentDevService {
|
|||||||
String authorizerAppid = authInfo.getAuthorizerAppid();
|
String authorizerAppid = authInfo.getAuthorizerAppid();
|
||||||
// 先查询 redis 看有没有
|
// 先查询 redis 看有没有
|
||||||
String redisKey = CacheConstants.COMPONENT_AUTHORIZATION_CODE + authorizerAppid;
|
String redisKey = CacheConstants.COMPONENT_AUTHORIZATION_CODE + authorizerAppid;
|
||||||
String redisAuthCode = redisCache.getCacheObject(redisKey);
|
// String redisAuthCode = redisCache.getCacheObject(redisKey);
|
||||||
if (StringUtils.isNotBlank(redisAuthCode)) {
|
// if (StringUtils.isNotBlank(redisAuthCode)) {
|
||||||
// 有,先删除
|
// // 有,先删除
|
||||||
redisCache.deleteObject(redisKey);
|
// redisCache.deleteObject(redisKey);
|
||||||
}
|
// }
|
||||||
// 删除预缓存码
|
// 删除预缓存码
|
||||||
String preAuthCodeKey = CacheConstants.PLATFORM_PRE_AUTH_CODE + authorizerAppid;
|
String preAuthCodeKey = CacheConstants.PLATFORM_PRE_AUTH_CODE + authorizerAppid;
|
||||||
redisCache.deleteObject(preAuthCodeKey);
|
redisCache.deleteObject(preAuthCodeKey);
|
||||||
@@ -611,4 +619,66 @@ public class AgentDevService {
|
|||||||
|
|
||||||
return resultJson.getString("errmsg");
|
return resultJson.getString("errmsg");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信一键登录
|
||||||
|
* @param dto
|
||||||
|
*/
|
||||||
|
public String wechatLogin(AgentWechatLoginDTO dto) {
|
||||||
|
String appId = dto.getAppId();
|
||||||
|
// 获取用户接口调用令牌
|
||||||
|
String authAccessToken = getAuthAccessToken(appId);
|
||||||
|
// 获取手机号
|
||||||
|
String phoneNumber = getPhoneNumberByCode(dto.getPhoneNumberCode(), authAccessToken);
|
||||||
|
// 获取openId
|
||||||
|
String openId = getOpenIdByCode(dto.getOpenIdCode(), appId);
|
||||||
|
// 通过 appid 查询 merchantId
|
||||||
|
String merchantId = pileMerchantInfoService.getMerchantIdByAppId(appId);
|
||||||
|
logger.info("微信一键登录 获取merchantId:{}", merchantId);
|
||||||
|
// 下面方法有判断 merchantId 是否为空,因此可直接传值
|
||||||
|
return memberService.memberRegisterAndLogin(phoneNumber, merchantId, openId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 code 获取登录用户手机号
|
||||||
|
* @param code
|
||||||
|
* @param authAccessToken
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getPhoneNumberByCode(String code, String authAccessToken) {
|
||||||
|
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + authAccessToken;
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("code", code);
|
||||||
|
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));
|
||||||
|
// 将返回值转成 json 对象
|
||||||
|
JSONObject resultJson = JSONObject.parseObject(result);
|
||||||
|
Integer errCode = resultJson.getInteger("errcode");
|
||||||
|
if (errCode == Constants.zero) {
|
||||||
|
return JSONObject.parseObject(resultJson.getString("phone_info")).getString("phoneNumber");
|
||||||
|
}
|
||||||
|
return resultJson.getString("errmsg");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过 code 获取 openid
|
||||||
|
* @param openIdCode
|
||||||
|
* @param appId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getOpenIdByCode(String openIdCode, String appId) {
|
||||||
|
GetComponentTokenDTO dto = GetComponentTokenDTO.builder()
|
||||||
|
.appId(PLATFORM_APP_ID)
|
||||||
|
.appSecret(PLATFORM_APP_SECRET)
|
||||||
|
.build();
|
||||||
|
String componentToken = getComponentToken(dto);
|
||||||
|
|
||||||
|
String url = "https://api.weixin.qq.com/sns/component/jscode2session?appid=" + appId
|
||||||
|
+ "&js_code=" + openIdCode + "&grant_type=authorization_code&component_appid=" + PLATFORM_APP_ID + "&component_access_token=" + componentToken;
|
||||||
|
String result = HttpUtils.sendGet(url);
|
||||||
|
JSONObject resultJson = JSONObject.parseObject(result);
|
||||||
|
return resultJson.getString("openid");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ public class MemberService {
|
|||||||
* @param merchantId 商户id
|
* @param merchantId 商户id
|
||||||
* @return token返给前端
|
* @return token返给前端
|
||||||
*/
|
*/
|
||||||
private String memberRegisterAndLogin(String phoneNumber, String merchantId, String openId) {
|
protected String memberRegisterAndLogin(String phoneNumber, String merchantId, String openId) {
|
||||||
if (StringUtils.isBlank(phoneNumber)) {
|
if (StringUtils.isBlank(phoneNumber)) {
|
||||||
throw new BusinessException(ReturnCodeEnum.CODE_GET_MOBILE_NUMBER_BY_CODE_ERROR);
|
throw new BusinessException(ReturnCodeEnum.CODE_GET_MOBILE_NUMBER_BY_CODE_ERROR);
|
||||||
}
|
}
|
||||||
@@ -146,7 +146,9 @@ public class MemberService {
|
|||||||
memberBasicInfo.setMemberId(memberId);
|
memberBasicInfo.setMemberId(memberId);
|
||||||
memberBasicInfo.setNickName("会员" + memberId);
|
memberBasicInfo.setNickName("会员" + memberId);
|
||||||
memberBasicInfo.setMobileNumber(phoneNumber);
|
memberBasicInfo.setMobileNumber(phoneNumber);
|
||||||
// memberBasicInfo.setMerchantId(Long.valueOf(merchantId));
|
if (StringUtils.isNotBlank(merchantId)) {
|
||||||
|
memberBasicInfo.setMerchantId(Long.valueOf(merchantId));
|
||||||
|
}
|
||||||
memberBasicInfo.setOpenId(openId);
|
memberBasicInfo.setOpenId(openId);
|
||||||
|
|
||||||
// 首次新建会员,同时新建会员钱包
|
// 首次新建会员,同时新建会员钱包
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.jsowell.pile.dto.agentDev;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 第三方平台登录DTO
|
||||||
|
*
|
||||||
|
* @author Lemon
|
||||||
|
* @Date 2023/8/3 15:18
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class AgentWechatLoginDTO {
|
||||||
|
/**
|
||||||
|
* 用于获取手机号的code
|
||||||
|
*/
|
||||||
|
private String phoneNumberCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户appid
|
||||||
|
*/
|
||||||
|
private String appId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于获取openId的code
|
||||||
|
*/
|
||||||
|
private String openIdCode;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user