From 2ed61ba13cc22a33406d0eda9239c71e9e22ba37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Thu, 27 Mar 2025 11:24:34 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/app/AppLoginController.java | 10 ++++++++ .../com/sqx/modules/app/dto/BindWxDTO.java | 14 +++++++++++ .../sqx/modules/app/service/UserService.java | 2 ++ .../app/service/impl/UserServiceImpl.java | 25 +++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 src/main/java/com/sqx/modules/app/dto/BindWxDTO.java diff --git a/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java b/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java index a581bf0c..4e3ed94a 100644 --- a/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java +++ b/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java @@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.sqx.common.annotation.Debounce; import com.sqx.common.utils.Result; +import com.sqx.modules.app.annotation.Login; +import com.sqx.modules.app.dto.BindWxDTO; import com.sqx.modules.app.dto.LoginDTO; import com.sqx.modules.app.dto.RegisterDTO; import com.sqx.modules.app.dto.UserInviteDTO; @@ -168,6 +170,14 @@ public class AppLoginController { return userService.register(registerDTO); } + @Login + @PostMapping("/bindWx") + @ResponseBody + @Debounce(interval = 2500, value = "#code") + public Result bindWx(@RequestBody BindWxDTO bindWxDTO, @RequestHeader Long userId) { + return userService.bindWx(bindWxDTO, userId); + } + @PostMapping("/bindWxOpenPhone") @ApiOperation("微信公众号绑定手机号") public Result bindWxOpenPhone(Long userId,String phone,String msg){ diff --git a/src/main/java/com/sqx/modules/app/dto/BindWxDTO.java b/src/main/java/com/sqx/modules/app/dto/BindWxDTO.java new file mode 100644 index 00000000..af0a5eb9 --- /dev/null +++ b/src/main/java/com/sqx/modules/app/dto/BindWxDTO.java @@ -0,0 +1,14 @@ +package com.sqx.modules.app.dto; + +import lombok.Data; + +import javax.validation.constraints.NotEmpty; + +/** + * @author Administrator + */ +@Data +public class BindWxDTO { + @NotEmpty(message = "code不能为空") + private String code; +} diff --git a/src/main/java/com/sqx/modules/app/service/UserService.java b/src/main/java/com/sqx/modules/app/service/UserService.java index 4fedeb33..e4d25b01 100644 --- a/src/main/java/com/sqx/modules/app/service/UserService.java +++ b/src/main/java/com/sqx/modules/app/service/UserService.java @@ -238,4 +238,6 @@ public interface UserService extends IService { Result removeUserBlack(Long userId, Integer status); Map queryPayAndExtractInfo(); + + Result bindWx(BindWxDTO bindWxDTO, Long userId); } diff --git a/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java b/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java index 5260427b..dd1b5191 100644 --- a/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java +++ b/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java @@ -80,6 +80,7 @@ import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import weixin.popular.api.SnsAPI; +import weixin.popular.bean.sns.SnsToken; import weixin.popular.util.JsonUtil; import javax.servlet.http.HttpServletRequest; @@ -1824,4 +1825,28 @@ public class UserServiceImpl extends ServiceImpl implements return result; } + + @Override + public Result bindWx(BindWxDTO bindWxDTO, Long userId) { + //微信appid + CommonInfo one = commonInfoService.findOne(5); + //微信秘钥 + CommonInfo two = commonInfoService.findOne(21); + SnsToken snsToken = SnsAPI.oauth2AccessToken(one.getValue(), two.getValue(), bindWxDTO.getCode()); + String openid = snsToken.getOpenid(); + int count = count(new LambdaQueryWrapper().eq(UserEntity::getWxOpenId, openid)); + if (count > 0) { + return Result.error("该微信已被其他用户绑定"); + } + + UserEntity userEntity = getOne(new LambdaQueryWrapper().eq(UserEntity::getUserId, userId)); + if (StringUtils.isNotBlank(userEntity.getWxOpenId())) { + return Result.error("当前用户已绑定微信"); + } + + userEntity.setWxOpenId(openid); + userEntity.setUpdateTime(DateUtil.now()); + update(userEntity, new LambdaQueryWrapper().eq(UserEntity::getUserId, userId)); + return Result.success(); + } } From cdc2a65330d79cfdbf0262e8a5d753963e80059e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Thu, 27 Mar 2025 11:42:53 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sqx/modules/app/service/impl/UserServiceImpl.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java b/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java index dd1b5191..e5e59f6b 100644 --- a/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java +++ b/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java @@ -1829,10 +1829,13 @@ public class UserServiceImpl extends ServiceImpl implements @Override public Result bindWx(BindWxDTO bindWxDTO, Long userId) { //微信appid - CommonInfo one = commonInfoService.findOne(5); + CommonInfo one = commonInfoService.findOne(937); //微信秘钥 - CommonInfo two = commonInfoService.findOne(21); + CommonInfo two = commonInfoService.findOne(936); SnsToken snsToken = SnsAPI.oauth2AccessToken(one.getValue(), two.getValue(), bindWxDTO.getCode()); + if (!snsToken.isSuccess()) { + return Result.error("获取失败"); + } String openid = snsToken.getOpenid(); int count = count(new LambdaQueryWrapper().eq(UserEntity::getWxOpenId, openid)); if (count > 0) { From 782eb71fa16c35e692f4cfb51afc762ebad3b432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Thu, 27 Mar 2025 11:45:33 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sqx/modules/app/controller/app/AppLoginController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java b/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java index 4e3ed94a..6ccfa249 100644 --- a/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java +++ b/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java @@ -174,7 +174,7 @@ public class AppLoginController { @PostMapping("/bindWx") @ResponseBody @Debounce(interval = 2500, value = "#code") - public Result bindWx(@RequestBody BindWxDTO bindWxDTO, @RequestHeader Long userId) { + public Result bindWx(@RequestBody BindWxDTO bindWxDTO, @RequestAttribute("userId") Long userId) { return userService.bindWx(bindWxDTO, userId); } From 6979421db70fb82198af295e8f298bbc064210f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Thu, 27 Mar 2025 13:32:57 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BB=91=E5=AE=9A=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/app/AppOrdersController.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java b/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java index e2d45358..f76dc426 100644 --- a/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java +++ b/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java @@ -1,8 +1,15 @@ package com.sqx.modules.orders.controller.app; +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.sqx.common.annotation.Debounce; import com.sqx.common.utils.Result; import com.sqx.modules.app.annotation.Login; +import com.sqx.modules.app.entity.UserEntity; +import com.sqx.modules.app.service.UserService; +import com.sqx.modules.common.entity.CommonInfo; +import com.sqx.modules.common.service.CommonInfoService; import com.sqx.modules.orders.service.OrdersService; import com.sqx.modules.sys.controller.AbstractController; import io.swagger.annotations.Api; @@ -26,6 +33,22 @@ public class AppOrdersController extends AbstractController { @Autowired private OrdersService ordersService; + @Autowired + private CommonInfoService commonInfoService; + @Autowired + private UserService userService; + + private void checkWxBind(Long userId) { + CommonInfo commonInfo = commonInfoService.findOne(938); + if (commonInfo == null || !"1".equals(commonInfo.getValue())) { + return; + } + UserEntity userEntity = userService.getOne(new LambdaQueryWrapper().eq(UserEntity::getUserId, userId).select(UserEntity::getWxOpenId)); + if (StrUtil.isBlank(userEntity.getWxOpenId())) { + throw new RuntimeException("请先绑定微信"); + } + } + /** * 生成商品订单 @@ -39,6 +62,7 @@ public class AppOrdersController extends AbstractController { @ApiOperation("生成商品订单") @Debounce(interval = 20000, value = "#userId") public Result insertCourseOrders(Long courseId,Long courseDetailsId, @RequestAttribute("userId") Long userId) { + checkWxBind(userId); return ordersService.insertCourseOrders(courseId, courseDetailsId,userId); } @@ -53,6 +77,7 @@ public class AppOrdersController extends AbstractController { @GetMapping("/insertCourseOrders/limit10") @ApiOperation("生成商品订单") public Result insertCourseOrdersLimit10(Long courseId, @RequestAttribute("userId") Long userId) { + checkWxBind(userId); return ordersService.insertCourseOrdersLimit10(courseId, userId); } @@ -60,6 +85,7 @@ public class AppOrdersController extends AbstractController { @GetMapping("/insertVipOrders") @ApiOperation("生成会员订单") public Result insertVipOrders(@ApiParam("会员详情信息") Long vipDetailsId, @RequestAttribute("userId") Long userId) { + checkWxBind(userId); return ordersService.insertVipOrders(vipDetailsId, userId); } From 853b918d54f0cb29ec26813a62d98b6c7be979e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Thu, 27 Mar 2025 13:35:22 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BB=91=E5=AE=9A=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sqx/modules/orders/controller/app/AppOrdersController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java b/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java index f76dc426..00cbc309 100644 --- a/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java +++ b/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java @@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.sqx.common.annotation.Debounce; +import com.sqx.common.exception.SqxException; import com.sqx.common.utils.Result; import com.sqx.modules.app.annotation.Login; import com.sqx.modules.app.entity.UserEntity; @@ -45,7 +46,7 @@ public class AppOrdersController extends AbstractController { } UserEntity userEntity = userService.getOne(new LambdaQueryWrapper().eq(UserEntity::getUserId, userId).select(UserEntity::getWxOpenId)); if (StrUtil.isBlank(userEntity.getWxOpenId())) { - throw new RuntimeException("请先绑定微信"); + throw new SqxException("请先绑定微信"); } } From 8f4f69781f38f94f2613736fffdb39a5a37fbe12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Thu, 27 Mar 2025 13:44:07 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BB=91=E5=AE=9A=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sqx/modules/app/service/impl/UserServiceImpl.java | 2 +- .../sqx/modules/orders/controller/app/AppOrdersController.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java b/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java index e5e59f6b..a5130233 100644 --- a/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java +++ b/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java @@ -1842,7 +1842,7 @@ public class UserServiceImpl extends ServiceImpl implements return Result.error("该微信已被其他用户绑定"); } - UserEntity userEntity = getOne(new LambdaQueryWrapper().eq(UserEntity::getUserId, userId)); + UserEntity userEntity = queryByUserId(userId); if (StringUtils.isNotBlank(userEntity.getWxOpenId())) { return Result.error("当前用户已绑定微信"); } diff --git a/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java b/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java index 00cbc309..ca14ccaf 100644 --- a/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java +++ b/src/main/java/com/sqx/modules/orders/controller/app/AppOrdersController.java @@ -44,7 +44,7 @@ public class AppOrdersController extends AbstractController { if (commonInfo == null || !"1".equals(commonInfo.getValue())) { return; } - UserEntity userEntity = userService.getOne(new LambdaQueryWrapper().eq(UserEntity::getUserId, userId).select(UserEntity::getWxOpenId)); + UserEntity userEntity = userService.queryByUserId(userId); if (StrUtil.isBlank(userEntity.getWxOpenId())) { throw new SqxException("请先绑定微信"); }