uni.request({ url: config.baseUrl + '/API/Task/getToken', method: 'GET', success(res) { that.baseUrl = res.data.domain that.token = res.data.token; // 获取到 token 后再进行上传 that.imgList.push(e.tempFilePaths[0]); const fileName = e.tempFilePaths[0].substr(e.tempFilePaths[0] .lastIndexOf('/') + 1); // 获取文件名 const fileExt = fileName.substr(fileName.lastIndexOf('.') + 1) .toLowerCase(); // 获取文件拓展名,并转换为小写 const today = new Date().toISOString().slice(0, 10); // 获取当天日期,格式为 yyyy-mm-dd const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate(); const hour = now.getHours(); const minute = now.getMinutes(); const currentDateTime = ${year}-${month}-${day}-${hour}:${minute}:00; var nums = []; that.uploadCount++; // 上传次数加一 const newFileName = currentDateTime + '' + that.uploadCount + '.' + fileExt; // 生成新的文件名,以上传次数为前缀 const path = that.fromData.c_name + '/' + that.fromData.a_name + '/' + that.fromData.m_name + '/' + that.fromData.mold + '/' + that.fromData.m_title + '/' + that.fromData.r_name + '-' + that.uploadCount + '/' + that.fromData.o_name + '/' + today; const key = path + '/' + newFileName that.imgData.push(key) uni.uploadFile({ url: 'https://upload-z1.qiniup.com', filePath: e.tempFilePaths[0], name: 'file', formData: { token: res.data.token, key: path + '/' + newFileName // 将路径和文件名拼接起来 }, success(res) { const data = JSON.parse(res.data); const key = data.key const parts = key.split("."); const KeynewName = parts.slice(0, -1).join(".") + ".jpg"; that.baseList.push(that.baseUrl + KeynewName) that.Keyname.push(KeynewName) for (var i = 0; i < that.Keyname.length; i++) { var num = that.Keyname[i].split('/').slice(-1)[0].split('.')[0].split('')[1]; nums.push(num); } console.log('存在的文件名:'+nums) }, fail(erro) { console.log(erro); } }); }, fail(err) { console.log("获取 Token 失败:", err); } });我想让这里的uploadCount先判断nums中存在哪些,uploadCount只能为1,2,3 如果nums存在1,2那么uploadCount只能为3
时间: 2024-04-05 16:29:03 浏览: 137
可以在上传之前先判断 nums 数组中是否存在 1 和 2,并将结果保存在两个变量中,比如 hasOne 和 hasTwo。然后根据这两个变量来判断 uploadCount 的值,如果 hasOne 和 hasTwo 都为 true,那么 uploadCount 只能为 3,否则 uploadCount 可以为 1、2 或 3。可以参考下面的代码:
```
// 判断 nums 数组中是否存在 1 和 2
var hasOne = nums.includes('1');
var hasTwo = nums.includes('2');
// 根据 hasOne 和 hasTwo 来判断 uploadCount 的值
if (hasOne && hasTwo) {
that.uploadCount = 3;
} else {
that.uploadCount = Math.floor(Math.random() * 2) + 1;
}
```
这段代码中,使用了 includes 方法来判断 nums 数组中是否存在 1 和 2,如果存在则将 hasOne 和 hasTwo 设置为 true,否则为 false。然后根据 hasOne 和 hasTwo 的值来判断 uploadCount 的值,如果都为 true,则 uploadCount 只能为 3,否则可以随机生成 1 或 2。
相关问题
为什么在我的页面能取到登录时的token 但是在调用接口时显示token失效//调用获取用户信息接口 getUserInfo() { const baseUrl = 'http://192.168....' uni.request({ url: baseUrl + '/api/v1/...', data: { //请求参数 id: this.id }, header: { Authorization: this.token //自定义请求头信息 }, //请求方法 默认为GET method: "GET", success: (res) => { console.log(res); this.user = res.data.data.user }, fail: function(err) { console.log(err) } }); },onLoad(option) { this.setStorage() this.getStorage() this.token = uni.getStorageSync('token') console.log('我的页面的token'); console.log(this.token); this.getUserInfo() }
在您的代码中,调用接口时显示token失效的原因可能是您在获取token后没有及时更新`this.token`的值,导致在调用`getUserInfo()`时传递的是过期的token。
要解决这个问题,您可以尝试在每次调用接口之前,确保`this.token`的值是最新的,可以通过以下步骤进行修改:
1. 在`getLogin()`方法中,在获取到新的token值后,调用`setStorage()`方法存储新的token,并更新`this.token`的值。
```javascript
success: (res) => {
console.log(res);
this.id = res.data.data.userInfo.id;
this.token = res.data.data.token; // 获取新的token值
console.log(this.token);
this.setStorage(); // 将新的token存储到本地缓存中
uni.reLaunch({
url: '../mine/mine?id=' + this.id
});
},
```
2. 在`onLoad()`方法中,通过`uni.getStorageSync('token')`来获取最新的token值,并更新`this.token`。
```javascript
onLoad(option) {
this.setStorage();
this.getStorage();
this.token = uni.getStorageSync('token'); // 获取最新的token值
console.log('我的页面的token');
console.log(this.token);
this.getUserInfo();
},
```
通过这样的修改,确保在每次调用接口时传递的都是最新的token值,应该能够解决token失效的问题。请注意在其他需要使用token的地方,也要更新对应的token值。
这是我登录页面的一段代码 如何修改//调用登录接口 getLogin() { const baseUrl = 'http://192.168....' uni.request({ url: baseUrl + '/api/v1/....', data: { //请求参数 mobile: this.phoneNumber, deptId: 204, verifyCode: this.code, openId: '' }, header: { Authorization: 'Bearer SJMj+yR1sZ65h4Rl3ghKh9OPIQ8tioGwpYTXnustCk7CjfyslkCtUhqLH26UjLbWpJy79Lrf/3ilIUWJxyeGSHfZu0aZ/7B9c1q1kkBxiycKtt/+KsKJTifmQVA/0yv8lmm0XbvjzLyc4/N/lUTRmg==' //自定义请求头信息 }, //请求方法 默认为GET method: "POST", success: (res) => { console.log(res); this.id = res.data.data.userInfo.id this.token = res.data.data.token console.log(this.token); this.setStorage() uni.reLaunch({ //跳转到家庭成员列表页面 url: '../mine/mine?id=' + this.id }); }, fail: function(err) { console.log(err) } }); }, //缓存数据 setStorage() { //这是一个同步接口。建议使用这个方法 //第一个参数本地缓存中的指定的 key //第二个参数需要存储的内容 uni.setStorageSync('token', this.token); console.log('存储成功'); },现在有一个需求 从登录页面获取接口本地存储token 登录成功后跳转到我的页面,但是现在token过期了 该如何修改代码
如果您的token过期了,您可以尝试通过以下步骤修改代码:
1. 找到 `getLogin()` 方法中的 `success` 回调函数。
2. 在回调函数中,获取到新的token值,替换掉原来的token值。可以通过 `res.data.data.token` 获取新的token值。
3. 调用 `setStorage()` 方法,将新的token存储到本地缓存中。
修改后的代码示例:
```javascript
success: (res) => {
console.log(res);
this.id = res.data.data.userInfo.id;
this.token = res.data.data.token; // 获取新的token值
console.log(this.token);
this.setStorage(); // 将新的token存储到本地缓存中
uni.reLaunch({
url: '../mine/mine?id=' + this.id
});
},
```
这样修改后,当您登录成功后,会将新的token存储到本地缓存中,并跳转到我的页面。请确保在其他需要使用token的地方,从本地缓存中读取最新的token值使用。
请注意,以上只是一个示例,具体的修改方式可能还需要根据您的需求和代码结构进行调整。另外,为了保证安全性,建议在实际应用中采用更加严谨的token管理方式,例如定时刷新token等。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](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)