'use strict'; const Service = require('../core/base_service'); const { Op } = require('sequelize'); const shunt = (uid) => { return uid[uid.length - 1]; }; const fileName = 'home'; const redisType = { "B": "db0", "C": "db1", "T": "db5", }; class HomeService extends Service { // 保存挂载点 async saveMountpoint(uid, body) { const { ctx } = this; const redis = ctx.app.redis; try { const uidLastCode = shunt(uid); const form = { user_uid: uid, mountpoint: body.mountpoint, password: body.password }; let redisKey; if (uidLastCode == 'B') { const bside = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } }); if (!bside || bside.organization == null) return false; form.organization = bside.organization; redisKey = bside.organization; } else { redisKey = uid; } const model = { "B": ctx.model.BNtripAdmin, "C": ctx.model2.CNtripAdmin, "T": ctx.model4.TNtripAdmin }; const save = await redis.get(redisType[uidLastCode]).get(redisKey + 'mountpoint'); if (save) { await redis.get(redisType[uidLastCode]).set(redisKey + 'mountpoint', JSON.stringify([body.mountpoint, ...JSON.parse(save)])); } else { const saved = await model[uidLastCode].findAll({ where: uidLastCode == 'B' ? { organization: redisKey } : { user_uid: redisKey } }); let mountpoint = []; saved.forEach(element => { mountpoint.push(element.dataValues.mountpoint); }); mountpoint.push(body.mountpoint); await redis.get(redisType[uidLastCode]).set(redisKey + 'mountpoint', JSON.stringify(mountpoint)); } const result = await model[uidLastCode].create(form); const systemForm = { mountpoint: body.mountpoint, password: body.password, status: uidLastCode == 'B' ? 1 : uidLastCode == 'C' ? 2 : 3, created_at: new Date(), updated_at: new Date() }; await ctx.model3.Mountpoint.create(systemForm); if (result) await redis.get(redisType[uidLastCode]).set(body.mountpoint, body.password); form.befor_edit = ''; form.created_at = new Date(); form.updated_at = new Date(); const modelName = { "B": ctx.model.BNtripRecord, "C": ctx.model2.CNtripRecord, "T": ctx.model4.TNtripRecord }; await modelName[uidLastCode].create(form); await redis.get(redisType[uidLastCode]).bgsave(); return result || false; } catch (error) { const blockName = fileName + "--" + "saveMountpoint"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 修改挂载点密码 async editPassword(uid, body) { const { ctx } = this; const redis = ctx.app.redis; try { const form = { user_uid: uid, mountpoint: body.beforEdit, }; const uidLastCode = shunt(uid); if (uidLastCode == 'B') { const bside = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } }); if (!bside || bside.organization == null) return false; form.organization = bside.organization; } const whereAdmin = { mountpoint: body.beforEdit }; const modelType = { "B": ctx.model.BNtripAdmin, "C": ctx.model2.CNtripAdmin, "T": ctx.model4.TNtripAdmin }; const findAdmin = await modelType[uidLastCode].findOne({ where: whereAdmin }); if (!findAdmin) return false; const updateAdminForm = { password: body.password }; const result = await modelType[uidLastCode].update(updateAdminForm, { where: whereAdmin }); await ctx.model3.Mountpoint.update(updateAdminForm, { where: whereAdmin }); if (result[0] !== 0) { await redis.get(redisType[uidLastCode]).set(body.beforEdit, body.password); await redis.get(redisType[uidLastCode]).bgsave(); } return result[0] == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "editPassword"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 修改挂载点名称 async editMountpoint(uid, body) { const { ctx } = this; const redis = ctx.app.redis; try { const form = { user_uid: uid, mountpoint: body.beforEdit }; let redisKey; const uidLastCode = shunt(uid); if (uidLastCode == 'B') { const bside = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } }); if (!bside || bside.organization == null) return false; form.organization = bside.organization; redisKey = bside.organization; } else { redisKey = uid; } const save = await redis.get(redisType[uidLastCode]).get(redisKey + 'mountpoint'); if (save) { await redis.get(redisType[uidLastCode]).set(redisKey + 'mountpoint', JSON.stringify([body.afterEdit, ...JSON.parse(save).filter(item => item !== body.beforEdit)])); } else { await redis.get(redisType[uidLastCode]).set(redisKey + 'mountpoint', JSON.stringify([body.afterEdit])); } const whereAdmin = { mountpoint: body.beforEdit }; const whereRecord = { mountpoint: body.beforEdit, befor_edit: '' }; const modelType = { "B": ctx.model.BNtripAdmin, "C": ctx.model2.CNtripAdmin, "T": ctx.model4.TNtripAdmin }; const model = { "B": ctx.model.BNtripRecord, "C": ctx.model2.CNtripRecord, "T": ctx.model4.TNtripRecord }; const modelName = { "B": ctx.model.BNtripClient, "C": ctx.model2.CNtripClient, "T": ctx.model4.TNtripClient }; const findAdmin = await modelType[uidLastCode].findOne({ where: whereAdmin }); if (!findAdmin) return false; const findRecord = await model[uidLastCode].findOne({ where: whereRecord }); if (!findRecord) { form.befor_edit = ''; form.created_at = new Date(); form.updated_at = new Date(); const createResult = await model[uidLastCode].create(form); if (!createResult) return false; } const updateAdminForm = { mountpoint: body.afterEdit }; const updateRecordForm = { mountpoint: body.afterEdit, befor_edit: body.beforEdit, updated_at: new Date() }; const result = await modelType[uidLastCode].update(updateAdminForm, { where: whereAdmin }); await ctx.model3.Mountpoint.update(updateAdminForm, { where: whereAdmin }); await model[uidLastCode].update(updateRecordForm, { where: whereRecord }); await modelName[uidLastCode].update(updateAdminForm, { where: whereAdmin }); if (result[0] !== 0) { await redis.get(redisType[uidLastCode]).del(body.beforEdit); await redis.get(redisType[uidLastCode]).set(body.afterEdit, findAdmin.dataValues.password); } await redis.get(redisType[uidLastCode]).bgsave(); return result[0] == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "editMountpoint"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 删除挂载点 async delMountpoint(uid, body) { const { ctx } = this; const redis = ctx.app.redis; try { let redisKey; const uidLastCode = shunt(uid); if (uidLastCode == 'B') { const bside = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } }); if (!bside || bside.organization == null) return false; redisKey = bside.organization; } else { redisKey = uid; } const save = await redis.get(redisType[uidLastCode]).get(redisKey + 'mountpoint'); if (save) { await redis.get(redisType[uidLastCode]).del(body.mountpoint); await redis.get(redisType[uidLastCode]).set(redisKey + 'mountpoint', JSON.stringify([...JSON.parse(save).filter(item => item !== body.mountpoint)])); await redis.get(redisType[uidLastCode]).bgsave(); } const where = { mountpoint: body.mountpoint }; const modelType = { "B": ctx.model.BNtripAdmin, "C": ctx.model2.CNtripAdmin, "T": ctx.model4.TNtripAdmin }; const modelName = { "B": ctx.model.BNtripClient, "C": ctx.model2.CNtripClient, "T": ctx.model4.TNtripClient }; const model = { "B": ctx.model.BNtripMsg, "C": ctx.model2.CNtripMsg, "T": ctx.model4.TNtripMsg }; const modelRecord = { "B": ctx.model.BNtripRecord, "C": ctx.model2.CNtripRecord, "T": ctx.model4.TNtripRecord }; const resetList = await modelType[uidLastCode].destroy({ where }); await modelName[uidLastCode].destroy({ where }); await model[uidLastCode].destroy({ where }); await modelRecord[uidLastCode].destroy({ where }); return resetList.length == 0 ? false : resetList; } catch (error) { const blockName = fileName + "--" + "delMountpoint"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 获取挂载点 async getMountpoint(uid) { const { ctx } = this; const redis = ctx.app.redis; try { let where; const uidLastCode = shunt(uid); const model = { "B": ctx.model.BNtripAdmin, "C": ctx.model2.CNtripAdmin, "T": ctx.model4.TNtripAdmin, }; let redisKey; if (uidLastCode == 'B') { const bside = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 } } }); if (!bside || bside.organization == null) return false; where = { organization: bside.organization }; redisKey = bside.organization; } else { where = { user_uid: uid }; redisKey = uid; } const mountpoint = await redis.get(redisType[uidLastCode]).get(redisKey + 'mountpoint'); if (mountpoint) { return JSON.parse(mountpoint); } else { const result = await model[uidLastCode].findAll({ where }); if (result.length == 0) return false; let mountpoint = []; result.forEach(element => { mountpoint.push(element.dataValues.mountpoint); }); await redis.get(redisType[uidLastCode]).set(redisKey + 'mountpoint', JSON.stringify(mountpoint)); await redis.get(redisType[uidLastCode]).bgsave(); return mountpoint; } } catch (error) { const blockName = fileName + "--" + "getMountpoint"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 搜索挂载点 async searchMountpoint(uid, body) { const { ctx } = this; const redis = ctx.app.redis; try { const uidLastCode = shunt(uid); const model = { "B": ctx.model.BNtripAdmin, "C": ctx.model2.CNtripAdmin, "T": ctx.model4.TNtripAdmin, }; const mountpoint = await redis.get(redisType[uidLastCode]).get(body.mountpoint); if (mountpoint) { return JSON.parse(mountpoint); } else { const where = { mountpoint: body.mountpoint }; const result = await model[uidLastCode].findOne({ where }); return result || false; } } catch (error) { const blockName = fileName + "--" + "searchMountpoint"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 全局寻获挂载点 async findMountpoint(body) { const { ctx } = this; const redis = ctx.app.redis; try { const where = { mountpoint: body.mountpoint }; const result = await ctx.model3.Mountpoint.findOne({ where }); if (result) { return result; } const attributes = ['mountpoint', 'password']; // 尝试从 Redis 获取数据 const [bside, cside, contest] = await Promise.all([ redis.get(redisType['B']).get(body.mountpoint), redis.get(redisType['C']).get(body.mountpoint), redis.get(redisType['T']).get(body.mountpoint) ]); // 检查 Redis 中是否有匹配的记录 const redisResult = bside || cside || contest; if (redisResult) { return { mountpoint: body.mountpoint, password: redisResult, status: bside ? 1 : cside ? 2 : 3 }; } // 如果 Redis 中没有匹配的记录,尝试从数据库中获取 const [bAdmin, cAdmin, tAdmin] = await Promise.all([ ctx.model.BNtripAdmin.findOne({ attributes, where }), ctx.model2.CNtripAdmin.findOne({ attributes, where }), ctx.model4.TNtripAdmin.findOne({ attributes, where }) ]); // 检查数据库中是否有匹配的记录 const admin = bAdmin || cAdmin || tAdmin; if (admin) { admin.dataValues.status = bAdmin ? 1 : cAdmin ? 2 : 3; return admin.dataValues; } return false; } catch (error) { const blockName = fileName + "--" + "findMountpoint"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 查看ntrip-clinet密码 async viewPassword(uid, body) { const { ctx } = this; try { const { mountpoint } = body; const uidLastCode = shunt(uid); const model = { "B": ctx.model.BNtripClient, "C": ctx.model2.CNtripClient, "T": ctx.model4.TNtripClient }; const attributes = ['base']; const where = { user_uid: uid, mountpoint }; const user = await model[uidLastCode].findOne({ attributes, where }); return user || false; } catch (error) { const blockName = fileName + "--" + "viewPassword"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 获取挂载点记录 async getRecord(uid, body) { const { ctx } = this; try { let bside; const uidLastCode = shunt(uid); if (uidLastCode == 'B') { bside = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } }); if (!bside || bside.organization == null) return false; } let { offset, limit, searchOptions, sortOptions } = body; offset = (offset - 1) * limit; let attributes = ['mountpoint', 'befor_edit', 'created_at', 'updated_at'], where = uidLastCode == 'B' ? { organization: bside.organization } : { user_uid: uid }, order = [], dbOptions, joinSubstring = [], substring = ['mountpoint', 'befor_edit'], key = [], between = ['created_at', 'updated_at']; attributes.forEach(element => { if (searchOptions[element]) { if (searchOptions[element] == '*空') { if (joinSubstring.includes(element)) { where[element] = null; } else { where[element] = [null, '']; } } else if (substring.includes(element)) { where[element] = { [Op.substring]: searchOptions[element] }; } else if (key.includes(element)) { where[element] = searchOptions[element] == "1" ? uidLastCode === 'B' ? 1 : 2 : 0; } else if (between.includes(element)) { where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] }; } } if (sortOptions[element] == true) { order.push([element, 'ASC']); } else if (sortOptions[element] == false) { order.push([element, 'DESC']); } }); dbOptions = { attributes, offset, limit, where, order }; const model = { "B": ctx.model.BNtripRecord, "C": ctx.model2.CNtripRecord, "T": ctx.model4.TNtripRecord, }; const result = await model[uidLastCode].findAndCountAll(dbOptions); return result.rows.length == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "getRecord"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 存储消息记录 async saveMsg(uid, form) { const { ctx } = this; try { const where = { mountpoint: form.mountpoint }; const model = { "B": ctx.model.BNtripAdmin, "C": ctx.model2.CNtripAdmin, "T": ctx.model4.TNtripAdmin, }; const uidLastCode = shunt(uid); const bind = await model[uidLastCode].findOne({ where }); if (!bind) return false; form.user_uid = bind.dataValues.user_uid; if (uidLastCode == 'B') { form.organization = bind.dataValues.organization; } const modelName = { "B": ctx.model.BNtripMsg, "C": ctx.model2.BNtripMsg, "T": ctx.model4.BNtripMsg }; const result = await modelName[uidLastCode].create(form); return result || false; } catch (error) { const blockName = fileName + "--" + "saveMsg"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 设置ntrip-clinet密码 async setPassword(uid, body) { const { ctx } = this; try { const uidLastCode = shunt(uid); const { mountpoint, password } = body; const model = { "B": ctx.model.BSide, "C": ctx.model2.CSide, "T": ctx.model4.Account }; const user = await model[uidLastCode].findOne({ where: { uid, status: { [Op.not]: 2 } } }); if (!user) return false; const form = { user_uid: uid, mountpoint, }; if (uidLastCode == 'B') { form.organization = user.dataValues.organization; } const modelName = { "B": ctx.model.BNtripClient, "C": ctx.model2.CNtripClient, "T": ctx.model4.TNtripClient }; // 将字符串编码为 UTF-8 字节数组 const bytes = new TextEncoder().encode(user.dataValues.account_name + ':' + password); // 将字节数组转换为二进制字符串 let binary = ''; bytes.forEach(byte => binary += String.fromCharCode(byte)); const existence = await modelName[uidLastCode].findOne({ where: form }); if (existence) { const existence = await modelName[uidLastCode].update({ base: btoa(binary) }, { where: form }); return existence[0] == 0 ? false : existence; } else { form.base = btoa(binary); const result = await modelName[uidLastCode].create(form); return result || false; } } catch (error) { const blockName = fileName + "--" + "setPassword"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 获取ntrip-clinet密码 async getPassword(body) { const { ctx } = this; try { const { mountpoint, base, status } = body; const model = { "1": ctx.model.BNtripClient, "2": ctx.model2.CNtripClient, "3": ctx.model4.TNtripClient }; const user = await model[status].findOne({ where: { mountpoint, base } }); return user || false; } catch (error) { const blockName = fileName + "--" + "getPassword"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 获取历史消息 async getHistoryMsg(uid, body) { const { ctx } = this; try { let bside; const uidLastCode = shunt(uid); if (uidLastCode == 'B') { bside = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } }); if (!bside || bside.organization == null) return false; } let { offset, limit, searchOptions, sortOptions } = body; offset = (offset - 1) * limit; let attributes = ['mountpoint', 'original_msg', 'created_at'], where = uidLastCode == 'B' ? { organization: bside.organization } : { user_uid: uid }, order = [], dbOptions, joinSubstring = [], substring = ['mountpoint', 'original_msg'], key = [], between = ['created_at']; attributes.forEach(element => { if (searchOptions[element]) { if (searchOptions[element] == '*空') { if (joinSubstring.includes(element)) { where[element] = null; } else { where[element] = [null, '']; } } else if (substring.includes(element)) { where[element] = { [Op.substring]: searchOptions[element] }; } else if (key.includes(element)) { where[element] = searchOptions[element] == "1" ? true : false; } else if (between.includes(element)) { where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] }; } } if (sortOptions[element] == true) { order.push([element, 'ASC']); } else if (sortOptions[element] == false) { order.push([element, 'DESC']); } }); dbOptions = { attributes, offset, limit, where, order }; const model = { "B": ctx.model.BNtripMsg, "C": ctx.model2.BNtripMsg, "T": ctx.model4.BNtripMsg }; const result = await model[uidLastCode].findAndCountAll(dbOptions); return result.rows.length == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "getHistoryMsg"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 删除历史消息 async delHistoryMsg(uid, modelType, password, requestMsg) { const { ctx } = this; try { const uidLastCode = shunt(uid); const model = { "B": ctx.model.BSide, "C": ctx.model2.CSide, "T": ctx.model4.Account, }; const userWhere = uidLastCode == 'B' ? { uid, status: { [Op.not]: 2 }, role: 1 } : { uid }; const result = await model[uidLastCode].findOne({ where: userWhere }); if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) return false; let where = {}; if (modelType == "single") { const { mountpoint, original_msg, origin_time } = requestMsg; where = uidLastCode == 'B' ? { organization: bside.organization, mountpoint, original_msg, created_at: origin_time } : { user_uid: uid, mountpoint, original_msg, created_at: origin_time } } else if (modelType == "multiple") { let mountpoint = [], original_msg = [], created_at = []; for (const key in requestMsg) { requestMsg[key].forEach(element => { mountpoint.push(element.mountpoint), original_msg.push(element.original_msg), created_at.push(element.origin_time) }); } where = uidLastCode == 'B' ? { organization: bside.organization, mountpoint, original_msg, created_at } : { user_uid: uid, mountpoint, original_msg, created_at } } else if (modelType == "all") { where = uidLastCode == 'B' ? { organization: bside.organization } : { user_uid: uid } } const modelName = { "B": ctx.model.BNtripMsg, "C": ctx.model2.BNtripMsg, "T": ctx.model4.BNtripMsg }; const resetList = await modelName[uidLastCode].destroy({ where }); return resetList.length == 0 ? false : resetList; } catch (error) { const blockName = fileName + "--" + "delHistoryMsg"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 获取单位用户 async bsideManager(uid, body) { const { ctx } = this; try { const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } }); if (!result || result.organization == null) return false; let { offset, limit, searchOptions } = body; offset = (offset - 1) * limit; let attributes = ['account_name', 'user_name', 'phone'], where = { organization: result.organization, role: { [Op.not]: 1 } }, dbOptions, joinSubstring = []; attributes.forEach(element => { if (searchOptions[element]) { if (searchOptions[element] == '*空') { if (joinSubstring.includes(element)) { where[element] = null; } else { where[element] = [null, '']; } } else { where[element] = { [Op.substring]: searchOptions[element] }; } } }); dbOptions = { attributes, offset, limit, where }; const userList = await ctx.model.BSide.findAndCountAll(dbOptions); return userList.rows.length == 0 ? false : userList; } catch (error) { const blockName = fileName + "--" + "bsideManager"; ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error); return 'error'; } } } module.exports = HomeService;