403 lines
17 KiB
JavaScript
403 lines
17 KiB
JavaScript
'use strict';
|
|
|
|
const Service = require('../core/base_service');
|
|
|
|
const { Op } = require('sequelize');
|
|
|
|
const shunt = (uid) => {
|
|
return uid[uid.length - 1];
|
|
};
|
|
|
|
const fileName = 'home';
|
|
|
|
class HomeService extends Service {
|
|
// 获取卡号
|
|
async getCard(uid) {
|
|
const { ctx } = this;
|
|
try {
|
|
const where = { user_uid: uid };
|
|
const uidLastCode = shunt(uid);
|
|
const model = {
|
|
"B": ctx.model.BuserBind,
|
|
"C": ctx.model2.CuserBind,
|
|
"T": ctx.model4.TuserBind,
|
|
};
|
|
const result = await model[uidLastCode].findOne({ where });
|
|
return result || false;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "getCard";
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 搜索卡号
|
|
async searchCard(cardID) {
|
|
const { ctx } = this;
|
|
try {
|
|
const where = { card_id: cardID };
|
|
const result = await ctx.model3.BDCard.findOne({ where });
|
|
return result || false;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "searchCard";
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 绑卡
|
|
async bindCard(uid, body) {
|
|
const { ctx } = this;
|
|
try {
|
|
const { cardID, IMSI } = body;
|
|
const where = {
|
|
card_id: cardID,
|
|
card_imsi: IMSI
|
|
};
|
|
const status = shunt(uid) == 'B' ? 1 : 2;
|
|
const defaults = {
|
|
card_id: cardID,
|
|
card_imsi: IMSI,
|
|
type: 0,
|
|
status,
|
|
created_at: new Date(),
|
|
updated_at: new Date()
|
|
};
|
|
const [card] = await ctx.model3.BDCard.findOrCreate({ where, defaults });
|
|
const model = {
|
|
"B": ctx.model.BuserBind,
|
|
"C": ctx.model2.CuserBind,
|
|
"T": ctx.model4.TuserBind
|
|
};
|
|
const uidLastCode = shunt(uid);
|
|
const bindCheck = await model[uidLastCode].findOne({ where: { user_uid: uid } });
|
|
if (card.status !== 0 || bindCheck.value) {
|
|
return false;
|
|
} else {
|
|
const cardResult = await ctx.model3.BDCard.update({ status }, { where });
|
|
const bindForm = {
|
|
user_uid: uid,
|
|
card_id: cardID
|
|
};
|
|
const resultForm = {
|
|
user_uid: uid,
|
|
card_id: cardID,
|
|
type: 1,
|
|
created_at: new Date()
|
|
};
|
|
const modelName = {
|
|
"B": ctx.model.BindBrecord,
|
|
"C": ctx.model2.BindCrecord
|
|
};
|
|
const bindResult = await model[uidLastCode].create(bindForm);
|
|
const recordResult = await modelName[uidLastCode].create(resultForm);
|
|
return cardResult[0] && bindResult && recordResult ? true : false;
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "bindCard";
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 解绑
|
|
async unBindingCard(uid, body) {
|
|
const { ctx } = this;
|
|
try {
|
|
const { cardID, IMSI } = body;
|
|
const cardWhere = {
|
|
card_id: cardID,
|
|
card_imsi: IMSI
|
|
};
|
|
const cardResult = await ctx.model3.BDCard.update({ status: 0 }, { where: cardWhere });
|
|
if (cardResult[0] !== 0) {
|
|
const where = {
|
|
user_uid: uid,
|
|
card_id: cardID
|
|
};
|
|
const form = {
|
|
user_uid: uid,
|
|
card_id: cardID,
|
|
type: 0,
|
|
created_at: new Date()
|
|
};
|
|
const uidLastCode = shunt(uid);
|
|
const model = {
|
|
"B": ctx.model.BuserBind,
|
|
"C": ctx.model2.CuserBind,
|
|
};
|
|
const modelName = {
|
|
"B": ctx.model.BindBrecord,
|
|
"C": ctx.model2.BindCrecord
|
|
};
|
|
const bindResult = await model[uidLastCode].destroy({ where });
|
|
const recordResult = await modelName[uidLastCode].create(form);
|
|
return bindResult && recordResult ? true : false;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "unBindingCard";
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// B端单位管理员解绑
|
|
async unBindManager(uid, modelType, password, requestMsg) {
|
|
const { ctx } = this;
|
|
try {
|
|
const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } });
|
|
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) return false;
|
|
let where = {};
|
|
let attributes = ['uid'];
|
|
if (modelType == "single") {
|
|
where = requestMsg;
|
|
} else if (modelType == "multiple") {
|
|
let account_name = [], user_name = [], phone = [];
|
|
for (const key in requestMsg) {
|
|
requestMsg[key].forEach(element => {
|
|
account_name.push(element.account_name),
|
|
user_name.push(element.user_name),
|
|
phone.push(element.phone)
|
|
});
|
|
}
|
|
where = { account_name, user_name, phone };
|
|
} else if (modelType == "all") {
|
|
const role = { [Op.not]: 1 };
|
|
where = { organization: result.dataValues.organization, role };
|
|
}
|
|
|
|
const userResult = await ctx.model.BSide.findAll({ attributes, where });
|
|
if (!userResult[0]) return false;
|
|
let user_uid = [];
|
|
userResult.forEach(element => {
|
|
user_uid.push(element.dataValues.uid);
|
|
});
|
|
attributes = ['card_id'];
|
|
where = { user_uid };
|
|
const bindResult = await ctx.model.BuserBind.findAll({ attributes, where });
|
|
if (!bindResult[0]) return false;
|
|
let card_id = [], recordForm = [];
|
|
bindResult.forEach((element, index) => {
|
|
card_id.push(element.dataValues.card_id);
|
|
recordForm.push({ user_uid: user_uid[index], card_id: card_id[index], type: 0, created_at: new Date() });
|
|
});
|
|
where = { user_uid, card_id };
|
|
const unbindResult = await ctx.model.BuserBind.destroy({ where });
|
|
const recordResult = await ctx.model.BindBrecord.bulkCreate(recordForm);
|
|
where = { card_id };
|
|
const cardResult = await ctx.model3.BDCard.update({ status: 0 }, { where });
|
|
return unbindResult !== 0 && recordResult[0] && cardResult[0] !== 0 ? true : false;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "unBindManager";
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 获取绑定记录
|
|
async getRecord(uid, body) {
|
|
const { ctx } = this;
|
|
try {
|
|
let { offset, limit, searchOptions, sortOptions } = body;
|
|
offset = (offset - 1) * limit;
|
|
let attributes = ['card_id', 'type', 'created_at'],
|
|
where = { user_uid: uid }, order = [], dbOptions, joinSubstring = [],
|
|
substring = ['card_id'], key = ['type'], between = ['created_at'];
|
|
const uidLastCode = shunt(uid);
|
|
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.BindBrecord,
|
|
"C": ctx.model2.BindCrecord,
|
|
};
|
|
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 shortMsg(form) {
|
|
const { ctx } = this;
|
|
try {
|
|
const where = { card_id: form.card_id };
|
|
const card = await ctx.model3.BDCard.findOne({ where });
|
|
if (!card) {
|
|
ctx.app.middleware.utils.log('../../../unknownCardLog', form);
|
|
return false;
|
|
};
|
|
const model = {
|
|
1: ctx.model.BuserBind,
|
|
2: ctx.model2.CuserBind,
|
|
3: ctx.model4.TuserBind,
|
|
};
|
|
const bind = await model[card.dataValues.status].findOne({ where });
|
|
if (!bind) return false;
|
|
form.user_uid = bind.dataValues.user_uid;
|
|
const uidLastCode = shunt(form.user_uid);
|
|
const modelName = {
|
|
"B": ctx.model.BShortMsg,
|
|
"C": ctx.model2.CShortMsg,
|
|
"T": ctx.model4.TShortMsg,
|
|
};
|
|
const result = await modelName[uidLastCode].create(form);
|
|
return result || false;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "shortMsg";
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 获取历史消息
|
|
async getHistoryMsg(uid, body) {
|
|
const { ctx } = this;
|
|
try {
|
|
let { offset, limit, searchOptions, sortOptions } = body;
|
|
offset = (offset - 1) * limit;
|
|
let attributes = ['card_id', 'encode_type', 'msg_data', 'created_at'],
|
|
where = { user_uid: uid }, order = [], dbOptions, joinSubstring = [],
|
|
substring = ['card_id', 'msg_data'], key = ['encode_type'], 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 uidLastCode = shunt(uid);
|
|
const model = {
|
|
"B": ctx.model.BShortMsg,
|
|
"C": ctx.model2.CShortMsg,
|
|
"T": ctx.model4.TShortMsg,
|
|
};
|
|
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 result = await model[uidLastCode].findOne({ where: { uid } });
|
|
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) return false;
|
|
let where = {};
|
|
if (modelType == "single") {
|
|
const { card_id, encode_type, msg_data, origin_time } = requestMsg;
|
|
where = { user_uid: uid, card_id, encode_type, msg_data, created_at: origin_time };
|
|
} else if (modelType == "multiple") {
|
|
let card_id = [], encode_type = [], msg_data = [], created_at = [];
|
|
for (const key in requestMsg) {
|
|
requestMsg[key].forEach(element => {
|
|
card_id.push(element.card_id),
|
|
encode_type.push(element.encode_type),
|
|
msg_data.push(element.msg_data),
|
|
created_at.push(element.origin_time)
|
|
});
|
|
}
|
|
where = { user_uid: uid, card_id, encode_type, msg_data, created_at };
|
|
} else if (modelType == "all") {
|
|
where = { user_uid: uid };
|
|
}
|
|
const modelName = {
|
|
"B": ctx.model.BShortMsg,
|
|
"C": ctx.model2.CShortMsg,
|
|
"T": ctx.model4.TShortMsg,
|
|
};
|
|
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; |