Explorar el Código

微信开发--网关

chenyongxing hace 8 años
padre
commit
732da15640

+ 105 - 0
web-gateway/src/main/java/com/yihu/jw/controller/base/wx/WechatController.java

@ -0,0 +1,105 @@
package com.yihu.jw.controller.base.wx;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yihu.jw.fegin.base.wx.WechatFegin;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.restmodel.wx.WxContants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@Api(description = "微信配置")
public class WechatController{
    private Logger logger= LoggerFactory.getLogger(WechatController.class);
    @Autowired
    private WechatFegin wechatFegin;
    @Autowired
    private Tracer tracer;
    @ApiOperation(value = "创建微信配置")
    @PostMapping(value = "/"+WxContants.Wechat.api_common+"/"+WxContants.Wechat.api_create)
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "-1"),//超时时间
            @HystrixProperty(name = "execution.timeout.enabled", value = "false") })
    public Envelop createWechat(
            @ApiParam(name = "json_data", value = "", defaultValue = "") @RequestBody String jsonData) {
        tracer.getCurrentSpan().logEvent("开始调用微服务创建微信配置");
        Envelop wechat =wechatFegin.createWechat(jsonData);
        tracer.getCurrentSpan().logEvent("创建微信配置微服务结束");
        return wechat;
    }
    @ApiOperation(value = "更新微信配置")
    @PutMapping(value = "/"+WxContants.Wechat.api_common+"/"+WxContants.Wechat.api_update)
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "-1"),//超时时间
            @HystrixProperty(name = "execution.timeout.enabled", value = "false") })
    public Envelop updateWechat(
            @ApiParam(name = "json_data", value = "", defaultValue = "")
            @RequestBody String jsonData) {
        Envelop wechat =wechatFegin.updateWechat(jsonData);
        return wechat;
    }
    @DeleteMapping(value = "/"+WxContants.Wechat.api_common+"/"+WxContants.Wechat.api_delete)
    @ApiOperation(value = "删除微信配置", notes = "删除微信配置")
    public Envelop deleteWechat(
            @ApiParam(name = "code", value = "code")
            @RequestParam(value = "code", required = true) String code) {
        Envelop wechat =wechatFegin.deleteWechat(code);
        return wechat;
    }
    @GetMapping(value = "/"+WxContants.Wechat.api_common+"/"+WxContants.Wechat.api_getByCode)
    @ApiOperation(value = "根据code查找微信配置", notes = "根据code查找微信配置")
    public Envelop findByCode(
            @ApiParam(name = "code", value = "code")
            @RequestParam(value = "code", required = true) String code
    ) {
       return wechatFegin.findByCode(code);
    }
    @RequestMapping(value = "/"+WxContants.Wechat.api_common+"/"+WxContants.Wechat.api_getWechats, method = RequestMethod.GET)
    @ApiOperation(value = "获取微信配置列表(分页)")
    public Envelop getWechats(
            @ApiParam(name = "fields", value = "返回的字段,为空返回全部字段", defaultValue = "id,code,name,saasId,appId,appSecret,baseUrl,remark")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器,为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序,规则参见说明文档", defaultValue = "+name,+createTime")
            @RequestParam(value = "sorts", required = false) String sorts,
            @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
            @RequestParam(value = "size", required = false) int size,
            @ApiParam(name = "page", value = "页码", defaultValue = "1")
            @RequestParam(value = "page", required = false) int page,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Envelop envelop = wechatFegin.getWechats(fields,filters,sorts,size,page);
        return envelop;
    }
    @GetMapping(value = "/"+WxContants.Wechat.api_common+"/"+WxContants.Wechat.api_getWechatNoPage)
    @ApiOperation(value = "获取微信列表配置,不分页")
    public Envelop getWechatNoPage(
            @ApiParam(name = "fields", value = "返回的字段,为空返回全部字段", defaultValue = "id,code,name,saasId,appId,appSecret,baseUrl,remark")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器,为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序,规则参见说明文档", defaultValue = "+name,+createTime")
            @RequestParam(value = "sorts", required = false) String sorts) throws Exception {
        return wechatFegin.getWechatNoPage(fields,filters,sorts);
    }
}

