问题:Uni-App的图形验证码phpsession每次请求都变导致无法校验
因为Uni-App的请求都是提示验证码失效明明输入的是正确的,后来发现每次发送请求的phpsessionid都不一样,导致后台获取session中的图形验证码,获取不到
前端代码:
//获取图形验证码
async refreshCaptcha() {
try {
const [err, res] = await uni.request({
url: BASE_URL.BASE_URL + 'ShopHome/Member/verify',
method: 'GET',
responseType: 'arraybuffer',
params: { t: Date.now() },
header: { 'Cache-Control': 'no-cache' }
});
//设置sessionid,发送验证码的时候需要提交这个cookie加到header里
uni.removeStorageSync('captchaKey');
var cookie = res.header['Set-Cookie']
//字符串分割成数组
var cookieArray = cookie.split(/,(?=[^,]*=)/)
//分号拼接数组
var newCookie = cookieArray.join(';')
console.log(newCookie);
uni.setStorageSync('captchaKey', newCookie);
//if (err || res.statusCode !== 200) return;
// 使用uni-app官方转换方法 ✅
const base64 = uni.arrayBufferToBase64(res.data);
this.captchaImage = `data:image/png;base64,${base64}`;
} catch (err) {
uni.showToast({ title: '加载失败', icon: 'none' });
}
},
//获取验证码
getCode() {
let that = this;
if (!this.form.username || !uni.$u.test.mobile(this.form.username)) {
uni.$u.toast('请输入正确的手机号')
return
}
if(!this.form.verify)
{
uni.$u.toast('请输入图形验证码')
return
}
if (this.$refs.uCode.canGetCode) {
// 模拟向后端请求验证码
uni.showLoading({
title: '正在获取验证码'
})
let data = {
mobile: this.form.username,
verify:this.form.verify,
type: 4,
}
uni.request({
url: BASE_URL.BASE_URL + 'ShopHome/Member/smssend',
method: 'GET',
header: {
'Content-Type': 'application/x-www-form-urlencoded',
cookie: uni.getStorageSync('captchaKey')
},
data: data,
success: function(res) {
uni.hideLoading();
if (res.data.code == 200) {
uni.$u.toast('验证码已发送');
that.$refs.uCode.start();
} else {
uni.$u.toast(res.data.msg)
}
},
})
} else {
uni.$u.toast('倒计时结束后再发送');
}
},