当前位置:网站首页>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 ,摸着石头过河太难了,都是血泪啊
边栏推荐
- Install the tp6.0 framework under windows, picture and text. Thinkphp6.0 installation tutorial
- 2021年OWASP-TOP10
- 《财富自由之路》读书之一点体会
- cartographer_pose_graph_2d
- PHP one sentence Trojan horse
- cartographer_local_trajectory_builder_2d
- C# 40. byte[]与16进制string互转
- 百度API地图的标注不是居中显示,而是显示在左上角是怎么回事?已解决!
- 慢慢学JVM之缓存行和伪共享
- Keras actual combat cifar10 in tensorflow
猜你喜欢

Practical cases | getting started and mastering tkinter+pyinstaller

cartographer_ optimization_ problem_ 2d

SDN based DDoS attack mitigation

基于SDN的DDoS攻击缓解

The localstorage browser stores locally to limit the number of forms submitted when tourists do not log in.

Codeforces Round #800 (Div. 2)

小小面试题之GET和POST的区别

apktool 工具使用文档

SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor
![[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022](/img/64/5b2aec7a26c64c104c86e200f83b2d.png)
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
随机推荐
Tp5.0 framework PDO connection MySQL error: too many connections solution
The first gift of the project, the flying oar contract!
Redis installation on Linux
Leetcode114. 二叉树展开为链表
Leetcode513. Find the value in the lower left corner of the tree
基于SDN的DDoS攻击缓解
递归遍历目录结构和树状展现
Tensorflow and deep learning day 3
Mysql 源码阅读(二)登录连接调试
The beautiful scenery is natural, and the wonderful pen is obtained by chance -- how is the "wonderful pen" refined?
Learn cache lines and pseudo sharing of JVM slowly
Red team scoring method statistics
Thoughts triggered by the fact that app applications are installed on mobile phones and do not display icons
Procedural life
data = self._data_queue.get(timeout=timeout)
C# 39. string类型和byte[]类型相互转换(实测)
慢慢学JVM之缓存行和伪共享
Beidou navigation technology and industrial application of "chasing dreams in space and feeling for Beidou"
【红队】要想加入红队,需要做好哪些准备?
[unity3d] human computer interaction input