微信公众号静默授权

html授权地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=https域名/工程名/auth&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect

后台:

 @RequestMapping({"auth"})
    public String auth(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String htmlPath = props.getStr("htmlPath");
        String code = request.getParameter("code");
        String state = request.getParameter("state");
        Map<String, String> result = weixinutils.getUserInfoAccessToken(code);
        String openId = (String)result.get("openid");
        System.out.println("openid=" + openId);
        String token;
        if (StringUtils.isEmpty(openId)) {
            token = htmlPath + "?openId=" + openId;
            request.setAttribute("url", token);
            return "sys/wei_xin_render";
        } else {
            token = this.redisUtils.get("token");
            System.out.println("=======================token=" + token);
            String access_token = "";
            String name64;
            if (StringUtils.isNotBlank(token)) {
                access_token = token;
            } else {
                String appid = props.getStr("appid");
                name64 = props.getStr("AppSecret");
                Map<String, String> accessToken = weixinutils.getAccessToken(appid, name64);
                access_token = (String)accessToken.get("access_token");
                this.redisUtils.set("token", access_token);
            }

            Map<String, String> userInfo = weixinutils.getAutoUserInfo(access_token, openId);
            name64 = "";
            String headPic = StringUtil.addPath("default.jpg");
            Integer sex = 0;
            if (Integer.parseInt((String)userInfo.get("subscribe")) == 1) {
                headPic = (String)userInfo.get("headimgurl");
                String nickName = (String)userInfo.get("nickname");
                sex = Integer.parseInt((String)userInfo.get("sex"));
                name64 = StringUtil.base64NikeName(nickName);
            }

            YwEmployee employee = (YwEmployee)this.employeeService.getOne((Wrapper)((QueryWrapper)(new QueryWrapper()).eq("deleted", 0)).eq("wx_openid", openId));
            System.out.println("员工=" + employee);
            boolean bindFlag = false;
            String url;
            if (null == employee) {
                employee = new YwEmployee();
                employee.setDeleted(0).setWxHeader(headPic).setWxNickname(name64).setGmtCreate(new Date()).setWxOpenid(openId).setSex(sex);
                this.employeeService.add(employee);
                url = htmlPath + "?openId=" + openId;
                request.setAttribute("url", url);
                return "sys/wei_xin_render";
            } else {
                if (!employee.getWxNickname().equals(name64) || !employee.getWxHeader().equals(headPic)) {
                    employee.setWxNickname(name64).setWxHeader(headPic).setSex(sex);
                    this.employeeService.updateById(employee);
                }

                url = htmlPath + "?openId=" + openId;
                request.setAttribute("url", url);
                return "sys/wei_xin_render";
            }
        }
    }

工具类:

import cn.hutool.http.HttpUtil;
import cn.hutool.setting.dialect.Props;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.HashMap;
import java.util.Map;


public  class weixinutils {

    static Props props = new Props("config.properties", "utf-8");
    /**
     * @auther: Zww
     * 功能描述: 
     * @param: 获取全局的accessToken
     * @return:
     * @date: 2019/5/28 15:58
     */
    public static Map<String, String> getAccessToken(String wx_appid, String wx_appsecret) {
        Map<String, String> data = new HashMap();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ wx_appid + "&secret=" + wx_appsecret;
        JsonObject accessTokenInfo = null;
        try {
            Gson token_gson = new Gson();
            String result = HttpUtil.get(url);
            System.out.println("全局的accessToken="+result);
            accessTokenInfo = token_gson.fromJson(result, JsonObject.class);
            data.put("access_token", accessTokenInfo.get("access_token").toString().replaceAll("\"", ""));
            data.put("expires_in", accessTokenInfo.get("expires_in").toString().replaceAll("\"", ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return data;
    }


    /**
     * @auther: Zww
     * 功能描述:
     * @param: 获取授权AccessToken
     * @return:
     * @date: 2019/5/28 15:39
     */
    public static Map<String, String> getUserInfoAccessToken(String code) {
        Props props = new Props("config.properties", "utf-8");
        String appid = props.getStr("appid");
        String AppSecret = props.getStr("AppSecret");
        JsonObject object = null;
        Map<String, String> data = new HashMap<>();
        String url = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appid, AppSecret, code);
        String result = HttpUtil.get(url);
        Gson token_gson = new Gson();
        object = token_gson.fromJson(result, JsonObject.class);
        data.put("openid", object.get("openid").toString().replaceAll("\"", ""));
        data.put("access_token", object.get("access_token").toString().replaceAll("\"", ""));
        return data;
    }



/**网页授权获取用户信息
 * 获取用户信息      subscribe = 0 没关注公众号获取不了用户信息  1 关注公众号能获取用户信息
 *
 * @param accessToken  全局的
 * @param openId
 * @return
 */
    public static Map<String, String> getAutoUserInfo(String accessToken, String openId) {
        Map<String, String> data = new HashMap();
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";
        JsonObject userInfo = null;
        try {
            String result = HttpUtil.get(url);
            System.out.println("用户信息"+result);
            Gson token_gson = new Gson();
            userInfo = token_gson.fromJson(result, JsonObject.class);
            data.put("subscribe", userInfo.get("subscribe").toString().replaceAll("\"", ""));
            data.put("openid", userInfo.get("openid").toString().replaceAll("\"", ""));
            if(Integer.parseInt(userInfo.get("subscribe").toString().replaceAll("\"", ""))==1){
                data.put("sex", userInfo.get("sex").toString().replaceAll("\"", ""));
                data.put("nickname", userInfo.get("nickname").toString().replaceAll("\"", ""));
                data.put("city", userInfo.get("city").toString().replaceAll("\"", ""));
                data.put("province", userInfo.get("province").toString().replaceAll("\"", ""));
                data.put("country", userInfo.get("country").toString().replaceAll("\"", ""));
                data.put("headimgurl", userInfo.get("headimgurl").toString().replaceAll("\"", ""));
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
        }
        return data;
    }









    


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值