小程序openid转公众号openid逻辑

This commit is contained in:
kuang.yife 2023-10-09 00:03:50 +08:00
parent 1dd6bc0065
commit 3a5cd3b210
7 changed files with 53 additions and 9 deletions

View File

@ -281,7 +281,7 @@ public class OrderController extends BaseController {
// 预约时间
params.put("time4", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", orderMaster.getExpectTimeStart()));
// 消息推送
WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getOpenId(), WxMsgEnum.TEXT, params);
WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getWxOpenId(), WxMsgEnum.TEXT, params);
} catch (Exception e) {
// 暂时不做任何操作
logger.error(e.getMessage(), e);
@ -453,7 +453,7 @@ public class OrderController extends BaseController {
paramsNew.put("thing14", acceptWorker.getName());
// 预约时间
paramsNew.put("time4", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", om.getExpectTimeStart()));
WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), acceptWorker.getOpenId(), WxMsgEnum.TEXT, paramsNew);
WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), acceptWorker.getWxOpenId(), WxMsgEnum.TEXT, paramsNew);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
@ -557,7 +557,7 @@ public class OrderController extends BaseController {
// 预约时间
paramsNew.put("time4", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", orderMaster.getExpectTimeStart()));
// 消息推送
WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getOpenId(), WxMsgEnum.TEXT, paramsNew);
WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getWxOpenId(), WxMsgEnum.TEXT, paramsNew);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -1050,7 +1050,7 @@ public class OrderDetailController extends BaseController {
// 服务区域
paramsNew.put("thing4", om.getAddress());
// 消息推送
WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getOpenId(), WxMsgEnum.TEXT, paramsNew);
WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getWxOpenId(), WxMsgEnum.TEXT, paramsNew);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -1,5 +1,7 @@
package com.ghy.web.controller.tool;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ghy.common.config.WxConfig;
import com.ghy.common.core.controller.BaseController;
@ -29,7 +31,9 @@ import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author clunt
@ -196,9 +200,36 @@ public class WxController extends BaseController {
if(worker != null && StringUtils.isEmpty(worker.getWxOpenId())){
// 公众号token
String wxToken = WechatMsgUtils.getToken();
String wxUserOpenidList = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/user/get?access_token="+wxToken+"&next_openid=NEXT_OPENID");
String wxUserOpenidList = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/user/get?access_token="+wxToken+"&next_openid=");
logger.info("公众号获取的用户列表集合:{}", wxUserOpenidList);
JSONObject wxOpenidJson = JSONObject.parseObject(wxUserOpenidList);
JSONObject openIdListJson = wxOpenidJson.getJSONObject("data");
List<String> openidList = openIdListJson.getObject("openid", ArrayList.class);
JSONArray openidJsonArray = new JSONArray();
openidList.forEach(model->{
JSONObject openidJson = new JSONObject();
openidJson.put("openid", model);
openidJson.put("lang", "zh_CN");
openidJsonArray.add(openidJson);
});
JSONObject jsonParam = new JSONObject();
jsonParam.put("user_list", openidJsonArray);
String unionUrl = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="+WechatMsgUtils.getToken();;
logger.info("调用获取用户信息,请求url:{}, 请求body:{}", unionUrl, jsonParam.toJSONString());
String getUnionResult = HttpUtil.post(unionUrl, jsonParam.toJSONString());
logger.info("获取公众号union列表:{}", getUnionResult);
JSONObject unionJson = JSONObject.parseObject(getUnionResult);
JSONArray unionJsonArray = unionJson.getJSONArray("user_info_list");
for (int index = 0 ; index<unionJsonArray.size(); index ++){
JSONObject json = unionJsonArray.getJSONObject(index);
if(json.getString("unionid").equals(unionId)){
Worker updateWorker = new Worker();
updateWorker.setWorkerId(worker.getWorkerId());
updateWorker.setWxOpenId(json.getString("openid"));
logger.info("关联后的worker信息:{}", worker);
workerService.updateWorker(updateWorker);
}
}
}
}

View File

@ -22,6 +22,11 @@
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>

View File

@ -65,7 +65,7 @@ public class WechatMsgUtils {
String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
//整体参数map
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("touser", "oLvgx6KxLrIy7i_O0pd15wUl2tAI");
paramMap.put("touser", userOpenId);
paramMap.put("page", "index");
switch (mesType) {
case TEXT:

View File

@ -9,7 +9,7 @@
<result property="phone" column="phone"/>
<result property="openId" column="open_id"/>
<result property="unionId" column="union_id"/>
<result property="wxUnionId" column="wx_open_id"/>
<result property="wxOpenId" column="wx_open_id"/>
<result property="alipayAccount" column="alipay_account"/>
<result property="alipayName" column="alipay_name"/>
<result property="password" column="password"/>
@ -31,7 +31,7 @@
<sql id="selectWorker">
SELECT
w.worker_id, w.name, w.account, w.phone, w.password, w.open_id, w.dept_id, w.status, w.worker_logo_url,
w.worker_id, w.name, w.account, w.phone, w.password, w.open_id, w.wx_open_id, w.dept_id, w.status, w.worker_logo_url,
w.leader_team_rate, w.leader_team_money, w.create_by, w.create_time, w.update_by, w.update_time,
w.remark, w.type, w.store_status, w.alipay_account, w.alipay_name,
sd.dept_name, sdc.banner_url
@ -42,7 +42,7 @@
<sql id="selectBasic">
SELECT
w.worker_id, w.name, w.account, w.phone, w.password, w.open_id, w.dept_id, w.status, w.worker_logo_url,
w.worker_id, w.name, w.account, w.phone, w.password, w.open_id, w.wx_open_id, w.dept_id, w.status, w.worker_logo_url,
w.leader_team_rate, w.leader_team_money, w.create_by, w.create_time, w.update_by, w.update_time,
w.remark, w.type, w.store_status, w.alipay_account, w.alipay_name
FROM worker w
@ -152,6 +152,7 @@
<set>
<if test="type != null"> type = #{type},</if>
<if test="status != null"> status = #{status},</if>
<if test="wxOpenId != null"> wx_open_id = #{wxOpenId},</if>
<if test="storeStatus != null"> store_status = #{storeStatus},</if>
<if test="account != null and account != ''"> account = #{account},</if>
<if test="phone != null and phone != ''"> phone = #{phone},</if>

View File

@ -40,6 +40,7 @@
<Adapay.version>1.2.10</Adapay.version>
<commons.codec.version>1.10</commons.codec.version>
<httpcomponents.version>4.5.13</httpcomponents.version>
<hutool.version>5.8.22</hutool.version>
<!-- 打包时跳过单元测试 -->
<skipTests>true</skipTests>
</properties>
@ -66,6 +67,12 @@
<version>${qiniu.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>