liusheng
2025-02-26 7f8b679d1643c546bb061882f99668d9639d56cc
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
package com.ruoyi.web.controller.smartor.tools;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.smartor.domain.WeChatSendVo;
import com.smartor.service.WeChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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
 */
@Api(description = "微信接口")
@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;
 
    @Autowired
    private WeChatService weChatService;
 
 
    /**
     * 获取模板信息
     *
     * @return
     */
    @ApiOperation("获取模板信息")
    @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
     */
//    @ApiOperation("微信公众号信息发送")
//    @PostMapping("/sendMessageToFollowers")
//    public AjaxResult sendMessageToFollowers(@RequestBody WeChatSendVo weChatSendVo) {
//        return toAjax(weChatService.sendMessageToFollowers(weChatSendVo));
//    }
 
 
    /**
     * 获取 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;
        }
    }
 
 
}