Bladeren bron

报备跟进的提醒

姜永辉 1 jaar geleden
bovenliggende
commit
3b1477c27d

+ 27 - 0
src/main/java/com/dk/oauth/config/WxConfig.java

@@ -78,5 +78,32 @@ public class WxConfig {
     @Value("${upload.path}")
     private String uploadPath;
 
+    /**
+     * @desc   : 公众号appid
+     * @author : 姜永辉
+     * @date   : 2024/07/12 9:03
+     */
+    @Value("${wx.wxPublicAccountAppId}")
+    private String wxPublicAccountAppId;
+    /**
+     * @desc   : 公众号Secret
+     * @author : 姜永辉
+     * @date   : 2024/07/12 9:03
+     */
+    @Value("${wx.wxPublicAccountAppSecret}")
+    private String wxPublicAccountAppSecret;
+
+    /**
+     * @desc   : 公众号发送消息的api
+     * @author : 姜永辉
+     * @date   : 2024/07/12 9:03
+     */
+    @Value("${wx.token}")
+    private String wechatUrlToken;
+
+    @Value("${wx.user_public_openid}")
+    private String userPublicOpenid;
 
+    @Value("${wx.user_info_public_unionid}")
+    private String userInfoPublicUnionid;
 }

+ 10 - 0
src/main/java/com/dk/oauth/controller/oauth/AccessTokenController.java

@@ -223,4 +223,14 @@ public class AccessTokenController {
         return authAccessTokenService.getOwnerCount(userLogin.getUserId());
     }
 
+    /**
+     * @desc   : 查询微信登录用户信息
+     * @author : 宋扬
+     * @date   : 2024/3/1 16:01
+     */
+    @PostMapping({"/oauth/wx/get_user/{userId}"})
+    public ResponseResultVO getUser(@PathVariable String userId) {
+        return authAccessTokenService.getUser(userId);
+    }
+
 }

+ 9 - 1
src/main/java/com/dk/oauth/entity/PublicOpenUnion.java

@@ -1,9 +1,12 @@
 package com.dk.oauth.entity;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.dk.common.infrastructure.annotaiton.ExportTitle;
+import com.dk.common.infrastructure.handler.UuidTypeHandler;
 import com.dk.common.model.pojo.PageInfo;
 import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
@@ -26,7 +29,12 @@ import java.io.Serializable;
 @TableName(value = "t_wx_public_open_union", autoResultMap = true, schema = "dkic_a")
 @ApiModel(value = "微信公众号关联", description = "表名:t_wx_public_open_union")
 public class PublicOpenUnion extends PageInfo<PublicOpenUnion> implements Serializable {
-    private Long id;
+    /**
+     * ID
+     */
+    @ApiModelProperty(value = "ID")
+    @TableField(typeHandler = UuidTypeHandler.class)
+    private String id;
     private String publicUnionId; // 公众号unionid
     private String publicOpenId;// 公众号openid
 }

+ 32 - 0
src/main/java/com/dk/oauth/scheduled/UserInfoPublicOpenIdScheduled.java

@@ -0,0 +1,32 @@
+package com.dk.oauth.scheduled;
+
+import com.dk.oauth.service.IPublicOpenUnionService;
+import com.dk.oauth.service.impl.PublicOpenUnionService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+/**
+ * @desc   : 定时任务
+ * @author : 姜永辉
+ * @date   : 2023/10/24 13:31
+ */
+@Slf4j
+@Component
+public class UserInfoPublicOpenIdScheduled {
+    @Autowired
+    IPublicOpenUnionService service;
+
+    // 添加定时任务 每天凌晨 1 点执行一次
+    @Scheduled(cron = "0 0 1 * * ?")
+    public void updateUserPublicOpenIds(){
+        log.info("准备获取微信公账号的openid");
+        try {
+            service.updateUserPublicOpenIds();
+        }catch (Exception e){
+            log.info("获取微信公账号的openid异常" + e.getMessage());
+        }
+        log.info("准备获取微信公账号的openid完成");
+    }
+}

+ 2 - 0
src/main/java/com/dk/oauth/service/IAuthAccessTokenService.java

@@ -49,4 +49,6 @@ public interface IAuthAccessTokenService extends IService<AuthAccessToken> {
     ResponseResultVO logout(UserWxLogin userWxLogin);
 
     ResponseResultVO<?> getOwnerCount(String uuid);
+
+    ResponseResultVO<?> getUser(String userId);
 }

+ 7 - 0
src/main/java/com/dk/oauth/service/IPublicOpenUnionService.java

@@ -0,0 +1,7 @@
+package com.dk.oauth.service;
+
+import com.dk.common.response.ResponseResultVO;
+
+public interface IPublicOpenUnionService {
+    public ResponseResultVO updateUserPublicOpenIds() ;
+}

+ 12 - 0
src/main/java/com/dk/oauth/service/impl/AuthAccessTokenServiceImpl.java

@@ -785,5 +785,17 @@ public class AuthAccessTokenServiceImpl extends ServiceImpl<AuthAccessTokenMappe
         return ResponseResultUtil.success(count);
     }
 
