获取openID
我要化了,这谁顶得住啊
我知道要获取code,知道要向微信请求但是完全不会,打扰了,我就是传说中的弟中弟。
1.在pom中添加依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.4</version>
</dependency>
2.写一个控制器 Test_openId
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;
//import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@SpringBootApplication
@RestController
@RequestMapping("/login")
public class Test_openId {
private String openid = "";
@RequestMapping("/is_login")
@ResponseBody
public String is_login(HttpServletRequest request,String code) throws IOException {
// String code=new String();
JSONObject result = new JSONObject();
if (code != null) {
String getOpenIdUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
String appid = "...";//appid 去微信开放里面开通小程序获得
String secret = "...";
String requestUrl = getOpenIdUrl.replace("JSCODE", code);
requestUrl = requestUrl.replace("APPID", appid);
requestUrl = requestUrl.replace("SECRET", secret);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(requestUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
//向微信发送请求并获取response
String response = httpClient.execute(httpGet, responseHandler);
System.out.println(response);
JSONObject jsonObject = JSON.parseObject(response);
openid = jsonObject.get("openid").toString();
result.put("code", 200);
result.put("msg", openid);
return result.toJSONString();
} else {
result.put("code", 400);
result.put("msg", "No Code.");
}
if(openid==null)
{
return "code fail";
}
return "";
}
}
3.再整个前端
用的是uni,在微信开发者工具上运行的。
<template>
<view class="content">
<view>
<text class="title">{{openid}}</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello',
openid:"sdfsdfds"
}
},
onLoad() {
this.getOpebid();
},
methods: {
getOpebid(){
uni.login({
provider: 'weixin',
success: (res) => {
console.log('login success:', res);
// 更新保存在 store 中的登录状态
uni.request({
url:"http://127.0.0.1:8080/login/is_login",
dataType:'json',
method:'POST',
data:{
code:res.code,
},
header:{
'content-type':'application/x-www-form-urlencoded',
},
success:(res)=>{
console.log(res);
this.openid=res.data.msg;
}
})
},
fail: (err) => {
console.log('login fail:', err);
}
});
}
}
}
</script>
<style>
.content {
text-align: center;
height: 400upx;
}
.logo {
height: 200upx;
width: 200upx;
margin-top: 200upx;
}
.title {
font-size: 36upx;
color: #8f8f94;
}
</style>
启动之后在页面显示你的openid并且在你的控制台也会打印一遍