|
@ -8,6 +8,7 @@ import com.yihu.jw.util.HttpUtil;
|
|
import com.yihu.jw.wx.dao.WxMenuDao;
|
|
import com.yihu.jw.wx.dao.WxMenuDao;
|
|
import com.yihu.jw.wx.model.WxAccessToken;
|
|
import com.yihu.jw.wx.model.WxAccessToken;
|
|
import com.yihu.jw.wx.model.WxMenu;
|
|
import com.yihu.jw.wx.model.WxMenu;
|
|
|
|
import com.yihu.jw.wx.model.WxWechat;
|
|
import org.json.JSONObject;
|
|
import org.json.JSONObject;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@ -26,6 +27,9 @@ public class WxMenuService extends BaseJpaService<WxMenu, WxMenuDao> {
|
|
@Autowired
|
|
@Autowired
|
|
private WxMenuDao wxMenuDao;
|
|
private WxMenuDao wxMenuDao;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private WechatService wechatService;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private WxAccessTokenService wxAccessTokenService;
|
|
private WxAccessTokenService wxAccessTokenService;
|
|
|
|
|
|
@ -44,6 +48,9 @@ public class WxMenuService extends BaseJpaService<WxMenu, WxMenuDao> {
|
|
@Transient
|
|
@Transient
|
|
public WxMenu updateWxMenu(WxMenu wxMenu) {
|
|
public WxMenu updateWxMenu(WxMenu wxMenu) {
|
|
if(canSaveOrUpata(wxMenu)){
|
|
if(canSaveOrUpata(wxMenu)){
|
|
|
|
if (StringUtils.isEmpty(wxMenu.getId())) {
|
|
|
|
throw new ApiException(WxContants.Wechat.message_fail_id_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
return wxMenuDao.save(wxMenu);
|
|
return wxMenuDao.save(wxMenu);
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
@ -57,7 +64,7 @@ public class WxMenuService extends BaseJpaService<WxMenu, WxMenuDao> {
|
|
}
|
|
}
|
|
String supMenucode = wxMenu.getSupMenucode();
|
|
String supMenucode = wxMenu.getSupMenucode();
|
|
if(StringUtils.isEmpty(supMenucode)) {//如果是空,则为父菜单
|
|
if(StringUtils.isEmpty(supMenucode)) {//如果是空,则为父菜单
|
|
List<WxMenu> childMenus = findChildMenus(wxMenu.getWechatCode(), wxMenu.getSupMenucode());
|
|
|
|
|
|
List<WxMenu> childMenus = findChildMenus(wxMenu.getWechatCode(), wxMenu.getCode());
|
|
if(childMenus!=null){
|
|
if(childMenus!=null){
|
|
for(WxMenu wxmenu:childMenus){
|
|
for(WxMenu wxmenu:childMenus){
|
|
wxmenu.setStatus(-1);
|
|
wxmenu.setStatus(-1);
|
|
@ -82,14 +89,31 @@ public class WxMenuService extends BaseJpaService<WxMenu, WxMenuDao> {
|
|
return wxMenuDao.findByWechatCode(wechatCode);
|
|
return wxMenuDao.findByWechatCode(wechatCode);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据wechatCode查找所有菜单
|
|
|
|
* @param wechatCode
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public List<WxMenu> findParentMenuByWechatCode(String wechatCode){
|
|
|
|
return wxMenuDao.findParentMenuByWechatCode(wechatCode);
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据wechatCode在微信公众号创建菜单
|
|
* 根据wechatCode在微信公众号创建菜单
|
|
* @param wechatCode
|
|
* @param wechatCode
|
|
* @return JSONObject
|
|
* @return JSONObject
|
|
*/
|
|
*/
|
|
public JSONObject createWechatMenu(String wechatCode) {
|
|
public JSONObject createWechatMenu(String wechatCode) {
|
|
|
|
WxWechat wechat = wechatService.findByCode(wechatCode);
|
|
|
|
if(wechat==null){
|
|
|
|
throw new ApiException(WxContants.Wechat.message_fail_wxWechat_is_no_exist, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
|
|
//首先根据wechatCode获取菜单,然后封装成json字符串
|
|
//首先根据wechatCode获取菜单,然后封装成json字符串
|
|
List<WxMenu> menus = wxMenuDao.findByWechatCode(wechatCode);
|
|
List<WxMenu> menus = wxMenuDao.findByWechatCode(wechatCode);
|
|
|
|
if(menus==null){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_WxMenu_is_no_exist, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
String menuJsonString = getMenuToString(menus, wechatCode);
|
|
String menuJsonString = getMenuToString(menus, wechatCode);
|
|
WxAccessToken wxAccessTokenByCode = wxAccessTokenService.getWxAccessTokenByCode(wechatCode);
|
|
WxAccessToken wxAccessTokenByCode = wxAccessTokenService.getWxAccessTokenByCode(wechatCode);
|
|
String token = wxAccessTokenByCode.getAccessToken();
|
|
String token = wxAccessTokenByCode.getAccessToken();
|
|
@ -145,8 +169,8 @@ public class WxMenuService extends BaseJpaService<WxMenu, WxMenuDao> {
|
|
if(!StringUtils.isEmpty(menu.getType())){
|
|
if(!StringUtils.isEmpty(menu.getType())){
|
|
menuJsonStr += ",\"type\":\"" + menu.getType()+"\"";
|
|
menuJsonStr += ",\"type\":\"" + menu.getType()+"\"";
|
|
}
|
|
}
|
|
if(!StringUtils.isEmpty(menu.getKey())){
|
|
|
|
menuJsonStr += ",\"key\":\"" + menu.getKey()+"\"";
|
|
|
|
|
|
if(!StringUtils.isEmpty(menu.getMenuKey())){
|
|
|
|
menuJsonStr += ",\"key\":\"" + menu.getMenuKey()+"\"";
|
|
}
|
|
}
|
|
if(!StringUtils.isEmpty(menu.getUrl())){
|
|
if(!StringUtils.isEmpty(menu.getUrl())){
|
|
menuJsonStr += ",\"url\":\"" + menu.getUrl()+"\"";
|
|
menuJsonStr += ",\"url\":\"" + menu.getUrl()+"\"";
|
|
@ -177,26 +201,101 @@ public class WxMenuService extends BaseJpaService<WxMenu, WxMenuDao> {
|
|
if (StringUtils.isEmpty(wxMenu.getCode())) {
|
|
if (StringUtils.isEmpty(wxMenu.getCode())) {
|
|
throw new ApiException(WxContants.WxMenu.message_fail_code_is_null, CommonContants.common_error_params_code);
|
|
throw new ApiException(WxContants.WxMenu.message_fail_code_is_null, CommonContants.common_error_params_code);
|
|
}
|
|
}
|
|
if (StringUtils.isEmpty(wxMenu.getWechatCode())) {
|
|
|
|
|
|
String wechatCode = wxMenu.getWechatCode();
|
|
|
|
if (StringUtils.isEmpty(wechatCode)) {
|
|
throw new ApiException(WxContants.WxMenu.message_fail_wechatCode_is_null, CommonContants.common_error_params_code);
|
|
throw new ApiException(WxContants.WxMenu.message_fail_wechatCode_is_null, CommonContants.common_error_params_code);
|
|
}
|
|
}
|
|
if (StringUtils.isEmpty(wxMenu.getName())) {
|
|
|
|
|
|
String name = wxMenu.getName();
|
|
|
|
if (StringUtils.isEmpty(name)) {
|
|
throw new ApiException(WxContants.WxMenu.message_fail_name_is_null, CommonContants.common_error_params_code);
|
|
throw new ApiException(WxContants.WxMenu.message_fail_name_is_null, CommonContants.common_error_params_code);
|
|
}
|
|
}
|
|
if(StringUtils.isEmpty(wxMenu.getType())){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_type_is_null, CommonContants.common_error_params_code);
|
|
|
|
|
|
if (StringUtils.isEmpty(wxMenu.getStatus())) {
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_status_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
if(StringUtils.isEmpty(wxMenu.getSort())){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_sort_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
//根据wechatCode查找是否存在微信配置
|
|
|
|
WxWechat wxWechat = wechatService.findByCode(wechatCode);
|
|
|
|
if(wxWechat==null){
|
|
|
|
throw new ApiException(WxContants.Wechat.message_fail_wxWechat_is_no_exist, CommonContants.common_error_params_code);
|
|
}
|
|
}
|
|
if("click".equals(wxMenu.getType().toUpperCase())){
|
|
|
|
String key = wxMenu.getKey();
|
|
|
|
|
|
String supMenucode = wxMenu.getSupMenucode();
|
|
|
|
if (!StringUtils.isEmpty(supMenucode)) {//不为空,说明是子菜单,判断父菜单是否存在
|
|
|
|
//说明是子菜单
|
|
|
|
//判断父菜单是否存在
|
|
|
|
WxMenu parentMenuCode = findByCode(supMenucode);
|
|
|
|
if(parentMenuCode==null){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_supMenuCode_is_no_exist, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
//查询已经存在的子菜单条数
|
|
|
|
List<WxMenu> childMenus = findChildMenus(wechatCode, supMenucode);
|
|
|
|
if(childMenus.size()==5){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_childMenu_is_to_much, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
if(name.getBytes().length>60){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_name_is_to_long, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
if(StringUtils.isEmpty(wxMenu.getType())){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_type_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
//查找父菜单
|
|
|
|
List<WxMenu> parentMenus = findParentMenuByWechatCode(wechatCode);
|
|
|
|
if(parentMenus!=null){
|
|
|
|
if(parentMenus.size()==3){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_parentMenu_is_to_much, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//说明是父菜单
|
|
|
|
if(name.getBytes().length>16){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_name_is_to_long, CommonContants.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)){
|
|
if(StringUtils.isEmpty(key)){
|
|
throw new ApiException(WxContants.WxMenu.message_fail_wxMenuKey_is_null, CommonContants.common_error_params_code);
|
|
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_key_is_null, CommonContants.common_error_params_code);
|
|
}
|
|
}
|
|
if(key.length()>128){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_wxMenuKey_is_toLong, CommonContants.common_error_params_code);
|
|
|
|
|
|
if(key.getBytes().length>128){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_key_is_toLong, CommonContants.common_error_params_code);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if(StringUtils.isEmpty(wxMenu.getSort())){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_sort_is_null, CommonContants.common_error_params_code);
|
|
|
|
|
|
String url = wxMenu.getUrl();
|
|
|
|
if("view".equals(type)){
|
|
|
|
if(StringUtils.isEmpty(url)){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_url_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
if(url.getBytes().length>128){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_url_is_toLong, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if("miniprogram".equals(type)){
|
|
|
|
if(StringUtils.isEmpty(url)){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_url_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
if(url.getBytes().length>128){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_url_is_toLong, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
if(StringUtils.isEmpty(wxMenu.getAppid())){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_appid_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
if(StringUtils.isEmpty(wxMenu.getPagepath())){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_pagepath_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if("media_id".equals(type)||"view_limited".equals(type)){
|
|
|
|
if(StringUtils.isEmpty(wxMenu.getMediaId())){
|
|
|
|
throw new ApiException(WxContants.WxMenu.message_fail_mediaId_is_null, CommonContants.common_error_params_code);
|
|
|
|
}
|
|
}
|
|
}
|
|
WxMenu wxMenuTemp = null;
|
|
WxMenu wxMenuTemp = null;
|
|
if(StringUtils.isEmpty(wxMenu.getSupMenucode())){//如果是空,则为父菜单
|
|
if(StringUtils.isEmpty(wxMenu.getSupMenucode())){//如果是空,则为父菜单
|