钉钉小程序前端存储及使用token
时间: 2024-05-19 07:11:49 浏览: 10
钉钉小程序前端存储token的方式有多种,以下是其中两种比较常见的方式:
1. 使用小程序自带的存储功能
可以使用小程序自带的存储API(dd.setStorage和dd.getStorage)将token存储在本地,然后在需要使用token的地方调用dd.getStorage获取token即可。这种方式的优点是简单易用,缺点是如果用户清空了小程序缓存,token也会被清空。
2. 使用js-cookie库
js-cookie是一个简单的、轻量级的JavaScript库,它允许你在浏览器中读取和写入cookie。可以使用js-cookie将token存储在cookie中,然后在需要使用token的地方调用js-cookie获取token即可。这种方式的优点是相对稳定,即使用户清空了小程序缓存,也不会影响token的存储。
需要注意的是,无论使用哪种方式存储token,都需要注意token的安全性,不能将敏感信息存储在客户端,以防泄漏。
相关问题
钉钉前端存储及使用token
钉钉前端存储 token 可以使用浏览器提供的 localStorage 或 sessionStorage 进行存储。一般情况下,我们会将 token 存储在 sessionStorage 中,因为它只在当前会话中有效,并且在浏览器关闭后会被自动清除。
以下是存储和使用 token 的示例代码:
```javascript
// 存储 token
sessionStorage.setItem('token', 'your_token_here');
// 获取 token
const token = sessionStorage.getItem('token');
```
当我们需要使用 token 时,可以将它添加到请求头中,以便后端服务验证身份。以下是一个使用 axios 发送请求时添加 token 的示例代码:
```javascript
import axios from 'axios';
const token = sessionStorage.getItem('token');
const config = {
headers: { Authorization: `Bearer ${token}` }
};
axios.get('/api/data', config).then(response => {
console.log(response.data);
});
```
注意,这里使用的是 Bearer Token 方式,即在请求头中添加 `Authorization: Bearer <token>`。当然,具体的方案需要根据后端服务的要求来确定。
钉钉小程序 推送消息
钉钉小程序可以通过服务端接口将消息推送给用户。以下是推送消息的步骤:
1. 获取access_token
使用应用的appKey和appSecret调用获取access_token接口,获取到的access_token有两小时的有效期。
2. 构造消息体
构造消息体,包括消息类型、接收人、消息内容等信息。钉钉支持的消息类型有:文本消息、图片消息、链接消息、Markdown消息、ActionCard消息、FeedCard消息。
例如,构造一条文本消息:
```
{
"msgtype": "text",
"text": {
"content": "这是一条测试消息"
},
"at": {
"atMobiles": ["手机号码1", "手机号码2"],
"isAtAll": false
}
}
```
其中,atMobiles表示@的手机号码列表,isAtAll表示是否@所有人。
3. 调用推送接口
使用获取到的access_token和构造好的消息体,调用推送接口将消息发送给用户。
例如,推送一条文本消息:
```
POST https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=ACCESS_TOKEN
Content-Type: application/json;charset=utf-8
{
"agent_id": 123456,
"userid_list": ["userid1", "userid2"],
"msg": {
"msgtype": "text",
"text": {
"content": "这是一条测试消息"
},
"at": {
"atMobiles": ["手机号码1", "手机号码2"],
"isAtAll": false
}
}
}
```
其中,agent_id表示应用的agentId,userid_list表示接收消息的用户列表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)