uniapp请求微信接口getUserProfile获得code,后台java代码获得openid

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/718332

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.