|
@ -0,0 +1,200 @@
|
|
|
package com.yihu.wlyy.wechat.util;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.wlyy.entity.dict.SystemDict;
|
|
|
import com.yihu.wlyy.entity.wechat.WechatTag;
|
|
|
import com.yihu.wlyy.repository.dict.SystemDictDao;
|
|
|
import com.yihu.wlyy.repository.wechat.WechatTagDao;
|
|
|
import com.yihu.wlyy.util.HttpUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2017/8/4.
|
|
|
* 微信标签功能
|
|
|
*/
|
|
|
@Component
|
|
|
public class WeiXinTagUtil {
|
|
|
@Autowired
|
|
|
private WechatTagDao wechatTagDao;
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(WeiXinTagUtil.class);
|
|
|
@Autowired
|
|
|
private WeiXinAccessTokenUtils weiXinAccessTokenUtils;
|
|
|
@Autowired
|
|
|
private HttpUtil httpUtil;
|
|
|
@Value("${wechat.appSecret}")
|
|
|
private String appSecret;
|
|
|
|
|
|
// 创建标签
|
|
|
private static String create_tag = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=";
|
|
|
//删除标签
|
|
|
private static String delete_tag = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=";
|
|
|
//查询标签
|
|
|
private static String query_tag = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=";
|
|
|
//给用户打标签
|
|
|
private static String add_user_tag = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=";
|
|
|
//给用户删除标签
|
|
|
private static String delete_user_tag = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=";
|
|
|
//获取用户标签
|
|
|
private static String query_user_tag = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=";
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 根据openID给用户打标签,并且同步更新用户表
|
|
|
*
|
|
|
* @param openIds
|
|
|
* @param tagId
|
|
|
* @return
|
|
|
*/
|
|
|
public String addTagWithOpenid(List<String> openIds, Integer tagId) {
|
|
|
try {
|
|
|
net.sf.json.JSONObject params = new net.sf.json.JSONObject();
|
|
|
|
|
|
params.put("openid_list", net.sf.json.JSONArray.fromObject(openIds));
|
|
|
params.put("tagid", tagId);
|
|
|
String result = httpUtil.sendPost(add_user_tag + weiXinAccessTokenUtils.getAccessToken(), params.toString());
|
|
|
|
|
|
net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(result);
|
|
|
|
|
|
if (resultJson.containsKey("errcode")) {
|
|
|
Integer status = resultJson.getInt("errcode");
|
|
|
if (status != 0) {
|
|
|
throw new Exception("创建失败," + result);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据openID给用户取消标签,并且同步更新用户表
|
|
|
*
|
|
|
* @param openIds
|
|
|
* @param tagId
|
|
|
* @return
|
|
|
*/
|
|
|
public String deleteTagWithOpenid(List<String> openIds, Integer tagId) {
|
|
|
try {
|
|
|
net.sf.json.JSONObject params = new net.sf.json.JSONObject();
|
|
|
|
|
|
params.put("openid_list", net.sf.json.JSONArray.fromObject(openIds));
|
|
|
params.put("tagid", tagId);
|
|
|
String result = httpUtil.sendPost(delete_user_tag + weiXinAccessTokenUtils.getAccessToken(), params.toString());
|
|
|
|
|
|
net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(result);
|
|
|
|
|
|
if (resultJson.containsKey("errcode")) {
|
|
|
Integer status = resultJson.getInt("errcode");
|
|
|
if (status != 0) {
|
|
|
throw new Exception("创建失败," + result);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取用户身上的标签列表
|
|
|
*
|
|
|
* @param openId
|
|
|
* @return
|
|
|
*/
|
|
|
public String queryTagWithOpenid(String openId) {
|
|
|
try {
|
|
|
net.sf.json.JSONObject params = new net.sf.json.JSONObject();
|
|
|
|
|
|
params.put("openid", openId);
|
|
|
String result = httpUtil.sendGet(query_user_tag + weiXinAccessTokenUtils.getAccessToken(), params.toString());
|
|
|
|
|
|
net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(result);
|
|
|
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 新增标签
|
|
|
*
|
|
|
* @param name
|
|
|
* @return
|
|
|
*/
|
|
|
public String createTag(String name) throws Exception {
|
|
|
JSONObject params = new JSONObject();
|
|
|
|
|
|
JSONObject tagName = new JSONObject();
|
|
|
tagName.put("name", name);
|
|
|
params.put("tag", tagName);
|
|
|
|
|
|
// 发送模板消息
|
|
|
String result = httpUtil.sendPost(create_tag + weiXinAccessTokenUtils.getAccessToken(), params.toJSONString());
|
|
|
logger.info("create tag resutl:" + result);
|
|
|
net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(result);
|
|
|
if (resultJson.containsKey("errcode")) {
|
|
|
Integer status = resultJson.getInt("errcode");
|
|
|
if (status != 0) {
|
|
|
throw new Exception("创建失败," + result);
|
|
|
}
|
|
|
}
|
|
|
Integer tag = resultJson.getJSONObject("tag").getInt("id");
|
|
|
WechatTag newWechatTag = new WechatTag();
|
|
|
newWechatTag.setCreateTime(new Date());
|
|
|
newWechatTag.setTagName(name);
|
|
|
newWechatTag.setDel(1);
|
|
|
newWechatTag.setTagId(tag);
|
|
|
wechatTagDao.save(newWechatTag);
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除标签
|
|
|
*
|
|
|
* @param tagId
|
|
|
* @return
|
|
|
*/
|
|
|
public String deleteTag(Integer tagId) throws Exception {
|
|
|
JSONObject params = new JSONObject();
|
|
|
|
|
|
JSONObject tagName = new JSONObject();
|
|
|
tagName.put("id", tagId);
|
|
|
params.put("tag", tagName);
|
|
|
|
|
|
// 发送模板消息
|
|
|
String result = httpUtil.sendPost(delete_tag + weiXinAccessTokenUtils.getAccessToken(), params.toJSONString());
|
|
|
logger.info("delete tag resutl:" + result);
|
|
|
net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(result);
|
|
|
if (resultJson.containsKey("errcode")) {
|
|
|
Integer status = resultJson.getInt("errcode");
|
|
|
if (status != 0) {
|
|
|
throw new Exception("创建失败," + result);
|
|
|
}
|
|
|
}
|
|
|
//删除我们自己表的标签表数据
|
|
|
wechatTagDao.deleteTag(tagId);
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
public String queryTags() {
|
|
|
String result = httpUtil.sendGet(query_tag + weiXinAccessTokenUtils.getAccessToken() + "&secret=" + appSecret, "");
|
|
|
return result;
|
|
|
}
|
|
|
}
|