提交 5a93283d authored 作者: xinjunguo's avatar xinjunguo

--no commit message

上级 e1b5b3d2
package com.zrqx.third.wechatlogin;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties.Redis;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zrqx.core.enums.ResponseCodeEnum;
import com.zrqx.core.exception.BaseException;
import com.zrqx.core.model.third.wechartlogin.WeChartThirdReturn;
import com.zrqx.core.util.CallBack;
import com.zrqx.third.constant.RequestPath;
import com.zrqx.third.util.HttpsUtils;
import com.zrqx.third.wechatlogin.config.WeChatLoginH5Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(RequestPath.WECHAT_H5_LOGIN)
@Api(description = "微信客户端内访问第三方页面授权登录")
public class WeChatLoginH5Controller {
@ApiOperation(value = "微信登录", notes = "微信登录")
@GetMapping({ "/query" })
public CallBack<WeChartThirdReturn> query(String code, HttpServletRequest request) throws Exception {
String appid="appid="+WeChatLoginH5Config.APPID;
String secret="&secret="+WeChatLoginH5Config.SECRET;
String code1="&code="+code;
String grant_type="&grant_type="+WeChatLoginH5Config.GRANT_TYPE;
String param = appid + secret + code1 + grant_type;
String result = HttpsUtils.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token", param);
JSONObject object = (JSONObject) JSON.parse(result);
if (object.containsKey("errcode")) {
throw new BaseException(ResponseCodeEnum.EXCEPTION.getCode(), object.get("errcode").toString());
}
String openId = object.get("openid").toString();
String access_token = object.get("access_token").toString();
String param1 = "access_token=" + access_token + "&openid=" + openId;
String result1 = HttpsUtils.sendGet("https://api.weixin.qq.com/sns/userinfo", param1);
JSONObject object1 = (JSONObject) JSON.parse(result1);
if (object1.containsKey("errcode")) {
throw new BaseException(ResponseCodeEnum.EXCEPTION.getCode(), object1.get("errcode").toString());
}
String nickname = object1.get("nickname").toString();
String headimgurl = object1.get("headimgurl").toString();
String unionid = object1.get("unionid").toString();
WeChartThirdReturn user = new WeChartThirdReturn();
user.setOpenid(openId);
user.setNickname(nickname);
user.setHeadImgUrl(headimgurl);
user.setUnionid(unionid);
return CallBack.success(user);
}
}
\ No newline at end of file
package com.zrqx.third.wechatlogin.config;
public class WeChatLoginH5Config {
public static final String APPID = "wxa036be65443d937f";
public static final String SECRET = "e2070c6ac1a3c465042320d17a946479";
public static final String GRANT_TYPE = "authorization_code";
public static final String RESPONSE_TYPE = "code";
public static final String SCOPE = "snsapi_login";
public static final String REDIRECTURI = "www.zgsccbs.com";
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论