trick9191 6 лет назад
Родитель
Сommit
c1d6b8afbf

+ 18 - 0
common/common-entity/src/main/java/com/yihu/jw/base/wx/WxGraphicSceneGroupDO.java

@ -0,0 +1,18 @@
package com.yihu.jw.base.wx;
import com.yihu.jw.IdEntity;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
 * Created by Trick on 2018/8/15.
 */
@Entity
@Table(name = "wx_graphic_scene_group")
public class WxGraphicSceneGroupDO extends IdEntity implements java.io.Serializable{
    private String wechat_id;//微信Id
    private String scene;//场景值(根据场景值组装图文消息)
    private String graphic_id;//场景关联微信图文素材id
}

+ 46 - 0
common/common-entity/src/main/java/com/yihu/jw/base/wx/WxMenuJsonDO.java

@ -0,0 +1,46 @@
package com.yihu.jw.base.wx;
import com.yihu.jw.IdEntityWithOperation;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
 * Created by Trick on 2018/8/13.
 */
@Entity
@Table(name = "wx_menu_json")
public class WxMenuJsonDO extends IdEntityWithOperation implements java.io.Serializable{
    private String wechatId;//微信Id
    private String content;//'微信模板内容',
    private Integer status;//-1删除 0 冻结 1可用',
    @Column(name = "wechat_id")
    public String getWechatId() {
        return wechatId;
    }
    public void setWechatId(String wechatId) {
        this.wechatId = wechatId;
    }
    @Column(name = "content")
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    @Column(name = "status")
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
}

+ 86 - 0
common/common-entity/src/main/java/com/yihu/jw/base/wx/WxReplySceneDO.java

@ -0,0 +1,86 @@
package com.yihu.jw.base.wx;
import com.yihu.jw.IdEntityWithOperation;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
 * Created by Trick on 2018/8/15.
 */
@Entity
@Table(name = "wx_reply_scene")
public class WxReplySceneDO extends IdEntityWithOperation implements java.io.Serializable{
    private String wechatId;//微信id(唯一)
    private String appOriginId;//原始ID(唯一)
    private String msgType;//微信消息类型:text,event,image,voice,video,shortvideo,location,link
    private String event;//微信事件类型:SCAN,LOCATION,CLICK,subscribe,unsubscribe
    private String scene;//图文消息/自定义消息,分组场景
    private String content;//居民回复内容,消息字段
    private String status;//状态(-1删除 0 冻结 1可用
    @Column(name = "wechat_id")
    public String getWechatId() {
        return wechatId;
    }
    public void setWechatId(String wechatId) {
        this.wechatId = wechatId;
    }
    @Column(name = "app_origin_id")
    public String getAppOriginId() {
        return appOriginId;
    }
    public void setAppOriginId(String appOriginId) {
        this.appOriginId = appOriginId;
    }
    @Column(name = "msg_type")
    public String getMsgType() {
        return msgType;
    }
    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }
    @Column(name = "event")
    public String getEvent() {
        return event;
    }
    public void setEvent(String event) {
        this.event = event;
    }
    @Column(name = "scene")
    public String getScene() {
        return scene;
    }
    public void setScene(String scene) {
        this.scene = scene;
    }
    @Column(name = "content")
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    @Column(name = "status")
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}

+ 47 - 59
svr/svr-base/src/main/java/com/yihu/jw/business/wx/controller/WxMenuController.java

@ -1,58 +1,46 @@
//package com.yihu.jw.business.wx.controller;
//
//import com.yihu.jw.base.wx.WxMenuDO;
//import com.yihu.jw.base.wx.WxWechatDO;
//import com.yihu.jw.business.wx.WechatResponse;
//import com.yihu.jw.business.wx.service.WechatService;
//import com.yihu.jw.business.wx.service.WxMenuService;
//import com.yihu.jw.exception.ApiException;
//import com.yihu.jw.restmodel.common.Envelop;
//import com.yihu.jw.restmodel.common.EnvelopRestController;
//import com.yihu.jw.restmodel.base.wx.WxMenuVO;
//import com.yihu.jw.restmodel.base.wx.WxWechatVO;
//import com.yihu.jw.rm.base.WechatRequestMapping;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//import org.apache.commons.lang.StringUtils;
//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.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
///**
// * Created by Administrator on 2017/5/19 0019.
// */
//@RestController
//@RequestMapping(WechatRequestMapping.api_common)
//@Api(value = "微信菜单相关操作", description = "微信菜单相关操作")
//public class WxMenuController extends EnvelopRestController {
//
//    @Autowired
//    private WxMenuService wxMenuService;
//
//    @Autowired
//    private WechatService wechatService;
//
//    @PostMapping(value = WechatRequestMapping.WxMenu.api_create, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ApiOperation(value = "添加微信菜单", notes = "添加微信菜单")
//    public Envelop createWxMenu(
//            @ApiParam(name = "json_data", value = "", defaultValue = "")
//            @RequestBody String jsonData) {
//        try {
//            WxMenuDO wxMenu = toEntity(jsonData, WxMenuDO.class);
//            return Envelop.getSuccess(WechatRequestMapping.WxMenu.message_success_create, wxMenuService.createWxMenu(wxMenu));
//        } catch (ApiException e) {
//            return Envelop.getError(e.getMessage(), e.getErrorCode());
//        }
//    }
//
package com.yihu.jw.business.wx.controller;
import com.yihu.jw.business.wx.service.WechatService;
import com.yihu.jw.business.wx.service.WxMenuService;
import com.yihu.jw.exception.ApiException;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.restmodel.common.EnvelopRestController;
import com.yihu.jw.rm.base.WechatRequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by Administrator on 2017/5/19 0019.
 */
