微信小程序开发购物达人

微信订阅消息体Java实现

2020-04-12  本文已影响0人  小孩孜

话不多说,直接上代码:

SubscribeMsgData.java订阅消息体对象,字符串长度超长可自动截取

package xin.xihc.rebate.bean;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 订阅消息对象
 *
 * @author Leo.Xi
 * @date 2019/10/25
 * @since 1.0
 **/
public class SubscribeMsgData {

    /** 顺序值 */
    private int index = 1;

    /** 数据data */
    private LinkedHashMap<String, Keyword> data = new LinkedHashMap<>();

    public static SubscribeMsgData create() {
        return new SubscribeMsgData();
    }

    /**
     * thing.DATA   事物  20个以内字符 可汉字、数字、字母或符号组合
     *
     * @param thing
     * @author Leo.Xi
     * @date 2019/10/25
     * @since 0.0.1
     */
    public SubscribeMsgData thing(String thing) {
        if (thing.length() > 20) {
            thing = thing.substring(0, 20);
        }
        data.put("thing" + index, new Keyword(thing));
        ++index;
        return this;
    }

    /**
     * number.DATA  数字  32位以内数字 只能数字,可带小数
     *
     * @param number
     * @author Leo.Xi
     * @date 2019/10/25
     * @since 0.0.1
     */
    public SubscribeMsgData number(String number) {
        if (number.length() > 32) {
            number = number.substring(0, 32);
        }
        data.put("number" + index, new Keyword(number));
        ++index;
        return this;
    }

    /**
     * letter.DATA  字母  32位以内字母 只能字母
     *
     * @param letter
     * @author Leo.Xi
     * @date 2019/10/25
     * @since 0.0.1
     */
    public SubscribeMsgData letter(String letter) {
        if (letter.length() > 32) {
            letter = letter.substring(0, 32);
        }
        data.put("letter" + index, new Keyword(letter));
        ++index;
        return this;
    }

    /**
     * date.DATA    日期  年月日格式(支持+24小时制时间)   例如:2019年10月1日,或:2019年10月1日 15:01
     *
     * @param date
     * @author Leo.Xi
     * @date 2019/10/25
     * @since 0.0.1
     */
    public SubscribeMsgData date(String date) {
        data.put("date" + index, new Keyword(date));
        ++index;
        return this;
    }

    /**
     * amount.DATA  金额  1个币种符号+10位以内纯数字,可带小数,结尾可带“元”    可带小数
     *
     * @param amount
     * @author Leo.Xi
     * @date 2019/10/25
     * @since 0.0.1
     */
    public SubscribeMsgData amount(String amount) {
        data.put("amount" + index, new Keyword(amount));
        ++index;
        return this;
    }

    /**
     * phrase.DATA  汉字  5个以内汉字  5个以内纯汉字,例如:配送中
     *
     * @param phrase
     * @author Leo.Xi
     * @date 2019/10/25
     * @since 0.0.1
     */
    public SubscribeMsgData phrase(String phrase) {
        if (phrase.length() > 5) {
            phrase = phrase.substring(0, 5);
        }
        data.put("phrase" + index, new Keyword(phrase));
        ++index;
        return this;
    }

    public Map<String, Keyword> data() {
        return this.data;
    }


    @Data
    @AllArgsConstructor
    public static class Keyword {
        private String value;
    }

}

调用的代码:

// 计算到账时间
String paymentTime = DateUtil.getDateTimeFromNow(
                        configBean.getSystemConfig().getDelaySettlementDays() * 24 * 60 * 60, "yyyy.MM.dd");
// 订阅消息
SubscribeMsgData subscribeMsgData = SubscribeMsgData.create().thing(flow.getType()
                                                                    .equals(CapitalOperateType.REBATE) ? "返利" : "分红")
                                                                    .date(DateUtil.formatDateTime(new Date(),
                                                                            "yyyy年MM月dd日 HH:mm"))
                                                                    .amount(flow.getRealAmount() + "元")
                                                                    .thing("预计" + paymentTime + "到账,可提现");
wxTemplateMsg.sendSubscribeMsg(flow.getUserId(), subscribeMsgData, WxTemplateMsg.MsgType.SETTLEMENT);
    /**
     * 发送订阅消息
     *
     * @param userId 用户ID
     * @param data   发送模板内容
     * @param type   消息类型
     * @return
     * @author Leo.Xi
     * @date 2019/8/18
     * @since 0.0.1
     */
    public void sendSubscribeMsg(String userId, SubscribeMsgData data, MsgType type) {
        try {
            String openId = wxUserInfoDao.getOpenId(userId);
            HashMap<String, Object> params = new HashMap<>(4);
            params.put("touser", openId);
            Template template = TEMPLATES_SUBSCRIBE.get(type);
            params.put("template_id", template.getId());
            params.put("page", template.getPage());
            params.put("data", data.data());
            String post = RestClientUtils.post(String.format(URL_POST_SUBSCRIBE_MSG, ACCESS_TOKEN), JsonUtil
                    .toNoNullJsonStr(params, false), String.class);
            log.info("发送订阅消息结果:" + post);
        } catch (Throwable e) {
            // ignore
        }
    }

结算消息通知

订单确认收货后的结算通知
上一篇 下一篇

猜你喜欢

热点阅读