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