chenyongxing 8 år sedan
förälder
incheckning
33a2dd4885

+ 39 - 2
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/wx/MWxMenu.java

@ -6,21 +6,58 @@ import java.util.Date;
 * Created by Administrator on 2017/5/20 0020.
 */
public class MWxMenu {
    private Long id;
    private Long id;//主键id
    private String code;//业务code
    private String wechatCode;//关联的微信code 关联表 Wx_Wechat
    private String supMenucode;//父菜单id 如果是一级菜单 此字段为空
    private String type;//菜单类型 1 view 2click
    private String type;//菜单类型
    private String name;//菜单名称
    private String key;//click等点击类型必须
    private Integer sort;//菜单排序 父菜单排序 不包含子菜单那
    private String url;//url
    private String mediaId;//点用新增永久素材接口返回的合法media_id
    private String appid;//小程序的appid
    private String pagepath;//小程序的页面程序
    private String updateUser;//更新人
    private Date updateTime;//更新时间
    private Date createTime;//创建时间
    private String createUser;//创建人
    private String remark;//备注
    private Integer status; //状态 -1 已删除 0可用
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getMediaId() {
        return mediaId;
    }
    public void setMediaId(String mediaId) {
        this.mediaId = mediaId;
    }
    public String getAppid() {
        return appid;
    }
    public void setAppid(String appid) {
        this.appid = appid;
    }
    public String getPagepath() {
        return pagepath;
    }
    public void setPagepath(String pagepath) {
        this.pagepath = pagepath;
    }
    public Long getId() {
        return id;
    }

+ 0 - 9
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/wx/MWxWechat.java

@ -10,7 +10,6 @@ public class MWxWechat {
    private Long id;
    private String code;//业务code
    private String saasId;//'saas配置id'
    private String weichatId;//微信的id
    private String name;//名称
    private String status;//'类型 -1 已删除 0待审核 1审核通过 2 审核不通过'
    private String type;//'1:服务号 2 订阅号
@ -49,14 +48,6 @@ public class MWxWechat {
        this.saasId = saasId;
    }
    public String getWeichatId() {
        return weichatId;
    }
    public void setWeichatId(String weichatId) {
        this.weichatId = weichatId;
    }
    public String getName() {
        return name;
    }

+ 18 - 0
svr-lib-parent-pom/pom.xml

@ -54,6 +54,8 @@
        <version.swagger-ui>2.4.0</version.swagger-ui>
        <version.quartz>2.3.0</version.quartz>
        <version.logback>1.2.3</version.logback>
        <version.json>20160212</version.json>
        <version.springside>4.2.3-GA</version.springside>
    </properties>
    <!--dependencyManagement作用子配置不写版本默认继承父配置-->
    <dependencyManagement>
@ -122,6 +124,7 @@
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
                <version>${version.springCloud}</version>
            </dependency>
            <!--hystrix仪表盘-->
            <dependency>
@ -400,6 +403,21 @@
                <version>${version.logback}</version>
            </dependency>
            <!--log end-->
            <!-- json start-->
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>${version.json}</version>
            </dependency>
            <!--json end -->
            <!-- springside-core start-->
            <dependency>
                <groupId>org.springside</groupId>
                <artifactId>springside-core</artifactId>
                <version>${version.springside}</version>
            </dependency>
            <!-- springside-core end-->
        </dependencies>
    </dependencyManagement>

+ 8 - 0
svr/svr-base/pom.xml

@ -108,5 +108,13 @@
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springside</groupId>
            <artifactId>springside-core</artifactId>
        </dependency>
    </dependencies>
</project>

+ 115 - 0
svr/svr-base/src/main/java/com/yihu/jw/util/HttpUtil.java

@ -0,0 +1,115 @@
package com.yihu.jw.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/*
 * MD5 算法
 */
public class HttpUtil {
	private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
	/**
	 * 向指定URL发送GET方法的请求
	 *
	 * @param url
	 *            发送请求的URL
	 * @param param
	 *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
	 * @return URL 所代表远程资源的响应结果
	 */
	public static String sendGet(String url, String param) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url + "?" + param;
			URL realUrl = new URL(urlNameString);
			// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立实际的连接
			connection.connect();
			// 定义 BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return result;
	}
	/**
	 * 向指定 URL 发送POST方法的请求
	 *
	 * @param url
	 *            发送请求的 URL带上参数
	 * @param param
	 *            POST参数。
	 * @return 所代表远程资源的响应结果
	 */
	public static String sendPost(String url, String param) {
		StringBuffer buffer = new StringBuffer();
		PrintWriter out = null;
		BufferedReader in = null;
		HttpURLConnection conn = null;
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			conn = (HttpURLConnection) realUrl.openConnection();
			conn.setRequestMethod("POST");
			conn.setConnectTimeout(5000);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestProperty("Content-Type", "application/text");
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
			osw.write(param.toString());
			osw.flush();
			// 读取返回内容
			BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
			String temp;
			while ((temp = br.readLine()) != null) {
				buffer.append(temp);
				buffer.append("\n");
			}
		} catch (Exception e) {
			logger.error("push message error:", e);
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return buffer.toString();
	}
}

+ 38 - 1
svr/svr-base/src/main/java/com/yihu/jw/wx/controller/WxMenuController.java

@ -11,12 +11,17 @@ import com.yihu.jw.wx.service.WxMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@ -32,7 +37,7 @@ public class WxMenuController extends EnvelopRestController {
    private WxMenuService wxMenuService;
    @PostMapping(value = WxContants.WxMenu.api_create, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建微信菜单", notes = "创建微信菜单")
    @ApiOperation(value = "添加微信菜单", notes = "添加微信菜单")
    public Envelop createWxMenu(
            @ApiParam(name = "json_data", value = "", defaultValue = "")
            @RequestBody String jsonData) {
@ -128,4 +133,36 @@ public class WxMenuController extends EnvelopRestController {
        return Envelop.getSuccessList(WxContants.WxMenu.message_success_find_functions,mWxMenus);
    }
    /**
     * 创建微信公众号菜单
     *
     * @return
     */
    @ApiOperation(value = "创建微信公众号菜单", notes = "创建微信公众号菜单")
    @RequestMapping(value = "/menu/create")
    @ResponseBody
    public String createWechatMenu(
            @ApiParam(name = "json_data", value = "", defaultValue = "")
            @RequestBody String wechatCode){
        try{
            String params ="";
            wxMenuService.createWechatMenu(wechatCode);
            //String url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + getAccessToken();
            //
            //// 请求微信接口创建菜单
            //String jsonStr = HttpUtil.sendPost(url, params);
            //JSONObject result = new JSONObject(jsonStr);
            //if(result != null && result.get("errcode").toString().equals("0") && result.getString("errmsg").equals("ok")){
            //    return write(200,"创建成功!","data",jsonStr);
            //}else{
            //    return write(-1,"创建失败!","data",jsonStr);
            //}
            return null;
        }catch (Exception e){
            //return error(-1,"创建失败");
            return null;
        }
    }
}

+ 2 - 0
svr/svr-base/src/main/java/com/yihu/jw/wx/controller/WxTemplateController.java

@ -125,4 +125,6 @@ public class WxTemplateController extends EnvelopRestController {
        List<MWxTemplate> mMWxTemplates = convertToModels(list, new ArrayList<>(list.size()), MWxTemplate.class, fields);
        return Envelop.getSuccessList(WxContants.WxTemplate.message_success_find_functions,mMWxTemplates);
    }
    //微信模版消息测试
}

+ 5 - 0
svr/svr-base/src/main/java/com/yihu/jw/wx/dao/WxMenuDao.java

@ -5,6 +5,8 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
 * Created by Administrator on 2017/5/19 0019.
 */
@ -12,4 +14,7 @@ public interface WxMenuDao  extends PagingAndSortingRepository<WxMenu, Long>, Jp
    @Query("from WxMenu m where m.code = ?1")
    WxMenu findByCode(String code);
    @Query("from WxMenu m where m.wechatCode =?1 and m.status = 1 order  by m.supMenucode ,m.sort")
    List<WxMenu> findByWechatCode(String wechatCode);
}

+ 4 - 4
svr/svr-base/src/main/java/com/yihu/jw/wx/model/WxAccessToken.java

@ -18,14 +18,14 @@ public class WxAccessToken extends IdEntity implements java.io.Serializable {
	private String wechatCode;//关联的微信code 关联表 Wx_Wechat
	private String accessToken;//调用微信返回的accesstoken
	private long addTimestamp;//创建时间
	private Integer expiresIn;//凭证有效时间(秒)
	private long expiresIn;//凭证有效时间(秒)
	private Date czrq;//操作时间
	/** default constructor */
	public WxAccessToken() {
	}
	/** minimal constructor */
	public WxAccessToken(Long id, String accessToken, long addTimestamp,
			Integer expiresIn, Date czrq) {
			long expiresIn, Date czrq) {
		this.id = id;
		this.accessToken = accessToken;
		this.addTimestamp = addTimestamp;
@ -81,11 +81,11 @@ public class WxAccessToken extends IdEntity implements java.io.Serializable {
	}
	@Column(name = "expires_in", nullable = false)
	public Integer getExpiresIn() {
	public Long getExpiresIn() {
		return this.expiresIn;
	}
	public void setExpiresIn(Integer expiresIn) {
	public void setExpiresIn(Long expiresIn) {
		this.expiresIn = expiresIn;
	}

+ 1 - 1
svr/svr-base/src/main/java/com/yihu/jw/wx/model/WxGraphicMessage.java

@ -24,7 +24,7 @@ public class WxGraphicMessage extends IdEntity implements java.io.Serializable {
    private String updateUserName;//修改人名称
    private Date updateTime;//修改时间
    private String remark;
    private Integer status; //状态 -1 已删除 0可用
    private Integer status;  //状态 -1删除 0 冻结 1可用
    public WxGraphicMessage(String code, String wechatCode, String name, String value, String keyword, String createUser, String createUserName, Date createTime, String updateUser, String updateUserName, Date updateTime, String remark, Integer status) {
        this.code = code;

+ 45 - 4
svr/svr-base/src/main/java/com/yihu/jw/wx/model/WxMenu.java

@ -16,26 +16,34 @@ public class WxMenu extends IdEntity implements java.io.Serializable {
    private String code;//业务code
    private String wechatCode;//关联的微信code 关联表 Wx_Wechat
    private String supMenucode;//父菜单id 如果是一级菜单 此字段为空
    private String type;//菜单类型 1 view 2click
    private String type;//菜单类型
    private String name;//菜单名称
    private String key;//click等点击类型必须
    private Integer sort;//菜单排序 父菜单排序 不包含子菜单那
    private String url;//url
    private String mediaId;//点用新增永久素材接口返回的合法media_id
    private String appid;//小程序的appid
    private String pagepath;//小程序的页面程序
    private String updateUser;//更新人
    private Date updateTime;//更新时间
    private Date createTime;//创建时间
    private String createUser;//创建人
    private String remark;//备注
    private Integer status; //状态 -1 已删除 0可用
    private Integer status; //状态 -1删除 0 冻结 1可用
    public WxMenu(Long id, String code, String wechatCode, String supMenucode, String type, String name, Integer sort, String url, String updateUser, Date updateTime, Date createTime, String createUser, String remark, Integer status) {
        this.id = id;
    public WxMenu(String code, String wechatCode, String supMenucode, String type, String name, String key, Integer sort, String url, String mediaId, String appid, String pagepath, String updateUser, Date updateTime, Date createTime, String createUser, String remark, Integer status) {
        this.code = code;
        this.wechatCode = wechatCode;
        this.supMenucode = supMenucode;
        this.type = type;
        this.name = name;
        this.key = key;
        this.sort = sort;
        this.url = url;
        this.mediaId = mediaId;
        this.appid = appid;
        this.pagepath = pagepath;
        this.updateUser = updateUser;
        this.updateTime = updateTime;
        this.createTime = createTime;
@ -44,6 +52,39 @@ public class WxMenu extends IdEntity implements java.io.Serializable {
        this.status = status;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getMediaId() {
        return mediaId;
    }
    @Column(name = "media_id", length = 200)
    public void setMediaId(String mediaId) {
        this.mediaId = mediaId;
    }
    public String getAppid() {
        return appid;
    }
    public void setAppid(String appid) {
        this.appid = appid;
    }
    public String getPagepath() {
        return pagepath;
    }
    public void setPagepath(String pagepath) {
        this.pagepath = pagepath;
    }
    /**
     * default constructor
     */

+ 1 - 1
svr/svr-base/src/main/java/com/yihu/jw/wx/model/WxTemplate.java

@ -26,7 +26,7 @@ public class WxTemplate extends IdEntity implements java.io.Serializable {
    private String updateUserName;//修改人名称
    private Date updateTime;//修改时间
    private String remark;
    private Integer status; //状态 -1 已删除 0可用
    private Integer status;  //状态 -1删除 0 冻结 1可用
    public WxTemplate(Long id, String code, String name, String wechatCode, String templateCode, String value, String createUser, String createUserName, Date createTime, String updateUser, String updateUserName, Date updateTime, String remark, Integer status) {
        this.id = id;

+ 1 - 12
svr/svr-base/src/main/java/com/yihu/jw/wx/model/WxWechat.java

@ -15,7 +15,6 @@ public class WxWechat extends IdEntity implements java.io.Serializable {
    // Fields
    private String code;//业务code
    private String saasId;//'saas配置id'
    private String weichatId;//微信的id
    private String name;//名称
    private String status;//'类型 -1 已删除 0待审核 1审核通过 2 审核不通过'
    private String type;//'1:服务号 2 订阅号
@ -30,10 +29,9 @@ public class WxWechat extends IdEntity implements java.io.Serializable {
    private Date updateTime;//'修改时间'
    private String remark;//'备注'
    public WxWechat(String code, String saasId, String weichatId, String name, String status, String type, String appId, String appSecret, String baseUrl, String createUser, String createUserName, Date createTime, String updateUser, String updateUserName, Date updateTime, String remark) {
    public WxWechat(String code, String saasId, String name, String status, String type, String appId, String appSecret, String baseUrl, String createUser, String createUserName, Date createTime, String updateUser, String updateUserName, Date updateTime, String remark) {
        this.code = code;
        this.saasId = saasId;
        this.weichatId = weichatId;
        this.name = name;
        this.status = status;
        this.type = type;
@ -74,15 +72,6 @@ public class WxWechat extends IdEntity implements java.io.Serializable {
        this.saasId = saasId;
    }
    @Column(name = "weichat_id", length = 50)
    public String getWeichatId() {
        return this.weichatId;
    }
    public void setWeichatId(String weichatId) {
        this.weichatId = weichatId;
    }
    @Column(name = "name", length = 200)
    public String getName() {
        return this.name;

+ 0 - 3
svr/svr-base/src/main/java/com/yihu/jw/wx/service/WechatService.java

@ -50,9 +50,6 @@ public class WechatService extends BaseJpaService<WxWechat, WechatDao> {
        if (StringUtils.isEmpty(wechat.getAppSecret())) {
            throw new ApiException(WxContants.Wechat.message_fail_appSecret_is_null, CommonContants.common_error_params_code);
        }
        if (StringUtils.isEmpty(wechat.getCode())) {
            throw new ApiException(WxContants.Wechat.message_fail_code_is_null, CommonContants.common_error_params_code);
        }
        WxWechat wechatTem = wechatDao.findByAppIdExcludeCode(wechat.getAppId(),wechat.getCode());
        if(wechatTem!=null){
            throw new ApiException(WxContants.Wechat.message_fail_appId_exist, CommonContants.common_error_params_code);

+ 50 - 4
svr/svr-base/src/main/java/com/yihu/jw/wx/service/WxAccessTokenService.java

@ -4,12 +4,17 @@ import com.yihu.jw.mysql.query.BaseJpaService;
import com.yihu.jw.restmodel.common.CommonContants;
import com.yihu.jw.restmodel.exception.ApiException;
import com.yihu.jw.restmodel.wx.WxContants;
import com.yihu.jw.util.HttpUtil;
import com.yihu.jw.wx.dao.WechatDao;
import com.yihu.jw.wx.dao.WxAccessTokenDao;
import com.yihu.jw.wx.model.WxAccessToken;
import com.yihu.jw.wx.model.WxWechat;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springside.modules.utils.Clock;
import java.util.List;
@ -22,17 +27,58 @@ public class WxAccessTokenService extends BaseJpaService<WxAccessToken, WxAccess
    @Autowired
    private WxAccessTokenDao wxAccessTokenDao;
    @Autowired
    private WechatDao wechatDao;
    /**
     * 根据wechatCode查找最新一条
     * @param wechatCode
     * @return
     */
    public WxAccessToken getWxAccessTokenByCode(String wechatCode) {
        List<WxAccessToken> wxAccessTokens =  wxAccessTokenDao.getWxAccessTokenByCode(wechatCode);
        if(wxAccessTokens!=null&&wxAccessTokens.size()>0){
            return wxAccessTokens.get(0);
        try {
            List<WxAccessToken> wxAccessTokens =  wxAccessTokenDao.getWxAccessTokenByCode(wechatCode);
            if(wxAccessTokens!=null&&wxAccessTokens.size()>0){
                for (WxAccessToken accessToken : wxAccessTokens) {
                    if ((System.currentTimeMillis() - accessToken.getAddTimestamp()) < (accessToken.getExpiresIn() * 1000)) {
                        return accessToken;
                    } else {
                        wxAccessTokenDao.delete(accessToken);
                        break;
                    }
                }
                String token_url = "https://api.weixin.qq.com/cgi-bin/token";
                String appId="";
                String appSecret="";
                //根据wechatCode查找出appid和appSecret
                WxWechat wxWechat = wechatDao.findByCode(wechatCode);
                if(wxWechat==null){
                    return null;
                }
                appId = wxWechat.getAppId();
                appSecret = wxWechat.getAppSecret();
                String params = "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
                String result = HttpUtil.sendGet(token_url, params);
                JSONObject json = new JSONObject(result);
                if (json.has("access_token")) {
                    String token = json.get("access_token").toString();
                    String expires_in = json.get("expires_in").toString();
                    WxAccessToken newaccessToken = new WxAccessToken();
                    newaccessToken.setAccessToken(token);
                    newaccessToken.setExpiresIn(Long.parseLong(expires_in));
                    newaccessToken.setAddTimestamp(System.currentTimeMillis());
                    newaccessToken.setCzrq(new Clock.DefaultClock().getCurrentDate());
                    wxAccessTokenDao.save(newaccessToken);
                    return newaccessToken;
                } else {
                    return null;
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }
    @Transactional

+ 18 - 0
svr/svr-base/src/main/java/com/yihu/jw/wx/service/WxMenuService.java

@ -11,6 +11,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.persistence.Transient;
import java.util.List;
/**
 * Created by Administrator on 2017/5/19 0019.
@ -58,4 +59,21 @@ public class WxMenuService extends BaseJpaService<WxMenu, WxMenuDao> {
    public WxMenu findByCode(String code) {
        return wxMenuDao.findByCode(code);
    }
    public List<WxMenu> findByWechatCode(String wechatCode){
        return wxMenuDao.findByWechatCode(wechatCode);
    }
    public void createWechatMenu(String wechatCode) {
        //首先根据wechatCode获取菜单,然后封装成json字符串
        List<WxMenu> menus = wxMenuDao.findByWechatCode(wechatCode);
    }
    public String getMenu(List<WxMenu> menus){
        if(menus!=null){
            for(WxMenu wxMenu:menus)
        }
    }
}