146 lines
5.4 KiB
Java
146 lines
5.4 KiB
Java
package com.peanut.modules.wx.controller;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.peanut.common.utils.ConstantPropertiesUtil;
|
||
import com.peanut.common.utils.HttpClientUtils;
|
||
import com.peanut.common.utils.R;
|
||
import com.peanut.modules.book.service.MyUserService;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Controller;
|
||
import org.springframework.web.bind.annotation.GetMapping;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.ResponseBody;
|
||
|
||
import java.io.UnsupportedEncodingException;
|
||
import java.net.URLEncoder;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
@Slf4j
|
||
@Controller
|
||
@RequestMapping("/api/ucenter/wx")
|
||
public class WeixinApiController {
|
||
|
||
@Autowired
|
||
private MyUserService userInfoService;
|
||
|
||
|
||
//微信扫描后回调的方法
|
||
// @GetMapping("callback")
|
||
// public String callback(String code,String state) {
|
||
// //1、获取到临时票据 code
|
||
// System.out.println("code"+code);
|
||
// //2、拿着code和微信id和秘钥,请求微信固定地址
|
||
// StringBuffer baseAccessTokenUrl = new StringBuffer()
|
||
// .append("https://api.weixin.qq.com/sns/oauth2/access_token")
|
||
// .append("?appid=%s")
|
||
// .append("&secret=%s")
|
||
// .append("&code=%s")
|
||
// .append("&grant_type=authorization_code");
|
||
//
|
||
//
|
||
// String accessTokenUrl = String.format(baseAccessTokenUrl.toString(),
|
||
// ConstantPropertiesUtil.WX_OPEN_APP_ID,
|
||
// ConstantPropertiesUtil.WX_OPEN_APP_SECRET,
|
||
// code);
|
||
// //使用httpclient请求这个地址
|
||
// try {
|
||
// String accesstokenInfo = HttpClientUtils.get(accessTokenUrl);
|
||
// System.out.println("accesstokenInfo:"+accesstokenInfo);
|
||
//
|
||
// //从返回字符串获取两个值 openid 和 access_token
|
||
// JSONObject jsonObject = JSONObject.parseObject(accesstokenInfo);
|
||
// String access_token = jsonObject.getString("access_token");
|
||
// String openid = jsonObject.getString("openid");
|
||
//
|
||
//
|
||
// //判断数据库是否存在微信扫码记录
|
||
// //根据openid判断
|
||
// UserInfo userInfo = userInfoService.selectWxInfoOpenId(openid);
|
||
//
|
||
// if (null == userInfo) {
|
||
// //拿着openid 和 access_token 请求微信地址,得到扫码人信息
|
||
// String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
|
||
// "?access_token=%s" +
|
||
// "&openid=%s";
|
||
// String userInfoUrl = String.format(baseUserInfoUrl, access_token, openid);
|
||
//
|
||
// String resultInfo = HttpClientUtils.get(userInfoUrl);
|
||
// System.out.println("resultInfo"+resultInfo);
|
||
//
|
||
// JSONObject resultUserInfo = JSONObject.parseObject(resultInfo);
|
||
//
|
||
// //获取昵称
|
||
// String nickname = resultUserInfo.getString("nickname");
|
||
//
|
||
// //获取头像
|
||
// String headimgurl = resultUserInfo.getString("headimgurl");
|
||
//
|
||
// userInfo = new UserInfo();
|
||
// userInfo.setNickName(nickname);
|
||
// userInfo.setOpenid(openid);
|
||
// userInfo.setStatus(1);
|
||
// userInfoService.save(userInfo);
|
||
// }
|
||
//
|
||
//
|
||
//
|
||
//
|
||
// //返回map
|
||
// Map<String,String> map = new HashMap<>();
|
||
// String name = userInfo.getName();
|
||
// if (StringUtils.isEmpty(name)) {
|
||
// name = userInfo.getNickName();
|
||
// }
|
||
//
|
||
// if (StringUtils.isEmpty(name)) {
|
||
// name = userInfo.getPhone();
|
||
// }
|
||
//
|
||
// map.put("name",name);
|
||
//
|
||
// //前端判断,如果openid不为空,说明没有绑定手机号,进行绑定,为空说明已经绑定,不再绑定
|
||
//
|
||
// if (StringUtils.isEmpty(userInfo.getPhone())) {
|
||
// map.put("openid",userInfo.getOpenid());
|
||
// } else {
|
||
// map.put("openid","");
|
||
// }
|
||
//
|
||
// String token = JwtHelper.createToken(userInfo.getId(), name);
|
||
// map.put("token",token);
|
||
//
|
||
// //注意我这里跳转的是一个前端的页面带着用户的信息
|
||
// return "redirect:"+ ConstantPropertiesUtil.YYGH_BASE_URL + "/weixin/callback?token="+map.get("token")+
|
||
// "&openid="+map.get("openid")+"&name="+URLEncoder.encode(map.get("name"),"utf-8");
|
||
// } catch (Exception e) {
|
||
// e.printStackTrace();
|
||
// return null;
|
||
// }
|
||
// }
|
||
|
||
|
||
//1、生成微信扫描二维码
|
||
//生成二维码需要的参数
|
||
@ResponseBody
|
||
@GetMapping("/getLoginParam")
|
||
public R getLoginParam() {
|
||
try {
|
||
Map<String,Object> map = new HashMap<>();
|
||
String wxOpenRedirectUrl = ConstantPropertiesUtil.WX_OPEN_REDIRECT_URL;
|
||
String wxRedirectUri = URLEncoder.encode(wxOpenRedirectUrl, "utf-8");
|
||
map.put("appid", ConstantPropertiesUtil.WX_OPEN_APP_ID);
|
||
map.put("scope","snsapi_login");
|
||
map.put("redirect_uri",wxRedirectUri);
|
||
map.put("state",System.currentTimeMillis());
|
||
return R.ok(map);
|
||
} catch (UnsupportedEncodingException e) {
|
||
e.printStackTrace();
|
||
return null;
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|