mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-22 03:27:23 +08:00
代码添加行号
This commit is contained in:
@@ -33,48 +33,47 @@
|
||||
**方式1,简单粗暴**
|
||||
|
||||
``` js
|
||||
// 1、首先在登录时,将 tokenValue 存储在本地,例如:
|
||||
uni.setStorageSync('tokenValue', tokenValue);
|
||||
|
||||
// 2、在发起ajax请求的地方,获取这个值,并塞到header里
|
||||
uni.request({
|
||||
url: 'https://www.example.com/request', // 仅为示例,并非真实接口地址。
|
||||
header: {
|
||||
"content-type": "application/x-www-form-urlencoded",
|
||||
"satoken": uni.getStorageSync('tokenValue') // 关键代码, 注意参数名字是 satoken
|
||||
},
|
||||
success: (res) => {
|
||||
console.log(res.data);
|
||||
}
|
||||
});
|
||||
|
||||
// 1、首先在登录时,将 tokenValue 存储在本地,例如:
|
||||
uni.setStorageSync('tokenValue', tokenValue);
|
||||
|
||||
// 2、在发起ajax请求的地方,获取这个值,并塞到header里
|
||||
uni.request({
|
||||
url: 'https://www.example.com/request', // 仅为示例,并非真实接口地址。
|
||||
header: {
|
||||
"content-type": "application/x-www-form-urlencoded",
|
||||
"satoken": uni.getStorageSync('tokenValue') // 关键代码, 注意参数名字是 satoken
|
||||
},
|
||||
success: (res) => {
|
||||
console.log(res.data);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
**方式2,更加灵活**
|
||||
|
||||
``` js
|
||||
// 1、首先在登录时,将tokenName和tokenValue一起存储在本地,例如:
|
||||
uni.setStorageSync('tokenName', tokenName);
|
||||
uni.setStorageSync('tokenValue', tokenValue);
|
||||
|
||||
// 2、在发起ajax的地方,获取这两个值, 并组织到head里
|
||||
var tokenName = uni.getStorageSync('tokenName'); // 从本地缓存读取tokenName值
|
||||
var tokenValue = uni.getStorageSync('tokenValue'); // 从本地缓存读取tokenValue值
|
||||
var header = {
|
||||
"content-type": "application/x-www-form-urlencoded" // 防止后台拿不到参数
|
||||
};
|
||||
if (tokenName != undefined && tokenName != '') {
|
||||
header[tokenName] = tokenValue;
|
||||
// 1、首先在登录时,将tokenName和tokenValue一起存储在本地,例如:
|
||||
uni.setStorageSync('tokenName', tokenName);
|
||||
uni.setStorageSync('tokenValue', tokenValue);
|
||||
|
||||
// 2、在发起ajax的地方,获取这两个值, 并组织到head里
|
||||
var tokenName = uni.getStorageSync('tokenName'); // 从本地缓存读取tokenName值
|
||||
var tokenValue = uni.getStorageSync('tokenValue'); // 从本地缓存读取tokenValue值
|
||||
var header = {
|
||||
"content-type": "application/x-www-form-urlencoded" // 防止后台拿不到参数
|
||||
};
|
||||
if (tokenName != undefined && tokenName != '') {
|
||||
header[tokenName] = tokenValue;
|
||||
}
|
||||
|
||||
// 3、后续在发起请求时将 header 对象塞到请求头部
|
||||
uni.request({
|
||||
url: 'https://www.example.com/request', // 仅为示例,并非真实接口地址。
|
||||
header: header,
|
||||
success: (res) => {
|
||||
console.log(res.data);
|
||||
}
|
||||
|
||||
// 3、后续在发起请求时将 header 对象塞到请求头部
|
||||
uni.request({
|
||||
url: 'https://www.example.com/request', // 仅为示例,并非真实接口地址。
|
||||
header: header,
|
||||
success: (res) => {
|
||||
console.log(res.data);
|
||||
}
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
4. 只要按照如此方法将`token`值传递到后端,`sa-token`就能像传统PC端一样自动读取到`token`值,进行鉴权
|
||||
|
Reference in New Issue
Block a user