|
|
@@ -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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|