Merge branch 'tkk' into test

This commit is contained in:
Tankaikai
2024-09-25 16:26:22 +08:00
3 changed files with 60 additions and 2 deletions

View File

@@ -1,9 +1,13 @@
package com.chaozhanggui.system.cashierservice.alipayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipayEncrypt;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import lombok.SneakyThrows;
@@ -42,6 +46,11 @@ public class AlipayUtil {
*/
@Value("${alipay.sdk.config.alipayPublicKey}")
private String alipayPublicKey;
/**
* 支付宝公钥
*/
@Value("${alipay.sdk.config.encryptKey}")
private String encryptKey;
/**
* 创建支付宝客户端
@@ -90,4 +99,36 @@ public class AlipayUtil {
return response.getOpenId();
}
/**
* 获取支付宝用户的手机号码
* @param encryptedData 密文
* @return mobile
*/
public String getMobile(String encryptedData) throws Exception{
if(StrUtil.isEmpty(encryptedData)){
throw new AlipayApiException("加密数据不能为空");
}
try {
log.info("解密前的数据,返回结果:{}", encryptedData);
String resp = AlipayEncrypt.decryptContent(encryptedData, "AES", encryptKey, "UTF-8");
log.info("解密后的数据,返回结果:{}", resp);
boolean isJson = JSONUtil.isJson(resp);
if(!isJson){
throw new AlipayApiException("解密后的数据不是json格式");
}
JSONObject jsonObject = JSONUtil.parseObj(resp);
String code = jsonObject.getStr("code");
String msg = jsonObject.getStr("msg");
String mobile = jsonObject.getStr("mobile");
if("10000".equals(code)){
return mobile;
}else{
throw new AlipayApiException(code,msg);
}
}catch (AlipayApiException e){
log.error("获取支付宝用户的手机号码失败,错误码:{},错误信息:{}", e.getErrCode(), e.getErrMsg());
throw e;
}
}
}

View File

@@ -111,12 +111,13 @@ public class LoginContoller {
// 1.接收小程序发送的code
// 2.开发者服务器 登录凭证校验接口 appi + appsecret + code
JSONObject wxResp = WechatUtil.getSessionKeyOrOpenId(code, customAppId, customSecrete);
Integer errCode = wxResp.getInteger("errcode");
//Integer errCode = wxResp.getInteger("errcode");
log.info("微信获取openid响应报文{}", wxResp.toJSONString());
boolean hasOpenId = wxResp.containsKey("openid");
if (!hasOpenId) {
return Result.fail("登录失败:" + wxResp.getString("errmsg"));
}
String a = "{\"width\":\"58\",\"printerNum\":\"1\",\"categoryList\":[{\"id\":30,\"name\":\"奶茶\",\"shortName\":null,\"tree\":null,\"pid\":null,\"pic\":null,\"merchantId\":null,\"shopId\":null,\"style\":null,\"isShow\":null,\"detail\":null,\"sort\":null,\"keyWord\":null,\"createdAt\":null,\"updatedAt\":null},{\"id\":35,\"name\":\"酒水饮料\",\"shortName\":null,\"tree\":null,\"pid\":null,\"pic\":null,\"merchantId\":null,\"shopId\":null,\"style\":null,\"isShow\":null,\"detail\":null,\"sort\":null,\"keyWord\":null,\"createdAt\":null,\"updatedAt\":null},{\"id\":65,\"name\":\"火锅\",\"shortName\":null,\"tree\":null,\"pid\":null,\"pic\":null,\"merchantId\":null,\"shopId\":null,\"style\":null,\"isShow\":null,\"detail\":null,\"sort\":null,\"keyWord\":null,\"createdAt\":null,\"updatedAt\":null},{\"id\":95,\"name\":\"串串\",\"shortName\":null,\"tree\":null,\"pid\":null,\"pic\":null,\"merchantId\":null,\"shopId\":null,\"style\":null,\"isShow\":null,\"detail\":null,\"sort\":null,\"keyWord\":null,\"createdAt\":null,\"updatedAt\":null},{\"id\":96,\"name\":\"烧烤\",\"shortName\":null,\"tree\":null,\"pid\":null,\"pic\":null,\"merchantId\":null,\"shopId\":null,\"style\":null,\"isShow\":null,\"detail\":null,\"sort\":null,\"keyWord\":null,\"createdAt\":null,\"updatedAt\":null}],\"model\":\"normal\",\"feet\":\"0\",\"autoCut\":\"1\"}";
// 3.接收微信接口服务 获取返回的参数
String openid = wxResp.getString("openid");
return loginService.wxCustomLogin(openid, "", "", "", IpUtil.getIpAddr(request));
@@ -182,8 +183,23 @@ public class LoginContoller {
if (ObjectUtil.isNull(map) || ObjectUtil.isEmpty(map) || !map.containsKey("code") || ObjectUtil.isEmpty(map.get("code"))) {
Result.fail("code不能为空");
}
String code = map.get("code").toString();
// 三方登录来源 wechat、alipay
String source = map.getOrDefault("source",AuthSource.WECHAT.getValue());
String code = map.get("code");
if (AuthSource.ALIPAY.getValue().equals(source)) {
try {
String mobile = alipayUtil.getMobile(code);
return Result.success(CodeEnum.SUCCESS, mobile);
}catch (AlipayApiException e){
log.error("获取手机号失败:",e);
return Result.fail("获取手机号失败:"+e.getErrMsg());
}catch (Exception e){
e.printStackTrace();
log.error("登录手机号失败:",e);
return Result.fail("获取手机号失败:未知错误");
}
}
String encryptedData = map.get("encryptedData");
String ivStr = map.get("iv");