|
@@ -2,20 +2,25 @@ package com.miaxis.wx.service.impl;
|
|
|
|
|
|
import cn.hutool.json.XML;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.miaxis.common.constant.Constants;
|
|
|
+import com.miaxis.common.core.domain.entity.SysDictData;
|
|
|
import com.miaxis.common.core.domain.entity.UserInfo;
|
|
|
import com.miaxis.common.utils.StringUtils;
|
|
|
import com.miaxis.common.utils.wx.MessageUtil;
|
|
|
import com.miaxis.feign.dto.WxMessageCusom;
|
|
|
import com.miaxis.feign.service.IWxSendService;
|
|
|
+import com.miaxis.system.service.ISysDictTypeService;
|
|
|
import com.miaxis.system.service.ISysUserService;
|
|
|
import com.miaxis.user.service.IUserInfoService;
|
|
|
+import com.miaxis.wx.domain.WxForeverCode;
|
|
|
import com.miaxis.wx.service.IWxGzhService;
|
|
|
import com.miaxis.wx.service.IWxMessageEvenService;
|
|
|
import com.miaxis.wx.service.WxService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -50,6 +55,8 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
|
|
|
|
private final WxService wxService;
|
|
|
|
|
|
+ private final ISysDictTypeService dictTypeService;
|
|
|
+
|
|
|
|
|
|
@Value("${zzgzh.appid}")
|
|
|
private String appid;
|
|
@@ -120,10 +127,38 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
|
String msgType = jsonObjectData.getStr("MsgType");
|
|
|
//获取用户信息
|
|
|
String userInfoResult = wxSendService.userInfo(this.getGzhToken(), fromUserName, "zh_CN");
|
|
|
- JSONObject jsonObj = JSONObject.parseObject(userInfoResult);
|
|
|
+ JSONObject userInfoJson = JSONObject.parseObject(userInfoResult);
|
|
|
|
|
|
// 普通消息
|
|
|
if (MessageUtil.MESSAGE_TEXT.equals(msgType)) { // 文本消息
|
|
|
+ List<SysDictData> configs = dictTypeService.selectDictDataByType("partner_config");
|
|
|
+ String parentUnionId = null ;
|
|
|
+ for (SysDictData sysDictData:configs){
|
|
|
+ String dictValue = sysDictData.getDictValue();
|
|
|
+ String[] values = dictValue.split(",");
|
|
|
+ String password = values[0];
|
|
|
+ if (msgType.equals(password)){
|
|
|
+ parentUnionId = sysDictData.getDictLabel();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //推荐码生成
|
|
|
+ if (parentUnionId!= null){
|
|
|
+ String unionId = userInfoJson.getString("unionid");
|
|
|
+ UserInfo user = userService.getStudentByUnionid(unionId);
|
|
|
+ //如果推荐码为空,则生成一个
|
|
|
+ if (user.getRecommendCode()== null){
|
|
|
+ String code = null ;
|
|
|
+ do {
|
|
|
+ code = getRandomString(6);
|
|
|
+ }while (isRepeat(code));
|
|
|
+ user.setRecommendCode(code);
|
|
|
+ user.setParentUnionId(parentUnionId);
|
|
|
+ userInfoService.updateById(user);
|
|
|
+ }
|
|
|
+ return MessageUtil.initText(fromUserName, toUserName, "您的推荐码是:"+user.getRecommendCode());
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
} else if (MessageUtil.MESSAGE_IMAGE.equals(msgType)) { // 图片消息
|
|
|
log.info("2.2...");
|
|
@@ -135,20 +170,24 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
|
String event = jsonObjectData.getStr("Event");
|
|
|
if (MessageUtil.MESSAGE_SUBSCRIBE.equals(event)) { // 关注事件 或 扫描二维码关注事件
|
|
|
log.info("3.1...");
|
|
|
- String res = wxSendService.userInfo( this.getGzhToken(), fromUserName, "zh_CN");
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
- String unionId = jsonObject.getString("unionid");
|
|
|
+
|
|
|
+ String unionId = userInfoJson.getString("unionid");
|
|
|
+ //如果是新用户,则保存用户
|
|
|
UserInfo user = userService.getStudentByUnionid(unionId);
|
|
|
- UserInfo userInfo = new UserInfo();
|
|
|
- userInfo.setGzhOpenid(fromUserName);
|
|
|
- userInfo.setUnionId(unionId);
|
|
|
+ if (user == null){
|
|
|
+ UserInfo userInfo = new UserInfo();
|
|
|
+ userInfo.setGzhOpenid(fromUserName);
|
|
|
+ userInfo.setUnionId(unionId);
|
|
|
+ userInfoService.save(userInfo);
|
|
|
+ }
|
|
|
+
|
|
|
//存在Ticket为扫码关注
|
|
|
if (org.apache.commons.lang3.StringUtils.isNotEmpty(jsonObjectData.getStr("Ticket"))){
|
|
|
log.info("3.1.2..");
|
|
|
result = MessageUtil.initText(fromUserName,toUserName," ");
|
|
|
|
|
|
}else {
|
|
|
- result = MessageUtil.initText(fromUserName, toUserName, "欢迎关注金牌车教,您的学车好伙伴!");
|
|
|
+ result = MessageUtil.initText(fromUserName, toUserName, "欢迎关注速达财仝,您的学车好伙伴!");
|
|
|
}
|
|
|
|
|
|
|
|
@@ -178,11 +217,25 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ private boolean isRepeat(String code) {
|
|
|
+ QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
|
|
|
+ wrapper.eq("recommend_code",code);
|
|
|
+ UserInfo one = userInfoService.getOne(wrapper);
|
|
|
+ return one!= null;
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ //length用户要求产生字符串的长度
|
|
|
+ public static String getRandomString(int length){
|
|
|
+ String str="abcdefghjkmnpqrstuvwxyz123456789";
|
|
|
+ Random random=new Random();
|
|
|
+ StringBuffer sb=new StringBuffer();
|
|
|
+ int bound = str.length();
|
|
|
+ for(int i=0;i<length;i++){
|
|
|
+ int number=random.nextInt(bound);
|
|
|
+ sb.append(str.charAt(number));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
|
|
|
private void sendSchool(StringBuffer buffer, List<Map> schools,String fromUserName) {
|
|
|
if (schools.size() <= 10){
|