Commit 2c5a955c by zhanghj

V1.1.0--提交微信公众号授权和小程序免密相关代码

git-svn-id: svn://192.168.3.233/frms/code/car-service/trunk/car-service@20254 aeb6102e-e053-11e5-9fec-f9ebc45caf4b
1 parent 32a9b32c
package com.glsx.common.utils.wehcat;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.utils.HttpUtils;
/**
* 微信小程序工具类
*/
public class WechatMiniUtils {
private static Logger logger = LoggerFactory.getLogger(WechatMiniUtils.class);
//public String GET_OPENID_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$APPID$&secret=$SECRET$&code=$CODE$&grant_type=authorization_code";
/*
* 获取小程序的openid
*/
public static String GET_OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$APPID$&secret=$SECRET$&js_code=$JSCODE$&grant_type=authorization_code";
public static Map<String, Object> getWechatMiniOpenId(String appid, String secret, String code) throws Exception {
Map<String, Object> wto = new HashMap<String, Object>();
String url = GET_OPENID_URL.replace("$APPID$", appid).replace("$SECRET$", secret).replace("$JSCODE$", code);
HttpUtils httpUtils = new HttpUtils();
String result = httpUtils.get(url, null);
logger.info(url + " 获取openId接口返回:" + result);
if (StringUtils.isNotEmpty(result)) {
JSONObject openIdJO = JSON.parseObject(result);
for (String key : openIdJO.keySet()) {
wto.put(key, openIdJO.getString(key));
}
}
return wto;
}
}
......@@ -41,7 +41,7 @@ import com.glsx.common.utils.RedisCacheUtil;
/**
* 〈一句话功能简述〉<br>
* 〈微信工具类〉
*
* 微信公众号相关工具类
* @author liuyf
* @create 2018/4/27
* @since 1.0.0
......
......@@ -104,6 +104,7 @@ public class ShiroConfig {
LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
// 对静态资源设置匿名访问
filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/index.html", "anon");
//对外开放api接口
filterChainDefinitionMap.put("/api/**", "anon");
filterChainDefinitionMap.put("/test/**", "anon");
......
......@@ -13,6 +13,7 @@ import javax.validation.groups.Default;
import com.glsx.usercenter.mapper.SysUserMapper;
import com.glsx.usercenter.pojo.user.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
......@@ -956,4 +957,21 @@ public class UserController extends BaseController {
SysMechanic mechanic = sysUserService.getMechanicByUserId(mechanicId);
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), mechanic);
}
/**
*
* @author zhanghj
* @Title: getPublicNumberCode
* @Description: TODO
* @param code
* @return
* ResultObject
*
*/
@RequestMapping(value="/getWechatOpenId", method={RequestMethod.POST})
public ResultObject getWechatOpenId(HttpServletRequest request,String publicCode, String miniCode){
logger.info("publicCode=====" + publicCode + ",miniCode=" + miniCode);
sysUserService.getWechatOpenId(request, publicCode, miniCode);
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg());
}
}
......@@ -6,6 +6,9 @@ import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.glsx.common.feign.client.OrderClient;
import com.glsx.common.utils.Page;
import com.glsx.usercenter.dto.ServiceInfo;
......@@ -277,4 +280,18 @@ public interface SysUserService {
*
*/
List<SysMerchant> getMerchantList(SysMerchant merchant);
/**
*
* @author zhanghj
* @Title: getWechatOpenId
* @Description: TODO 获取用户的的公众号和小程序openid
* @param request
* @param publicCode
* @param miniCode
* void
*
*/
void getWechatOpenId(HttpServletRequest request, String publicCode,
String miniCode);
}
package com.glsx.usercenter.service.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.feign.client.OrderClient;
import com.glsx.usercenter.constants.ResultCodeEnum;
import com.glsx.usercenter.exception.SysException;
import com.glsx.usercenter.pojo.user.*;
import com.glsx.usercenter.utils.ShiroUtil;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
......@@ -23,11 +18,18 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.glsx.common.feign.client.OrderClient;
import com.glsx.common.utils.DateUtil;
import com.glsx.common.utils.Page;
import com.glsx.common.utils.PropertiesUtil;
import com.glsx.common.utils.wehcat.WechatMiniUtils;
import com.glsx.common.utils.wehcat.WechatUtils;
import com.glsx.usercenter.constants.Constants;
import com.glsx.usercenter.constants.ResultCodeEnum;
import com.glsx.usercenter.dto.ServiceInfo;
import com.glsx.usercenter.exception.SysException;
import com.glsx.usercenter.mapper.SysOrganizationMapper;
import com.glsx.usercenter.mapper.SysResourceMatchingMapper;
import com.glsx.usercenter.mapper.SysUserImageMapper;
......@@ -36,9 +38,23 @@ import com.glsx.usercenter.mapper.SysUserOrgMapper;
import com.glsx.usercenter.mapper.SysUserRoleMapper;
import com.glsx.usercenter.mapper.SysUserServicesMapper;
import com.glsx.usercenter.pojo.menu.SysOrganization;
import com.glsx.usercenter.pojo.user.SysManager;
import com.glsx.usercenter.pojo.user.SysMechanic;
import com.glsx.usercenter.pojo.user.SysMerchant;
import com.glsx.usercenter.pojo.user.SysResourceMatching;
import com.glsx.usercenter.pojo.user.SysResourceMatchingExample;
import com.glsx.usercenter.pojo.user.SysUser;
import com.glsx.usercenter.pojo.user.SysUserExample;
import com.glsx.usercenter.pojo.user.SysUserImage;
import com.glsx.usercenter.pojo.user.SysUserImageExample;
import com.glsx.usercenter.pojo.user.SysUserOrg;
import com.glsx.usercenter.pojo.user.SysUserRole;
import com.glsx.usercenter.pojo.user.SysUserServices;
import com.glsx.usercenter.pojo.user.SysUserServicesExample;
import com.glsx.usercenter.pojo.vo.UserQueryVo;
import com.glsx.usercenter.service.IAreaService;
import com.glsx.usercenter.service.SysUserService;
import com.glsx.usercenter.utils.ShiroUtil;
/**
......@@ -754,4 +770,79 @@ public class SysUserServiceImpl implements SysUserService {
// TODO Auto-generated method stub
return sysUserMapper.getMerchantList(merchant);
}
@Override
public void getWechatOpenId(HttpServletRequest request, String publicCode,
String miniCode) {
// TODO Auto-generated method stub
Integer userId = ShiroUtil.getUserId();
SysUser user = getUserById(userId);
Integer userType = user.getUserType();
String miniOpenId = null;
String publicOpenId = null;
if (StringUtils.isNotEmpty(publicCode)) {
String appid = PropertiesUtil.getProperty("wechat_bhg_public_appid");
String secret = PropertiesUtil.getProperty("wechat_bhg_public_secret");
WechatUtils wechatUtils = new WechatUtils();
Map<String, Object> result;
try {
result = wechatUtils.getWechatTokenAndOpenId(appid, secret, publicCode);
if (result != null) {
publicOpenId = (String) result.get("openid");
}
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error("获取微信公众号openid失败", e);
}
}
if (StringUtils.isNotEmpty(miniCode)) {
Map<String, String> map = getWechatMiniProperties(userType);
String appid = map.get("appid");
String secret = map.get("secret");
try {
Map<String, Object> result= WechatMiniUtils.getWechatMiniOpenId(appid, secret, miniCode);
if (result != null) {
miniOpenId = (String) result.get("openid");
}
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error("获取微信小程序openid失败", e);
}
}
SysUser sysUser = new SysUser();
sysUser.setId(userId);
sysUser.setPublicOpenId(publicOpenId);
sysUser.setMiniOpenId(miniOpenId);
sysUserMapper.updateByPrimaryKey(sysUser);
}
/**
*
* @author zhanghj
* @Title: getWechatMiniProperties
* @Description: TODO 返回不同端的微信小程序配置
* @param userType
* @return
* Map<String,String>
*
*/
private Map<String, String> getWechatMiniProperties(Integer userType) {
// TODO Auto-generated method stub
Map<String, String> map = new HashMap<String, String>();
String miniAppid = null;
String miniSecret = null;
if (com.glsx.common.constants.Constants.USER_TYPE_SP == userType) {
miniAppid = PropertiesUtil.getProperty("wechat.mini.service.appid");
miniSecret = PropertiesUtil.getProperty("wechat.mini.service.secret");
} else if(com.glsx.common.constants.Constants.USER_TYPE_MERCHANT == userType) {
miniAppid = PropertiesUtil.getProperty("wechat.mini.merchant.appid");
miniSecret = PropertiesUtil.getProperty("wechat.mini.merchant.secret");
}else if(com.glsx.common.constants.Constants.USER_TYPE_MECHANIC == userType) {
miniAppid = PropertiesUtil.getProperty("wechat.mini.mechanic.appid");
miniSecret = PropertiesUtil.getProperty("wechat.mini.mechanic.secret");
}
map.put("appid", miniAppid);
map.put("secret", miniSecret);
return map;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!