Browse Source

wechat springconfig

tall3
zhizhi wu 4 years ago
parent
commit
0be4512726
  1. 136
      wechatutil/src/main/java/com/ccsens/wechatutil/api/DebugController.java
  2. 58
      wechatutil/src/main/java/com/ccsens/wechatutil/config/BeanConfig.java
  3. 318
      wechatutil/src/main/java/com/ccsens/wechatutil/config/SpringConfig.java

136
wechatutil/src/main/java/com/ccsens/wechatutil/api/DebugController.java

@ -1,68 +1,68 @@
package com.ccsens.wechatutil.api; //package com.ccsens.wechatutil.api;
//
import cn.hutool.core.date.DateUtil; //import cn.hutool.core.date.DateUtil;
import com.ccsens.util.JsonResponse; //import com.ccsens.util.JsonResponse;
import com.ccsens.util.PropUtil; //import com.ccsens.util.PropUtil;
import com.ccsens.util.WebConstant; //import com.ccsens.util.WebConstant;
import com.ccsens.util.wx.WxXcxUtil; //import com.ccsens.util.wx.WxXcxUtil;
import com.ccsens.wechatutil.bean.dto.WechatCode; //import com.ccsens.wechatutil.bean.dto.WechatCode;
import com.ccsens.wechatutil.wxmini.MiniCodeUtil; //import com.ccsens.wechatutil.wxmini.MiniCodeUtil;
import com.ccsens.wechatutil.wxofficial.OfficialAccountMessageUtil; //import com.ccsens.wechatutil.wxofficial.OfficialAccountMessageUtil;
import io.swagger.annotations.Api; //import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams; //import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; //import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping; //import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; //import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; //import org.springframework.web.bind.annotation.RestController;
//
import javax.servlet.http.HttpServletRequest; //import javax.servlet.http.HttpServletRequest;
//
@Api(tags = "DEBUG" , description = "DebugController | ") //@Api(tags = "DEBUG" , description = "DebugController | ")
@RestController //@RestController
@RequestMapping("/debug") //@RequestMapping("/debug")
@Slf4j //@Slf4j
public class DebugController { //public class DebugController {
//
//
//
@ApiOperation(value = "/测试",notes = "") // @ApiOperation(value = "/测试",notes = "")
@ApiImplicitParams({ // @ApiImplicitParams({
}) // })
@RequestMapping(value="",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"}) // @RequestMapping(value="",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
public JsonResponse debug(HttpServletRequest request) throws Exception { // public JsonResponse debug(HttpServletRequest request) throws Exception {
//
return JsonResponse.newInstance().ok("测试"); // return JsonResponse.newInstance().ok("测试");
} // }
//
@ApiOperation(value = "/测试",notes = "") // @ApiOperation(value = "/测试",notes = "")
@ApiImplicitParams({ // @ApiImplicitParams({
}) // })
@RequestMapping(value="code",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"}) // @RequestMapping(value="code",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
public JsonResponse debugWxCode(HttpServletRequest request) throws Exception { // public JsonResponse debugWxCode(HttpServletRequest request) throws Exception {
//生成二维码 // //生成二维码
String fileName = "/gameQrCode/" + DateUtil.today() + "/" + System.currentTimeMillis() + ".png"; // String fileName = "/gameQrCode/" + DateUtil.today() + "/" + System.currentTimeMillis() + ".png";
String path = WebConstant.UPLOAD_PATH_BASE + fileName; // String path = WebConstant.UPLOAD_PATH_BASE + fileName;
//
WechatCode.WechatCodeB wechatCodeB = new WechatCode.WechatCodeB(); // WechatCode.WechatCodeB wechatCodeB = new WechatCode.WechatCodeB();
wechatCodeB.setPage(WebConstant.QRCODE_GAME); // wechatCodeB.setPage(WebConstant.QRCODE_GAME);
wechatCodeB.setScene("id=1&type=SQ"); // wechatCodeB.setScene("id=1&type=SQ");
MiniCodeUtil.getWxCodeB(wechatCodeB, path); // MiniCodeUtil.getWxCodeB(wechatCodeB, path);
// WxXcxUtil.getWxCodeC(WebConstant.QRCODE_GAME+"?id=" + gameRecord.getId() + "&type=" + gameType.getCode(), path, gameType.getCode()); //// WxXcxUtil.getWxCodeC(WebConstant.QRCODE_GAME+"?id=" + gameRecord.getId() + "&type=" + gameType.getCode(), path, gameType.getCode());
//
// gameRecord.setQrCodeUrl(PropUtil.qrCode + fileName); //// gameRecord.setQrCodeUrl(PropUtil.qrCode + fileName);
log.info("调用微信生成二维码"); // log.info("调用微信生成二维码");
return JsonResponse.newInstance().ok("测试"); // return JsonResponse.newInstance().ok("测试");
} // }
//
@ApiOperation(value = "/测试公众号消息",notes = "") // @ApiOperation(value = "/测试公众号消息",notes = "")
@ApiImplicitParams({ // @ApiImplicitParams({
}) // })
@RequestMapping(value="message",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"}) // @RequestMapping(value="message",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
public JsonResponse debugWxMessage(HttpServletRequest request) throws Exception { // public JsonResponse debugWxMessage(HttpServletRequest request) throws Exception {
// OfficialAccountMessageUtil.officialMessage(); //// OfficialAccountMessageUtil.officialMessage();
log.info("发送公众号消息"); // log.info("发送公众号消息");
return JsonResponse.newInstance().ok("测试"); // return JsonResponse.newInstance().ok("测试");
} // }
//
} //}

58
wechatutil/src/main/java/com/ccsens/wechatutil/config/BeanConfig.java

@ -1,31 +1,31 @@
package com.ccsens.wechatutil.config; //package com.ccsens.wechatutil.config;
//
import com.ccsens.wechatutil.intercept.MybatisInterceptor; //import com.ccsens.wechatutil.intercept.MybatisInterceptor;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
//
/** ///**
* @description: // * @description:
* @author: wuHuiJuan // * @author: wuHuiJuan
* @create: 2019/12/03 18:01 // * @create: 2019/12/03 18:01
*/ // */
@Configuration //@Configuration
public class BeanConfig { //public class BeanConfig {
//// @Bean
//// public static PropertySourcesPlaceholderConfigurer properties(){
//// PropertySourcesPlaceholderConfigurer conf = new PropertySourcesPlaceholderConfigurer();
//// YamlPropertiesFactoryBean yml = new YamlPropertiesFactoryBean();
//// yml.setResources(new ClassPathResource("business.yml"));
//// conf.setProperties(yml.getObject());
//// return conf;
//// }
//
// /**
// * 注册拦截器
// */
// @Bean // @Bean
// public static PropertySourcesPlaceholderConfigurer properties(){ // public MybatisInterceptor mybatisInterceptor() {
// PropertySourcesPlaceholderConfigurer conf = new PropertySourcesPlaceholderConfigurer(); // MybatisInterceptor interceptor = new MybatisInterceptor();
// YamlPropertiesFactoryBean yml = new YamlPropertiesFactoryBean(); // return interceptor;
// yml.setResources(new ClassPathResource("business.yml"));
// conf.setProperties(yml.getObject());
// return conf;
// } // }
//}
/**
* 注册拦截器
*/
@Bean
public MybatisInterceptor mybatisInterceptor() {
MybatisInterceptor interceptor = new MybatisInterceptor();
return interceptor;
}
}

318
wechatutil/src/main/java/com/ccsens/wechatutil/config/SpringConfig.java

@ -1,171 +1,171 @@
package com.ccsens.wechatutil.config; //package com.ccsens.wechatutil.config;
//
//
import cn.hutool.core.lang.Snowflake; //import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil; //import cn.hutool.core.util.IdUtil;
import com.ccsens.util.config.DruidProps; //import com.ccsens.util.config.DruidProps;
import com.fasterxml.jackson.databind.DeserializationFeature; //import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; //import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule; //import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; //import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Value; //import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType; //import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter; //import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter; //import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; //import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*; //import org.springframework.web.servlet.config.annotation.*;
//
import javax.annotation.Resource; //import javax.annotation.Resource;
import javax.sql.DataSource; //import javax.sql.DataSource;
import java.nio.charset.Charset; //import java.nio.charset.Charset;
import java.util.ArrayList; //import java.util.ArrayList;
import java.util.List; //import java.util.List;
//
@Configuration //@Configuration
//public class SpringConfig extends WebMvcConfigurationSupport { ////public class SpringConfig extends WebMvcConfigurationSupport {
public class SpringConfig implements WebMvcConfigurer { //public class SpringConfig implements WebMvcConfigurer {
@Resource // @Resource
private DruidProps druidPropsUtil; // private DruidProps druidPropsUtil;
@Value("${spring.snowflake.workerId}") // @Value("${spring.snowflake.workerId}")
private String workerId; // private String workerId;
@Value("${spring.snowflake.datacenterId}") // @Value("${spring.snowflake.datacenterId}")
private String datacenterId; // private String datacenterId;
//
/** // /**
* 配置Converter // * 配置Converter
* @return // * @return
*/ // */
@Bean
public HttpMessageConverter<String> responseStringConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(
Charset.forName("UTF-8"));
return converter;
}
@Bean
public HttpMessageConverter<Object> responseJsonConverter(){
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
List<MediaType> mediaTypeList = new ArrayList<>();
mediaTypeList.add(MediaType.TEXT_HTML);
mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(mediaTypeList);
//converter.setObjectMapper();
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
converter.setObjectMapper(objectMapper);
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//super.configureMessageConverters(converters);
converters.add(responseStringConverter());
converters.add(responseJsonConverter());
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
// .allowedMethods("*") // 允许提交请求的方法,*表示全部允许
.allowedOrigins("*") // #允许向该服务器提交请求的URI,*表示全部允许
.allowCredentials(true) // 允许cookies跨域
.allowedHeaders("*") // #允许访问的头信息,*表示全部
.maxAge(18000L); // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
}
/**
* 配置视图解析器 SpringBoot建议使用Thymeleaf代替jsp动态页面默认路径resources/template静态页面默认路径: resources/static
* @return
*/
// @Bean // @Bean
// public ViewResolver getViewResolver() { // public HttpMessageConverter<String> responseStringConverter() {
// InternalResourceViewResolver resolver = new InternalResourceViewResolver(); // StringHttpMessageConverter converter = new StringHttpMessageConverter(
// resolver.setPrefix("/WEB-INF/views/"); // Charset.forName("UTF-8"));
// resolver.setSuffix(".jsp"); // return converter;
// return resolver; // }
//
// @Bean
// public HttpMessageConverter<Object> responseJsonConverter(){
// MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
// List<MediaType> mediaTypeList = new ArrayList<>();
// mediaTypeList.add(MediaType.TEXT_HTML);
// mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
// converter.setSupportedMediaTypes(mediaTypeList);
//
// //converter.setObjectMapper();
// ObjectMapper objectMapper = new ObjectMapper();
// SimpleModule simpleModule = new SimpleModule();
// simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
// simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
// objectMapper.registerModule(simpleModule);
// objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// converter.setObjectMapper(objectMapper);
//
// return converter;
// }
//
// @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// //super.configureMessageConverters(converters);
// converters.add(responseStringConverter());
// converters.add(responseJsonConverter());
// } // }
//
// @Override
// public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
// configurer.favorPathExtension(false);
// }
//
// @Override // @Override
// public void configureDefaultServletHandling( // public void addCorsMappings(CorsRegistry registry) {
// DefaultServletHandlerConfigurer configurer) { // registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
// configurer.enable(); //// .allowedMethods("*") // 允许提交请求的方法,*表示全部允许
// .allowedOrigins("*") // #允许向该服务器提交请求的URI,*表示全部允许
// .allowCredentials(true) // 允许cookies跨域
// .allowedHeaders("*") // #允许访问的头信息,*表示全部
// .maxAge(18000L); // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
//
// } // }
/**
* 配置静态资源
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/uploads/**")
.addResourceLocations("file:///home/cloud/wechatutil/uploads/");
//super.addResourceHandlers(registry);
}
// //
// /** // /**
// * 配置拦截器 // * 配置视图解析器 SpringBoot建议使用Thymeleaf代替jsp,动态页面默认路径:resources/template,静态页面默认路径: resources/static
// * @param registry // * @return
// */
//// @Bean
//// public ViewResolver getViewResolver() {
//// InternalResourceViewResolver resolver = new InternalResourceViewResolver();
//// resolver.setPrefix("/WEB-INF/views/");
//// resolver.setSuffix(".jsp");
//// return resolver;
//// }
//// @Override
//// public void configureDefaultServletHandling(
//// DefaultServletHandlerConfigurer configurer) {
//// configurer.enable();
//// }
//
//
// /**
// * 配置静态资源
// */ // */
// @Override // @Override
// public void addInterceptors(InterceptorRegistry registry) { // public void addResourceHandlers(ResourceHandlerRegistry registry) {
//// addPathPatterns 用于添加拦截规则 // registry.addResourceHandler("swagger-ui.html")
//// excludePathPatterns 用于排除拦截 // .addResourceLocations("classpath:/META-INF/resources/");
// registry.addInterceptor(tokenInterceptor()) // registry.addResourceHandler("/webjars/**")
//// .addPathPatterns("/projects/**") // .addResourceLocations("classpath:/META-INF/resources/webjars/");
//// .addPathPatterns("/messages/**") //
// .addPathPatterns("/users/**") // registry.addResourceHandler("/uploads/**")
// .excludePathPatterns("/users/signin") // .addResourceLocations("file:///home/cloud/wechatutil/uploads/");
// .excludePathPatterns("/users/smscode") // //super.addResourceHandlers(registry);
// .excludePathPatterns("/users/signup") // }
// .excludePathPatterns("/users/password") ////
// .excludePathPatterns("/users/account") //// /**
// .excludePathPatterns("/users/token") //// * 配置拦截器
// .excludePathPatterns("/users/claims") //// * @param registry
// .excludePathPatterns("/users/code") //// */
// .excludePathPatterns("/users/userId"); //// @Override
//// .addPathPatterns("/plugins/**") //// public void addInterceptors(InterceptorRegistry registry) {
//// .addPathPatterns("/delivers/**") ////// addPathPatterns 用于添加拦截规则
//// .addPathPatterns("/tasks/**") ////// excludePathPatterns 用于排除拦截
//// .addPathPatterns("/members/**") //// registry.addInterceptor(tokenInterceptor())
//// .addPathPatterns("/templates/**") ////// .addPathPatterns("/projects/**")
//// .addPathPatterns("/hardware/**"); ////// .addPathPatterns("/messages/**")
//// super.addInterceptors(registry); //// .addPathPatterns("/users/**")
//// .excludePathPatterns("/users/signin")
//// .excludePathPatterns("/users/smscode")
//// .excludePathPatterns("/users/signup")
//// .excludePathPatterns("/users/password")
//// .excludePathPatterns("/users/account")
//// .excludePathPatterns("/users/token")
//// .excludePathPatterns("/users/claims")
//// .excludePathPatterns("/users/code")
//// .excludePathPatterns("/users/userId");
////// .addPathPatterns("/plugins/**")
////// .addPathPatterns("/delivers/**")
////// .addPathPatterns("/tasks/**")
////// .addPathPatterns("/members/**")
////// .addPathPatterns("/templates/**")
////// .addPathPatterns("/hardware/**");
////// super.addInterceptors(registry);
//// }
////
//// @Bean
//// public TokenInterceptor tokenInterceptor(){
//// return new TokenInterceptor();
//// }
//
// /**
// * 配置数据源(单数据源)
// */
// @Bean
// public DataSource dataSource(){
// return druidPropsUtil.createDruidDataSource();
// } // }
// //
// @Bean // @Bean
// public TokenInterceptor tokenInterceptor(){ // public Snowflake snowflake(){
// return new TokenInterceptor(); //// return new Snowflake(Long.valueOf(workerId),Long.valueOf(datacenterId));
// return IdUtil.createSnowflake(Long.valueOf(workerId),Long.valueOf(datacenterId));
// } // }
//}
/**
* 配置数据源单数据源
*/
@Bean
public DataSource dataSource(){
return druidPropsUtil.createDruidDataSource();
}
@Bean
public Snowflake snowflake(){
// return new Snowflake(Long.valueOf(workerId),Long.valueOf(datacenterId));
return IdUtil.createSnowflake(Long.valueOf(workerId),Long.valueOf(datacenterId));
}
}
Loading…
Cancel
Save