From de7b91a0eb1df6e402f93ea51d50b486f7a82984 Mon Sep 17 00:00:00 2001 From: kira <164851225@qq.com> Date: Mon, 23 Jul 2018 17:35:05 +0800 Subject: [PATCH] add wx msg --- .../system/SysCustomerServiceMapper.java | 3 + .../manage/openim/core/OpenimClient.java | 25 +++++ .../core/impl/CustomerServiceServiceImpl.java | 92 ++++++++++++------- .../system/SysCustomerServiceMapper.xml | 5 + src/main/ui/service_customer.html | 4 +- 5 files changed, 93 insertions(+), 36 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/system/SysCustomerServiceMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/system/SysCustomerServiceMapper.java index db617a130..8e119f1ed 100644 --- a/src/main/java/au/com/royalpay/payment/manage/mappers/system/SysCustomerServiceMapper.java +++ b/src/main/java/au/com/royalpay/payment/manage/mappers/system/SysCustomerServiceMapper.java @@ -27,5 +27,8 @@ public interface SysCustomerServiceMapper { void update(JSONObject record); void onoff(@Param("managerId") String managerId, @Param("status") int status); + + @AutoSql(type = SqlType.SELECT) + List findAllWithDetail(); } diff --git a/src/main/java/au/com/royalpay/payment/manage/openim/core/OpenimClient.java b/src/main/java/au/com/royalpay/payment/manage/openim/core/OpenimClient.java index 24a6de1bc..488a36e6d 100644 --- a/src/main/java/au/com/royalpay/payment/manage/openim/core/OpenimClient.java +++ b/src/main/java/au/com/royalpay/payment/manage/openim/core/OpenimClient.java @@ -21,6 +21,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; /** @@ -110,4 +111,28 @@ public class OpenimClient { throw new ServerErrorException("System error"); } } + + public static void main(String[] args) { + String appkey = "24980715"; + String secret = "60b1ba680a6f27ffc701f10b6d5be00e"; + String url = "https://eco.taobao.com/router/rest"; + + TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret); + OpenimUsersAddRequest req = new OpenimUsersAddRequest(); + List list2 = new ArrayList(); + Userinfos obj3 = new Userinfos(); + list2.add(obj3); + obj3.setNick("king"); + obj3.setIconUrl("https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1948403834,955109320&fm=85&s=0D06E5134AD145F30C8D6D680300303A"); + obj3.setUserid("kira1"); + obj3.setPassword("xxxxxx"); + req.setUserinfos(list2); + OpenimUsersAddResponse rsp = null; + try { + rsp = client.execute(req); + } catch (ApiException e) { + e.printStackTrace(); + } + System.out.println(rsp.getBody()); + } } diff --git a/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/CustomerServiceServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/CustomerServiceServiceImpl.java index 5f0efa0cc..3ba1ce652 100644 --- a/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/CustomerServiceServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/openim/core/impl/CustomerServiceServiceImpl.java @@ -7,6 +7,7 @@ import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.manage.openim.beans.OpenimUserVO; import au.com.royalpay.payment.manage.openim.core.CustomerServiceService; import au.com.royalpay.payment.manage.openim.core.OpenimApi; +import au.com.royalpay.payment.tools.connections.mpsupport.MpWechatApi; import au.com.royalpay.payment.tools.connections.mpsupport.MpWechatApiProvider; import au.com.royalpay.payment.tools.connections.mpsupport.beans.TemplateMessage; import au.com.royalpay.payment.tools.exceptions.BadRequestException; @@ -16,15 +17,17 @@ import com.alibaba.fastjson.JSONObject; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.BoundListOperations; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; +import java.util.Date; import java.util.List; -import java.util.Map; import javax.annotation.Resource; @@ -47,10 +50,14 @@ public class CustomerServiceServiceImpl implements CustomerServiceService { private SysCustomerServiceMapper sysCustomerServiceMapper; @Resource private MpWechatApiProvider mpWechatApiProvider; - private final Map unReadMap = new HashMap<>(); + @Resource + private StringRedisTemplate stringRedisTemplate; + private final String REDIS_UNREADMSG_PREFIX = "REDIS_UNREADMSG_PREFIX"; + private final String REDIS_UNREADMSG_SEND_PREFIX = "REDIS_UNREADMSG_SEND_PREFIX"; @Value("${im.openim.appkey}") private String appkey; + @Override public JSONObject checkAndSave(JSONObject account) { JSONObject client = null; @@ -67,7 +74,7 @@ public class CustomerServiceServiceImpl implements CustomerServiceService { account = managerMapper.findDetail(account.getString("manager_id")); isPartner = false; } - String uid = account.getString("username") + (isPartner ? "_"+ client.getString("client_moniker"): ""); + String uid = account.getString("username") + (isPartner ? "_" + client.getString("client_moniker") : ""); JSONObject saveRecord = new JSONObject(); saveRecord.put("nick", account.getString("display_name")); saveRecord.put("password", DigestUtils.md5Hex("uid")); @@ -78,7 +85,7 @@ public class CustomerServiceServiceImpl implements CustomerServiceService { saveRecord.put("user_id", uid); } openimApi.addUser(saveRecord); - saveRecord.put("appkey",appkey); + saveRecord.put("appkey", appkey); return saveRecord; } @@ -132,57 +139,74 @@ public class CustomerServiceServiceImpl implements CustomerServiceService { @Override public void onoff(JSONObject manager, boolean onoff) { JSONObject record = new JSONObject(); - record.put("manager_id",manager.getString("manager_id")); - record.put("onoff",onoff); + record.put("manager_id", manager.getString("manager_id")); + record.put("onoff", onoff); udpateOrSave(record); } @Override public void addUnreadMsg(JSONObject params) { - String key = params.getString("uid"); - if(unReadMap.containsKey(key)) { - unReadMap.put(key, unReadMap.get(key) + 1); - }else { - unReadMap.put(key,1L); + String uid = params.getString("uid"); + String tuid = params.getString("tuid"); + if (StringUtils.isEmpty(uid) || StringUtils.isEmpty(tuid)) { + return; + } + BoundListOperations ops = stringRedisTemplate.boundListOps("REDIS_UNREADMSG_PREFIX" + tuid); + for (int i = 0; i < ops.size(); i++) { + String jStr = ops.index(i); + if (jStr.contains(uid)) { + JSONObject record = JSONObject.parseObject(jStr); + record.put("unreadMsg", record.getIntValue("unreadMsg") + 1); + ops.set(i, record.toJSONString()); + return; + } } + params.put("unreadMsg", 1); + ops.rightPush(params.toJSONString()); } @Override public void sendUnreadWxMsg() { - unReadMap.entrySet().parallelStream().forEach(p->{ - JSONObject account = clientAccountMapper.findByUsername(p.getKey()); - if(StringUtils.isNotEmpty(account.getString("wx_openid"))){ -// TemplateMessage msg = initAccountMsg(tradeInfo, client.getString("short_name"), time, String.valueOf(counts), openId, paymentApi.getTemplateId("transaction-daily")); -// MpWechatApi paymentApi = mpWechatApiProvider.getNewPaymentApi(); -// paymentApi.sendTemplateMessage(msg); + List servants = sysCustomerServiceMapper.findAllWithDetail(); + MpWechatApi paymentApi = mpWechatApiProvider.getNewPaymentApi(); + servants.parallelStream().forEach(p -> { + p.getString("user_id"); + BoundListOperations ops = stringRedisTemplate.boundListOps("REDIS_UNREADMSG_PREFIX" + p.getString("user_id")); + if (ops.size() < 1) { + return; } - p.getKey(); + int clients = 0; + int unreadMsg = 0; + for (int i = 0; i < ops.size(); i++) { + clients++; + unreadMsg += JSONObject.parseObject(ops.index(i)).getIntValue("unreadMsg"); + } + TemplateMessage msg = initUnreadMsg(unreadMsg, clients, p.getString("wx_openid"), paymentApi.getTemplateId("commission")); + paymentApi.sendTemplateMessage(msg); + stringRedisTemplate.delete("REDIS_UNREADMSG_PREFIX" + p.getString("user_id")); }); } - private TemplateMessage initAccountMsg(JSONObject tradeInfo, String short_name, String time, String clientCounts, String openId, String templateId) { - + private TemplateMessage initUnreadMsg(int unreadMsg, int clients, String openId, String templateId) { TemplateMessage msg = new TemplateMessage(openId, templateId, null); - String detail = "收款总额:" + tradeInfo.getBigDecimal("total") + "$, 收款笔数:" + tradeInfo.getString("orders") + ", 付款人数:" + tradeInfo.getString("customers"); - msg.put("first", "每日交易汇总数据(" + tradeInfo.getString("trade_date") + ")", "#000000"); - msg.put("keyword1", short_name, "#000000"); - msg.put("keyword2", time, "#000000"); - msg.put("keyword3", clientCounts + "家", "#000000"); - msg.put("keyword4", detail, "#0000ff"); - msg.put("remark", "详情请查看商户后台、APP或皇家支付小程序", "#000000"); - msg.setMiniprogram("wx54445a7c56432d57", "/pages/welcome/welcome"); + msg.put("first", "你好,你有" + unreadMsg + "个未读消息", "#000000"); + msg.put("keyword1", "商户", "#000000"); + msg.put("keyword2", "问题咨询", "#000000"); + msg.put("keyword3", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"), "#000000"); + msg.put("keyword4", "你有" + clients + "个商户咨询待处理", "#0000ff"); + msg.put("remark", "", "#000000"); return msg; } - private void udpateOrSave(JSONObject servant){ + private void udpateOrSave(JSONObject servant) { JSONObject record = sysCustomerServiceMapper.findByManagerId(servant.getString("manager_id")); - if(record==null){ + if (record == null) { JSONObject manager = managerMapper.findDetail(servant.getString("manager_id")); - servant.put("manager_id",manager.getString("manager_id")); - servant.put("user_id",manager.getString("username")); + servant.put("manager_id", manager.getString("manager_id")); + servant.put("user_id", manager.getString("username")); sysCustomerServiceMapper.save(servant); - }else { + } else { sysCustomerServiceMapper.update(servant); } diff --git a/src/main/resources/au/com/royalpay/payment/manage/mappers/system/SysCustomerServiceMapper.xml b/src/main/resources/au/com/royalpay/payment/manage/mappers/system/SysCustomerServiceMapper.xml index f92f98ddd..ff15768e0 100644 --- a/src/main/resources/au/com/royalpay/payment/manage/mappers/system/SysCustomerServiceMapper.xml +++ b/src/main/resources/au/com/royalpay/payment/manage/mappers/system/SysCustomerServiceMapper.xml @@ -11,4 +11,9 @@ set status = #{status} where manager_id = #{managerId} + \ No newline at end of file diff --git a/src/main/ui/service_customer.html b/src/main/ui/service_customer.html index a59dc8a1f..8ab67e6c6 100644 --- a/src/main/ui/service_customer.html +++ b/src/main/ui/service_customer.html @@ -12,8 +12,8 @@ container: document.getElementById('J_demo'), width: 700, height: 500, - uid: 'imuser123', - appkey: 24960261, + uid: 'kira1', + appkey: 24980715, credential: 'xxxxxx', touid: 'dalong', logo: 'http://img.alicdn.com/tps/i3/TB12LD9IFXXXXb3XpXXSyFWJXXX-82-82.png',