Commit 2e44f766 authored by shiyu's avatar shiyu

公众号订阅

parent f3320628
......@@ -21,6 +21,11 @@ public class OfficialAccountSubscribeRecord implements Entity {
*/
private String openid;
/**
* unionid
*/
private String unionid;
/**
* 平台类型(1微信,2支付宝,3其他)
*/
......@@ -56,6 +61,7 @@ public class OfficialAccountSubscribeRecord implements Entity {
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", openid=").append(openid);
sb.append(", unionid=").append(unionid);
sb.append(", platformType=").append(platformType);
sb.append(", phone=").append(phone);
sb.append(", createTime=").append(createTime);
......@@ -80,6 +86,7 @@ public class OfficialAccountSubscribeRecord implements Entity {
OfficialAccountSubscribeRecord other = (OfficialAccountSubscribeRecord) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getOpenid() == null ? other.getOpenid() == null : this.getOpenid().equals(other.getOpenid()))
&& (this.getUnionid() == null ? other.getUnionid() == null : this.getUnionid().equals(other.getUnionid()))
&& (this.getPlatformType() == null ? other.getPlatformType() == null : this.getPlatformType().equals(other.getPlatformType()))
&& (this.getPhone() == null ? other.getPhone() == null : this.getPhone().equals(other.getPhone()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
......@@ -93,6 +100,7 @@ public class OfficialAccountSubscribeRecord implements Entity {
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getOpenid() == null) ? 0 : getOpenid().hashCode());
result = prime * result + ((getUnionid() == null) ? 0 : getUnionid().hashCode());
result = prime * result + ((getPlatformType() == null) ? 0 : getPlatformType().hashCode());
result = prime * result + ((getPhone() == null) ? 0 : getPhone().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
......@@ -111,6 +119,7 @@ public class OfficialAccountSubscribeRecord implements Entity {
public enum Column {
id("id", "id", "INTEGER", false),
openid("openid", "openid", "VARCHAR", false),
unionid("unionid", "unionid", "VARCHAR", false),
platformType("platform_type", "platformType", "INTEGER", false),
phone("phone", "phone", "VARCHAR", false),
createTime("create_time", "createTime", "TIMESTAMP", false),
......
......@@ -422,6 +422,148 @@ public class OfficialAccountSubscribeRecordExample {
return (Criteria) this;
}
public Criteria andUnionidIsNull() {
addCriterion("unionid is null");
return (Criteria) this;
}
public Criteria andUnionidIsNotNull() {
addCriterion("unionid is not null");
return (Criteria) this;
}
public Criteria andUnionidEqualTo(String value) {
addCriterion("unionid =", value, "unionid");
return (Criteria) this;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table official_account_subscribe_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
public Criteria andUnionidEqualToColumn(OfficialAccountSubscribeRecord.Column column) {
addCriterion(new StringBuilder("unionid = ").append(column.getEscapedColumnName()).toString());
return (Criteria) this;
}
public Criteria andUnionidNotEqualTo(String value) {
addCriterion("unionid <>", value, "unionid");
return (Criteria) this;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table official_account_subscribe_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
public Criteria andUnionidNotEqualToColumn(OfficialAccountSubscribeRecord.Column column) {
addCriterion(new StringBuilder("unionid <> ").append(column.getEscapedColumnName()).toString());
return (Criteria) this;
}
public Criteria andUnionidGreaterThan(String value) {
addCriterion("unionid >", value, "unionid");
return (Criteria) this;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table official_account_subscribe_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
public Criteria andUnionidGreaterThanColumn(OfficialAccountSubscribeRecord.Column column) {
addCriterion(new StringBuilder("unionid > ").append(column.getEscapedColumnName()).toString());
return (Criteria) this;
}
public Criteria andUnionidGreaterThanOrEqualTo(String value) {
addCriterion("unionid >=", value, "unionid");
return (Criteria) this;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table official_account_subscribe_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
public Criteria andUnionidGreaterThanOrEqualToColumn(OfficialAccountSubscribeRecord.Column column) {
addCriterion(new StringBuilder("unionid >= ").append(column.getEscapedColumnName()).toString());
return (Criteria) this;
}
public Criteria andUnionidLessThan(String value) {
addCriterion("unionid <", value, "unionid");
return (Criteria) this;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table official_account_subscribe_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
public Criteria andUnionidLessThanColumn(OfficialAccountSubscribeRecord.Column column) {
addCriterion(new StringBuilder("unionid < ").append(column.getEscapedColumnName()).toString());
return (Criteria) this;
}
public Criteria andUnionidLessThanOrEqualTo(String value) {
addCriterion("unionid <=", value, "unionid");
return (Criteria) this;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table official_account_subscribe_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
public Criteria andUnionidLessThanOrEqualToColumn(OfficialAccountSubscribeRecord.Column column) {
addCriterion(new StringBuilder("unionid <= ").append(column.getEscapedColumnName()).toString());
return (Criteria) this;
}
public Criteria andUnionidLike(String value) {
addCriterion("unionid like", value, "unionid");
return (Criteria) this;
}
public Criteria andUnionidNotLike(String value) {
addCriterion("unionid not like", value, "unionid");
return (Criteria) this;
}
public Criteria andUnionidIn(List<String> values) {
addCriterion("unionid in", values, "unionid");
return (Criteria) this;
}
public Criteria andUnionidNotIn(List<String> values) {
addCriterion("unionid not in", values, "unionid");
return (Criteria) this;
}
public Criteria andUnionidBetween(String value1, String value2) {
addCriterion("unionid between", value1, value2, "unionid");
return (Criteria) this;
}
public Criteria andUnionidNotBetween(String value1, String value2) {
addCriterion("unionid not between", value1, value2, "unionid");
return (Criteria) this;
}
public Criteria andPlatformTypeIsNull() {
addCriterion("platform_type is null");
return (Criteria) this;
......
......@@ -95,6 +95,7 @@ public class User implements Serializable {
/**
* 微信登录openid
* 存的是unionid
*/
private String weixinOpenid;
......
......@@ -30,18 +30,18 @@ public class OfficialAccountSubscribeRecordDaoImpl implements OfficialAccountSub
}
@Override
public boolean isExisted(String openid) {
public boolean isExisted(String unionid) {
OfficialAccountSubscribeRecordExample example = new OfficialAccountSubscribeRecordExample();
OfficialAccountSubscribeRecordExample.Criteria criteria = example.createCriteria();
criteria.andOpenidEqualTo(openid);
criteria.andUnionidEqualTo(unionid);
return officialAccountSubscribeRecordMapper.countByExample(example) > 0;
}
@Override
public boolean isSubscribed(String openid) {
public boolean isSubscribed(String unionid) {
OfficialAccountSubscribeRecordExample example = new OfficialAccountSubscribeRecordExample();
OfficialAccountSubscribeRecordExample.Criteria criteria = example.createCriteria();
criteria.andOpenidEqualTo(openid);
criteria.andUnionidEqualTo(unionid);
criteria.andEnabledEqualTo(1);
return officialAccountSubscribeRecordMapper.countByExample(example) > 0;
}
......
......@@ -7,7 +7,6 @@ import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface OfficialAccountSubscribeRecordMapper {
long countByExample(OfficialAccountSubscribeRecordExample example);
......
......@@ -4,6 +4,7 @@
<resultMap id="BaseResultMap" type="com.wwdz.ch.db.domain.OfficialAccountSubscribeRecord">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="openid" jdbcType="VARCHAR" property="openid" />
<result column="unionid" jdbcType="VARCHAR" property="unionid" />
<result column="platform_type" jdbcType="INTEGER" property="platformType" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
......@@ -69,7 +70,7 @@
</where>
</sql>
<sql id="Base_Column_List">
id, openid, platform_type, phone, create_time, update_time, enabled
id, openid, unionid, platform_type, phone, create_time, update_time, enabled
</sql>
<select id="selectByExample" parameterType="com.wwdz.ch.db.domain.OfficialAccountSubscribeRecordExample" resultMap="BaseResultMap">
select
......@@ -98,13 +99,13 @@
distinct
</if>
<choose>
<when test="selective != null and selective.length > 0">
<when test="selective != null and selective.length &gt; 0">
<foreach collection="selective" item="column" separator=",">
${column.escapedColumnName}
</foreach>
</when>
<otherwise>
id, openid, platform_type, phone, create_time, update_time, enabled
id, openid, unionid, platform_type, phone, create_time, update_time, enabled
</otherwise>
</choose>
from official_account_subscribe_record
......@@ -129,13 +130,13 @@
-->
select
<choose>
<when test="selective != null and selective.length > 0">
<when test="selective != null and selective.length &gt; 0">
<foreach collection="selective" item="column" separator=",">
${column.escapedColumnName}
</foreach>
</when>
<otherwise>
id, openid, platform_type, phone, create_time, update_time, enabled
id, openid, unionid, platform_type, phone, create_time, update_time, enabled
</otherwise>
</choose>
from official_account_subscribe_record
......@@ -155,12 +156,12 @@
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into official_account_subscribe_record (openid, platform_type, phone,
create_time, update_time, enabled
)
values (#{openid,jdbcType=VARCHAR}, #{platformType,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{enabled,jdbcType=INTEGER}
)
insert into official_account_subscribe_record (openid, unionid, platform_type,
phone, create_time, update_time,
enabled)
values (#{openid,jdbcType=VARCHAR}, #{unionid,jdbcType=VARCHAR}, #{platformType,jdbcType=INTEGER},
#{phone,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP},
#{enabled,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.wwdz.ch.db.domain.OfficialAccountSubscribeRecord">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
......@@ -171,6 +172,9 @@
<if test="openid != null">
openid,
</if>
<if test="unionid != null">
unionid,
</if>
<if test="platformType != null">
platform_type,
</if>
......@@ -191,6 +195,9 @@
<if test="openid != null">
#{openid,jdbcType=VARCHAR},
</if>
<if test="unionid != null">
#{unionid,jdbcType=VARCHAR},
</if>
<if test="platformType != null">
#{platformType,jdbcType=INTEGER},
</if>
......@@ -223,6 +230,9 @@
<if test="record.openid != null">
openid = #{record.openid,jdbcType=VARCHAR},
</if>
<if test="record.unionid != null">
unionid = #{record.unionid,jdbcType=VARCHAR},
</if>
<if test="record.platformType != null">
platform_type = #{record.platformType,jdbcType=INTEGER},
</if>
......@@ -247,6 +257,7 @@
update official_account_subscribe_record
set id = #{record.id,jdbcType=INTEGER},
openid = #{record.openid,jdbcType=VARCHAR},
unionid = #{record.unionid,jdbcType=VARCHAR},
platform_type = #{record.platformType,jdbcType=INTEGER},
phone = #{record.phone,jdbcType=VARCHAR},
create_time = #{record.createTime,jdbcType=TIMESTAMP},
......@@ -262,6 +273,9 @@
<if test="openid != null">
openid = #{openid,jdbcType=VARCHAR},
</if>
<if test="unionid != null">
unionid = #{unionid,jdbcType=VARCHAR},
</if>
<if test="platformType != null">
platform_type = #{platformType,jdbcType=INTEGER},
</if>
......@@ -283,6 +297,7 @@
<update id="updateByPrimaryKey" parameterType="com.wwdz.ch.db.domain.OfficialAccountSubscribeRecord">
update official_account_subscribe_record
set openid = #{openid,jdbcType=VARCHAR},
unionid = #{unionid,jdbcType=VARCHAR},
platform_type = #{platformType,jdbcType=INTEGER},
phone = #{phone,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
......@@ -317,13 +332,13 @@
select
'true' as QUERYID,
<choose>
<when test="selective != null and selective.length > 0">
<when test="selective != null and selective.length &gt; 0">
<foreach collection="selective" item="column" separator=",">
${column.escapedColumnName}
</foreach>
</when>
<otherwise>
id, openid, platform_type, phone, create_time, update_time, enabled
id, openid, unionid, platform_type, phone, create_time, update_time, enabled
</otherwise>
</choose>
from official_account_subscribe_record
......@@ -335,4 +350,5 @@
</if>
limit 1
</select>
</mapper>
\ No newline at end of file
......@@ -28,6 +28,8 @@ public class OfficialAccountApi {
private final static String GET_ACCESSTOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
private final static String GET_USER_BASE_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN";
@Autowired
RedisUtils redisUtils;
......@@ -45,7 +47,7 @@ public class OfficialAccountApi {
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
okHttpUtil.get();
String responseStr = okHttpUtil.async();
logger.info("appid : {}, 获取token response :{}", responseStr);
logger.info("appid : {}, 获取token response :{}", APPID, responseStr);
if (!responseStr.contains("access_token")) {
String errorCode = JsonUtils.getValueByPath(responseStr, "errcode");
String errmsg = JsonUtils.getValueByPath(responseStr, "errmsg");
......@@ -58,4 +60,33 @@ public class OfficialAccountApi {
return token;
}
/**
* 返回unionid
* @param openid
* @return
*/
public String getUnionidByopenid(String openid) {
String token = getAccessToken();
String url = GET_USER_BASE_INFO_URL + "&access_token=" + token + "&openid=" + openid;
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
okHttpUtil.get();
String responseStr = okHttpUtil.async();
logger.info("openid : {}, 获取用户基本信息 response :{}", openid, responseStr);
if (!responseStr.contains("unionid")) {
String errorCode = JsonUtils.getValueByPath(responseStr, "errcode");
String errmsg = JsonUtils.getValueByPath(responseStr, "errmsg");
//token失效
if (("40001").equals(errorCode)) {
refreshAccessToken();
}
logger.error("openid : {}, 获取用户基本信息, errorCode : {}, errmsg : {} ", openid, errorCode, errmsg);
return null;
}
String unionid = JsonUtils.getValueByPath(responseStr, "unionid");
logger.info("openid : {}, 对应的 unionid:{}", openid, unionid);
return unionid;
}
}
......@@ -87,10 +87,13 @@ public class OfficialAccountCallbackController {
if (SUBSCRIBE_EVENT.equals(event) || UNSUBSCRIBE_EVENT.equals(event)) {
String userOpenId = contentMap.get("FromUserName");
String createTime = contentMap.get("CreateTime");
Date date = new Date(Long.valueOf(createTime));
Date date = new Date(Long.valueOf(createTime) * 1000L);
logger.info("解析xml得到 openId:{}, event :{}, time:{}", userOpenId, event, date);
//新增或更新订阅记录
OfficialAccountSubscribeRecord officialAccountSubscribeRecord = new OfficialAccountSubscribeRecord();
//根据openid获取unionid
String unionid = officialAccountApi.getUnionidByopenid(userOpenId);
officialAccountSubscribeRecord.setUnionid(unionid);
officialAccountSubscribeRecord.setOpenid(userOpenId);
if (SUBSCRIBE_EVENT.equals(event)) {
officialAccountSubscribeRecord.setEnabled(1);
......@@ -98,13 +101,13 @@ public class OfficialAccountCallbackController {
officialAccountSubscribeRecord.setEnabled(0);
}
//查询有无历史订阅记录
boolean isExisted = officialAccountSubscribeRecordService.isExisted(userOpenId);
boolean isExisted = officialAccountSubscribeRecordService.isExisted(unionid);
if (isExisted) {
officialAccountSubscribeRecord.setUpdateTime(date);
officialAccountSubscribeRecordService.update(officialAccountSubscribeRecord);
} else {
//没有历史订阅记录,先根据openid查询手机号
User user = userDao.queryByOid(userOpenId);
//没有历史订阅记录,先根据unionid查询手机号
User user = userDao.queryByOid(unionid);
if (user != null) {
officialAccountSubscribeRecord.setPhone(user.getMobile());
}
......@@ -113,6 +116,7 @@ public class OfficialAccountCallbackController {
officialAccountSubscribeRecord.setPlatformType(OfficalAccountEnum.PlatformTypeEnum.WECHAT.getCode());
officialAccountSubscribeRecordService.insert(officialAccountSubscribeRecord);
}
logger.info("=============== 更新订阅记录成功 ==============");
}
} catch (Exception e) {
logger.error("微信公众号通知回调接口 error : {}", e);
......@@ -152,7 +156,7 @@ public class OfficialAccountCallbackController {
if (SUBSCRIBE_EVENT.equals(event) || UNSUBSCRIBE_EVENT.equals(event)) {
String userOpenId = contentMap.get("FromUserName");
String createTime = contentMap.get("CreateTime");
Date date = new Date(Long.valueOf(createTime));
Date date = new Date(Long.valueOf(createTime) * 1000L);
System.out.println(userOpenId + ">>" + event + ">>" + date);
}
}
......
......@@ -44,7 +44,7 @@ public class WxLoginManager {
/**
* 获取微信openid
*
* 为了公众号发送消息,这里实际获取的是unionid,同一用户小程序和公众号unionid相同
* @param loginCode
* @return
*/
......@@ -53,7 +53,9 @@ public class WxLoginManager {
String openId = "";
try {
WxMaJscode2SessionResult result = this.wxMaService.getUserService().getSessionInfo(loginCode);
openId = result.getOpenid();
//实际获取的是unionid,为了和公众号订阅用户关联
openId = result.getUnionid();
logger.info("获取WxMaJscode2SessionResult :{}", result);
logger.info("loginCode:{},获取openId成功 openId :{}", loginCode, openId);
} catch (Exception e) {
logger.error("loginCode:{}, 获取微信登录用户的openId失败:{}", loginCode, e);
......
......@@ -19,4 +19,11 @@ public class OfficialAccountApiTest {
public void refreshAccessToken() {
officialAccountApi.getAccessToken();
}
@Test
public void getUserBaseInfo() {
String openid = "oNPTN6QaVliZfKB53OzZRPBZr_r8";
officialAccountApi.getUnionidByopenid(openid);
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment