当前位置:网站首页>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 ,摸着石头过河太难了,都是血泪啊
边栏推荐
- Two step processing of string regular matching to get JSON list
- Thoughts triggered by the fact that app applications are installed on mobile phones and do not display icons
- Data storage: the difference between MySQL InnoDB and MyISAM
- Gd32f3x0 official PWM drive has a small positive bandwidth (inaccurate timing)
- cartographer_pose_graph_2d
- cartographer_optimization_problem_2d
- 红队得分方法统计
- Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
- First day of deep learning and tensorflow learning
- vscode config
猜你喜欢

【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...

Serious hazard warning! Log4j execution vulnerability is exposed!

Leetcode114. Expand binary tree into linked list

《财富自由之路》读书之一点体会

Classic theory: detailed explanation of three handshakes and four waves of TCP protocol

The parameter field of the callback address of the payment interface is "notify_url", and an error occurs after encoding and decoding the signed special character URL (,,,,,)

AD教程系列 | 4 - 创建集成库文件

cartographer_backend_constraint

Recursively traverse directory structure and tree presentation

Could not get unknown property ‘*‘ for SigningConfig container of type org. gradle. api. internal
随机推荐
The parameter field of the callback address of the payment interface is "notify_url", and an error occurs after encoding and decoding the signed special character URL (,,,,,)
PHP二维/多维数组按照指定的键值来进行升序和降序
Anaconda creates tensorflow environment
The first gift of the project, the flying oar contract!
Mise en file d'attente des messages en utilisant jedis Listening redis stream
Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
Security problems in wireless networks and modern solutions
[leetcode] 713: subarray with product less than k
数据存储:MySQL之InnoDB与MyISAM的区别
How to make your big file upload stable and fast?
A beginner's entry is enough: develop mobile IM from zero
程序人生
Codeforces Round #802 (Div. 2)(A-D)
Two step processing of string regular matching to get JSON list
Yunqi lab recommends experience scenarios this week, free cloud learning
Uni app ceiling fixed style
Data storage: the difference between MySQL InnoDB and MyISAM
Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
Apktool tool usage document