公众号发送模板消息

gradle配置

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.4'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    maven {
        url 'https://maven.aliyun.com/repository/public'
    }
    mavenCentral()
}


dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.google.code.gson:gson:2.8.8'
    //支付相关功能
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'
    implementation 'com.github.wechatpay-apiv3:wechatpay-java:0.2.1'
    implementation 'com.github.wechatpay-apiv3:wechatpay-java-shangmi:0.2.1'
    //公众号模板信息
    implementation 'org.apache.httpcomponents:httpclient:4.5.13'
    implementation 'org.apache.httpcomponents:httpcore'
    implementation 'com.github.binarywang:weixin-java-mp:4.1.0'
    implementation 'cn.hutool:hutool-http:5.7.5' // Hutool HTTP
    implementation 'cn.hutool:hutool-json:5.7.5' // Hutool JSON

    //数据库
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'mysql:mysql-connector-java:8.0.26'
    implementation 'com.fasterxml.jackson.core:jackson-databind'
}

tasks.named('test') {
    useJUnitPlatform()
}

代码

package io.linfeng.modules.officialaccounts.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.linfeng.modules.officialaccounts.service.UnionIdTransformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TemplateMessageManager {
    
    @Autowired
    private UnionIdTransformService unionIdTransformService;
    
    public String getAccessToken(){
        return unionIdTransformService.getOfficialAccountAccessToken();
    }

    public void sendChatMessageRemind(String openId){
        sendMessageRemind(openId,"您收到了一条新消息","聊天消息");
    }

    public void sendCommentMessageRemind(String openId){
        sendMessageRemind(openId,"您的商品有一条新评论","商品评论");
    }


    /**
     * 发送消息提醒
     * @param openId
     * @param key 工单名称
     * @param value 工单类型
     */
    private void sendMessageRemind(String openId,String key,String value){
        String token=getAccessToken();
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="  + token;
        Map<String,Object> content=new HashMap<>();
        JSONObject data = JSONUtil.createObj();
        //value 为需要设置的值   color为字体颜色
        data.put("thing4",new JSONObject().put("value",key));
        data.put("phrase2",new JSONObject().put("value",value));


        JSONObject miniProgram = JSONUtil.createObj();
        miniProgram.set("appid","wx42f93423792f65d5");
        miniProgram.set("path","gc_market/pages/chat/index");

        content.put("touser",openId);
        content.put("template_id","UMQCYRXxk0jnBXSdqOm0GIFApM3R149ZzcPhRfHZ72w");
        content.put("miniprogram",miniProgram);
        content.put("data",data);
        String resp = HttpUtil.post(requestUrl,new JSONObject(content).toString());
        System.out.println(content.toString());
        JSONObject result = JSONUtil.parseObj(resp);
        System.out.println("发送消息:" + resp);
        return;
    }

}

 

posted @ 2024-09-19 15:46  byxiaobai  阅读(8)  评论(0编辑  收藏  举报