Browse Source

微信公众号

v1.0
15989082884@163.com 2 years ago
parent
commit
73fdcac851
  1. 9
      src/main/java/com/qs/serve/modules/wx/api/WxMpJsapiApi.java
  2. 25
      src/main/java/com/qs/serve/modules/wx/api/WxMpMenuApi.java
  3. 6
      src/main/java/com/qs/serve/modules/wx/common/conf/WxMpConfig.java
  4. 7
      src/main/java/com/qs/serve/modules/wx/service/impl/WxPushServiceImpl.java

9
src/main/java/com/qs/serve/modules/wx/api/WxMpJsapiApi.java

@ -1,9 +1,11 @@
package com.qs.serve.modules.wx.api; package com.qs.serve.modules.wx.api;
import com.qs.serve.modules.wx.common.conf.WxMpConfig;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -17,7 +19,8 @@ import org.springframework.web.bind.annotation.PathVariable;
//@RestController //@RestController
//@RequestMapping("/api/mp/jsapi/{appid}") //@RequestMapping("/api/mp/jsapi/{appid}")
public class WxMpJsapiApi { public class WxMpJsapiApi {
private final WxMpService wxService; @Autowired(required = false)
private WxMpConfig wxMpConfig;
/** /**
* 获取票据 * 获取票据
@ -27,9 +30,9 @@ public class WxMpJsapiApi {
*/ */
@GetMapping("/getJsapiTicket") @GetMapping("/getJsapiTicket")
public String getJsapiTicket(@PathVariable String appid) throws WxErrorException { public String getJsapiTicket(@PathVariable String appid) throws WxErrorException {
final WxJsapiSignature jsapiSignature = this.wxService.switchoverTo(appid).createJsapiSignature("111"); final WxJsapiSignature jsapiSignature = wxMpConfig.wxMpService().switchoverTo(appid).createJsapiSignature("111");
System.out.println(jsapiSignature); System.out.println(jsapiSignature);
return this.wxService.getJsapiTicket(true); return wxMpConfig.wxMpService().getJsapiTicket(true);
} }
} }

25
src/main/java/com/qs/serve/modules/wx/api/WxMpMenuApi.java

