微信小程序订阅消息
一、小程序订阅模板准备
使用微信小程序账号登录微信公众平台,找到如下页面,点击选用,选择自己需要的合适的模板。
微信发送订阅消息接口文档
1.1、获取模板id及模板字段名称
注意点: 消息字段的类型是有校验的,充电度数是number类型,则传入的string中不能使用0.00度。则会报错。
二、Java服务端代码
2.1、编写消息模板配置文件
用于保存消息的id和pages
package com.nis.charge.config.wechat;
import lombok.Getter;
@Getter
public enum WechatMsgTempConfig {
START_CHARGE("76-w0HT6wfZVP0F8DtVNuGyOWWMALAmQHPgBBgcQUgY", "/pages/mine/charge-record/charge-record"),
;
private String tempId;
private String page;
WechatMsgTempConfig(String tempId, String page) {
this.tempId = tempId;
this.page = page;
}
}
2.2、编写订阅消息请求对象
package com.nis.charge.domain.dto.subscribeMsg.model.wechat;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class WechatMiniMsgParams {
private String access_token;
private String template_id;
private String page;
private String touser;
private Object data;
private String miniprogram_state;
private String lang;
}
2.3、编写充电开始订阅消息模板对象
2.2.1、DataEntry
package com.nis.charge.domain.dto.subscribeMsg.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DataEntry implements Serializable {
private static final long serialVersionUID = 7946471752462379765L;
private String value;
}
2.2.2、ChargeStartModel
package com.nis.charge.domain.dto.subscribeMsg.model.wechat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.nis.charge.domain.dto.subscribeMsg.model.DataEntry;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ChargeStartModel {
private DataEntry character_string11;
private DataEntry time2;
private DataEntry thing5;
private DataEntry character_string6;
private DataEntry thing7;
}
2.4、编写发送接口
@Override
public boolean sendChargeStartMsg(String openId, String orderId, String createTime, String placeName, String equipCode, String portName) {
boolean sendSuccess = false;
String accessToken = iWechatService.getAccessToken();
ChargeStartModel startCharge = ChargeStartModel.builder().character_string11(DataEntry.builder().value(orderId).build())
.time2(DataEntry.builder().value(createTime).build())
.thing5(DataEntry.builder().value(placeName).build())
.character_string6(DataEntry.builder().value(equipCode).build())
.thing7(DataEntry.builder().value(portName).build()).build();
WechatMiniMsgParams wechatMiniMsgParams = WechatMiniMsgParams.builder()
.touser(openId).template_id(WechatMsgTempConfig.START_CHARGE.getTempId()).page(WechatMsgTempConfig.START_CHARGE.getPage())
.data(startCharge).build();
JSONObject jsonResult = restTemplate.postForObject(WechatConstant.SEND_SUBSCRIBE_URL + "?access_token=" + accessToken, wechatMiniMsgParams, JSONObject.class);
log.info("充电开始推送消息返回参数:{}", JSON.toJSONString(jsonResult));
Integer errorCode = jsonResult.getInteger("errcode");
String errorMessage = jsonResult.getString("errmsg");
if (errorCode == 0) {
sendSuccess = true;
log.info("充电开始推送消息发送成功");
} else {
log.info("充电开始推送消息发送失败,errcode:{},errorMessage:{}", errorCode, errorMessage);
}
return sendSuccess;
}
三、uniapp代码
uni.requestSubscribeMessage({
tmplIds: ['76-w0HT6wfZVP0F8DtVNuGyOWWMALAmQHPgBBgcQUgY'],
complete(res) {
_this.wechatPay(chargeData)
}
})
四、测试
|