微信授权登陆

原创
2019/11/29 15:54
阅读数 58
package com.ctrl.aWxSpLogin;


import com.alibaba.fastjson.JSONObject;
import com.ctrl.mobile.respUtils.HttpUtil;
import com.thinkgem.jeesite.common.config.Global;

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

/**
 * @Author LiPeiMin
 * @Description //TODO
 * @Date 11:52 2019/11/18
 **/
public class SpLogin {
    /**
     * 开发者设置中的appId
     */
    private static String appId;
    /**
     * 开发者设置中的appSecret
     */
    private static String secret;

    static {
        try {
            appId = Global.getConfig("sp.appId");
            secret = Global.getConfig("sp.secret");
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Description TODO //授权登陆 获取opentoken 以及 sessionkey
     * @Param [wxCode]
     * @Author wuyu
     * @Date 16:24 2019/11/18
     */
    public static JSONObject getUserWXLoginInfo(String wxCode) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> requestUrlParam = new HashMap<String, String>();
        requestUrlParam.put("appid", appId);    //开发者设置中的appId
        requestUrlParam.put("secret", secret);    //开发者设置中的appSecret
        requestUrlParam.put("js_code", wxCode);    //小程序调用wx.login返回的code
        requestUrlParam.put("grant_type", "authorization_code");    //默认参数
        //发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
        JSONObject jsonObject = JSONObject.parseObject(HttpUtil.sendPost(requestUrl, requestUrlParam));
        return jsonObject;
    }

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Description TODO //获取AccessToken
     * @Param []
     * @Author wuyu
     * @Date 16:24 2019/11/18
     */
    public static JSONObject getAccessToken() {
        Map<String, String> requestUrlParam = new HashMap<String, String>();
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token";
        requestUrlParam.put("appid", appId);    //开发者设置中的appId
        requestUrlParam.put("secret", secret);    //开发者设置中的appSecret
        requestUrlParam.put("grant_type", "client_credential");    //默认参数
        //发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
        JSONObject jsonObject = JSONObject.parseObject(HttpUtil.sendPost(requestUrl, requestUrlParam));
        return jsonObject;
    }
    /**
     * @Description TODO //获取用户信息
     * @Param [accessToken, openId]
     * @Return com.alibaba.fastjson.JSONObject
     * @Author wuyu
     * @Date 13:43 2019/11/29
     */
    public static JSONObject getUserInfo(String accessToken, String openId) {
        Map<String, String> requestUrlParam = new HashMap<String, String>();
        String requestUrl = "https://api.weixin.qq.com/sns/userinfo";
        requestUrlParam.put("access_token", accessToken);    //accessToken
        requestUrlParam.put("openid", openId);    //openId
        //发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
        JSONObject jsonObject = JSONObject.parseObject(HttpUtil.sendPost(requestUrl, requestUrlParam));
        return jsonObject;
    }

}
package com.ctrl.mobile.ctrlSpController.loginController;

import com.alibaba.fastjson.JSONObject;
import com.ctrl.aSms.SMS;
import com.ctrl.aWxSpLogin.SpLogin;
import com.ctrl.ctrlapptokeninfo.entity.CtrlAppTokenInfo;
import com.ctrl.ctrlapptokeninfo.service.CtrlAppTokenInfoService;
import com.ctrl.ctrlmemberinfo.entity.CtrlMemberInfo;
import com.ctrl.ctrlmemberinfo.service.CtrlMemberInfoService;
import com.ctrl.mobile.respUtils.*;
import com.thinkgem.jeesite.common.utils.JedisUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LiPeiMin
 * @Description //TODO 登陆
 * @Date 11:35 2019/11/18
 **/
@Controller
@RequestMapping(value = "/sp/login")
public class LoginsController extends BaseController {
    @Value("${sms.registered}")
    private String registered;
    @Autowired
    private CtrlMemberInfoService ctrlMemberInfoService;
    @Autowired
    private CtrlAppTokenInfoService ctrlAppTokenInfoService;

    /**
     * @return java.lang.String
     * @Description TODO //微信授权 获取sessionkey以及openid
     * @Param [response, request, wxCode]
     * @Author wuyu
     * @Date 15:25 2019/11/18
     */
    @RequestMapping("authorization")
    public String authorization(HttpServletResponse response, HttpServletRequest request, String wxCode) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        if (StringUtils.isNotBlank(wxCode)) {
            //请求微信api获取用户的openid和sessionKey
            JSONObject jsonObject = SpLogin.getUserWXLoginInfo(wxCode);
            //获取access_token
            JSONObject jsonObject1 = SpLogin.getAccessToken();
            map.put("openid", jsonObject.get("openid").toString());
            map.put("sessionKey", jsonObject.get("session_key").toString());
            map.put("accessToken", jsonObject1.get("access_token").toString());
            //通过access_token 获取微信用户的信息
            JSONObject jsonObject2 = SpLogin.getUserInfo(jsonObject1.get("access_token").toString(), jsonObject.get("openid").toString());
            System.out.println(jsonObject2);
            RespUtils.getMsgSuccess(map);
        } else {
            RespUtils.getRespParamException(map);
        }
        return RespUtils.returnRespJson(response, map);
    }
}

 

展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部