From 71fd1accb1587e44d9fd7fb4fc766a3cddb43760 Mon Sep 17 00:00:00 2001 From: zhibing.pu <393733352@qq.com> Date: 星期四, 22 八月 2024 15:09:12 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/AliLoginController.java | 12 +++- ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WxLoginController.java | 7 + ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/TAppUserServiceImpl.java | 41 +++++++++++-- ruoyi-service/ruoyi-order/src/main/resources/mapper/order/TShoppingOrderMapper.xml | 16 ++-- ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/ali/tools/AliAppletTools.java | 57 ++++++++++++++++++ ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/TAppUserService.java | 5 + 6 files changed, 113 insertions(+), 25 deletions(-) diff --git a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/ali/tools/AliAppletTools.java b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/ali/tools/AliAppletTools.java index a31fb9e..0678b48 100644 --- a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/ali/tools/AliAppletTools.java +++ b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/ali/tools/AliAppletTools.java @@ -1,18 +1,24 @@ package com.ruoyi.account.ali.tools; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import com.alibaba.fastjson.parser.Feature; 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.internal.util.AlipaySignature; import com.alipay.api.request.AlipaySystemOauthTokenRequest; import com.alipay.api.request.AlipayUserInfoShareRequest; -import com.alipay.api.request.AlipayUserUserinfoShareRequest; import com.alipay.api.response.AlipaySystemOauthTokenResponse; import com.alipay.api.response.AlipayUserInfoShareResponse; -import com.alipay.api.response.AlipayUserUserinfoShareResponse; import com.ruoyi.account.ali.Constant.AliConstant; import com.ruoyi.account.ali.model.AliProperties; +import com.ruoyi.common.core.exception.ServiceException; import lombok.extern.slf4j.Slf4j; + +import java.util.Map; /** * @author xiaochen @@ -76,6 +82,53 @@ } /** + * 获取用户手机号 + * @param response + * @return + */ + public String getPhoneNumber(String response){ + //1. 获取验签和解密所需要的参数 + Map<String, String> openapiResult = JSON.parseObject(response,new TypeReference<Map<String, String>>() {}, Feature.OrderedField); + String signType = "RSA2"; + String charset = "UTF-8"; + String encryptType = "AES"; + String sign = openapiResult.get("sign"); + String content = openapiResult.get("response"); + //判断是否为加密内容 + boolean isDataEncrypted = !content.startsWith("{"); + boolean signCheckPass = false; + //2. 验签 + String signContent = content; + if (isDataEncrypted) { + signContent = "\"" + signContent + "\""; + } try { + signCheckPass = AlipaySignature.rsaCheck(signContent, sign, aliProperties.getAlipayPublicKey(), charset, signType); + } catch (AlipayApiException e) { + // 验签异常, 日志 + } if (!signCheckPass) { + //验签不通过(异常或者报文被篡改),终止流程(不需要做解密) + throw new ServiceException("验签失败"); + } + //3. 解密 + String plainData = null; + if (isDataEncrypted) { + try { + plainData = AlipayEncrypt.decryptContent(content, encryptType, "XABBSOeWDakvuG9TDez4Qg====", charset); + } catch (AlipayApiException e) { + //解密异常, 记录日志 + throw new ServiceException("解密异常"); + }} else { + plainData = content; + } + log.info("解密后的数据:{}", plainData); + String phoneNumber = ""; + if (plainData.contains("mobile")) { + phoneNumber = JSON.parseObject(plainData).getString("mobile"); + } + return phoneNumber; + } + + /** * 初始化支付宝配置 * @return */ diff --git a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/AliLoginController.java b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/AliLoginController.java index a437bb4..9062a30 100644 --- a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/AliLoginController.java +++ b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/AliLoginController.java @@ -18,6 +18,7 @@ import com.ruoyi.account.api.model.TAppUser; import com.ruoyi.account.service.TAppUserService; import com.ruoyi.common.core.exception.ServiceException; +import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.security.service.TokenService; import com.ruoyi.system.api.model.LoginUserApplet; @@ -56,10 +57,15 @@ AlipaySystemOauthTokenResponse response = new AliAppletTools(aliProperties).login(appletUserLogin.getCode()); if (response.isSuccess()) { // 获取用户信息 - AlipayUserInfoShareResponse userInfo = new AliAppletTools(aliProperties).getUserInfo(appletUserLogin.getToken()); - log.info("获取支付宝用户信息:{}", userInfo); +// AlipayUserInfoShareResponse userInfo = new AliAppletTools(aliProperties).getUserInfo(response.getAccessToken()); + if(StringUtils.isEmpty(appletUserLogin.getToken())){ + return AjaxResult.success(); + } + // 获取用户手机号 + String phoneNumber = new AliAppletTools(aliProperties).getPhoneNumber(appletUserLogin.getToken()); + log.info("获取支付宝用户信息:{}", phoneNumber); // 用户信息封装 - return AjaxResult.ok(appUserService.aliLogin(response,userInfo)); + return AjaxResult.success(appUserService.aliLogin(response,phoneNumber)); } else { String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response); log.warn("诊断结果:{}",diagnosisUrl); diff --git a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WxLoginController.java b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WxLoginController.java index 2fcec55..64f86fb 100644 --- a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WxLoginController.java +++ b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WxLoginController.java @@ -15,6 +15,7 @@ import com.ruoyi.account.wx.tools.WxAppletTools; import com.ruoyi.account.wx.tools.WxUtils; import com.ruoyi.common.core.exception.ServiceException; +import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.common.security.service.TokenService; @@ -22,7 +23,6 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; @@ -60,8 +60,11 @@ // if (StringUtils.isNotBlank(data.getSignature())) { // WxUtils.verifySignature(data.getRawData(), sessionKey, data.getSignature()); // } + if(StringUtils.isEmpty(data.getEncryptedData()) || StringUtils.isEmpty(data.getIv())){ + return AjaxResult.success(); + } AppletUserDecodeData appletUserDecodeData = WxUtils.encryptedData(data.getEncryptedData(), sessionKey, data.getIv()); appletUserDecodeData.setOpenId(openid); - return AjaxResult.ok(appUserService.wxLogin(appletUserDecodeData)); + return AjaxResult.success(appUserService.wxLogin(appletUserDecodeData)); } } diff --git a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/TAppUserService.java b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/TAppUserService.java index 957d698..9e59184 100644 --- a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/TAppUserService.java +++ b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/TAppUserService.java @@ -28,10 +28,11 @@ /** * 支付宝小程序登录用户封装 - * @param userInfo + * @param response * @return */ - Map<String, Object> aliLogin(AlipaySystemOauthTokenResponse response, AlipayUserInfoShareResponse userInfo); +// Map<String, Object> aliLogin(AlipaySystemOauthTokenResponse response, AlipayUserInfoShareResponse userInfo); + Map<String, Object> aliLogin(AlipaySystemOauthTokenResponse response,String phone); /** * 封装用户信息和token diff --git a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/TAppUserServiceImpl.java b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/TAppUserServiceImpl.java index c9fe14f..d3351ef 100644 --- a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/TAppUserServiceImpl.java +++ b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/TAppUserServiceImpl.java @@ -66,34 +66,59 @@ } @Override - public Map<String, Object> aliLogin(AlipaySystemOauthTokenResponse response, AlipayUserInfoShareResponse userInfo) { + public Map<String, Object> aliLogin(AlipaySystemOauthTokenResponse response,String phone) { // 通过手机号查询用户,是否已存在手动导入用户,包含微信用户 TAppUser appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class) - .eq(TAppUser::getPhone, userInfo.getMobile()) + .eq(TAppUser::getPhone, phone) .isNull(TAppUser::getAliOpenid) .last("LIMIT 1")); if(Objects.isNull(appUser)){ // 先使用openId和当前手机号进行查询 appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class) .eq(TAppUser::getAliOpenid, response.getOpenId()) - .eq(TAppUser::getPhone, userInfo.getMobile()) + .eq(TAppUser::getPhone, phone) .last("LIMIT 1")); if(Objects.isNull(appUser)){ appUser = new TAppUser(); - appUser.setPhone(userInfo.getMobile()); + appUser.setPhone(phone); } } if(Objects.nonNull(appUser.getStatus())){ throwInfo(appUser.getStatus()); } - appUser.setAvatar(userInfo.getAvatar()); - appUser.setCity(userInfo.getCity()); - appUser.setName(userInfo.getNickName()); - appUser.setProvince(userInfo.getProvince()); appUser.setAliOpenid(response.getOpenId()); this.saveOrUpdate(appUser); return this.getUserInfo(appUser); } +// @Override +// public Map<String, Object> aliLogin(AlipaySystemOauthTokenResponse response, AlipayUserInfoShareResponse userInfo) { +// // 通过手机号查询用户,是否已存在手动导入用户,包含微信用户 +// TAppUser appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class) +// .eq(TAppUser::getPhone, userInfo.getMobile()) +// .isNull(TAppUser::getAliOpenid) +// .last("LIMIT 1")); +// if(Objects.isNull(appUser)){ +// // 先使用openId和当前手机号进行查询 +// appUser = this.getOne(Wrappers.lambdaQuery(TAppUser.class) +// .eq(TAppUser::getAliOpenid, response.getOpenId()) +// .eq(TAppUser::getPhone, userInfo.getMobile()) +// .last("LIMIT 1")); +// if(Objects.isNull(appUser)){ +// appUser = new TAppUser(); +// appUser.setPhone(userInfo.getMobile()); +// } +// } +// if(Objects.nonNull(appUser.getStatus())){ +// throwInfo(appUser.getStatus()); +// } +// appUser.setAvatar(userInfo.getAvatar()); +// appUser.setCity(userInfo.getCity()); +// appUser.setName(userInfo.getNickName()); +// appUser.setProvince(userInfo.getProvince()); +// appUser.setAliOpenid(response.getOpenId()); +// this.saveOrUpdate(appUser); +// return this.getUserInfo(appUser); +// } @Override public Map<String, Object> getUserInfo(TAppUser appUser) { diff --git a/ruoyi-service/ruoyi-order/src/main/resources/mapper/order/TShoppingOrderMapper.xml b/ruoyi-service/ruoyi-order/src/main/resources/mapper/order/TShoppingOrderMapper.xml index b7b0d0b..a433566 100644 --- a/ruoyi-service/ruoyi-order/src/main/resources/mapper/order/TShoppingOrderMapper.xml +++ b/ruoyi-service/ruoyi-order/src/main/resources/mapper/order/TShoppingOrderMapper.xml @@ -58,10 +58,10 @@ and t1.code LIKE CONCAT('%',#{req.code},'%') </if> <if test="req.type != null and req.type != '' and req.type == 1"> - and t1.coupon_discount_amount != null and t1.coupon_discount_amount != 0 + and t1.coupon_discount_amount IS NOT NULL and t1.coupon_discount_amount != 0 </if> <if test="req.type != null and req.type != '' and req.type == 2"> - and t1.vip_discount_amount != null and t1.vip_discount_amount != 0 + and t1.vip_discount_amount IS NOT NULL and t1.vip_discount_amount != 0 </if> <if test="null != req.userIds and req.userIds.size()>0" > and t1.app_user_id in @@ -97,13 +97,13 @@ and t2.code LIKE CONCAT('%',#{req.code},'%') </if> <if test="req.type != null and req.type != '' and req.type == 1"> - and t2.coupon_discount_amount != null and t1.coupon_discount_amount != 0 + and t2.coupon_discount_amount IS NOT NULL and t2.coupon_discount_amount != 0 </if> <if test="req.type != null and req.type != '' and req.type == 2"> - and t2.vip_discount_amount != null and t1.vip_discount_amount != 0 + and t2.vip_discount_amount IS NOT NULL and t2.vip_discount_amount != 0 </if> <if test="req.type == null "> - and t2.vip_discount_amount != null and t1.vip_discount_amount != 0 and t2.coupon_discount_amount != null and t1.coupon_discount_amount != 0 + and t2.vip_discount_amount IS NOT NULL and t2.vip_discount_amount != 0 and t2.coupon_discount_amount IS NOT NULL and t2.coupon_discount_amount != 0 </if> <if test="null != req.userIds and req.userIds.size()>0" > and t2.app_user_id in @@ -129,12 +129,12 @@ 3 as orderType from t_vip_order t3 where 1 = 1 - and t3.type ==1 + and t3.type =1 <if test="null != req.code and req.code!=''"> and t3.code LIKE CONCAT('%',#{req.code},'%') </if> <if test="req.type == null "> - and t3.discount_amount != null and t3.discount_amount != 0 + and t3.discount_amount IS NOT NULL and t3.discount_amount != 0 </if> <if test="null != req.userIds and req.userIds.size()>0" > and t3.app_user_id in @@ -164,7 +164,7 @@ and t4.code LIKE CONCAT('%',#{req.code},'%') </if> <if test="req.type == null "> - and t4.discount_amount != null and t4.discount_amount != 0 + and t4.discount_amount IS NOT NULL and t4.discount_amount != 0 </if> <if test="null != req.userIds and req.userIds.size()>0" > and t4.app_user_id in -- Gitblit v1.7.1