+    /**
+     * @desc : 查询微信登录用户信息
+     * @author : jyh
+     * @date : 2024-02-20 17:00
+     */
+    public ResponseResultVO<?> getUser(String userId) {
+        UserLogin userLogin = userMapper.getByWxid(userId);
+        Map<String, Object> map = new HashMap<>();
+        map.put("publicOpenId",userLogin.getPublicOpenId());
+        return ResponseResultUtil.success(map);
+    }
+
 
 }

+ 107 - 0
src/main/java/com/dk/oauth/service/impl/PublicOpenUnionService.java

@@ -0,0 +1,107 @@
+package com.dk.oauth.service.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.dk.common.exception.BaseBusinessException;
+import com.dk.common.mapper.BaseMapper;
+import com.dk.common.response.ResponseCodeEnum;
+import com.dk.common.response.ResponseResultUtil;
+import com.dk.common.response.ResponseResultVO;
+import com.dk.common.util.HttpUtils;
+import com.dk.oauth.config.WxConfig;
+import com.dk.oauth.entity.PublicOpenUnion;
+import com.dk.oauth.mapper.PublicOpenUnionMapper;
+import com.dk.oauth.service.IPublicOpenUnionService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.HashMap;
+
+/**
+ * 公众号unionid和公众号openid API接口
+ *
+ * @author admin
+ * @since 2023-07-01 09:41:05
+ */
+@Transactional
+@Slf4j
+@Service("publicOpenUnionService")
+public class PublicOpenUnionService extends ServiceImpl<PublicOpenUnionMapper,
+            PublicOpenUnion> implements IPublicOpenUnionService {
+    @Autowired
+    private PublicOpenUnionMapper publicOpenUnionMapper;
+
+    @Autowired
+    private WxConfig config;
+
+    /**
+     * @author : jyh
+     * @date : 2023-10-11 16:23
+     * @desc : 调用中控服务获取公众号的token
+     */
+    public String getWxPublicAccountToken() {
+        ResponseResultVO<JSONObject> res = HttpUtils.post(config.getWechatUrlToken(),
+                new HashMap() {{
+                    put("appId", config.getWxPublicAccountAppId());
+                    put("appSecret", config.getWxPublicAccountAppSecret());
+                }});
+        if (res.getCode() == 200 && JSON.parseObject(JSON.toJSONString(res.getData())).get("code").toString().equals("200")) {
+            return JSON.parseObject(JSON.toJSONString(res.getData())).get("data").toString();
+        } else {
+            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), res.getMessage());
+        }
+    }
+
+
+    /**
+     * @desc : 准备获取微信公账号的openid unionid
+     * @author : 姜永辉
+     * @date : 2023/10/24 13:34
+     */
+    public ResponseResultVO updateUserPublicOpenIds() {
+        // 3.获取token 231011
+        String token = this.getWxPublicAccountToken();
+        log.info("当前微信公账号-----token:{}", token);
+        String nextOpenid = "";
+        ResponseResultVO<JSONObject> res = HttpUtils.post(config.getUserPublicOpenid() + token + "&next_openid="
+                + nextOpenid, null);
+        if (res.getCode() == ResponseCodeEnum.SUCCESS.getCode()  ) {
+            log.info("获取用户列表成功:{}", res);
+            JSONArray array = res.getData().getJSONObject("data").getJSONArray("openid");
+            if (array != null && array.size() > 0) {
+                String openid = "";
+                for (int i = 0; i < array.size(); i++) {
+                    openid = array.get(i).toString();
+                    nextOpenid = openid;
+                    // 查询数据库里是否有openid
+                    PublicOpenUnion pou = publicOpenUnionMapper.selectByOpenId(openid);
+                    if (pou != null) {
+                        continue;
+                    }
+                    // 获取用户基本信息(包括UnionID机制)
+                    ResponseResultVO<JSONObject> resUnionid = HttpUtils.post(config.getUserInfoPublicUnionid() +
+                            token + "&openid=" + openid + "&lang=zh_CN", null);
+                    if (resUnionid.getCode() == ResponseCodeEnum.SUCCESS.getCode() ) {
+                        log.info("获取用户基本信息(包括UnionID机制):{}", resUnionid);
+                        String unionid = resUnionid.getData().getString("unionid");
+                        PublicOpenUnion publicOpenUnion = new PublicOpenUnion();
+                        publicOpenUnion.setPublicOpenId(openid);
+                        publicOpenUnion.setPublicUnionId(unionid);
+                        publicOpenUnionMapper.insert(publicOpenUnion);
+                    } else {
+                        log.error("获取用户基本信息(包括UnionID机制)失败:{}", resUnionid.getData().toJSONString());
+                    }
+                }
+            }
+
+            return ResponseResultUtil.success();
+        } else {
+            log.error("获取用户列表失败:{}", res.getData().toJSONString());
+            return ResponseResultUtil.error(res.getData().toJSONString());
+        }
+    }
+}

+ 2 - 2
src/main/resources/mapper/UserMapper.xml

@@ -80,8 +80,8 @@
                current_cp,
                joined_cps,
                flg_valid,
-               txu.union_id,
-               txu.public_open_id
+               union_id,
+               public_open_id
         FROM dkic_a.t_wx_user
         where user_id = #{id}::uuid
     </select>