当前位置:网站首页>uniCloud云开发获取小程序用户openid
uniCloud云开发获取小程序用户openid
2022-06-26 05:25:00 【小杰学前端】
目录
一、介绍openid
我们在区分小程序用户时都需要用到openid,用户openid就相当于用户在小程序里的身份证。做为用户的唯一标示,所以获取到用户openid就显得很重要了,今天来教大家怎么样获取要用户的唯一标示 openid
二、openid获取难在哪
今天试了网上的很多方法,在真机调试的时候还是能够获取到的,结果一上传代码就获取不到了,应该也有很多同学遇到了这个问题,所以我们应该把code值传到后端去获取openid,因为在前端可能会被抓包或爬取到你的appid和secret,不安全,如果放在后端获取openid,除非你的服务器被攻击了,不然就是安全的。
三、云开发获取openid
1. 首先我们在 uniCloud 目录下的 cloudfunctions 下右击创建云函数

2. 编写云函数
'use strict';
exports.main = async (event, context) => {
let appid = "wxc00000000000";//你自己的appid
let secret = "xxxxxxxxxxxxxxxxxxxxxxxx";//你自己的appsecret
let url =
"https://api.weixin.qq.com/sns/jscode2session?appid=" +
appid +
"&secret=" +
secret +
"&js_code=" +
event.code +
"&grant_type=authorization_code";
let res = await uniCloud.httpclient.request(
url ,// 请求路径,
{
dataType:"json"
}
);
let openid = res.data.openid
return openid
};注意这里的 appid 和 secret 要用你自己的
3. 客户端调用uni.login(),用得到的code调用云函数
uni.login({
provider: "weixin",
success: function (res) {
uniCloud.callFunction({
name: 'openid',
data: {
code: res.code
},
success: (res) => {
console.log('云函数返回的值::::', res.result)
},
fail: () => {
console.log('云函数调用失败')
}
})
},
});4. 查看输出
现在我们就成功获取到了用户的 openid ,摸着石头过河太难了,都是血泪啊
边栏推荐
- Leetcode513. Find the value in the lower left corner of the tree
- Thoughts triggered by the fact that app applications are installed on mobile phones and do not display icons
- 《财富自由之路》读书之一点体会
- Apktool tool usage document
- 【MYSQL】MySQL 百万级数据量分页查询方法及其优化
- A beginner's entry is enough: develop mobile IM from zero
- [greedy college] Figure neural network advanced training camp
- uni-app吸顶固定样式
- RESNET in tensorflow_ Train actual combat
- LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
猜你喜欢

Windows下安装Tp6.0框架,图文。Thinkphp6.0安装教程

cartographer_optimization_problem_2d

Yunqi lab recommends experience scenarios this week, free cloud learning

Leetcode513. Find the value in the lower left corner of the tree

Leetcode114. Expand binary tree into linked list
![[arm] add desktop application for buildreoot of rk3568 development board](/img/9a/28015cdea7362261c39ffc7f6e13a9.png)
[arm] add desktop application for buildreoot of rk3568 development board
A beginner's entry is enough: develop mobile IM from zero

Command line interface of alluxio

Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!

创建 SSH 秘钥对 配置步骤
随机推荐
第九章 设置结构化日志记录(一)
Serious hazard warning! Log4j execution vulnerability is exposed!
[greedy college] recommended system engineer training plan
创建 SSH 秘钥对 配置步骤
The beautiful scenery is natural, and the wonderful pen is obtained by chance -- how is the "wonderful pen" refined?
As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
Mysql 源码阅读(二)登录连接调试
Use jedis to monitor redis stream to realize message queue function
[arm] add desktop application for buildreoot of rk3568 development board
The difference between get and post in small interview questions
[upsampling method opencv interpolation]
Muke.com actual combat course
ssh连win10报错:Permission denied (publickey,keyboard-interactive).
出色的学习能力,才是你唯一可持续的竞争优势
How to rewrite a pseudo static URL created by zenpart
Lstms in tensorflow_ Cell actual combat
Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
tensorlow:cifar100_ train
Mise en file d'attente des messages en utilisant jedis Listening redis stream