liusheng
2023-06-16 e53334611e8d51fb67e769cba898743f11dcf627
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.ruoyi.web.controller.smartor;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.smartor.domain.WeChatSendVo;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
 
/**
 * 微信接口
 *
 * @author liusheng
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/smartor/wechat")
@PropertySource(value = { "classpath:application-druid.yml" })
public class WeChatController extends BaseController {
 
    @Value("${appid}")
    private String appid;
 
    @Value("${appSecret}")
    private String appSecret;
 
 
    /**
     * 获取模板信息
     *
     * @return
     */
    @GetMapping("/getTemplateList")
    public JSONArray getTemplateList() {
        String url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=" + getAccessToken();
        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        try {
            Response response = httpClient.newCall(request).execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            return jsonObject.getJSONArray("template_list");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
 
 
    /**
     * 微信公众号信息发送
     *
     * @param weChatSendVo
     * @return
     */
    @PostMapping("/sendMessageToFollowers")
    public Boolean sendMessageToFollowers(@RequestBody WeChatSendVo weChatSendVo) {
        WxMpService wxMpService;
        wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(new WxMpInMemoryConfigStorage());
        WxMpInMemoryConfigStorage wxMpConfigStorage = (WxMpInMemoryConfigStorage) wxMpService.getWxMpConfigStorage();
        wxMpConfigStorage.setAppId(appid);
        wxMpConfigStorage.setSecret(appSecret);
        try {
            List<String> openIdList = wxMpService.getUserService().userList(null).getOpenids();
            for (String openId : openIdList) {
                WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser(openId).templateId(weChatSendVo.getTemplateId()).url(weChatSendVo.getUrl()).build();
                for (String key : weChatSendVo.getContent().keySet()) {
                    templateMessage.addData(new WxMpTemplateData(key, weChatSendVo.getContent().get(key).toString()));
                }
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return true;
    }
 
 
 
    /**
     * 获取 access_token
     */
    public String getAccessToken() {
        String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appSecret;
        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder().url(accessTokenUrl).build();
        try {
            Response response = httpClient.newCall(request).execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            return jsonObject.getString("access_token");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    //  }
 
 
}