@RestController
@RequestMapping(WechatRequestMapping.api_common)
@Api(value = "微信菜单相关操作", description = "微信菜单相关操作")
public class WxMenuController extends EnvelopRestController {
    @Autowired
    private WxMenuService wxMenuService;
    @Autowired
    private WechatService wechatService;
    @PostMapping(value = WechatRequestMapping.WxMenu.api_create, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "添加微信菜单", notes = "添加微信菜单")
    public Envelop createWxMenu(@ApiParam(name = "wxId", value = "微信id")
                                    @RequestParam(value = "wxId", required = false)String wxId) {
        try {
            return wxMenuService.createWxMenu(wxId);
        } catch (ApiException e) {
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
//    @PutMapping(value = WechatRequestMapping.WxMenu.api_update, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ApiOperation(value = "修改微信菜单", notes = "修改微信菜单")
//    public Envelop updateWxMenu(
@ -65,8 +53,8 @@
//            return Envelop.getError(e.getMessage(), e.getErrorCode());
//        }
//    }
//
//
//    @DeleteMapping(value = WechatRequestMapping.WxMenu.api_delete)
//    @ApiOperation(value = "删除微信菜单", notes = "删除微信菜单")
//    public Envelop deleteWxMenu(
@ -232,5 +220,5 @@
//            return Envelop.getError(e.getMessage(), e.getErrorCode());
//        }
//    }
//
//}
}

+ 26 - 27
svr/svr-base/src/main/java/com/yihu/jw/business/wx/dao/WechatDao.java

@ -1,27 +1,26 @@
//package com.yihu.jw.business.wx.dao;
//
//import com.yihu.jw.base.wx.WxWechatDO;
//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/20 0020.
// */
//public interface WechatDao extends PagingAndSortingRepository<WxWechatDO, String>, JpaSpecificationExecutor<WxWechatDO> {
//
//    @Query("from WxWechatDO w where w.appId = ?1 and w.status!=-1")
//    WxWechatDO findByAppId(String appId);
//
//
//    @Query("from WxWechatDO w where w.appId = ?1 and w.id!= ?2 and w.status!=-1")
//    WxWechatDO findByAppIdExcludeId(String appId, String id);
//
//    @Query("from WxWechatDO w where w.id = ?1 and w.status!=-1")
//    WxWechatDO findById(String id);
//
//    @Query("from WxWechatDO w where w.status!=-1")
//    List<WxWechatDO> findAll();
//}
package com.yihu.jw.business.wx.dao;
import com.yihu.jw.base.wx.WxWechatDO;
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/20 0020.
 */
public interface WechatDao extends PagingAndSortingRepository<WxWechatDO, String>, JpaSpecificationExecutor<WxWechatDO> {
    @Query("from WxWechatDO w where w.appId = ?1 and w.status!=-1")
    WxWechatDO findByAppId(String appId);
    @Query("from WxWechatDO w where w.appId = ?1 and w.id!= ?2 and w.status!=-1")
    WxWechatDO findByAppIdExcludeId(String appId, String id);
    @Query("from WxWechatDO w where w.id = ?1 and w.status!=-1")
    WxWechatDO findById(String id);
    @Query("from WxWechatDO w where w.status!=-1")
    List<WxWechatDO> findAll();
}

+ 17 - 17
svr/svr-base/src/main/java/com/yihu/jw/business/wx/dao/WxAccessTokenDao.java

@ -1,17 +1,17 @@
//package com.yihu.jw.business.wx.dao;
//
//import com.yihu.jw.base.wx.WxAccessTokenDO;
//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/18 0018.
// */
//public interface WxAccessTokenDao extends PagingAndSortingRepository<WxAccessTokenDO, String>, JpaSpecificationExecutor<WxAccessTokenDO> {
//
//    @Query("from WxAccessTokenDO w where w.wechatId =?1 order by w.addTimestamp desc")
//    List<WxAccessTokenDO> getWxAccessTokenById(String wechatId);
//}
package com.yihu.jw.business.wx.dao;
import com.yihu.jw.base.wx.WxAccessTokenDO;
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/18 0018.
 */
public interface WxAccessTokenDao extends PagingAndSortingRepository<WxAccessTokenDO, String>, JpaSpecificationExecutor<WxAccessTokenDO> {
    @Query("from WxAccessTokenDO w where w.wechatId =?1 order by w.addTimestamp desc")
    List<WxAccessTokenDO> getWxAccessTokenById(String wechatId);
}

+ 20 - 47
svr/svr-base/src/main/java/com/yihu/jw/business/wx/dao/WxMenuDao.java

@ -1,47 +1,20 @@
//package com.yihu.jw.business.wx.dao;
//
//import com.yihu.jw.base.wx.WxMenuDO;
//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.
// */
//public interface WxMenuDao  extends PagingAndSortingRepository<WxMenuDO, String>, JpaSpecificationExecutor<WxMenuDO> {
//
//
//    @Query("from WxMenuDO m where m.id = ?1 and m.status = 1")
//    WxMenuDO findById(String id);
//
//    @Query("from WxMenuDO m where m.wechatId =?1 and m.status = 1 order  by m.supMenuid ,m.sort")
//    List<WxMenuDO> findByWechatId(String wechatId);
//
//    @Query("from WxMenuDO m where m.wechatId = ?1 and  m.supMenuid= ?2 and m.status =1 order by m.sort")
//    List<WxMenuDO> findChildMenus(String wechatId, String supMenuid);
//
//    @Query("from WxMenuDO m where  m.supMenuid= ?1 and m.status =1 order by m.sort")
//    List<WxMenuDO> findChildMenus( String supMenuid);
//
//    /**
//     * 根据wechatId判断父菜单的sort是否重复
//     * @param wechatId
//     * @param sort
//     */
//    @Query("from WxMenuDO m where m.wechatId = ?1 and m.status =1 and m.sort =?2 and m.supMenuid ='0' and m.id != ?3")
//    WxMenuDO findByWechatIdExcludeSortFromParent(String wechatId, Integer sort,String id);
//
//    /**
//     * 根据wechatId,supMenucode判断子菜单的sort是否重复
//     * @param wechatId
//     * @param sort
//     */
//    @Query("from WxMenuDO m where m.wechatId = ?1 and m.status =1 and m.sort =?2 and m.supMenuid =?3 and m.id != ?4")
//    WxMenuDO findByWechatIdExcludeSortFromChild(String wechatId, Integer sort,String supMenuid,String id);
//
//    @Query("from WxMenuDO m where m.wechatId =?1 and m.status = 1 and  m.supMenuid = '0' order by m.supMenuid ,m.sort")
//    List<WxMenuDO> findParentMenuByWechatId(String wechatId);
//
//}
package com.yihu.jw.business.wx.dao;
import com.yihu.jw.base.wx.WxMenuDO;
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.
 */
public interface WxMenuDao  extends PagingAndSortingRepository<WxMenuDO, String>, JpaSpecificationExecutor<WxMenuDO> {
    @Query("FROM WxMenuDO w WHERE w.wechatId =?1 AND w.status ='1' AND w.supMenuid IS NULL ORDER BY w.sort ASC")
    List<WxMenuDO> findParentRootByWechatId(String wechatId);
    @Query("FROM WxMenuDO w WHERE w.wechatId =?1 AND w.status ='1' AND w.supMenuid IS NOT NULL ORDER BY w.sort ASC")
    List<WxMenuDO> findChilrenByWechatId(String wechatId);
}

+ 13 - 0
svr/svr-base/src/main/java/com/yihu/jw/business/wx/dao/WxMenuJsonDao.java

@ -0,0 +1,13 @@
package com.yihu.jw.business.wx.dao;
import com.yihu.jw.base.wx.WxMenuJsonDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
 * Created by Trick on 2018/8/13.
 */
public interface WxMenuJsonDao extends PagingAndSortingRepository<WxMenuJsonDO, String>, JpaSpecificationExecutor<WxMenuJsonDO> {
    WxMenuJsonDO findByWechatIdAndStatus(String wechatId,Integer status);
}

+ 13 - 114
svr/svr-base/src/main/java/com/yihu/jw/business/wx/service/WechatService.java

@ -1,114 +1,13 @@
//package com.yihu.jw.business.wx.service;
//
//import com.yihu.jw.base.wx.WxWechatDO;
//import com.yihu.jw.business.wx.dao.WechatDao;
//import com.yihu.jw.exception.ApiException;
//import com.yihu.jw.exception.code.ExceptionCode;
//import com.yihu.base.mysql.query.BaseJpaService;
//import com.yihu.jw.rm.base.WechatRequestMapping;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import javax.persistence.Transient;
//import java.util.*;
//
///**
// * Created by Administrator on 2017/5/20 0020.
// */
//@Service
//public class WechatService extends BaseJpaService<WxWechatDO, WechatDao> {
//
//    @Autowired
//    private WechatDao wechatDao;
//
//    @Transient
//    public WxWechatDO createWechat(WxWechatDO wechat) {
//        String code = UUID.randomUUID().toString().replaceAll("-", "");
//        wechat.setId(code);
//        if (StringUtils.isEmpty(wechat.getSaasId())) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_saasId_is_null, ExceptionCode.common_error_params_code);
//        }
//        if (StringUtils.isEmpty(wechat.getStatus())) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_status_is_null, ExceptionCode.common_error_params_code);
//        }
//        if (StringUtils.isEmpty(wechat.getType())) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_type_is_null, ExceptionCode.common_error_params_code);
//        }
//        if (StringUtils.isEmpty(wechat.getName())) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_name_is_null, ExceptionCode.common_error_params_code);
//        }
//        WxWechatDO wechatTem = wechatDao.findByAppId(wechat.getAppId());
//        if (wechatTem != null) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appId_exist, ExceptionCode.common_error_params_code);
//        }
//        return wechatDao.save(wechat);
//    }
//
//    @Transient
//    public WxWechatDO updateWxchat(WxWechatDO wechat) {
//        if (StringUtils.isEmpty(wechat.getSaasId())) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_saasId_is_null, ExceptionCode.common_error_params_code);
//        }
//        if (StringUtils.isEmpty(wechat.getStatus())) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_status_is_null, ExceptionCode.common_error_params_code);
//        }
//        if (StringUtils.isEmpty(wechat.getType())) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_type_is_null, ExceptionCode.common_error_params_code);
//        }
//        if (StringUtils.isEmpty(wechat.getName())) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_name_is_null, ExceptionCode.common_error_params_code);
//        }
//        WxWechatDO wechat1 = findById(wechat.getId());
//        if (wechat1 == null) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_wxWechat_is_no_exist, ExceptionCode.common_error_params_code);
//        }
//        WxWechatDO wechatTem = wechatDao.findByAppIdExcludeId(wechat.getAppId(), wechat.getId());
//        if (wechatTem != null) {
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appId_exist, ExceptionCode.common_error_params_code);
//        }
//        return wechatDao.save(wechat);
//    }
//
//
//    public WxWechatDO findById(String id) {
//        return wechatDao.findById(id);
//    }
//
//    @Transient
//    public void deleteWechat(String ids, String userId, String userName) {
//        if (!StringUtils.isEmpty(ids)) {
//            String[] codeArray = ids.split(",");
//            for (String code : codeArray) {
//                WxWechatDO wxWechat = wechatDao.findById(code);
//                if (wxWechat == null) {
//                    throw new ApiException(WechatRequestMapping.WxConfig.message_fail_id_no_exist, ExceptionCode.common_error_params_code);
//                }
//                wxWechat.setStatus(-1);
//                wxWechat.setUpdateUser(userId);
//                wxWechat.setUpdateUserName(userName);
//                wechatDao.save(wxWechat);
//            }
//        }
//    }
//
//    public List<WxWechatDO> findAll() {
//        return wechatDao.findAll();
//    }
//
//    /**
//     * key为code ,value为微信名字
//     *
//     * @return
//     */
//    public Map<String, String> getAllWechatConfig() {
//        List<WxWechatDO> wechats = findAll();
//        Map<String, String> map = new HashMap<>();
//        if (null != wechats) {
//            for (WxWechatDO wx : wechats) {
//                map.put(wx.getId(), wx.getName());
//            }
//        }
//        return map;
//    }
//}
package com.yihu.jw.business.wx.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * Created by Administrator on 2017/5/20 0020.
 */
@Service
@Transactional
public class WechatService {
}

+ 96 - 96
svr/svr-base/src/main/java/com/yihu/jw/business/wx/service/WxAccessTokenService.java

@ -1,96 +1,96 @@
//package com.yihu.jw.business.wx.service;
//
//import com.yihu.jw.base.wx.WxAccessTokenDO;
//import com.yihu.jw.base.wx.WxWechatDO;
//import com.yihu.jw.business.wx.dao.WechatDao;
//import com.yihu.jw.business.wx.dao.WxAccessTokenDao;
//import com.yihu.jw.exception.ApiException;
//import com.yihu.jw.exception.code.ExceptionCode;
//import com.yihu.base.mysql.query.BaseJpaService;
//import com.yihu.jw.rm.base.WechatRequestMapping;
//import com.yihu.jw.util.HttpUtil;
//import org.json.JSONObject;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import java.util.Date;
//import java.util.List;
//import java.util.UUID;
//
///**
// * Created by Administrator on 2017/5/18 0018.
// */
//@Service
//public class WxAccessTokenService extends BaseJpaService<WxAccessTokenDO, WxAccessTokenDao> {
//
//
//    private Logger logger= LoggerFactory.getLogger(WxAccessTokenService.class);
//
//    @Autowired
//    private WxAccessTokenDao wxAccessTokenDao;
//
//    @Autowired
//    private WechatDao wechatDao;
//
//    /**
//     * 根据wechatCode查找最新一条
//     * @param wechatId
//     * @return
//     */
//    public WxAccessTokenDO getWxAccessTokenById(String wechatId) {
//        try {
//            //根据wechatCode查找出appid和appSecret
//            WxWechatDO wxWechat = wechatDao.findById(wechatId);
//            if(wxWechat==null){
//                throw new ApiException(WechatRequestMapping.WxConfig.message_fail_wxWechat_is_no_exist, ExceptionCode.common_error_params_code);
//            }
//            List<WxAccessTokenDO> wxAccessTokens =  wxAccessTokenDao.getWxAccessTokenById(wechatId);
//            if(wxAccessTokens!=null&&wxAccessTokens.size()>0){
//                for (WxAccessTokenDO 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="";
//            appId = wxWechat.getAppId();
//            appSecret = wxWechat.getAppSecret();
//            if (StringUtils.isEmpty(appId)){
//                throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appId_is_null, ExceptionCode.common_error_params_code);
//            }
//            if (StringUtils.isEmpty(appSecret)){
//                throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appSecret_is_null, ExceptionCode.common_error_params_code);
//            }
//            String params = "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
//            String result = HttpUtil.sendGet(token_url, params);
//            logger.info("--------------微信返回结果:"+result+"---------------");
//            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();
//                WxAccessTokenDO newaccessToken = new WxAccessTokenDO();
//                newaccessToken.setAccessToken(token);
//                newaccessToken.setExpiresIn(Long.parseLong(expires_in));
//                newaccessToken.setAddTimestamp(System.currentTimeMillis());
//                newaccessToken.setCzrq(new Date());
//                newaccessToken.setCode(UUID.randomUUID().toString().replace("-",""));
//                newaccessToken.setWechatId(wechatId);
//                wxAccessTokenDao.save(newaccessToken);
//                return newaccessToken;
//            } else {
//                return null;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//}
package com.yihu.jw.business.wx.service;
import com.yihu.jw.base.wx.WxAccessTokenDO;
import com.yihu.jw.base.wx.WxWechatDO;
import com.yihu.jw.business.wx.dao.WechatDao;
import com.yihu.jw.business.wx.dao.WxAccessTokenDao;
import com.yihu.jw.exception.ApiException;
import com.yihu.jw.exception.code.ExceptionCode;
import com.yihu.jw.rm.base.WechatRequestMapping;
import com.yihu.jw.util.HttpUtil;
import com.yihu.mysql.query.BaseJpaService;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
 * Created by Administrator on 2017/5/18 0018.
 */
@Service
public class WxAccessTokenService extends BaseJpaService<WxAccessTokenDO, WxAccessTokenDao> {
    private Logger logger= LoggerFactory.getLogger(WxAccessTokenService.class);
    @Autowired
    private WxAccessTokenDao wxAccessTokenDao;
    @Autowired
    private WechatDao wechatDao;
    /**
     * 根据wechatCode查找最新一条
     * @param wechatId
     * @return
     */
    public WxAccessTokenDO getWxAccessTokenById(String wechatId) {
        try {
            //根据wechatCode查找出appid和appSecret
            WxWechatDO wxWechat = wechatDao.findById(wechatId);
            if(wxWechat==null){
                throw new ApiException(WechatRequestMapping.WxConfig.message_fail_wxWechat_is_no_exist, ExceptionCode.common_error_params_code);
            }
            List<WxAccessTokenDO> wxAccessTokens =  wxAccessTokenDao.getWxAccessTokenById(wechatId);
            if(wxAccessTokens!=null&&wxAccessTokens.size()>0){
                for (WxAccessTokenDO accessToken : wxAccessTokens) {
                    if ((System.currentTimeMillis() - accessToken.getAddTimestamp()) < (accessToken.getExpiresIn() * 500)) {
                        return accessToken;
                    } else {
                        wxAccessTokenDao.delete(accessToken);
                        break;
                    }
                }
            }
            String token_url = "https://api.weixin.qq.com/cgi-bin/token";
            String appId="";
            String appSecret="";
            appId = wxWechat.getAppId();
            appSecret = wxWechat.getAppSecret();
            if (StringUtils.isEmpty(appId)){
                throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appId_is_null, ExceptionCode.common_error_params_code);
            }
            if (StringUtils.isEmpty(appSecret)){
                throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appSecret_is_null, ExceptionCode.common_error_params_code);
            }
            String params = "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
            String result = HttpUtil.sendGet(token_url, params);
            logger.info("--------------微信返回结果:"+result+"---------------");
            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();
                WxAccessTokenDO newaccessToken = new WxAccessTokenDO();
                newaccessToken.setAccessToken(token);
                newaccessToken.setExpiresIn(Long.parseLong(expires_in));
                newaccessToken.setAddTimestamp(System.currentTimeMillis());
                newaccessToken.setCzrq(new Date());
                newaccessToken.setCode(UUID.randomUUID().toString().replace("-",""));
                newaccessToken.setWechatId(wechatId);
                wxAccessTokenDao.save(newaccessToken);
                return newaccessToken;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

+ 50 - 357
svr/svr-base/src/main/java/com/yihu/jw/business/wx/service/WxMenuService.java

@ -1,357 +1,50 @@
//package com.yihu.jw.business.wx.service;
//
//import com.yihu.jw.base.wx.WxAccessTokenDO;
//import com.yihu.jw.base.wx.WxMenuDO;
//import com.yihu.jw.base.wx.WxWechatDO;
//import com.yihu.jw.business.wx.dao.WxMenuDao;
//import com.yihu.jw.exception.ApiException;
//import com.yihu.jw.exception.code.ExceptionCode;
//import com.yihu.base.mysql.query.BaseJpaService;
//import com.yihu.jw.rm.base.WechatRequestMapping;
//import com.yihu.jw.util.HttpUtil;
//import org.json.JSONException;
//import org.json.JSONObject;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import javax.persistence.Transient;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.UUID;
//
///**
// * Created by Administrator on 2017/5/19 0019.
// */
//@Service
//public class WxMenuService extends BaseJpaService<WxMenuDO, WxMenuDao> {
//
//    private Logger logger= LoggerFactory.getLogger(WxMenuService.class);
//
//    @Autowired
//    private WxMenuDao wxMenuDao;
//
//    @Autowired
//    private WechatService wechatService;
//
//    @Autowired
//    private WxAccessTokenService wxAccessTokenService;
//
//    /**
//     * 将菜单保存至数据库
//     * @param wxMenu
//     * @return
//     */
//    public WxMenuDO createWxMenu(WxMenuDO wxMenu) {
//        String code = UUID.randomUUID().toString().replaceAll("-", "");
//        wxMenu.setId(code);
//        if(canSaveOrUpata(wxMenu)){
//            return wxMenuDao.save(wxMenu);
//        }
//        return null;
//    }
//
//    @Transient
//    public WxMenuDO updateWxMenu(WxMenuDO wxMenu) {
//        if (StringUtils.isEmpty(wxMenu.getId())) {
//            throw new ApiException(WechatRequestMapping.WxMenu.message_fail_id_is_null, ExceptionCode.common_error_params_code);
//        }
//        if(canSaveOrUpata(wxMenu)){
//            WxMenuDO wxMenu1 = findById(wxMenu.getId());
//            if(wxMenu1==null){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_WxMenu_is_no_exist, ExceptionCode.common_error_params_code);
//            }
//            return wxMenuDao.save(wxMenu);
//        }
//        return null;
//    }
//
//    @Transient
//    public void deleteWxMenu(String ids, String userId, String userName) {
//        if(!StringUtils.isEmpty(ids)) {
//            String[] codeArray = ids.split(",");
//            for (String code : codeArray) {
//                WxMenuDO wxMenu = wxMenuDao.findById(code);
//                if (wxMenu == null) {
//                    continue;
//                }
//                String supMenucode = wxMenu.getSupMenuid();
//                if (StringUtils.isEmpty(supMenucode)) {//如果是空,则为父菜单
//                    List<WxMenuDO> childMenus = findChildMenus(wxMenu.getWechatId(), wxMenu.getId());
//                    if (childMenus != null) {
//                        for (WxMenuDO wxmenu : childMenus) {
//                            wxmenu.setStatus(-1);
//                            wxmenu.setUpdateUser(userId);
//                            wxmenu.setUpdateUserName(userName);
//                            wxMenuDao.save(wxmenu);
//                        }
//                    }
//                }
//                wxMenu.setStatus(-1);
//                wxMenu.setUpdateUser(userId);
//                wxMenu.setUpdateUserName(userName);
//                wxMenuDao.save(wxMenu);
//            }
//        }
//    }
//
//    @Transient
//    public void delete(String codes,String userCode) {
//        if(!StringUtils.isEmpty(codes)) {
//            String[] codeArray = codes.split(",");
//            for (String code : codeArray) {
//                WxMenuDO wxMenu = wxMenuDao.findById(code);
//                if (wxMenu == null) {
//                    continue;
//                }
//                String supMenucode = wxMenu.getSupMenuid();
//                if (StringUtils.isEmpty(supMenucode)) {//如果是空,则为父菜单
//                    List<WxMenuDO> childMenus = findChildMenus(wxMenu.getWechatId(), wxMenu.getId());
//                    if (childMenus != null) {
//                        for (WxMenuDO wxmenu : childMenus) {
//                            wxmenu.setStatus(-1);
//                            wxmenu.setUpdateUser(userCode);
//                            wxMenuDao.save(wxmenu);
//                        }
//                    }
//                }
//                wxMenu.setStatus(-1);
//                wxMenu.setUpdateUser(userCode);
//                wxMenuDao.save(wxMenu);
//            }
//        }
//    }
//
//    public WxMenuDO findById(String id) {
//        return wxMenuDao.findById(id);
//    }
//    /**
//     * 根据wechatCode查找所有父菜单
//     * @param wechatCode
//     * @return
//     */
//    public List<WxMenuDO> findParentMenuByWechatCode(String wechatCode){
//        List<WxMenuDO> parentMenus = wxMenuDao.findParentMenuByWechatId(wechatCode);
//        return parentMenus;
//    }
//
//    /**
//     * 根据wechatCode在微信公众号创建菜单
//     * @param wechatId
//     * @return JSONObject
//     */
//    public JSONObject createWechatMenu(String wechatId) throws JSONException {
//        WxWechatDO wechat = wechatService.findById(wechatId);
//        if(wechat==null){
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_wxWechat_is_no_exist, ExceptionCode.common_error_params_code);
//        }
//        //首先根据wechatCode获取菜单,然后封装成json字符串
//        List<WxMenuDO> menus = wxMenuDao.findByWechatId(wechatId);
//        if(menus==null){
//            throw new ApiException(WechatRequestMapping.WxMenu.message_fail_WxMenu_is_no_exist, ExceptionCode.common_error_params_code);
//        }
//        String menuJsonString = getMenuToString(menus, wechatId);
//        logger.info("-----------------微信菜单json字符串:"+ menuJsonString+"--------------------");
//        WxAccessTokenDO wxAccessTokenByCode = wxAccessTokenService.getWxAccessTokenById(wechatId);
//        String token = wxAccessTokenByCode.getAccessToken();
//        // 请求微信接口创建菜单
//        String url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token;
//        String jsonStr = HttpUtil.sendPost(url, menuJsonString);
//        logger.info("------------------创建微信菜单,微信返回结果:"+jsonStr+"---------------------");
//        JSONObject result = new JSONObject(jsonStr);
//        return result;
//    }
//
//    private String getMenuToString(List<WxMenuDO> menus,String wechatCode){
//        String menuJsonStr = "";
//        List<WxMenuDO> parentMenus = new ArrayList<WxMenuDO>();//存储父菜单
//        if(menus!=null){
//            menuJsonStr = "{\"button\":[{";
//            for(WxMenuDO wxMenu:menus){
//                if(StringUtils.isEmpty(wxMenu.getSupMenuid())){//说明是父菜单
//                    parentMenus.add(wxMenu);
//                }
//            }
//        }
//        int j = 0;
//        for(WxMenuDO menu:parentMenus){//遍历父菜单
//            if(j==0){
//                j++;
//            }else{
//                menuJsonStr += ",{";
//            }
//            menuJsonStr = getString(menuJsonStr, menu);
//            //查找是否有子菜单
//            List<WxMenuDO> childMenus = findChildMenus(wechatCode, menu.getId());
//            if(childMenus!=null){
//                int i =0;
//                menuJsonStr += ",\"sub_button\":[{";
//                for(WxMenuDO childMenu:childMenus){
//                    if(i==0){
//                        i++;
//                    }else{
//                        menuJsonStr += ",{";
//                    }
//                    menuJsonStr = getString(menuJsonStr, childMenu);
//                    menuJsonStr += "}";
//                }
//                menuJsonStr += "]}";
//            }
//        }
//        menuJsonStr += "]}";
//        return menuJsonStr;
//    }
//
//    private String getString(String menuJsonStr, WxMenuDO menu) {
//        menuJsonStr += "\"name\":\""+ menu.getName()+"\"";
//        if(!StringUtils.isEmpty(menu.getType())){
//            menuJsonStr += ",\"type\":\"" + menu.getType()+"\"";
//        }
//        if(!StringUtils.isEmpty(menu.getMenuKey())){
//            menuJsonStr += ",\"key\":\"" + menu.getMenuKey()+"\"";
//        }
//        if(!StringUtils.isEmpty(menu.getUrl())){
//            menuJsonStr += ",\"url\":\"" + menu.getUrl()+"\"";
//        }
//        if(!StringUtils.isEmpty(menu.getMediaId())){
//            menuJsonStr += ",\"media_id\":\"" + menu.getMediaId()+"\"";
//        }
//        if(!StringUtils.isEmpty(menu.getAppid())){
//            menuJsonStr += ",\"appid\":\"" + menu.getAppid()+"\"";
//        }
//        if(!StringUtils.isEmpty(menu.getPagepath())){
//            menuJsonStr += ",\"pagepath\":\"" + menu.getPagepath()+"\"";
//        }
//        return menuJsonStr;
//    }
//
//    /**
//     * 查找子菜单
//     * @param wechatCode
//     * @param sup_menucode
//     * @return
//     */
//    public List<WxMenuDO> findChildMenus(String wechatCode,String sup_menucode ){
//        return wxMenuDao.findChildMenus(wechatCode,sup_menucode);
//    }
//
//    public List<WxMenuDO> findChildMenus(String parentCode ){
//        return wxMenuDao.findChildMenus(parentCode);
//    }
//
//    private boolean canSaveOrUpata(WxMenuDO wxMenu){
//        String wechatCode = wxMenu.getWechatId();
//        String supMenucode = wxMenu.getSupMenuid();
//        if(StringUtils.isEmpty(wechatCode)){
//            WxMenuDO parentMenuCode = findById(supMenucode);
//            wechatCode = parentMenuCode.getWechatId();
//            wxMenu.setWechatId(wechatCode);
//        }
//        String name = wxMenu.getName();
//        if (StringUtils.isEmpty(name)) {
//            throw new ApiException(WechatRequestMapping.WxMenu.message_fail_name_is_null, ExceptionCode.common_error_params_code);
//        }
//        if (StringUtils.isEmpty(wxMenu.getStatus())) {
//            throw new ApiException(WechatRequestMapping.WxMenu.message_fail_status_is_null, ExceptionCode.common_error_params_code);
//        }
//        if(StringUtils.isEmpty(wxMenu.getSort())){
//            throw new ApiException(WechatRequestMapping.WxMenu.message_fail_sort_is_null, ExceptionCode.common_error_params_code);
//        }
//        //根据wechatCode查找是否存在微信配置
//        WxWechatDO wxWechat = wechatService.findById(wechatCode);
//        if(wxWechat==null){
//            throw new ApiException(WechatRequestMapping.WxConfig.message_fail_wxWechat_is_no_exist, ExceptionCode.common_error_params_code);
//        }
//        if (!"0".equals(supMenucode)) {//不为0,说明是子菜单,判断父菜单是否存在
//            //说明是子菜单
//            //判断父菜单是否存在
//            WxMenuDO parentMenuCode = findById(supMenucode);
//            if(parentMenuCode==null){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_supMenuId_is_no_exist, ExceptionCode.common_error_params_code);
//            }
//
//            //查询已经存在的子菜单条数
//            List<WxMenuDO> childMenus = findChildMenus(wechatCode, supMenucode);
//            if(childMenus.size()==5){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_childMenu_is_to_much, ExceptionCode.common_error_params_code);
//            }
//            if(name.getBytes().length>60){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_name_is_to_long, ExceptionCode.common_error_params_code);
//            }
//            if(StringUtils.isEmpty(wxMenu.getType())){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_type_is_null, ExceptionCode.common_error_params_code);
//            }
//        }else{
//            //查找父菜单
//            List<WxMenuDO> parentMenus = findParentMenuByWechatCode(wechatCode);
//            if(parentMenus!=null){
//                if(parentMenus.size()==3){
//                    throw new ApiException(WechatRequestMapping.WxMenu.message_fail_parentMenu_is_to_much, ExceptionCode.common_error_params_code);
//                }
//            }
//
//            //说明是父菜单
//            if(name.getBytes().length>16){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_name_is_to_long, ExceptionCode.common_error_params_code);
//            }
//
//        }
//        String type1=wxMenu.getType();
//        if(type1==null){
//            type1="";
//        }
//        String type = type1.toUpperCase();
//        if("click".equals(type)){
//            String key = wxMenu.getMenuKey();
//            if(StringUtils.isEmpty(key)){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_key_is_null, ExceptionCode.common_error_params_code);
//            }
//            if(key.getBytes().length>128){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_key_is_toLong, ExceptionCode.common_error_params_code);
//            }
//        }
//        String url = wxMenu.getUrl();
//        if("view".equals(type)){
//            if(StringUtils.isEmpty(url)){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_url_is_null, ExceptionCode.common_error_params_code);
//            }
//            if(url.getBytes().length>128){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_url_is_toLong, ExceptionCode.common_error_params_code);
//            }
//        }
//        if("miniprogram".equals(type)){
//            if(StringUtils.isEmpty(url)){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_url_is_null, ExceptionCode.common_error_params_code);
//            }
//            if(url.getBytes().length>128){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_url_is_toLong, ExceptionCode.common_error_params_code);
//            }
//            if(StringUtils.isEmpty(wxMenu.getAppid())){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_appid_is_null, ExceptionCode.common_error_params_code);
//            }
//            if(StringUtils.isEmpty(wxMenu.getPagepath())){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_pagepath_is_null, ExceptionCode.common_error_params_code);
//            }
//        }
//        if("media_id".equals(type)||"view_limited".equals(type)){
//            if(StringUtils.isEmpty(wxMenu.getMediaId())){
//                throw new ApiException(WechatRequestMapping.WxMenu.message_fail_mediaId_is_null, ExceptionCode.common_error_params_code);
//            }
//        }
//        WxMenuDO wxMenuTemp = null;
//        if(StringUtils.isEmpty(wxMenu.getSupMenuid())){//如果是空,则为父菜单
//            //判断sort是否重复
//            wxMenuTemp = wxMenuDao.findByWechatIdExcludeSortFromParent(wxMenu.getWechatId(), wxMenu.getSort(),wxMenu.getId());
//        }else{//子菜单
//            wxMenuTemp = wxMenuDao.findByWechatIdExcludeSortFromChild(wxMenu.getWechatId(), wxMenu.getSort(), wxMenu.getSupMenuid(), wxMenu.getId());
//        }
//        if(null!=wxMenuTemp){
//            throw new ApiException(WechatRequestMapping.WxMenu.message_fail_sort_is_repeat, ExceptionCode.common_error_params_code);
//        }
//        wxMenu.setUpdateTime(new Date());
//        return true;
//    }
//}
package com.yihu.jw.business.wx.service;
import com.yihu.jw.base.wx.WxMenuDO;
import com.yihu.jw.base.wx.WxMenuJsonDO;
import com.yihu.jw.base.wx.WxWechatDO;
import com.yihu.jw.business.wx.dao.WechatDao;
import com.yihu.jw.business.wx.dao.WxMenuDao;
import com.yihu.jw.business.wx.dao.WxMenuJsonDao;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.rm.base.WechatRequestMapping;
import com.yihu.jw.util.HttpUtil;
import com.yihu.mysql.query.BaseJpaService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * Created by Administrator on 2017/5/19 0019.
 */
@Service
public class WxMenuService extends BaseJpaService<WxMenuDO, WxMenuDao>{
    @Autowired
    private WxAccessTokenService wxAccessTokenService;
    @Autowired
    private WxMenuJsonDao wxMenuJsonDao;
    @Autowired
    private WechatDao wechatDao;
    public Envelop createWxMenu(String wxId){
//        String url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" ;
        String url ="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+ wxAccessTokenService.getWxAccessTokenById(wxId).getAccessToken();
        WxMenuJsonDO wxMenuJsonDO = wxMenuJsonDao.findByWechatIdAndStatus(wxId,1);
        String params = wxMenuJsonDO.getContent();
        WxWechatDO wechatDO =  wechatDao.findById(wxId);
        // 替换服务器地址、APPID
        params = params.replaceAll("server_url", wechatDO.getBaseUrl());
        params = params.replaceAll("appId", wechatDO.getAppId());
        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")) {
            Envelop.getSuccess(WechatRequestMapping.WxMenu.message_success_update);
        }
        return Envelop.getError(result.toString());
    }
}

+ 5 - 0
svr/svr-wlyy-specialist/pom.xml

@ -102,6 +102,11 @@
            <groupId>com.yihu</groupId>
            <artifactId>mysql-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.yihu.ehr.ehr</groupId>
            <artifactId>commons-util</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>
    <build>