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;
|
}
|
}
|
// }
|
|
|
}
|