
后端
一 index.js 判断超过 60秒的时间函数
function getTokenTime(exp) {
let getTimes = parseInt(new Date().getTime() / 1000)
if ((getTimes - exp) > 60) {
console.log(getTimes - exp, getTimes, exp);
return true
}
}
二 在加入购物车接口的时候用了一下getTokenTime 这个函数,并跟 更新数据库的 token
router.post('/api/addCard', function (req, res, next) {
let goods_id = req.body.id;
let tokens = req.headers.token;
console.log(goods_id, tokens);
let tokenObj = jwt.decode(tokens)
if (getTokenTime(tokenObj.exp)) {
console.log(tokenObj.exp, "2 tokenObj.exptokenObj.exptokenObj.exp");
res.send({
data: {
code: 1000,
msg: "token已过期"
}
})
} else {
connection.query(`select * from user where tel ="${tokenObj.tel}"`, function (e, r) {
let uId = r[0].id
connection.query(`select * from shoplist where id = ${goods_id}`, function (e, r) {
let shopPrice = r[0].price
let shopTitle = r[0].title
let shopImgUrl = r[0].imgUrl
connection.query(`select * from cartlist where goods_id = ${goods_id} and uid = ${uId}`, function (e, results) {
if (results.length > 0) {
let num = results[0].num;
connection.query(`update cartlist set num = ${parseInt(num)+1 } where goods_id = ${goods_id} and uid = ${uId}`, function (err, result) {
res.send({
data: {
code: 200,
success: true,
msg: "添加成功!"
}
})
})
} else {
connection.query(`insert into cartlist (uid,goods_id,shop_price,shop_title,shop_imgUrl,num) values ("${uId}","${goods_id}","${shopPrice}","${shopTitle}","${shopImgUrl}","1") `, function (err, result) {
res.send({
data: {
code: 200,
success: true,
msg: "添加成功!"
}
})
})
}
})
})
})
}
})
三 userQuery.js 设置token有效时间 60秒
insertTel(option) {
let tel = option.tel || "新增手机号"
let pwd = option.pwd || "999999"
let jwt = require('jsonwebtoken');
let payload = {
tel: tel
}
let secret = "mixuebingcheng";
let token = jwt.sign(payload, secret, {
expiresIn: 60
})
return `insert into user (tel,pwd,imgUrl,name,token) values ("${tel}","${pwd}","../images/lvcha.jpeg","${tel}","${token}")`
}
四 在login 的 用户名密码登录 使用了登录的情况
router.post('/api/login', function (req, res, next) {
let params = {
tel: req.body.tel,
pwd: req.body.pwd
}
let jwt = require('jsonwebtoken');
let payload = {
tel: params.tel
}
let secret = "mixuebingcheng";
let token = jwt.sign(payload, secret, {
expiresIn: 60
})
connection.query(user.queuryTel(params), function (err, result) {
if (result.length > 0) {
let id = result[0].id
console.log(id, "ididi222222222222222");
connection.query(user.queuryPwd(params), function (err, result) {
if (result.length > 0) {
connection.query(`update user set token = "${token}" where id = ${id}`, function (e, r) {
})
res.send({
code: 200,
data: {
success: true,
msg: '登录成功,已更新token',
data: result[0]
}
})
} else {
res.send({
code: 302,
data: {
success: false,
msg: '密码错误'
}
})
}
})
} else {
res.send({
code: 301,
data: {
success: false,
msg: '手机号不存在'
}
})
}
})
})
前端 common api request.js
return axios(ocptions).then(v => {
let data = v.data.data
console.log(data, "data");
if (data.code == '1000') {
Indicator.close();
router.push('/login');
localStorage.removeItem('userData')
}
return new Promise((res, rej) => {
if (!v) return rej();
setTimeout(() => {
Indicator.close();
}, 300);
res(data)
});
})