Преглед на файлове

feat(app): 添加微信小程序登录和用户信息获取接口

- 在 application-dev.yml 中添加微信小程序配置- 在 AppUserController 中添加微信小程序登录和用户信息获取接口
- 重命名 UserMapper 为 AppUserMapper
- 在 jeecg_config.properties 中移除 exclude_table配置
- 新增 WxMaConfiguration 和 WxMaProperties 类用于微信小程序配置
SheepHy преди 3 седмици
родител
ревизия
f62d61d431

+ 131 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/config/WxMaConfiguration.java

@@ -0,0 +1,131 @@
+package org.jeecg.config;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
+import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
+import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
+import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
+import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
+import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
+import com.google.common.collect.Lists;
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.error.WxRuntimeException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.io.File;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ */
+@Slf4j
+@Configuration
+@EnableConfigurationProperties(WxMaProperties.class)
+public class WxMaConfiguration {
+    private final WxMaProperties properties;
+
+    @Autowired
+    public WxMaConfiguration(WxMaProperties properties) {
+        this.properties = properties;
+    }
+
+    @Bean
+    public WxMaService wxMaService() {
+        List<WxMaProperties.Config> configs = this.properties.getConfigs();
+        if (configs == null) {
+            throw new WxRuntimeException("微信配置错误!");
+        }
+        WxMaService maService = new WxMaServiceImpl();
+        maService.setMultiConfigs(
+                configs.stream()
+                        .map(a -> {
+                            WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
+//                WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool());
+                            // 使用上面的配置时,需要同时引入jedis-lock的依赖,否则会报类无法找到的异常
+                            config.setAppid(a.getAppid());
+                            config.setSecret(a.getSecret());
+                            config.setToken(a.getToken());
+                            config.setAesKey(a.getAesKey());
+                            config.setMsgDataFormat(a.getMsgDataFormat());
+                            return config;
+                        }).collect(Collectors.toMap(WxMaDefaultConfigImpl::getAppid, a -> a, (o, n) -> o)));
+        return maService;
+    }
+
+    @Bean
+    public WxMaMessageRouter wxMaMessageRouter(WxMaService wxMaService) {
+        final WxMaMessageRouter router = new WxMaMessageRouter(wxMaService);
+        router
+                .rule().handler(logHandler).next()
+                .rule().async(false).content("订阅消息").handler(subscribeMsgHandler).end()
+                .rule().async(false).content("文本").handler(textHandler).end()
+                .rule().async(false).content("图片").handler(picHandler).end()
+                .rule().async(false).content("二维码").handler(qrcodeHandler).end();
+        return router;
+    }
+
+    private final WxMaMessageHandler subscribeMsgHandler = (wxMessage, context, service, sessionManager) -> {
+        service.getMsgService().sendSubscribeMsg(WxMaSubscribeMessage.builder()
+                .templateId("此处更换为自己的模板id")
+                .data(Lists.newArrayList(
+                        new WxMaSubscribeMessage.MsgData("keyword1", "339208499")))
+                .toUser(wxMessage.getFromUser())
+                .build());
+        return null;
+    };
+
+    private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> {
+        log.info("收到消息:" + wxMessage.toString());
+        service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson())
+                .toUser(wxMessage.getFromUser()).build());
+        return null;
+    };
+
+    private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> {
+        service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息")
+                .toUser(wxMessage.getFromUser()).build());
+        return null;
+    };
+
+    private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> {
+        try {
+            WxMediaUploadResult uploadResult = service.getMediaService()
+                    .uploadMedia("image", "png",
+                            ClassLoader.getSystemResourceAsStream("tmp.png"));
+            service.getMsgService().sendKefuMsg(
+                    WxMaKefuMessage
+                            .newImageBuilder()
+                            .mediaId(uploadResult.getMediaId())
+                            .toUser(wxMessage.getFromUser())
+                            .build());
+        } catch (WxErrorException e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    };
+
+    private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> {
+        try {
+            final File file = service.getQrcodeService().createQrcode("123", 430);
+            WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia("image", file);
+            service.getMsgService().sendKefuMsg(
+                    WxMaKefuMessage
+                            .newImageBuilder()
+                            .mediaId(uploadResult.getMediaId())
+                            .toUser(wxMessage.getFromUser())
+                            .build());
+        } catch (WxErrorException e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    };
+
+}

+ 45 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/config/WxMaProperties.java

@@ -0,0 +1,45 @@
+package org.jeecg.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import java.util.List;
+
+/**
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ */
+@Data
+@ConfigurationProperties(prefix = "wx.miniapp")
+public class WxMaProperties {
+
+    private List<Config> configs;
+
+    @Data
+    public static class Config {
+        /**
+         * 设置微信小程序的appid
+         */
+        private String appid;
+
+        /**
+         * 设置微信小程序的Secret
+         */
+        private String secret;
+
+        /**
+         * 设置微信小程序消息服务器配置的token
+         */
+        private String token;
+
+        /**
+         * 设置微信小程序消息服务器配置的EncodingAESKey
+         */
+        private String aesKey;
+
+        /**
+         * 消息格式,XML或者JSON
+         */
+        private String msgDataFormat;
+    }
+
+}

+ 3 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/app/controller/AppUserController.java

@@ -1,5 +1,6 @@
 package org.jeecg.modules.app.controller;
 
+import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.media.Schema;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.extern.slf4j.Slf4j;
@@ -28,6 +29,7 @@ public class AppUserController {
      * @return {@link Result}<{@link ?}>
      */
     @GetMapping("/loginByCode")
+    @Operation(summary = "微信小程序登录接口")
     public Result<LoginUserVO> wechatLogin(@RequestParam @Schema(description = "微信登录返回的code") String code){
         return Result.ok(userService.wechatLogin(code));
     }
@@ -40,6 +42,7 @@ public class AppUserController {
      * @return {@link Result}<{@link ?}>
      */
     @PostMapping("/wechatRegister")
+    @Operation(summary = "微信小程序获取用户信息接口")
     public Result<LoginUserVO> wechatInfo(@RequestBody UserWechatRegisterDTO userWechatRegisterDTO, HttpServletRequest request){
         return Result.ok(userService.wechatInfo(userWechatRegisterDTO, request));
     }

+ 1 - 1
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/app/mapper/UserMapper.java → national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/app/mapper/AppUserMapper.java

@@ -5,5 +5,5 @@ import org.apache.ibatis.annotations.Mapper;
 import org.jeecg.modules.app.entity.User;
 
 @Mapper
-public interface UserMapper extends BaseMapper<User> {
+public interface AppUserMapper extends BaseMapper<User> {
 }

+ 2 - 2
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/app/service/impl/UserServiceImpl.java

@@ -14,7 +14,7 @@ import org.jeecg.common.system.util.JwtUtil;
 import org.jeecg.common.util.RedisUtil;
 import org.jeecg.modules.app.dto.UserWechatRegisterDTO;
 import org.jeecg.modules.app.entity.User;
-import org.jeecg.modules.app.mapper.UserMapper;
+import org.jeecg.modules.app.mapper.AppUserMapper;
 import org.jeecg.modules.app.service.IUserService;
 import org.jeecg.modules.app.vo.LoginUserVO;
 import org.springframework.beans.BeanUtils;
@@ -29,7 +29,7 @@ import static org.jeecg.common.constant.CommonConstant.*;
 import static org.jeecg.common.util.PasswordUtil.SALTA_APP;
 
 @Service
-public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
+public class UserServiceImpl extends ServiceImpl<AppUserMapper, User> implements IUserService {
     @Resource
     private WxMaService wxMaService;
     @Autowired

+ 10 - 0
national-motion-module-system/national-motion-system-start/src/main/resources/application-dev.yml

@@ -324,3 +324,13 @@ justauth:
     type: default
     prefix: 'demo::'
     timeout: 1h
+# 微信相关
+wx:
+  # 微信小程序
+  miniapp:
+    configs:
+      - appid:
+        secret:
+        token: #微信小程序消息服务器配置的token
+        aesKey: #微信小程序消息服务器配置的EncodingAESKey
+        msgDataFormat: JSON

+ 1 - 1
national-motion-module-system/national-motion-system-start/src/main/resources/jeecg/jeecg_config.properties

@@ -26,4 +26,4 @@ db_filed_convert=true
 page_search_filed_num=1
 #page_filter_fields
 page_filter_fields=create_time,create_by,update_time,update_by
-exclude_table=act_,ext_act_,design_,onl_,sys_,qrtz_
+exclude_table=