本文主要是介绍uniapp请求微信接口getUserProfile获得code,后台java代码获得openid,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- uniapp请求微信接口,登录getUserProfile 具体代码
- 后台java代码获得openid
uniapp请求微信接口,登录getUserProfile 具体代码
代码:
<template><view class="container"><view class="userinfo"><block v-if="!hasUserInfo"><button @click="getUserProfile"> 获取头像昵称 </button></block><block v-else><image @click="bindViewTap" class="userinfo-avatar" :src="userInfo.avatarUrl" mode="cover"></image><text class="userinfo-nickname">{{userInfo.nickName}}</text></block></view></view>
</template><script>export default {data() {return {userInfo: {},hasUserInfo: false,}},methods: {getUserProfile(e) {this.hasUserInfo = !this.hasUserInfo// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗var _this = thisuni.getUserProfile({desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写success: (res) => {console.log("res------------->", res);_this.userInfo = res.userInfo}})},}}
</script>
<style>
</style>
详情查看:微信开发文档https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
注意点
调用这个接口。
原本调用的是getUserInfo,但是接口调整了。就算写了getSetting也弹不出请求框,后来仔细看看,发现getUserInfo,都弃用了啊!!
一个笑死人的错误——碎碎念
- 用uniapp框架,发出的请求都是像uni.getUserProfile。开头就写uni
还有一点就是,语法不要混用吧,用uniapp框架,发出的请求都是像uni.getUserProfile。开头就写uni。所以,我无脑复制代码过来,发现运行不起来的时候。完了,又碰到我的知识盲区了。可恶,我一个后端,到底是为什么搞前端啊,岂可修。 - @click
uniapp的写法:<button @click="getUserProfile"> 获取头像昵称 </button>
微信的写法:<button bindtap="getUserProfile"> 获取头像昵称 </button>
归根结底还是我框架不熟悉,ε=(´ο`*)))唉
bindtap
就是微信那边的写法,用到uniapp中就用@click
后台java代码获得openid
1,引入jar包
<!--微信登录--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.4</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.1</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.5.1</version></dependency>
2,写工具类
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
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.DefaultHttpClient;
import org.apache.http.util.EntityUtils;import java.io.IOException;
import java.net.URLDecoder;/*** @create 2021/8/9 14:39* @description:微信登录验证帮助类*/
public class HttpRequestHelp {//private static Logger logger = Logger.getLogger(HttpRequestHelp.class); //日志记录/*** httpPost* @param url 路径* @param jsonParam 参数* @return*/public static JSONObject httpPost(String url,JSONObject jsonParam){return httpPost(url, jsonParam, false);}/*** post请求* @param url url地址* @param jsonParam 参数* @param noNeedResponse 不需要返回结果* @return*/public static JSONObject httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){//post请求返回结果DefaultHttpClient httpClient = new DefaultHttpClient();JSONObject jsonResult = null;HttpPost method = new HttpPost(url);try {if (null != jsonParam) {//解决中文乱码问题StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");entity.setContentEncoding("UTF-8");entity.setContentType("application/json");method.setEntity(entity);}HttpResponse result = httpClient.execute(method);url = URLDecoder.decode(url, "UTF-8");/**请求发送成功,并得到响应**/if (result.getStatusLine().getStatusCode() == 200) {String str = "";try {/**读取服务器返回过来的json字符串数据**/str = EntityUtils.toString(result.getEntity());if (noNeedResponse) {return null;}/**把json字符串转换成json对象**/jsonResult = JSONObject.parseObject(str);} catch (Exception e) {System.out.println("post请求提交失败:" + url);System.out.println(e);}}} catch (IOException e) {System.out.println("post请求提交失败:" + url+e);}return jsonResult;}/*** 发送get请求* @param url 路径* @return*/public static JSONObject httpGet(String url){//get请求返回结果JSONObject jsonResult = null;try {DefaultHttpClient client = new DefaultHttpClient();//发送get请求HttpGet request = new HttpGet(url);HttpResponse response = client.execute(request);/**请求发送成功,并得到响应**/if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {/**读取服务器返回过来的json字符串数据**/String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");/**把json字符串转换成json对象**/jsonResult = JSONObject.parseObject(strResult);url = URLDecoder.decode(url, "UTF-8");} else {System.out.println("get请求提交失败:" + url);}} catch (IOException e) {System.out.println("get请求提交失败:" + url);System.out.println(e);}return jsonResult;}@SuppressWarnings("resource")public static String httpsGet(String url,String charset){HttpClient httpClient = null;HttpGet httpGet = null;String result = null;try{httpClient = new SSLClient();httpGet = new HttpGet(url);httpGet.addHeader("Content-Type", "application/json");HttpResponse response = httpClient.execute(httpGet);if(response != null){HttpEntity resEntity = response.getEntity();if(resEntity != null){result = EntityUtils.toString(resEntity,charset);}}}catch(Exception ex){ex.printStackTrace();}return result;}}
3,写请求UserServiceImpl
//微信绑定private static final String WX_APPID = "你的小程序的appid";private static final String WX_SECRET = "你的小程序的密钥";private static final String WX_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";@AutowiredUserMapper userMapper;@Overridepublic CommonResult WXlogin(String code,String nickName,String imageUrl) {//拼接微信api请求地址,获取openIDString url = String.format(WX_URL,WX_APPID,WX_SECRET,code);//这一步调用上面写的的工具类JSONObject jsonObject = HttpRequestHelp.httpGet(url);System.out.println("jsonObject===>"+jsonObject);String openid = jsonObject.getString("openid");System.out.println("openid===>"+openid);//到这一步为止openid就获得了,下面就不用看了,我自存一下。//判断用户是否存在User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserOpenid, openid));if (null != openid && null == user) {user = new User();user.setUserOpenid(openid);user.setUserImageUrl(imageUrl);user.setUserNickname(nickName);userMapper.insert(user);User user1 = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserOpenid, openid));user1.setUserOpenid(null);return CommonResult.success(user1);}user.setUserOpenid(null);return CommonResult.success(user);}
B站BV1Uy4y1m7fx
从p=132开始看,这个视频讲的很详细。
这篇关于uniapp请求微信接口getUserProfile获得code,后台java代码获得openid的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!