From bc690764d3f50eba0d8131d915decb6f77ab6e39 Mon Sep 17 00:00:00 2001
From: ruying408 <1877972603@qq.com>
Date: Thu, 5 Sep 2024 00:27:05 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E:=20=E5=AF=B9=E7=AB=AF?=
=?UTF-8?q?=E7=94=A8=E6=88=B7=E5=AE=8C=E5=96=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 13 ++++
.../core/exception/CoolExceptionHandler.java | 10 ++++
.../controller/app/AppUserInfoController.java | 41 +++++++++++++
.../modules/user/entity/UserInfoEntity.java | 4 +-
.../modules/user/entity/UserWxEntity.java | 47 +++++++++++++++
.../modules/user/mapper/UserWxMapper.java | 10 ++++
.../modules/user/service/UserInfoService.java | 15 +++++
.../modules/user/service/UserWxService.java | 15 +++++
.../cool/modules/user/service/WxService.java | 48 +++++++++++++++
.../service/impl/UserInfoServiceImpl.java | 35 ++++++++++-
.../service/impl/UserLoginServiceImpl.java | 50 +++++++++++++---
.../user/service/impl/UserWxServiceImpl.java | 60 +++++++++++++++++++
.../cool/modules/user/util/UserSmsUtil.java | 2 +-
.../java/com/cool/CoolCodeGeneratorTest.java | 50 +++++++++++++---
14 files changed, 380 insertions(+), 20 deletions(-)
create mode 100644 src/main/java/com/cool/modules/user/entity/UserWxEntity.java
create mode 100644 src/main/java/com/cool/modules/user/mapper/UserWxMapper.java
create mode 100644 src/main/java/com/cool/modules/user/service/UserWxService.java
create mode 100644 src/main/java/com/cool/modules/user/service/WxService.java
create mode 100644 src/main/java/com/cool/modules/user/service/impl/UserWxServiceImpl.java
diff --git a/pom.xml b/pom.xml
index adc6348..8bfe854 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,6 +25,7 @@
2.0.51
2.5.0
0.9.16
+ 4.6.3.B
@@ -120,6 +121,18 @@
springdoc-openapi-starter-webmvc-ui
${springdoc-openapi.version}
+
+
+ com.github.binarywang
+ weixin-java-miniapp
+ ${weixin-java.version}
+
+
+ com.github.binarywang
+ weixin-java-mp
+ ${weixin-java.version}
+
+
jakarta.servlet
jakarta.servlet-api
diff --git a/src/main/java/com/cool/core/exception/CoolExceptionHandler.java b/src/main/java/com/cool/core/exception/CoolExceptionHandler.java
index 39cd40e..c18bbd2 100644
--- a/src/main/java/com/cool/core/exception/CoolExceptionHandler.java
+++ b/src/main/java/com/cool/core/exception/CoolExceptionHandler.java
@@ -3,6 +3,7 @@ package com.cool.core.exception;
import cn.hutool.core.util.ObjUtil;
import com.cool.core.request.R;
import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
@@ -25,6 +26,9 @@ public class CoolExceptionHandler {
r.put("code", e.getCode());
r.put("message", e.getMessage());
}
+ if (ObjUtil.isNotEmpty(e.getCause())) {
+ log.error(e.getCause().getMessage(), e.getCause());
+ }
return r;
}
@@ -58,4 +62,10 @@ public class CoolExceptionHandler {
log.error(e.getMessage(), e);
return R.error();
}
+
+ @ExceptionHandler(WxErrorException.class)
+ public R handleException(WxErrorException e) {
+ log.error(e.getMessage(), e);
+ return R.error(e.getMessage());
+ }
}
diff --git a/src/main/java/com/cool/modules/user/controller/app/AppUserInfoController.java b/src/main/java/com/cool/modules/user/controller/app/AppUserInfoController.java
index 764e82f..0f09313 100644
--- a/src/main/java/com/cool/modules/user/controller/app/AppUserInfoController.java
+++ b/src/main/java/com/cool/modules/user/controller/app/AppUserInfoController.java
@@ -1,5 +1,6 @@
package com.cool.modules.user.controller.app;
+import cn.hutool.json.JSONObject;
import com.cool.core.annotation.CoolRestController;
import com.cool.core.request.R;
import com.cool.core.util.CoolSecurityUtil;
@@ -10,6 +11,8 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestAttribute;
@RequiredArgsConstructor
@Tag(name = "用户信息", description = "用户信息")
@@ -26,4 +29,42 @@ public class AppUserInfoController {
return R.ok(EntityUtils.toMap(userInfoEntity,
"password"));
}
+
+ @Operation(summary = "更新用户信息")
+ @PostMapping("/updatePerson")
+ public R updatePerson(@RequestAttribute JSONObject requestParams) {
+ UserInfoEntity infoEntity = requestParams.toBean(UserInfoEntity.class);
+ infoEntity.setId(CoolSecurityUtil.getCurrentUserId());
+ return R.ok(
+ userInfoService.updateById(infoEntity)
+ );
+ }
+
+ @Operation(summary = "更新用户密码")
+ @PostMapping("/updatePassword")
+ public R updatePassword(
+ @RequestAttribute JSONObject requestParams
+ ) {
+ String password = requestParams.get("password", String.class);
+ String code = requestParams.get("code", String.class);
+ userInfoService.updatePassword(CoolSecurityUtil.getCurrentUserId(), password, code);
+ return R.ok();
+ }
+
+ @Operation(summary = "注销")
+ @PostMapping("/logoff")
+ public R logoff() {
+ userInfoService.logoff(CoolSecurityUtil.getCurrentUserId());
+ return R.ok();
+ }
+
+ @Operation(summary = "绑定手机号")
+ @PostMapping("/bindPhone")
+ public R bindPhone(
+ @RequestAttribute JSONObject requestParams) {
+ String phone = requestParams.get("phone", String.class);
+ String code = requestParams.get("code", String.class);
+ userInfoService.bindPhone(CoolSecurityUtil.getCurrentUserId(), phone, code);
+ return R.ok();
+ }
}
diff --git a/src/main/java/com/cool/modules/user/entity/UserInfoEntity.java b/src/main/java/com/cool/modules/user/entity/UserInfoEntity.java
index cf6f77c..776177e 100644
--- a/src/main/java/com/cool/modules/user/entity/UserInfoEntity.java
+++ b/src/main/java/com/cool/modules/user/entity/UserInfoEntity.java
@@ -27,10 +27,10 @@ public class UserInfoEntity extends BaseEntity {
private String phone;
@ColumnDefine(comment = "性别 0-未知 1-男 2-女", defaultValue = "0")
- private String gender;
+ private Integer gender;
@ColumnDefine(comment = "状态 0-禁用 1-正常 2-已注销", defaultValue = "1")
- private String status;
+ private Integer status;
@ColumnDefine(comment = "登录方式 0-小程序 1-公众号 2-H5", defaultValue = "0")
private String loginType;
diff --git a/src/main/java/com/cool/modules/user/entity/UserWxEntity.java b/src/main/java/com/cool/modules/user/entity/UserWxEntity.java
new file mode 100644
index 0000000..7789984
--- /dev/null
+++ b/src/main/java/com/cool/modules/user/entity/UserWxEntity.java
@@ -0,0 +1,47 @@
+package com.cool.modules.user.entity;
+
+import com.cool.core.base.BaseEntity;
+import com.mybatisflex.annotation.Table;
+import com.tangzc.autotable.annotation.Index;
+import com.tangzc.mybatisflex.autotable.annotation.ColumnDefine;
+import com.tangzc.mybatisflex.autotable.annotation.UniIndex;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+@Table(value = "user_wx", comment = "微信用户")
+public class UserWxEntity extends BaseEntity {
+
+ @Index
+ @ColumnDefine(comment = "微信unionid")
+ private String unionid;
+
+ @UniIndex
+ @ColumnDefine(comment = "微信openid", notNull = true)
+ private String openid;
+
+ @ColumnDefine(comment = "头像")
+ private String avatarUrl;
+
+ @ColumnDefine(comment = "昵称")
+ private String nickName;
+
+ @ColumnDefine(comment = "性别 0-未知 1-男 2-女", defaultValue = "0")
+ private Integer gender;
+
+ @ColumnDefine(comment = "语言")
+ private String language;
+
+ @ColumnDefine(comment = "城市")
+ private String city;
+
+ @ColumnDefine(comment = "省份")
+ private String province;
+
+ @ColumnDefine(comment = "国家")
+ private String country;
+
+ @ColumnDefine(comment = "类型 0-小程序 1-公众号 2-H5 3-APP", defaultValue = "0")
+ private Integer type;
+}
diff --git a/src/main/java/com/cool/modules/user/mapper/UserWxMapper.java b/src/main/java/com/cool/modules/user/mapper/UserWxMapper.java
new file mode 100644
index 0000000..f23a071
--- /dev/null
+++ b/src/main/java/com/cool/modules/user/mapper/UserWxMapper.java
@@ -0,0 +1,10 @@
+package com.cool.modules.user.mapper;
+
+import com.mybatisflex.core.BaseMapper;
+import com.cool.modules.user.entity.UserWxEntity;
+
+/**
+ * 微信用户
+ */
+public interface UserWxMapper extends BaseMapper {
+}
diff --git a/src/main/java/com/cool/modules/user/service/UserInfoService.java b/src/main/java/com/cool/modules/user/service/UserInfoService.java
index bcc2b31..a779e80 100644
--- a/src/main/java/com/cool/modules/user/service/UserInfoService.java
+++ b/src/main/java/com/cool/modules/user/service/UserInfoService.java
@@ -10,4 +10,19 @@ public interface UserInfoService extends BaseService {
* @return
*/
UserInfoEntity person(Long userId);
+
+ /**
+ * 更新用户密码
+ */
+ void updatePassword(Long userId, String password, String code);
+
+ /**
+ * 注销
+ */
+ void logoff(Long currentUserId);
+
+ /**
+ * 绑定手机号
+ */
+ void bindPhone(Long currentUserId, String phone, String code);
}
diff --git a/src/main/java/com/cool/modules/user/service/UserWxService.java b/src/main/java/com/cool/modules/user/service/UserWxService.java
new file mode 100644
index 0000000..28e0d65
--- /dev/null
+++ b/src/main/java/com/cool/modules/user/service/UserWxService.java
@@ -0,0 +1,15 @@
+package com.cool.modules.user.service;
+
+import com.cool.core.base.BaseService;
+import com.cool.modules.user.entity.UserWxEntity;
+
+/**
+ * 微信用户
+ */
+public interface UserWxService extends BaseService {
+
+ /**
+ * 获取小程序用户信息
+ */
+ UserWxEntity getMiniUserInfo(String code, String encryptedData, String iv);
+}
diff --git a/src/main/java/com/cool/modules/user/service/WxService.java b/src/main/java/com/cool/modules/user/service/WxService.java
new file mode 100644
index 0000000..d14e2ce
--- /dev/null
+++ b/src/main/java/com/cool/modules/user/service/WxService.java
@@ -0,0 +1,48 @@
+package com.cool.modules.user.service;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
+import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
+import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
+import cn.hutool.core.util.ObjUtil;
+import com.cool.core.util.CoolPluginInvokers;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.mp.api.WxMpService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class WxService {
+ private WxMaService wxMaService;
+
+ private WxMpService wxMpService;
+ private WxMaService getWxMaService() {
+ if (ObjUtil.isNotEmpty(wxMaService)) {
+ return wxMaService;
+ }
+ wxMaService = (WxMaService)CoolPluginInvokers.invoke("wx-sdk", "getWxMaService");
+ return wxMaService;
+ }
+ private WxMpService getWxMpService() {
+ if (ObjUtil.isNotEmpty(wxMpService)) {
+ return wxMpService;
+ }
+ wxMpService = (WxMpService)CoolPluginInvokers.invoke("wx-sdk", "getWxMpService");
+ return wxMpService;
+ }
+
+ public WxMaJscode2SessionResult getSessionInfo(String jsCode) throws WxErrorException {
+ return getWxMaService().getUserService()
+ .getSessionInfo(jsCode);
+ }
+
+ public WxMaPhoneNumberInfo getPhoneNumber(String jsCode) throws WxErrorException {
+ return getWxMaService().getUserService()
+ .getPhoneNumber(jsCode);
+ }
+
+ public WxMaUserInfo getUserInfo(String sessionKey, String encryptedData, String ivStr) throws WxErrorException {
+ return getWxMaService().getUserService()
+ .getUserInfo(sessionKey, encryptedData, ivStr);
+ }
+
+}
diff --git a/src/main/java/com/cool/modules/user/service/impl/UserInfoServiceImpl.java b/src/main/java/com/cool/modules/user/service/impl/UserInfoServiceImpl.java
index 52858b5..e66e993 100644
--- a/src/main/java/com/cool/modules/user/service/impl/UserInfoServiceImpl.java
+++ b/src/main/java/com/cool/modules/user/service/impl/UserInfoServiceImpl.java
@@ -1,19 +1,52 @@
package com.cool.modules.user.service.impl;
+import cn.hutool.crypto.digest.MD5;
import com.cool.core.base.BaseServiceImpl;
import com.cool.modules.user.entity.UserInfoEntity;
import com.cool.modules.user.mapper.UserInfoMapper;
import com.cool.modules.user.service.UserInfoService;
+import com.cool.modules.user.util.UserSmsUtil;
+import com.cool.modules.user.util.UserSmsUtil.SendSceneEnum;
+import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
+@RequiredArgsConstructor
public class UserInfoServiceImpl extends BaseServiceImpl implements
UserInfoService {
+ private final UserSmsUtil userSmsUtil;
+
@Override
public UserInfoEntity person(Long userId) {
- UserInfoEntity info = mapper.selectOneById(userId);
+ UserInfoEntity info = getById(userId);
info.setPassword(null);
return info;
}
+
+ @Override
+ public void updatePassword(Long userId, String password, String code) {
+ UserInfoEntity info = getById(userId);
+ userSmsUtil.checkVerifyCode(info.getPhone(), code, SendSceneEnum.ALL);
+ info.setPassword(MD5.create().digestHex(password));
+ info.updateById();
+ }
+
+ @Override
+ public void logoff(Long userId) {
+ UserInfoEntity info = new UserInfoEntity();
+ info.setId(userId);
+ info.setStatus(2);
+ info.setNickName("已注销-00" + userId);
+ info.updateById();
+ }
+
+ @Override
+ public void bindPhone(Long userId, String phone, String code) {
+ userSmsUtil.checkVerifyCode(phone, code, SendSceneEnum.ALL);
+ UserInfoEntity info = new UserInfoEntity();
+ info.setId(userId);
+ info.setPhone(phone);
+ info.updateById();
+ }
}
diff --git a/src/main/java/com/cool/modules/user/service/impl/UserLoginServiceImpl.java b/src/main/java/com/cool/modules/user/service/impl/UserLoginServiceImpl.java
index 8a9909d..c1c9a04 100644
--- a/src/main/java/com/cool/modules/user/service/impl/UserLoginServiceImpl.java
+++ b/src/main/java/com/cool/modules/user/service/impl/UserLoginServiceImpl.java
@@ -1,5 +1,6 @@
package com.cool.modules.user.service.impl;
+import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjUtil;
@@ -13,13 +14,17 @@ import com.cool.core.security.jwt.JwtTokenUtil;
import com.cool.core.security.jwt.JwtUser;
import com.cool.modules.base.service.sys.BaseSysLoginService;
import com.cool.modules.user.entity.UserInfoEntity;
-import com.cool.modules.user.mapper.UserInfoMapper;
+import com.cool.modules.user.entity.UserWxEntity;
+import com.cool.modules.user.service.UserInfoService;
import com.cool.modules.user.service.UserLoginService;
+import com.cool.modules.user.service.UserWxService;
+import com.cool.modules.user.service.WxService;
import com.cool.modules.user.util.UserSmsUtil;
import com.cool.modules.user.util.UserSmsUtil.SendSceneEnum;
import com.mybatisflex.core.query.QueryWrapper;
import java.util.List;
import lombok.RequiredArgsConstructor;
+import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
@@ -32,11 +37,15 @@ public class UserLoginServiceImpl implements UserLoginService {
private final JwtTokenUtil jwtTokenUtil;
- private final UserInfoMapper userInfoMapper;
+ private final UserInfoService userInfoService;
private final UserSmsUtil userSmsUtil;
private final BaseSysLoginService baseSysLoginService;
+
+ private final UserWxService userWxService;
+
+ private final WxService wxService;
private final static List authority =
List.of(new SimpleGrantedAuthority("ROLE_" + UserTypeEnum.APP.name()));
@@ -44,14 +53,14 @@ public class UserLoginServiceImpl implements UserLoginService {
public void smsCode(String phone, String captchaId, String code) {
// 校验图片验证码,不通过直接抛异常
baseSysLoginService.captchaCheck(captchaId, code);
- userSmsUtil.sendVerifyCode(phone, SendSceneEnum.login);
+ userSmsUtil.sendVerifyCode(phone, SendSceneEnum.ALL);
coolCache.del("verify:img:" + captchaId);
}
@Override
public Object phoneVerifyCode(String phone, String smsCode) {
// 校验短信验证码,不通过直接抛异常
- userSmsUtil.checkVerifyCode(phone, smsCode, SendSceneEnum.login);
+ userSmsUtil.checkVerifyCode(phone, smsCode, SendSceneEnum.ALL);
return generateTokenByPhone(phone);
}
@@ -68,7 +77,24 @@ public class UserLoginServiceImpl implements UserLoginService {
@Override
public Object mini(String code, String encryptedData, String iv) {
- return null;
+ UserWxEntity userWxEntity = userWxService.getMiniUserInfo(code, encryptedData, iv);
+ return wxLoginToken(userWxEntity);
+ }
+
+ private Object wxLoginToken(UserWxEntity userWxEntity) {
+ String unionId = ObjUtil.isNotEmpty(userWxEntity.getUnionid()) ? userWxEntity.getUnionid()
+ : userWxEntity.getOpenid();
+ UserInfoEntity userInfoEntity = userInfoService.getOne(
+ QueryWrapper.create().eq(UserInfoEntity::getUnionid, unionId));
+ if (ObjUtil.isEmpty(userInfoEntity)) {
+ userInfoEntity = new UserInfoEntity();
+ userInfoEntity.setNickName(ObjUtil.isNotEmpty(userWxEntity.getNickName()) ? userWxEntity.getNickName() : generateRandomNickname());
+ userInfoEntity.setGender(userWxEntity.getGender());
+ userInfoEntity.setAvatarUrl(userWxEntity.getAvatarUrl());
+ userInfoEntity.setUnionid(unionId);
+ userInfoEntity.save();
+ }
+ return generateToken(userInfoEntity, null);
}
@Override
@@ -88,12 +114,20 @@ public class UserLoginServiceImpl implements UserLoginService {
@Override
public Object miniPhone(String code, String encryptedData, String iv) {
+ try {
+ WxMaPhoneNumberInfo phoneNumber = wxService.getPhoneNumber(code);
+ CoolPreconditions.checkEmpty(phoneNumber, "微信登录失败");
+ return generateTokenByPhone(phoneNumber.getPhoneNumber());
+ } catch (WxErrorException e) {
+ CoolPreconditions.alwaysThrow(e.getMessage(), e);
+ }
+ CoolPreconditions.alwaysThrow("微信登录失败");
return null;
}
@Override
public Object password(String phone, String password) {
- UserInfoEntity userInfoEntity = userInfoMapper.selectOneByQuery(
+ UserInfoEntity userInfoEntity = userInfoService.getOne(
QueryWrapper.create().eq(UserInfoEntity::getPhone, phone));
CoolPreconditions.checkEmpty(userInfoEntity, "账号或密码错误");
if (userInfoEntity.getPassword().equals(MD5.create().digestHex(password))) {
@@ -108,7 +142,7 @@ public class UserLoginServiceImpl implements UserLoginService {
* 根据手机号找到用户生成token
*/
private Object generateTokenByPhone(String phone) {
- UserInfoEntity userInfoEntity = userInfoMapper.selectOneByQuery(
+ UserInfoEntity userInfoEntity = userInfoService.getOne(
QueryWrapper.create().eq(UserInfoEntity::getPhone, phone));
if (ObjUtil.isEmpty(userInfoEntity)) {
userInfoEntity = new UserInfoEntity();
@@ -134,7 +168,7 @@ public class UserLoginServiceImpl implements UserLoginService {
* 生成token
*/
private Dict generateToken(Long userId, String refreshToken) {
- UserInfoEntity userInfoEntity = userInfoMapper.selectOneById(userId);
+ UserInfoEntity userInfoEntity = userInfoService.getById(userId);
return generateToken(userInfoEntity, refreshToken);
}
private Dict generateToken(UserInfoEntity userInfoEntity, String refreshToken) {
diff --git a/src/main/java/com/cool/modules/user/service/impl/UserWxServiceImpl.java b/src/main/java/com/cool/modules/user/service/impl/UserWxServiceImpl.java
new file mode 100644
index 0000000..ed185e3
--- /dev/null
+++ b/src/main/java/com/cool/modules/user/service/impl/UserWxServiceImpl.java
@@ -0,0 +1,60 @@
+package com.cool.modules.user.service.impl;
+
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
+import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.ObjUtil;
+import com.cool.core.base.BaseServiceImpl;
+import com.cool.core.exception.CoolPreconditions;
+import com.cool.modules.user.entity.UserWxEntity;
+import com.cool.modules.user.mapper.UserWxMapper;
+import com.cool.modules.user.service.UserWxService;
+import com.cool.modules.user.service.WxService;
+import com.mybatisflex.core.query.QueryWrapper;
+import lombok.RequiredArgsConstructor;
+import me.chanjar.weixin.common.error.WxErrorException;
+import org.springframework.stereotype.Service;
+
+/**
+ * 微信用户
+ */
+@Service
+@RequiredArgsConstructor
+public class UserWxServiceImpl extends BaseServiceImpl implements UserWxService {
+
+ private final WxService wxService;
+
+ /**
+ * 获得小程序用户信息
+ */
+ public UserWxEntity getMiniUserInfo(String code, String encryptedData, String iv) {
+ // 获取 session
+ WxMaJscode2SessionResult result = null;
+ try {
+ result = wxService.getSessionInfo(code);
+ // 解密数据
+ WxMaUserInfo wxMaUserInfo = wxService.getUserInfo(result.getSessionKey(), encryptedData, iv);
+ if (ObjUtil.isNotEmpty(wxMaUserInfo)) {
+ UserWxEntity userWxEntity = BeanUtil.copyProperties(wxMaUserInfo, UserWxEntity.class);
+ userWxEntity.setOpenid(result.getOpenid());
+ userWxEntity.setUnionid(wxMaUserInfo.getUnionId());
+ return getBySave(userWxEntity, 0);
+ }
+ } catch (WxErrorException e) {
+ CoolPreconditions.alwaysThrow(e.getMessage(), e);
+ }
+ CoolPreconditions.alwaysThrow("获得小程序用户信息");
+ return null;
+ }
+
+ public UserWxEntity getBySave(UserWxEntity entity, int type) {
+ UserWxEntity one = this.getOne(
+ QueryWrapper.create().eq(UserWxEntity::getOpenid, entity.getOpenid()));
+ if (ObjUtil.isEmpty(one)) {
+ entity.setType(type);
+ super.save(entity);
+ return entity;
+ }
+ return one;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/cool/modules/user/util/UserSmsUtil.java b/src/main/java/com/cool/modules/user/util/UserSmsUtil.java
index 6ee80eb..cb39a54 100644
--- a/src/main/java/com/cool/modules/user/util/UserSmsUtil.java
+++ b/src/main/java/com/cool/modules/user/util/UserSmsUtil.java
@@ -27,7 +27,7 @@ public class UserSmsUtil {
* 短信发送场景枚举
*/
public enum SendSceneEnum {
- login, // 登录
+ ALL,
}
private final CoolPluginService coolPluginService;
diff --git a/src/test/java/com/cool/CoolCodeGeneratorTest.java b/src/test/java/com/cool/CoolCodeGeneratorTest.java
index 40e9456..acf8caf 100644
--- a/src/test/java/com/cool/CoolCodeGeneratorTest.java
+++ b/src/test/java/com/cool/CoolCodeGeneratorTest.java
@@ -3,20 +3,54 @@ package com.cool;
import com.cool.core.code.CodeGenerator;
import com.cool.core.code.CodeModel;
import com.cool.core.code.CodeTypeEnum;
+import com.cool.modules.user.entity.*;
+import com.mybatisflex.annotation.Table;
+import java.util.List;
public class CoolCodeGeneratorTest {
public static void main(String[] args) {
CodeGenerator codeGenerator = new CodeGenerator();
codeGenerator.init();
+ List list = List.of(UserWxEntity.class);
- CodeModel codeModel = new CodeModel();
- codeModel.setType(CodeTypeEnum.ADMIN);
- codeModel.setName("测试CURD");
- codeModel.setModule("demo");
-// codeModel.setEntity(DemoEntity.class);
+ list.forEach(o -> {
+ Table annotation = (Table) o.getAnnotation(Table.class);
+ CodeModel codeModel = new CodeModel();
+ codeModel.setType(CodeTypeEnum.APP);
+ codeModel.setName(annotation.comment());
+ codeModel.setModule(getFirstWord(o.getSimpleName()));
+ codeModel.setEntity(o);
+ // 生成 controller
+// codeGenerator.controller(codeModel);
+ // 生成 mapper
+ codeGenerator.mapper(codeModel);
+ // 生成 service
+ codeGenerator.service(codeModel);
+ });
+ }
- codeGenerator.controller(codeModel);
- codeGenerator.mapper(codeModel);
- codeGenerator.service(codeModel);
+ public static String getFirstWord(String className) {
+ if (className == null || className.isEmpty()) {
+ return "";
+ }
+
+ StringBuilder firstWord = new StringBuilder();
+ boolean foundFirstWord = false;
+
+ for (char c : className.toCharArray()) {
+ if (Character.isUpperCase(c)) {
+ if (foundFirstWord) {
+ break;
+ }
+ firstWord.append(c);
+ foundFirstWord = true;
+ } else {
+ if (foundFirstWord) {
+ firstWord.append(c);
+ }
+ }
+ }
+
+ return firstWord.toString().toLowerCase();
}
}