+ 37 - 0
web-gateway/src/main/java/com/yihu/jw/fegin/base/wx/WechatFegin.java

@ -0,0 +1,37 @@
package com.yihu.jw.fegin.base.wx;
import com.yihu.jw.fegin.fallbackfactory.base.wx.WechatFeginFallbackFactory;
import com.yihu.jw.restmodel.common.CommonContants;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.restmodel.wx.WxContants;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(
        name = CommonContants.svr_base // name值是eurika的实例名字
        ,fallbackFactory  = WechatFeginFallbackFactory.class
)
@RequestMapping(value = WxContants.Wechat.api_common)
public interface WechatFegin {
    @RequestMapping(value = WxContants.Wechat.api_create, method = RequestMethod.POST)
    Envelop createWechat( @RequestBody String jsonData);
    @RequestMapping(value = WxContants.Wechat.api_update, method = RequestMethod.PUT)
    Envelop updateWechat(String jsonData);
    @RequestMapping(value = WxContants.Wechat.api_delete,method = RequestMethod.DELETE)
    Envelop deleteWechat(String code);
    @RequestMapping(value = WxContants.Wechat.api_getByCode,method = RequestMethod.GET)
    Envelop findByCode(String code);
    @RequestMapping(value = WxContants.Wechat.api_getWechats ,method = RequestMethod.GET)
    Envelop getWechats(String fields, String filters, String sorts, int page, int size);
    @RequestMapping(value = WxContants.Wechat.api_getWechatNoPage,method = RequestMethod.GET )
    Envelop getWechatNoPage(String fields, String filters, String sorts);
}

+ 22 - 0
web-gateway/src/main/java/com/yihu/jw/fegin/fallback/base/wx/WechatFeginFallback.java

@ -0,0 +1,22 @@
package com.yihu.jw.fegin.fallback.base.wx;
import com.yihu.jw.fegin.PatientFegin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * Created by chenweida on 2017/5/13.
 */
@Component
public class WechatFeginFallback implements PatientFegin {
    private Logger logger = LoggerFactory.getLogger(WechatFeginFallback.class);
    @Override
    public String findByCode( @RequestParam(value = "code", required = true) String code) {
        logger.info("进入断路器");
        return "断路器启动";
    }
}

+ 48 - 0
web-gateway/src/main/java/com/yihu/jw/fegin/fallbackfactory/base/wx/WechatFeginFallbackFactory.java

@ -0,0 +1,48 @@
package com.yihu.jw.fegin.fallbackfactory.base.wx;
import com.yihu.jw.fegin.base.wx.WechatFegin;
import com.yihu.jw.restmodel.common.Envelop;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class WechatFeginFallbackFactory implements FallbackFactory<WechatFegin> {
    @Override
    public WechatFegin create(Throwable e) {
        return new WechatFegin() {
            public Envelop createWechat(String jsonData) {
                Envelop envelop = new Envelop();
                envelop.getError(e.getMessage(),-1);
                return envelop;
            }
            public Envelop updateWechat(String jsonData) {
                Envelop envelop = new Envelop();
                envelop.getError(e.getMessage(),-1);
                return envelop;
            }
            public Envelop deleteWechat(String code){
                Envelop envelop = new Envelop();
                envelop.getError(e.getMessage(),-1);
                return envelop;
            }
            public Envelop findByCode(String code){
                Envelop envelop = new Envelop();
                envelop.getError(e.getMessage(),-1);
                return envelop;
            }
            public Envelop getWechats(String fields, String filters, String sorts, int page, int size){
                Envelop envelop = new Envelop();
                envelop.getError(e.getMessage(),-1);
                return envelop;
            }
            public Envelop getWechatNoPage(String fields, String filters, String sorts){
                Envelop envelop = new Envelop();
                envelop.getError(e.getMessage(),-1);
                return envelop;
            }
        };
    }
}