Files
2024-09-27 01:09:14 +08:00

130 lines
3.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const sql = require("../mysql");
const redis = require("../redis");
const BroadcastChannel = require("../BroadcastChannel");
/**
* 用户已扫码但是未确认这个接口只是为了通知web端用户当前的进展状态
*/
router.get('/SweepStatus', async (req, res, next) => {
let uuid = req.query.uuid;
BroadcastChannel.SweepStatus.postMessage(uuid);
res.json({ status: 200, msg: '发送成功', data: null })
});
/**
* 移动端App确认登录按钮点击时候的ajax处理
*/
router.get('/login', async (req, res, next) => {
let uuid = req.query.uuid; // uuid
let uid = req.query.uid; // 用户id
let RedisUid = await redis.get(uuid);
if (RedisUid == null || RedisUid == undefined) {
res.json({
status: 404,
msg: '二维码失效或不存在',
data: null
});
} else {
if (RedisUid.length !== 0) {
res.json({
status: 404,
msg: '已登录,请勿重复扫码',
data: null
});
} else {
// 查询用户信息
let user = (await sql.query({ sql: 'select * from user where id = ?', data: [ uid ] }))[0];
// 保存用户数据到对应key的redis中
redis.set(uuid, JSON.stringify(user));
res.json({
status: 200,
msg: '扫码成功',
data: null
});
}
}
});
/**
* TODO
* 被移动端App轮询的接口主要让移动端知道我移动端现在已登录的用户所对
* 应的web端是否在请求让我授权登录如果在web端在请求登录那么移动端自
* 动进入授权页面让用户授权,授权后结束轮询,否则一直轮询
* PS: uniapp 无法连接到 Node.js 的 socket.io所以只能用很low而且
* 很消耗移动端资源的轮询了。等解决这个问题自然用socket重写这里实现
*/
router.get('/InquiryWebIsLogining', async (req, res, next) => {
let uid = req.query.uid; // 用户id
// console.log("uid: ", uid);
let UserId = await redis.get(uid);
// 没有查询到redis的 uid 数据的话
if (UserId == null || UserId == undefined) {
res.json({
status: 404,
msg: '没有对应的web端请求登录',
data: null
});
} else {
// UserId 有数值但是为空表示web请求登录但移动端未授权
if (UserId.length == 0) {
res.json({
status: 200,
msg: 'web端正在请求授权',
data: null
});
// UserId 有数值但是不为空表示web请求登录移动端已授权并且redis未超时
} else {
res.json({
status: 500,
msg: '授权成功,请勿重复授权',
data: null
})
}
}
});
/**
* 授权web端登录的接口主要被移动端App调用拿到移动端给的用户id
* 然后从数据库获取用户的详细数据通过广播把用户数据通知给web端
*/
router.get('/AuthorizedWeb', async (req, res, next) => {
let uid = req.query.uid; // 用户id
let UserId = await redis.get(uid);
if (UserId == null || UserId == undefined) {
res.json({
status: 404,
msg: '没有对应的web端请求登录',
data: null
});
} else {
// UserId 有数值,但是为空
if (UserId.length == 0) {
// 查询用户信息
let user = (await sql.query({ sql: 'select * from user where id = ?', data: [ uid ] }))[0];
// 保存用户数据到对应key的redis中
redis.set(uid, JSON.stringify(user));
BroadcastChannel.AuthorizedWeb.postMessage(user);
res.json({
status: 200,
msg: '成功授权',
data: null
});
// UserId 有数值但是不为空表示web请求登录移动端已授权并且redis未超时
} else {
res.json({
status: 404,
msg: '授权成功,请勿重复授权',
data: null
})
}
}
});
module.exports = router;