|
@@ -8,7 +8,6 @@ import cn.hutool.extra.qrcode.QrCodeUtil;
|
|
|
import cn.hutool.extra.qrcode.QrConfig;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.miaxis.common.constant.Constants;
|
|
|
import com.miaxis.common.utils.http.HttpUtils;
|
|
|
import com.miaxis.feign.dto.WxGzhMini;
|
|
|
import com.miaxis.feign.dto.WxMessageCusom;
|
|
@@ -17,6 +16,8 @@ import com.miaxis.feign.dto.WxQrTicket;
|
|
|
import com.miaxis.feign.service.IWxSendService;
|
|
|
import com.miaxis.spread.domain.WxSpreadRelation;
|
|
|
import com.miaxis.spread.service.IWxSpreadRelationService;
|
|
|
+import com.miaxis.wx.domain.WxForeverCode;
|
|
|
+import com.miaxis.wx.service.IWxForeverCodeService;
|
|
|
import com.miaxis.wx.service.IWxMessageEvenService;
|
|
|
import com.miaxis.wx.service.IWxTicketService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
@@ -30,7 +31,6 @@ import java.io.File;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author wwl
|
|
@@ -42,6 +42,8 @@ import java.util.concurrent.TimeUnit;
|
|
|
@RequiredArgsConstructor
|
|
|
public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
|
|
|
|
|
|
+ private final static Integer expireTime = 2592000;//30天
|
|
|
+
|
|
|
private final RedisTemplate redisTemplate;
|
|
|
|
|
|
private final IWxSendService wxSendService;
|
|
@@ -50,6 +52,8 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
|
|
|
|
|
|
private final IWxSpreadRelationService wxSpreadRelationService;
|
|
|
|
|
|
+ private final IWxForeverCodeService wxForeverCodeService;
|
|
|
+
|
|
|
@Value("${file.ticketPath}")
|
|
|
private String ticketPath;
|
|
|
|
|
@@ -69,20 +73,19 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
|
|
|
/**
|
|
|
* 扫码关注事件
|
|
|
* @param fromUserName 发送方帐号(一个OpenID)
|
|
|
- * @param ticket 二维码的ticket
|
|
|
+ * @param eventKey
|
|
|
* @param token 凭证
|
|
|
* @param userName 微信名称
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public String scanSubscribeEvent(String fromUserName, String ticket,String token,String userName) {
|
|
|
+ public String scanSubscribeEvent(String fromUserName, String eventKey,String token,String userName) {
|
|
|
|
|
|
String message = "";
|
|
|
//判断是否存在父、子级推广关系
|
|
|
List<WxSpreadRelation> spreadRelations = wxSpreadRelationService.list(new QueryWrapper<WxSpreadRelation>().eq("openid", fromUserName));
|
|
|
List<WxSpreadRelation> spreads = wxSpreadRelationService.list(new QueryWrapper<WxSpreadRelation>().eq("parent_openid", fromUserName));
|
|
|
- String openid = (String) redisTemplate.opsForValue().get(Constants.GZH_TICKET_KEY + ticket);
|
|
|
-
|
|
|
+ String openid = eventKey.substring(8);
|
|
|
if (spreadRelations.isEmpty() && spreads.isEmpty()){
|
|
|
//保存推广关系表
|
|
|
WxSpreadRelation wxSpreadRelation = new WxSpreadRelation();
|
|
@@ -107,32 +110,47 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
|
|
|
* click获取推广二维码事件
|
|
|
* @param fromUserName 发送方帐号(一个OpenID)
|
|
|
* @param token 凭证
|
|
|
+ * @param wxForeverCode
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public String gainTicketEvent(String fromUserName,String token) {
|
|
|
+ public String gainTicketEvent(String fromUserName, String token, boolean isForever, WxForeverCode wxForeverCode) {
|
|
|
|
|
|
//发送客服信息
|
|
|
messageCustomSend(fromUserName,token);
|
|
|
|
|
|
// 根据fromUserName查询缓存 判断用户推广码是否过期
|
|
|
- if (redisTemplate.hasKey(Constants.GZH_MATERIAL_KEY + fromUserName)){
|
|
|
- String mediaId = (String) redisTemplate.opsForValue().get(Constants.GZH_MATERIAL_KEY + fromUserName);
|
|
|
- return mediaId;
|
|
|
+// if (redisTemplate.hasKey(Constants.GZH_MATERIAL_KEY + fromUserName)){
|
|
|
+// String mediaId = (String) redisTemplate.opsForValue().get(Constants.GZH_MATERIAL_KEY + fromUserName);
|
|
|
+// return mediaId;
|
|
|
+// }
|
|
|
+ String url = null ;
|
|
|
+ if (wxForeverCode != null && wxForeverCode.getUrl() != null){
|
|
|
+ url = wxForeverCode.getUrl();
|
|
|
+ }else{
|
|
|
+ //生成带参数临时二维码ticket
|
|
|
+
|
|
|
+ WxQrTicket wxQrTicket = null ;
|
|
|
+ if (isForever){
|
|
|
+ wxQrTicket = createForeverTicket(fromUserName);
|
|
|
+ }else{
|
|
|
+ wxQrTicket = createShortTimeTicket(fromUserName);
|
|
|
+ }
|
|
|
+
|
|
|
+ String wxTicketResult = wxSendService.generateTicket(token, wxQrTicket);
|
|
|
+ JSONObject jsonStr = JSONObject.parseObject(wxTicketResult);
|
|
|
+ log.info("---------ticket-------" + jsonStr);
|
|
|
+ if (isForever){
|
|
|
+ //更新口令数据对象
|
|
|
+ wxForeverCode.setOpenid(fromUserName);
|
|
|
+ // wxForeverCode.setNickName(jsonObj.getString("nickname"));
|
|
|
+ wxForeverCode.setUrl(jsonStr.getString("url"));
|
|
|
+ wxForeverCodeService.updateById(wxForeverCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ url = jsonStr.getString("url");
|
|
|
}
|
|
|
|
|
|
- //生成带参数二维码ticket(永久)
|
|
|
- WxQrTicket wxQrTicket = new WxQrTicket();
|
|
|
- wxQrTicket.setExpire_seconds(259200);//3天
|
|
|
- wxQrTicket.setAction_name("QR_SCENE");//临时
|
|
|
- JSONObject jsonObject1 = new JSONObject();
|
|
|
- JSONObject jsonObject2 = new JSONObject();
|
|
|
- jsonObject1.put("scene_id","123");
|
|
|
- jsonObject2.put("scene",jsonObject1);
|
|
|
- wxQrTicket.setAction_info(jsonObject2);
|
|
|
- String wxTicketResult = wxSendService.generateTicket(token, wxQrTicket);
|
|
|
- JSONObject jsonStr = JSONObject.parseObject(wxTicketResult);
|
|
|
- log.info("---------ticket-------" + jsonStr);
|
|
|
|
|
|
//获取用户信息
|
|
|
String userInfoResult = wxSendService.userInfo(token, fromUserName, "zh_CN");
|
|
@@ -141,7 +159,7 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
|
|
|
|
|
|
//根据ticket生成二维码图片存储
|
|
|
File ticketFile = QrCodeUtil.generate(
|
|
|
- jsonStr.getString("url"), //二维码内容
|
|
|
+ url, //二维码内容
|
|
|
QrConfig.create().setImg(ticketPath + "logo.jpg"), //附带logo
|
|
|
FileUtil.file(ticketPath + fromUserName + ".jpg")//写出到的文件
|
|
|
);
|
|
@@ -166,14 +184,17 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
|
|
|
);
|
|
|
|
|
|
DateTime date = DateUtil.date();
|
|
|
- Date newDate = DateUtil.offsetDay(date, 3);
|
|
|
+ Date newDate = DateUtil.offsetDay(date, 30);
|
|
|
String dateStr = DateUtil.format(newDate, "yyyy-MM-dd");
|
|
|
+ if (isForever){
|
|
|
+ dateStr = "永久";
|
|
|
+ }
|
|
|
log.info(dateStr);
|
|
|
|
|
|
ImgUtil.pressText(
|
|
|
FileUtil.file(ticketPath+fromUserName+"_text1.jpg"),
|
|
|
FileUtil.file(ticketPath+fromUserName+"_text2.jpg"),
|
|
|
- "二维码有效期至:"+dateStr, Color.black, //文字
|
|
|
+ "二维码有效期:"+dateStr, Color.black, //文字
|
|
|
new Font("黑体", Font.BOLD, 28), //字体
|
|
|
0, //x坐标修正值。 默认在中间,偏移量相对于中间偏移
|
|
|
-80, //y坐标修正值。 默认在中间,偏移量相对于中间偏移
|
|
@@ -191,8 +212,8 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
|
|
|
log.info("------result:" + resultJSON);
|
|
|
|
|
|
//缓存用户推广码相关信息
|
|
|
- redisTemplate.opsForValue().set(Constants.GZH_TICKET_KEY+jsonStr.getString("ticket"),fromUserName,259200, TimeUnit.SECONDS);
|
|
|
- redisTemplate.opsForValue().set(Constants.GZH_MATERIAL_KEY+fromUserName,resultJSON.getString("media_id"),259200, TimeUnit.SECONDS);
|
|
|
+// redisTemplate.opsForValue().set(Constants.GZH_TICKET_KEY+jsonStr.getString("ticket"),fromUserName,259200, TimeUnit.SECONDS);
|
|
|
+// redisTemplate.opsForValue().set(Constants.GZH_MATERIAL_KEY+fromUserName,resultJSON.getString("media_id"),259200, TimeUnit.SECONDS);
|
|
|
|
|
|
//删除服务器文件
|
|
|
File imgFile = new File(ticketPath + fromUserName+"_img.jpg");
|
|
@@ -227,6 +248,29 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ private WxQrTicket createForeverTicket(String fromUserName) {
|
|
|
+ WxQrTicket wxQrTicket = new WxQrTicket();
|
|
|
+ wxQrTicket.setAction_name("QR_LIMIT_STR_SCENE");//临时字符串参数
|
|
|
+ JSONObject jsonObject1 = new JSONObject();
|
|
|
+ JSONObject jsonObject2 = new JSONObject();
|
|
|
+ jsonObject1.put("scene_str",fromUserName);
|
|
|
+ jsonObject2.put("scene",jsonObject1);
|
|
|
+ wxQrTicket.setAction_info(jsonObject2);
|
|
|
+ return wxQrTicket;
|
|
|
+ }
|
|
|
+
|
|
|
+ private WxQrTicket createShortTimeTicket(String fromUserName) {
|
|
|
+ WxQrTicket wxQrTicket = new WxQrTicket();
|
|
|
+ wxQrTicket.setExpire_seconds(expireTime);
|
|
|
+ wxQrTicket.setAction_name("QR_STR_SCENE");//临时字符串参数
|
|
|
+ JSONObject jsonObject1 = new JSONObject();
|
|
|
+ JSONObject jsonObject2 = new JSONObject();
|
|
|
+ jsonObject1.put("scene_str",fromUserName);
|
|
|
+ jsonObject2.put("scene",jsonObject1);
|
|
|
+ wxQrTicket.setAction_info(jsonObject2);
|
|
|
+ return wxQrTicket;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 发送客服消息
|