diff --git a/pom.xml b/pom.xml
index 71045ee..4ba3da2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -163,6 +163,14 @@
1.2.62
+
+
+ org.springframework.boot
+ spring-boot-configuration-processor
+ true
+
+
+
diff --git a/src/main/java/com/cultural/heritage/config/WxOpenConfig.java b/src/main/java/com/cultural/heritage/config/WxOpenConfig.java
new file mode 100644
index 0000000..1062e31
--- /dev/null
+++ b/src/main/java/com/cultural/heritage/config/WxOpenConfig.java
@@ -0,0 +1,44 @@
+package com.cultural.heritage.config;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
+import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+@Data
+@Slf4j
+@Configuration
+@ConfigurationProperties(prefix = "wx.mini")
+public class WxOpenConfig {
+
+ private String appId;
+
+ private String appSecret;
+
+ private WxMaService wxMaService;
+
+ /**
+ * 单例模式
+ */
+ public WxMaService getWxMaService() {
+ if (wxMaService != null) {
+ return wxMaService;
+ }
+ synchronized (this) {
+ if (wxMaService != null) {
+ return wxMaService;
+ }
+ WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
+ config.setAppid(appId);
+ config.setSecret(appSecret);
+ WxMaService service = new WxMaServiceImpl();
+ service.setWxMaConfig(config);
+ wxMaService = service;
+ return wxMaService;
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/cultural/heritage/controller/user/UserController.java b/src/main/java/com/cultural/heritage/controller/user/UserController.java
index 206fd7f..c1ddc02 100644
--- a/src/main/java/com/cultural/heritage/controller/user/UserController.java
+++ b/src/main/java/com/cultural/heritage/controller/user/UserController.java
@@ -1,11 +1,14 @@
package com.cultural.heritage.controller.user;
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils;
+import com.cultural.heritage.config.WxOpenConfig;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonRequest;
@@ -17,6 +20,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.DigestUtils;
@@ -41,6 +45,10 @@ public class UserController {
private UserService userService;
+ @Resource
+ private WxOpenConfig wxOpenConfig;
+
+
/**
* 用户登录
* @param userLoginRequest 用户登录请求体
@@ -61,6 +69,34 @@ public class UserController {
return ResultUtils.success(userVO);
}
+
+
+
+ /**
+ * 用户登录(微信小程序)
+ * @param code 用户登录凭证
+ * @param request http
+ * @return 用户登录信息
+ */
+ @GetMapping("/login/wx_open")
+ public BaseResponse userLoginByWxOpen(@RequestParam("code") String code, HttpServletRequest request) {
+ WxMaJscode2SessionResult sessionInfo;
+ try {
+ WxMaService wxMaService = wxOpenConfig.getWxMaService();
+ sessionInfo = wxMaService.jsCode2SessionInfo(code);
+ String openid = sessionInfo.getOpenid();
+ if (StringUtils.isAnyBlank(openid)) {
+ throw new BusinessException(ErrorCode.SYSTEM_ERROR);
+ }
+ return ResultUtils.success(userService.userLoginByMpOpen(sessionInfo, request));
+ } catch (WxErrorException e) {
+ log.error("userLoginByWxOpen error", e);
+ throw new BusinessException(ErrorCode.SYSTEM_ERROR, "登录失败,系统错误");
+ }
+
+ }
+
+
/**
* 退出登录
* @param request http
@@ -72,6 +108,17 @@ public class UserController {
return ResultUtils.success(result);
}
+ /**
+ * 获取当前登录用户
+ * @param request http
+ * @return 用户登录信息
+ */
+ @GetMapping("/get/login")
+ public BaseResponse getLoginUser(HttpServletRequest request) {
+ User user = userService.getLoginUser(request);
+ return ResultUtils.success(userService.getUserVO(user));
+ }
+
/**
diff --git a/src/main/java/com/cultural/heritage/controller/user/WeChatController.java b/src/main/java/com/cultural/heritage/controller/user/WeChatController.java
index d844c46..5a993ed 100644
--- a/src/main/java/com/cultural/heritage/controller/user/WeChatController.java
+++ b/src/main/java/com/cultural/heritage/controller/user/WeChatController.java
@@ -31,4 +31,7 @@ public class WeChatController {
return jsonObject;
}
+
+
+
}
diff --git a/src/main/java/com/cultural/heritage/model/vo/UserVO.java b/src/main/java/com/cultural/heritage/model/vo/UserVO.java
index 960daf3..55c29ee 100644
--- a/src/main/java/com/cultural/heritage/model/vo/UserVO.java
+++ b/src/main/java/com/cultural/heritage/model/vo/UserVO.java
@@ -37,6 +37,7 @@ public class UserVO implements Serializable {
*/
private String userRole;
+
private static final long serialVersionUID = 1L;
}
diff --git a/src/main/java/com/cultural/heritage/service/user/UserService.java b/src/main/java/com/cultural/heritage/service/user/UserService.java
index 89a99a7..e2b2e3f 100644
--- a/src/main/java/com/cultural/heritage/service/user/UserService.java
+++ b/src/main/java/com/cultural/heritage/service/user/UserService.java
@@ -1,5 +1,6 @@
package com.cultural.heritage.service.user;
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.dto.user.UserQueryRequest;
@@ -45,4 +46,10 @@ public interface UserService extends IService {
* 获取脱敏的用户信息
*/
List getUserVO(List userList);
+
+
+ /**
+ * 用户登录(微信小程序)
+ */
+ UserVO userLoginByMpOpen(WxMaJscode2SessionResult sessionInfo, HttpServletRequest request);
}
diff --git a/src/main/java/com/cultural/heritage/service/user/impl/UserServiceImpl.java b/src/main/java/com/cultural/heritage/service/user/impl/UserServiceImpl.java
index 3578d49..ee9723a 100644
--- a/src/main/java/com/cultural/heritage/service/user/impl/UserServiceImpl.java
+++ b/src/main/java/com/cultural/heritage/service/user/impl/UserServiceImpl.java
@@ -1,10 +1,13 @@
package com.cultural.heritage.service.user.impl;
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
+import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.constant.CommonConstant;
import com.cultural.heritage.exception.BusinessException;
+import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.mapper.UserMapper;
import com.cultural.heritage.model.dto.user.UserQueryRequest;
import com.cultural.heritage.model.entity.User;
@@ -110,6 +113,9 @@ public class UserServiceImpl extends ServiceImpl implements Us
return currentUser;
}
+ /**
+ * 获取用户查询条件
+ */
@Override
public QueryWrapper getQueryWrapper(UserQueryRequest userQueryRequest) {
if (userQueryRequest == null) {
@@ -142,4 +148,36 @@ public class UserServiceImpl extends ServiceImpl implements Us
return userList.stream().map(this::getUserVO).collect(Collectors.toList());
}
+ /**
+ * 用户登录(微信小程序)
+ */
+ @Override
+ public UserVO userLoginByMpOpen(WxMaJscode2SessionResult sessionInfo, HttpServletRequest request) {
+ String openid = sessionInfo.getOpenid();
+
+ // 查询用户是否存在
+ QueryWrapper queryWrapper = new QueryWrapper<>();
+ queryWrapper.eq("miniOpenId", openid);
+ User user = this.getOne(queryWrapper);
+ // 被封号,禁止登录
+ if (user != null && UserRoleEnum.BAN.getValue().equals(user.getUserRole())) {
+ throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "该用户已封禁,禁止登录");
+ }
+ // 用户不存在则创建
+ if (user == null) {
+ user = new User();
+ String userAccount = RandomUtil.randomNumbers(14);
+ String userPassword = RandomUtil.randomString(16);
+ user.setUserAccount(userAccount);
+ user.setUserPassword(userPassword);
+ user.setMiniOpenId(openid);
+ user.setUserName("普通用户");
+ boolean result = this.save(user);
+ ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR, "登录失败");
+ // 记住用户的登录态
+ }
+ request.getSession().setAttribute(USER_LOGIN_STATE, user);
+ return this.getUserVO(user);
+ }
+
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index f86a08a..194ecbe 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -47,7 +47,8 @@ hwyun:
endPoint: obs.cn-north-4.myhuaweicloud.com
bucketName: carbon2
-weixin:
+
+wx:
mini:
- appid: wx3f968a09e31d6bed
- secret: 847bdda7c2b01e88d59948b9ba50ef8d
+ appId: wx3f968a09e31d6bed
+ appSecret: 847bdda7c2b01e88d59948b9ba50ef8d