|
@@ -1,6 +1,7 @@
|
|
package com.management.platform.controller;
|
|
package com.management.platform.controller;
|
|
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
@@ -436,6 +437,112 @@ public class WechatAccountController {
|
|
return msg;
|
|
return msg;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 创建微信公众号菜单
|
|
|
|
+ @RequestMapping("/createMenu")
|
|
|
|
+ public HttpRespMsg createMenu(Integer companyId) {
|
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
|
+ WechatAccount wechatAccount = wechatAccountService.getOne(new QueryWrapper<WechatAccount>().eq("company_id", companyId));
|
|
|
|
+ if (wechatAccount==null){
|
|
|
|
+ httpRespMsg.setError("该公司没有配置公众号相关的参数");
|
|
|
|
+ return httpRespMsg;
|
|
|
|
+ }
|
|
|
|
+ String accessToken = wechatAccountService.getAccessToken(companyId, wechatAccount.getAppId());
|
|
|
|
+ log.info("accessToken==>{}", accessToken);
|
|
|
|
+ boolean menuWithMiniProgram = createMenuWithMiniProgram(accessToken);
|
|
|
|
+ if (menuWithMiniProgram){
|
|
|
|
+ return httpRespMsg;
|
|
|
|
+ }else {
|
|
|
|
+ httpRespMsg.setError("创建失败");
|
|
|
|
+ return httpRespMsg;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建包含小程序链接的菜单
|
|
|
|
+ * @param accessToken 微信access_token
|
|
|
|
+ * @return 是否创建成功
|
|
|
|
+ */
|
|
|
|
+ public boolean createMenuWithMiniProgram(String accessToken) {
|
|
|
|
+ // 构建菜单JSON
|
|
|
|
+ String menuJson = buildMenuJson();
|
|
|
|
+
|
|
|
|
+ // 设置请求头
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
+
|
|
|
|
+ // 创建请求实体
|
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(menuJson, headers);
|
|
|
|
+
|
|
|
|
+ // 构建完整URL
|
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ // 发送POST请求
|
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.exchange(
|
|
|
|
+ requestUrl,
|
|
|
|
+ HttpMethod.POST,
|
|
|
|
+ requestEntity,
|
|
|
|
+ String.class
|
|
|
|
+ );
|
|
|
|
+ log.info("responseEntity==>" + responseEntity);
|
|
|
|
+
|
|
|
|
+ // 解析响应
|
|
|
|
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
|
+ JSONObject jsonResult = JSON.parseObject(responseEntity.getBody());
|
|
|
|
+ log.info("解析响应"+jsonResult.toJSONString());
|
|
|
|
+ return jsonResult.getInteger("errcode") == 0;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 构建包含小程序链接的菜单JSON
|
|
|
|
+ */
|
|
|
|
+ private static String buildMenuJson() {
|
|
|
|
+ // 示例菜单结构:主菜单包含3个子菜单,其中一个是小程序链接
|
|
|
|
+ JSONObject menu = new JSONObject();
|
|
|
|
+
|
|
|
|
+ // 第一个菜单项 - 普通点击菜单
|
|
|
|
+ /*JSONObject button1 = new JSONObject();
|
|
|
|
+ button1.put("type", "click");
|
|
|
|
+ button1.put("name", "今日新闻");
|
|
|
|
+ button1.put("key", "V1001_TODAY_NEWS");*/
|
|
|
|
+
|
|
|
|
+ // 第二个菜单项 - 包含子菜单
|
|
|
|
+ JSONObject button2 = new JSONObject();
|
|
|
|
+ button2.put("name", "关于我们");
|
|
|
|
+
|
|
|
|
+ /*JSONObject subButton1 = new JSONObject();
|
|
|
|
+ subButton1.put("type", "view");
|
|
|
|
+ subButton1.put("name", "官网首页");
|
|
|
|
+ subButton1.put("url", "https://www.yourdomain.com");*/
|
|
|
|
+
|
|
|
|
+ JSONObject subButton2 = new JSONObject();
|
|
|
|
+ subButton2.put("type", "miniprogram");
|
|
|
|
+ subButton2.put("name", "打开小程序");
|
|
|
|
+ subButton2.put("url", "https://mp.weixin.qq.com/"); // 不支持小程序的老版本客户端将打开此url
|
|
|
|
+ subButton2.put("appid", "wxaaf19cfbbe1ff950"); // 小程序appid
|
|
|
|
+ subButton2.put("pagepath", "pages/training/training"); // 小程序页面路径
|
|
|
|
+
|
|
|
|
+ button2.put("sub_button", new JSONObject[]{ subButton2});
|
|
|
|
+
|
|
|
|
+ // 第三个菜单项 - 普通点击菜单
|
|
|
|
+ JSONObject button3 = new JSONObject();
|
|
|
|
+ button3.put("type", "click");
|
|
|
|
+ button3.put("name", "联系我们");
|
|
|
|
+ button3.put("key", "V1001_CONTACT_US");
|
|
|
|
+
|
|
|
|
+ menu.put("button", new JSONObject[]{button2,button3});
|
|
|
|
+
|
|
|
|
+ return menu.toJSONString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
private String getUserIp(HttpServletRequest request) {
|
|
private String getUserIp(HttpServletRequest request) {
|
|
// 1. 检查云服务商特殊头部
|
|
// 1. 检查云服务商特殊头部
|