Commit 2ca77442 authored by shiyu's avatar shiyu

公众号消息测试

parent f4af76be
package com.wwdz.ch.core.entity;
import com.xxdxxs.entity.Entity;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class ConsignSaleSubscribeMsg implements Entity, AbstractSubscribeMsg{
private String templetId;
/**
* 寄售单号
*/
private String saleId;
/**
* 商品名称
* 会拼接上预估的价格
*/
private String itemName;
/**
* 寄售单号
* 估价的时间
*/
private String time;
@Override
public Map<String, Object> getTempletParam() {
Map<String, Object> map = new HashMap<>();
map.put("character_string1.DATA", new HashMap(){{put("value", saleId);}});
map.put("thing2.DATA", new HashMap(){{put("value", itemName);}});
map.put("time4.DATA", new HashMap(){{put("value", time);}});
return map;
}
@Override
public String getTempletId() {
return templetId;
}
}
...@@ -21,6 +21,7 @@ dts: ...@@ -21,6 +21,7 @@ dts:
officialaccount-token: Uu6GHYOiP1xJ6GaBMyXfnITufAGJTe3Q officialaccount-token: Uu6GHYOiP1xJ6GaBMyXfnITufAGJTe3Q
officialaccount-key: noeN1amYRvzrVmE2GnC5VnsIfgPEmv6bhX54wJOARlM officialaccount-key: noeN1amYRvzrVmE2GnC5VnsIfgPEmv6bhX54wJOARlM
get-officialaccount-accesstoken-url: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s get-officialaccount-accesstoken-url: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s
officialaccount-templetId: WJkhzyY0_7AYqh19_ujK9yZhj4HohF8ETJvG5NYA9A8
#腾讯IM配置 #腾讯IM配置
im: im:
......
...@@ -20,6 +20,7 @@ dts: ...@@ -20,6 +20,7 @@ dts:
officialaccount-token: Uu6GHYOiP1xJ6GaBMyXfnITufAGJTe3Q officialaccount-token: Uu6GHYOiP1xJ6GaBMyXfnITufAGJTe3Q
officialaccount-key: noeN1amYRvzrVmE2GnC5VnsIfgPEmv6bhX54wJOARlM officialaccount-key: noeN1amYRvzrVmE2GnC5VnsIfgPEmv6bhX54wJOARlM
get-officialaccount-accesstoken-url: https://api.weixin.qq.com/cgi-bin/stable_token get-officialaccount-accesstoken-url: https://api.weixin.qq.com/cgi-bin/stable_token
officialaccount-templetId: WJkhzyY0_7AYqh19_ujK9yZhj4HohF8ETJvG5NYA9A8
#腾讯IM配置 #腾讯IM配置
......
...@@ -41,6 +41,9 @@ public class OfficialAccountApi { ...@@ -41,6 +41,9 @@ public class OfficialAccountApi {
@Value("${dts.wx.officialaccount-secret}") @Value("${dts.wx.officialaccount-secret}")
private String OFFICIAL_ACCOUNT_APPSECRET; private String OFFICIAL_ACCOUNT_APPSECRET;
@Value("${dts.wx.officialaccount-templetId}")
private String OFFICIALACCOUNT_TEMPLETID;
@Value("${dts.wx.app-id}") @Value("${dts.wx.app-id}")
private String APPLET_APPID; private String APPLET_APPID;
...@@ -59,6 +62,7 @@ public class OfficialAccountApi { ...@@ -59,6 +62,7 @@ public class OfficialAccountApi {
String accessTokenUrl = String.format(GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL, OFFICIAL_ACCOUNT_APPID, OFFICIAL_ACCOUNT_APPSECRET); String accessTokenUrl = String.format(GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL, OFFICIAL_ACCOUNT_APPID, OFFICIAL_ACCOUNT_APPSECRET);
String res = OkHttpUtil.builder().url(accessTokenUrl) String res = OkHttpUtil.builder().url(accessTokenUrl)
.get().sync(); .get().sync();
logger.info("测试环境获取token返回结果: {}", res);
String token = JsonUtils.getValueByPath(res, "access_token"); String token = JsonUtils.getValueByPath(res, "access_token");
return token; return token;
} }
...@@ -147,7 +151,7 @@ public class OfficialAccountApi { ...@@ -147,7 +151,7 @@ public class OfficialAccountApi {
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url); OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("touser", openId); map.put("touser", openId);
map.put("template_id", abstractSubscribeMsg.getTempletId()); map.put("template_id", OFFICIALACCOUNT_TEMPLETID);
Map<String, Object> miniprogramMap = new HashMap<>(); Map<String, Object> miniprogramMap = new HashMap<>();
String link = wxLoginManager.getLink(MSG_URL); String link = wxLoginManager.getLink(MSG_URL);
miniprogramMap.put("appid", APPLET_APPID); miniprogramMap.put("appid", APPLET_APPID);
......
...@@ -2,6 +2,7 @@ package com.wwdz.ch.wx.api; ...@@ -2,6 +2,7 @@ package com.wwdz.ch.wx.api;
import com.wwdz.ch.core.consts.OfficalAccountEnum; import com.wwdz.ch.core.consts.OfficalAccountEnum;
import com.wwdz.ch.core.entity.ConsignSaleSubscribeMsg;
import com.wwdz.ch.core.type.Result; import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.db.dao.UserDao; import com.wwdz.ch.db.dao.UserDao;
import com.wwdz.ch.db.domain.OfficialAccountSubscribeRecord; import com.wwdz.ch.db.domain.OfficialAccountSubscribeRecord;
...@@ -9,8 +10,10 @@ import com.wwdz.ch.db.domain.User; ...@@ -9,8 +10,10 @@ import com.wwdz.ch.db.domain.User;
import com.wwdz.ch.wx.api.aes.AesException; import com.wwdz.ch.wx.api.aes.AesException;
import com.wwdz.ch.wx.api.aes.WXBizMsgCrypt; import com.wwdz.ch.wx.api.aes.WXBizMsgCrypt;
import com.wwdz.ch.wx.service.OfficialAccountSubscribeRecordService; import com.wwdz.ch.wx.service.OfficialAccountSubscribeRecordService;
import com.xxdxxs.utils.DateUtils;
import com.xxdxxs.utils.StringUtils; import com.xxdxxs.utils.StringUtils;
import com.xxdxxs.utils.XmlUtils; import com.xxdxxs.utils.XmlUtils;
import io.swagger.annotations.ApiOperation;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -177,4 +180,25 @@ public class OfficialAccountCallbackController { ...@@ -177,4 +180,25 @@ public class OfficialAccountCallbackController {
return token; return token;
} }
@ApiOperation(value = "测试发送消息")
@GetMapping("/testForSendMsg")
public Result testForSendMsg() {
try {
logger.info("【请求开始】测试发送公众号消息");
String openId = "oNPTN6QaVliZfKB53OzZRPBZr_r8";
String saleId = "CSO2312151346268093384";
String itemName = "测试商品" + " 图文估价为200-300元之间";
ConsignSaleSubscribeMsg consignSaleSubscribeMsg = new ConsignSaleSubscribeMsg();
consignSaleSubscribeMsg.setSaleId(saleId);
consignSaleSubscribeMsg.setItemName(itemName);
consignSaleSubscribeMsg.setTime(DateUtils.toString(new Date()));
officialAccountApi.sendModelMsg(openId, consignSaleSubscribeMsg);
return Result.success();
} catch (Exception e) {
logger.error("测试发送公众号消息 error:{}" ,e);
}
return Result.failed();
}
} }
...@@ -3,20 +3,22 @@ package com.wwdz.ch.wx.web; ...@@ -3,20 +3,22 @@ package com.wwdz.ch.wx.web;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.wwdz.ch.core.consts.ResultCode; import com.wwdz.ch.core.consts.ResultCode;
import com.wwdz.ch.core.entity.ConsignSaleSubscribeMsg;
import com.wwdz.ch.core.type.Result; import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.db.domain.User; import com.wwdz.ch.db.domain.User;
import com.wwdz.ch.db.dto.request.UserRequestDto; import com.wwdz.ch.db.dto.request.UserRequestDto;
import com.wwdz.ch.wx.api.OfficialAccountApi;
import com.wwdz.ch.wx.service.OfficialAccountSubscribeRecordService; import com.wwdz.ch.wx.service.OfficialAccountSubscribeRecordService;
import com.wwdz.ch.wx.service.UserService; import com.wwdz.ch.wx.service.UserService;
import com.xxdxxs.utils.DateUtils;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.Date;
import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/wx/officialAccount") @RequestMapping("/wx/officialAccount")
...@@ -30,6 +32,9 @@ public class OfficialAccountController { ...@@ -30,6 +32,9 @@ public class OfficialAccountController {
@Autowired @Autowired
UserService userService; UserService userService;
@Autowired
OfficialAccountApi officialAccountApi;
@ApiOperation(value = "判断用户是否关注过公众号") @ApiOperation(value = "判断用户是否关注过公众号")
@PostMapping("/isSubscribed") @PostMapping("/isSubscribed")
public Result isSubscribed(@RequestBody UserRequestDto dto) { public Result isSubscribed(@RequestBody UserRequestDto dto) {
...@@ -47,4 +52,6 @@ public class OfficialAccountController { ...@@ -47,4 +52,6 @@ public class OfficialAccountController {
} }
return officialAccountSubscribeRecordService.isSubscribed(unionid); return officialAccountSubscribeRecordService.isSubscribed(unionid);
} }
} }
package com.wwdz.ch.wx.api; package com.wwdz.ch.wx.api;
import com.wwdz.ch.core.entity.ConsignSaleSubscribeMsg;
import com.wwdz.ch.core.entity.OfficialAccountFollowSubscribeMsg; import com.wwdz.ch.core.entity.OfficialAccountFollowSubscribeMsg;
import com.xxdxxs.utils.DateUtils;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
@SpringBootTest @SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
public class OfficialAccountApiTest { public class OfficialAccountApiTest {
...@@ -29,10 +33,12 @@ public class OfficialAccountApiTest { ...@@ -29,10 +33,12 @@ public class OfficialAccountApiTest {
@Test @Test
public void sendSubscribeMsg() { public void sendSubscribeMsg() {
String openId = "oNPTN6QaVliZfKB53OzZRPBZr_r8"; String openId = "oNPTN6QaVliZfKB53OzZRPBZr_r8";
OfficialAccountFollowSubscribeMsg officialAccountFollowSubscribeMsg = new OfficialAccountFollowSubscribeMsg(); String saleId = "CSO2312151346268093384";
officialAccountFollowSubscribeMsg.setFollowUser("测试用户"); String itemName = "测试商品" + " 图文估价为200-300元之间";
officialAccountFollowSubscribeMsg.setTip("测试帖子"); ConsignSaleSubscribeMsg consignSaleSubscribeMsg = new ConsignSaleSubscribeMsg();
officialAccountFollowSubscribeMsg.setFollowTime("2023-10-16 06:00:11"); consignSaleSubscribeMsg.setSaleId(saleId);
officialAccountApi.sendModelMsg(openId, officialAccountFollowSubscribeMsg); consignSaleSubscribeMsg.setItemName(itemName);
consignSaleSubscribeMsg.setTime(DateUtils.toString(new Date()));
officialAccountApi.sendModelMsg(openId, consignSaleSubscribeMsg);
} }
} }
\ 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