begin new project
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
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.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
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.RequestBody;
|
||||
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;
|
||||
|
||||
public static void main(String[] args) {
|
||||
String token = getAccessToken("ed1eb116f91a49a4ec1ec37c6f8f9495");
|
||||
String tel = login(token,"0a3nul100iDgdU1T0J300E82Yk1nul1O");
|
||||
// System.out.println(tel);
|
||||
}
|
||||
|
||||
|
||||
public static String getAccessToken(String secret) {
|
||||
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||
// 拼接URL
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/token?" +
|
||||
"appid=wxff97e3bb73cf57d2"+
|
||||
"&secret="+secret+
|
||||
"&grant_type=client_credential";
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.setHeader("Content-Type", "application/json;charset=utf8");
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
response = httpClient.execute(httpGet);
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
|
||||
String token = jsonObject.getString("access_token");
|
||||
return token;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
// 释放资源
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
public static String login(String accessToken,String code) {
|
||||
// 创建httpClient对象
|
||||
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?" +
|
||||
"access_token="+accessToken;
|
||||
Map<String, Object> entity = new HashMap<>();
|
||||
entity.put("code", code);
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
|
||||
httpPost.setEntity(new StringEntity(JSONObject.toJSONString(entity),"utf-8"));
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
response = httpClient.execute(httpPost);
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
|
||||
JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
|
||||
String tel = phoneInfo.getString("phoneNumber");
|
||||
return tel;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
// 释放资源
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//微信扫描后回调的方法
|
||||
// @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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user