@ -1,5 +1,6 @@
package com.qs.serve.modules.wx.api; package com.qs.serve.modules.wx.api;
import com.qs.serve.modules.wx.common.conf.WxMpConfig;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu; import me.chanjar.weixin.common.bean.menu.WxMenu;
@ -8,6 +9,7 @@ import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu; import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
@ -27,7 +29,8 @@ import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
//@RestController //@RestController
//@RequestMapping("/api/mp/menu/{appid}") //@RequestMapping("/api/mp/menu/{appid}")
public class WxMpMenuApi { public class WxMpMenuApi {
private final WxMpService wxService; @Autowired(required = false)
private WxMpConfig wxMpConfig;
/** /**
* 自定义菜单创建接口 * 自定义菜单创建接口
@ -40,7 +43,7 @@ public class WxMpMenuApi {
*/ */
@PostMapping("/create") @PostMapping("/create")
public String menuCreate(@PathVariable String appid, @RequestBody WxMenu menu) throws WxErrorException { public String menuCreate(@PathVariable String appid, @RequestBody WxMenu menu) throws WxErrorException {
return this.wxService.switchoverTo(appid).getMenuService().menuCreate(menu); return wxMpConfig.wxMpService().switchoverTo(appid).getMenuService().menuCreate(menu);
} }
//@GetMapping("/createByData") //@GetMapping("/createByData")
@ -89,7 +92,7 @@ public class WxMpMenuApi {
if (servletRequestAttributes != null) { if (servletRequestAttributes != null) {
HttpServletRequest request = servletRequestAttributes.getRequest(); HttpServletRequest request = servletRequestAttributes.getRequest();
URL requestURL = new URL(request.getRequestURL().toString()); URL requestURL = new URL(request.getRequestURL().toString());
String url = this.wxService.switchoverTo(appid).getOAuth2Service().buildAuthorizationUrl( String url = wxMpConfig.wxMpService().switchoverTo(appid).getOAuth2Service().buildAuthorizationUrl(
String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appid), String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appid),
WxConsts.OAuth2Scope.SNSAPI_USERINFO, null); WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
button34.setUrl(url); button34.setUrl(url);
@ -100,8 +103,8 @@ public class WxMpMenuApi {
button3.getSubButtons().add(button33); button3.getSubButtons().add(button33);
button3.getSubButtons().add(button34); button3.getSubButtons().add(button34);
this.wxService.switchover(appid); wxMpConfig.wxMpService().switchover(appid);
return this.wxService.getMenuService().menuCreate(menu); return wxMpConfig.wxMpService().getMenuService().menuCreate(menu);
} }
/** /**
@ -115,7 +118,7 @@ public class WxMpMenuApi {
*/ */
@PostMapping("/createByJson") @PostMapping("/createByJson")
public String menuCreate(@PathVariable String appid, @RequestBody String json) throws WxErrorException { public String menuCreate(@PathVariable String appid, @RequestBody String json) throws WxErrorException {
return this.wxService.switchoverTo(appid).getMenuService().menuCreate(json); return wxMpConfig.wxMpService().switchoverTo(appid).getMenuService().menuCreate(json);
} }
/** /**
@ -126,7 +129,7 @@ public class WxMpMenuApi {
*/ */
@GetMapping("/delete") @GetMapping("/delete")
public void menuDelete(@PathVariable String appid) throws WxErrorException { public void menuDelete(@PathVariable String appid) throws WxErrorException {
this.wxService.switchoverTo(appid).getMenuService().menuDelete(); wxMpConfig.wxMpService().switchoverTo(appid).getMenuService().menuDelete();
} }
/** /**
@ -138,7 +141,7 @@ public class WxMpMenuApi {
*/ */
@GetMapping("/delete/{menuId}") @GetMapping("/delete/{menuId}")
public void menuDelete(@PathVariable String appid, @PathVariable String menuId) throws WxErrorException { public void menuDelete(@PathVariable String appid, @PathVariable String menuId) throws WxErrorException {
this.wxService.switchoverTo(appid).getMenuService().menuDelete(menuId); wxMpConfig.wxMpService().switchoverTo(appid).getMenuService().menuDelete(menuId);
} }
/** /**
@ -149,7 +152,7 @@ public class WxMpMenuApi {
*/ */
@GetMapping("/get") @GetMapping("/get")
public WxMpMenu menuGet(@PathVariable String appid) throws WxErrorException { public WxMpMenu menuGet(@PathVariable String appid) throws WxErrorException {
return this.wxService.switchoverTo(appid).getMenuService().menuGet(); return wxMpConfig.wxMpService().switchoverTo(appid).getMenuService().menuGet();
} }
/** /**
@ -162,7 +165,7 @@ public class WxMpMenuApi {
*/ */
@GetMapping("/menuTryMatch/{userid}") @GetMapping("/menuTryMatch/{userid}")
public WxMenu menuTryMatch(@PathVariable String appid, @PathVariable String userid) throws WxErrorException { public WxMenu menuTryMatch(@PathVariable String appid, @PathVariable String userid) throws WxErrorException {
return this.wxService.switchoverTo(appid).getMenuService().menuTryMatch(userid); return wxMpConfig.wxMpService().switchoverTo(appid).getMenuService().menuTryMatch(userid);
} }
/** /**
@ -182,6 +185,6 @@ public class WxMpMenuApi {
*/ */
@GetMapping("/getSelfMenuInfo") @GetMapping("/getSelfMenuInfo")
public WxMpGetSelfMenuInfoResult getSelfMenuInfo(@PathVariable String appid) throws WxErrorException { public WxMpGetSelfMenuInfoResult getSelfMenuInfo(@PathVariable String appid) throws WxErrorException {
return this.wxService.switchoverTo(appid).getMenuService().getSelfMenuInfo(); return wxMpConfig.wxMpService().switchoverTo(appid).getMenuService().getSelfMenuInfo();
} }
} }

6
src/main/java/com/qs/serve/modules/wx/common/conf/WxMpConfig.java

@ -50,10 +50,13 @@ public class WxMpConfig {
private final ScanHandler scanHandler; private final ScanHandler scanHandler;
private final WxAppService wxAppService; private final WxAppService wxAppService;
private final RedisProperties redisProperties; private final RedisProperties redisProperties;
private static WxMpService wxMpService = null;
@Bean @Bean
@Scope(value = "singleton")
public WxMpService wxMpService() { public WxMpService wxMpService() {
if(wxMpService!=null){
return wxMpService;
}
//超时时间 //超时时间
LambdaQueryWrapper<WxApp> appWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<WxApp> appWrapper = new LambdaQueryWrapper<>();
appWrapper.eq(WxApp::getAppType, WxStatusConst.WxApp_AppType_2); appWrapper.eq(WxApp::getAppType, WxStatusConst.WxApp_AppType_2);
@ -82,6 +85,7 @@ public class WxMpConfig {
configStorage.setAesKey(app.getAesKey()); configStorage.setAesKey(app.getAesKey());
return configStorage; return configStorage;
}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o))); }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
WxMpConfig.wxMpService = service;
return service; return service;
} }

7
src/main/java/com/qs/serve/modules/wx/service/impl/WxPushServiceImpl.java

@ -3,6 +3,7 @@ package com.qs.serve.modules.wx.service.impl;
import com.qs.serve.common.util.AuthContextUtils; import com.qs.serve.common.util.AuthContextUtils;
import com.qs.serve.common.util.JsonUtil; import com.qs.serve.common.util.JsonUtil;
import com.qs.serve.common.util.SmsReflectUtil; import com.qs.serve.common.util.SmsReflectUtil;
import com.qs.serve.modules.wx.common.conf.WxMpConfig;
import com.qs.serve.modules.wx.entity.WxTemplateMsg; import com.qs.serve.modules.wx.entity.WxTemplateMsg;
import com.qs.serve.modules.wx.entity.WxUser; import com.qs.serve.modules.wx.entity.WxUser;
import com.qs.serve.modules.wx.entity.dto.SmsBaseDto; import com.qs.serve.modules.wx.entity.dto.SmsBaseDto;
@ -13,6 +14,7 @@ import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -26,7 +28,8 @@ import org.springframework.stereotype.Service;
@AllArgsConstructor @AllArgsConstructor
public class WxPushServiceImpl implements WxPushService { public class WxPushServiceImpl implements WxPushService {
private final WxMpService wxMpService; @Autowired(required = false)
private WxMpConfig wxMpConfig;
private final WxTemplateMsgService wxTemplateMsgService; private final WxTemplateMsgService wxTemplateMsgService;
@ -36,7 +39,7 @@ public class WxPushServiceImpl implements WxPushService {
String msgId = null; String msgId = null;
try { try {
// 发送模板消息 // 发送模板消息
msgId = wxMpService.switchoverTo(appId).getTemplateMsgService().sendTemplateMsg(message); msgId = wxMpConfig.wxMpService().switchoverTo(appId).getTemplateMsgService().sendTemplateMsg(message);
} catch (WxErrorException e) { } catch (WxErrorException e) {
e.printStackTrace(); e.printStackTrace();
} }

Loading…
Cancel
Save