Commit 6e107fb8 by Sxy

fix: 代码规范

parent a4f2ea31
...@@ -11,7 +11,7 @@ class OrgService extends ServiceBase { ...@@ -11,7 +11,7 @@ class OrgService extends ServiceBase {
const uone = await this.db.models.user.findOne({ where: { org_id: orgid } }); const uone = await this.db.models.user.findOne({ where: { org_id: orgid } });
if (!uone) { if (!uone) {
// 先检查是否组织下有人员存在 // 先检查是否组织下有人员存在
return this.db.transaction(async (t) => { return this.db.transaction(async (t) => {
const inst = await self.dao.model.findById(orgid); const inst = await self.dao.model.findById(orgid);
const parentid = inst.org_id; const parentid = inst.org_id;
await inst.destroy({ force: true, transaction: t }); await inst.destroy({ force: true, transaction: t });
...@@ -22,10 +22,13 @@ class OrgService extends ServiceBase { ...@@ -22,10 +22,13 @@ class OrgService extends ServiceBase {
order: [['code', 'ASC']], order: [['code', 'ASC']],
where: { id: parentid }, transaction: t, where: { id: parentid }, transaction: t,
include: [ include: [
{ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ {
{ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']] }, model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
] }, { model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']] },
] }); ],
},
],
});
return orgparent.orgs; return orgparent.orgs;
}); });
} }
...@@ -34,7 +37,7 @@ class OrgService extends ServiceBase { ...@@ -34,7 +37,7 @@ class OrgService extends ServiceBase {
async update(p, q, req) { async update(p, q, req) {
const self = this; const self = this;
const cmkey = p.comkey;// 如果是来自租户界面的修改 const cmkey = p.comkey;// 如果是来自租户界面的修改
return this.db.transaction(async (t) => { return this.db.transaction(async (t) => {
p.isLeaf = p.isPosition; p.isLeaf = p.isPosition;
const orgupdate = await self.dao.model.findOne({ where: { id: p.id }, transaction: t }); const orgupdate = await self.dao.model.findOne({ where: { id: p.id }, transaction: t });
await self.dao.model.update(p, { where: { id: p.id }, transaction: t }); await self.dao.model.update(p, { where: { id: p.id }, transaction: t });
...@@ -60,14 +63,19 @@ class OrgService extends ServiceBase { ...@@ -60,14 +63,19 @@ class OrgService extends ServiceBase {
order: [['code', 'ASC']], order: [['code', 'ASC']],
where: { id: orgupdate.org_id }, transaction: t, where: { id: orgupdate.org_id }, transaction: t,
include: [ include: [
{ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ {
{ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
{
model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
],
},
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, { model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
] }, ],
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, },
] },
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, { model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
] }); ],
});
return orgparent.orgs; return orgparent.orgs;
}); });
} }
...@@ -78,10 +86,10 @@ class OrgService extends ServiceBase { ...@@ -78,10 +86,10 @@ class OrgService extends ServiceBase {
} }
return null; return null;
} }
async changePos(toorgid, uid) { async changePos(toorgid, uid) {
// 查询出当前用户,设置当前用户的orgid为修改目标 // 查询出当前用户,设置当前用户的orgid为修改目标
const self = this; const self = this;
return this.db.transaction(async (t) => { return this.db.transaction(async (t) => {
const ufind = await self.db.models.user.findById(uid); const ufind = await self.db.models.user.findById(uid);
...@@ -89,7 +97,8 @@ class OrgService extends ServiceBase { ...@@ -89,7 +97,8 @@ class OrgService extends ServiceBase {
where: { id: toorgid }, where: { id: toorgid },
include: [ include: [
{ model: self.db.models.role, as: 'roles' }, { model: self.db.models.role, as: 'roles' },
] }); ],
});
ufind.org_id = toorgid; ufind.org_id = toorgid;
ufind.opath = org.orgpath; ufind.opath = org.orgpath;
if (org.isMain) { // 如果是主岗 if (org.isMain) { // 如果是主岗
...@@ -110,9 +119,9 @@ class OrgService extends ServiceBase { ...@@ -110,9 +119,9 @@ class OrgService extends ServiceBase {
const self = this; const self = this;
const cmkey = p.comkey; const cmkey = p.comkey;
return this.db.transaction(async (t) => { return this.db.transaction(async (t) => {
if (cmkey) { if (cmkey) {
const tmpcompany = await self.db.models.company.findOne({ where: { companykey: cmkey }, transaction: t }); const tmpcompany = await self.db.models.company.findOne({ where: { companykey: cmkey }, transaction: t });
p.company_id = tmpcompany.id; p.company_id = tmpcompany.id;
} }
const roles = await self.db.models.role.findAll({ where: { id: { [self.db.Op.in]: p.Roles } } }); const roles = await self.db.models.role.findAll({ where: { id: { [self.db.Op.in]: p.Roles } } });
...@@ -126,15 +135,20 @@ class OrgService extends ServiceBase { ...@@ -126,15 +135,20 @@ class OrgService extends ServiceBase {
order: [['code', 'ASC']], order: [['code', 'ASC']],
where: { id: orgnew.org_id }, transaction: t, where: { id: orgnew.org_id }, transaction: t,
include: [ include: [
{ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ {
{ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
{
model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
],
},
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, { model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
] }, ],
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, },
] },
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, { model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
] }); ],
});
return orgparent.orgs; return orgparent.orgs;
}); });
} }
...@@ -143,32 +157,42 @@ class OrgService extends ServiceBase { ...@@ -143,32 +157,42 @@ class OrgService extends ServiceBase {
order: [['code', 'ASC']], order: [['code', 'ASC']],
where: { id }, where: { id },
include: [ include: [
{ model: this.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ {
{ model: this.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ model: this.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
{
model: this.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
{ model: this.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
],
},
{ model: this.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, { model: this.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
] }, ],
{ model: this.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, },
] },
{ model: this.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, { model: this.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
] }); ],
});
return org.orgs; return org.orgs;
} }
async initOrgs(company, appid) { async initOrgs(company, appid) {
const self = this; const self = this;
return this.db.transaction(async (t) => { return this.db.transaction(async (t) => {
const org = await self.dao.model.findOne({ const org = await self.dao.model.findOne({
order: [['code', 'ASC']], order: [['code', 'ASC']],
// where:{name:company.name,company_id:company.id,app_id:appid},transaction:t, // where:{name:company.name,company_id:company.id,app_id:appid},transaction:t,
where: { name: company.name, company_id: company.id }, transaction: t, where: { name: company.name, company_id: company.id }, transaction: t,
include: [ include: [
{ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ {
{ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [ model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
{
model: self.db.models.org, as: 'orgs', order: [['code', 'ASC']], include: [
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
],
},
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, { model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
] }, ],
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, },
] },
{ model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] }, { model: self.db.models.role, as: 'roles', attributes: ['id', 'code', 'name'] },
] }); ],
});
if (org != null) { if (org != null) {
return org; return org;
} }
...@@ -176,7 +200,7 @@ class OrgService extends ServiceBase { ...@@ -176,7 +200,7 @@ class OrgService extends ServiceBase {
code: `root${company.id}`, code: `root${company.id}`,
name: company.name, name: company.name,
isLeaf: false, isLeaf: false,
isPostion: false, isPosition: false,
isMain: false, isMain: false,
orgpath: '/', orgpath: '/',
company_id: company.id, company_id: company.id,
......
const system = require('../../../system');
const ServiceBase = require('../../sve.base');
class MsgHistoryService extends ServiceBase {
constructor() {
super(ServiceBase.getDaoName(MsgHistoryService));
this.msgnoticeDao = system.getObject('db.msg.msgnoticeDao');
this.userDao = system.getObject('db.auth.userDao');
this.redisClient = system.getObject('util.redisClient');
}
async saveMsg(msg) {
const self = this;
console.log('save msg ', msg);
// 事务
await this.db.transaction(async (t) => {
// 1.保存聊天信息
msg = await self.dao.create(msg, t);
// 2.保存好友信息
await self.msgnoticeDao.saveNotice(msg, t);
});
return msg;
}
async pushBusinessLicenseMsg(senderId, targetId, businessLicense_id) {
if (!businessLicense_id) {
return 0;
}
const notice = await this.msgnoticeDao.findOne({ fromId: senderId, toId: targetId });
if (notice && notice.businessLicense_id == businessLicense_id) {
return 0;
}
const senderUser = await this.userDao.findById(senderId);
const targetUser = await this.userDao.findById(targetId);
const senderChannel = `${senderUser.app_id}${senderUser.id}`;
const targetChannel = `${targetUser.app_id}${targetUser.id}`;
const sender = `${senderUser.app_id}${senderUser.id}${senderUser.headUrl}`;
const target = `${targetUser.app_id}${targetUser.id}${targetUser.headUrl}`;
const msg = {
msgType: 'mryzLicense',
sender,
senderId,
target,
targetId,
content: businessLicense_id,
isRead: false,
businessLicense_id,
};
const obj = await this.saveMsg(msg);
const bl = await this.businesslicenseDao.findById(businessLicense_id);
msg.businessLicense = bl;
msg.id = obj.id;
msg.created_at = obj.created_at;
this.redisClient.publish(senderChannel, JSON.stringify(msg));
this.redisClient.publish(targetChannel, JSON.stringify(msg));
return 1;
}
async getChatList(senderId, targetId, maxId, pageSize) {
const sql = 'SELECT * FROM `msghistory` WHERE id < :maxId AND ((senderId = :senderId AND targetId = :targetId) OR (targetId = :senderId AND senderId = :targetId)) ORDER BY id DESC LIMIT :pageSize ';
const params = { senderId, targetId, maxId, pageSize };
const list = await this.dao.customQuery(sql, params);
if (!list || list.length == 0) {
return [];
}
const licenseIds = [];
const msgIds = [];
list.forEach((item) => {
if (item.msgType == 'mryzLicense') {
licenseIds.push(Number(item.businessLicense_id));
}
msgIds.push(item.id);
});
if (licenseIds.length > 0) {
const licenseSql = `SELECT * FROM yz_business_license WHERE id IN (${licenseIds.join(',')}) `;
const licenseList = await this.businesslicenseDao.customQuery(licenseSql);
const licenseMap = [];
licenseList.forEach((item) => {
licenseMap[`id${item.id}`] = item;
});
list.forEach((item) => {
if (item.msgType == 'mryzLicense') {
item.businessLicense = licenseMap[`id${item.businessLicense_id}`];
}
});
}
const self = this;
setTimeout(() => {
self.setRead(senderId, targetId, list);
}, 1000);
return list;
}
async setRead(senderId, targetId, list) {
if (!list || list.length == 0) {
return;
}
const target = await this.userDao.findById(targetId);
if (!target) {
return;
}
const pushIds = [];
for (const item of list) {
if (item.isRead || senderId != item.targetId) {
continue;
}
pushIds.push(item.id);
}
if (pushIds.length == 0) {
return;
}
this.dao.updateByWhere({ isRead: true }, { where: { id: { [this.db.Op.in]: pushIds } } });
const channel = `${target.app_id}${target.id}`;
const rs = await this.redisClient.publish(channel, JSON.stringify({ type: 'readmsg', data: pushIds }));
console.log(rs, '------------------------------------------ publish result ');
}
async readMsg(userId, id) {
const msg = await this.dao.findById(id);
if (!msg || userId != msg.targetId) {
return 0;
}
msg.isRead = true;
await msg.save();
const user = await this.userDao.findById(msg.senderId);
if (!user) {
return 0;
}
const channel = `${user.app_id}${user.id}`;
return await this.redisClient.publish(channel, JSON.stringify({ type: 'readmsg', data: [msg.id] }));
}
}
module.exports = MsgHistoryService;
const system = require('../../../system');
const ServiceBase = require('../../sve.base');
class MsgNoticeService extends ServiceBase {
constructor() {
super(ServiceBase.getDaoName(MsgNoticeService));
this.userDao = system.getObject('db.auth.userDao');
this.msghistoryDao = system.getObject('db.msg.msghistoryDao');
}
getApp(appkey) {
return this.cacheManager.AppCache.cacheApp(appkey);
}
async getUserList(userId) {
const list = await this.dao.model.findAll({
where: {
fromId: userId,
},
order: [
['updated_at', 'DESC'],
],
raw: true,
});
if (!list || list.length == 0) {
return [];
}
const msgIds = [];
const businessLicenseIds = [];
const userIds = [];
for (var item of list) {
msgIds.push(item.lastMsgId);
businessLicenseIds.push(item.businessLicense_id);
userIds.push(item.toId);
}
const msgMap = [];
const businessLicenseMap = [];
const userMap = [];
const unreadMap = [];
// 最后一条聊天记录
if (msgIds.length > 0) {
const msgList = await this.msghistoryDao.customQuery(`SELECT * FROM msghistory WHERE id IN (${msgIds.join(',')}) `);
msgList.forEach((item) => {
msgMap[`id${item.id}`] = item;
});
}
// 最后一次聊天关联执照
if (businessLicenseIds.length > 0) {
const licenseList = await this.businesslicenseDao.customQuery(`SELECT * FROM yz_business_license WHERE id IN (${businessLicenseIds.join(',')}) `);
const serviceTypeIds = [];
for (var item of licenseList) {
serviceTypeIds.push(item.serviceTypeOneId);
serviceTypeIds.push(item.serviceTypeTwoId);
}
if (serviceTypeIds.length > 0) {
const sql = `SELECT id, name FROM \`p_service_type\` WHERE id IN (${serviceTypeIds.join(',')}) `;
const typeList = await this.dao.customQuery(sql);
var typeMap = [];
if (typeList && typeList.length > 0) {
for (const t of typeList) {
typeMap[`type_id_${t.id}`] = t.name;
if (t.id == item.serviceTypeOneId) {
item.serviceTypeOneName = t.name;
} else if (t.id == item.serviceTypeTwoId) {
item.serviceTypeTwoName = t.name;
} else {}
}
}
}
for (var item of licenseList) {
item.serviceTypeOneName = typeMap[`type_id_${item.serviceTypeOneId}`];
item.serviceTypeTwoName = typeMap[`type_id_${item.serviceTypeTwoId}`];
}
licenseList.forEach((item) => {
businessLicenseMap[`id${item.id}`] = item;
});
}
// 聊天好友用户信息
if (userIds.length > 0) {
const userList = await this.userDao.customQuery(`SELECT * FROM p_user WHERE id IN (${userIds.join(',')}) `);
userList.forEach((item) => {
userMap[`id${item.id}`] = item;
});
}
// 未读消息数量
const unreadList = await this.userDao.customQuery(`SELECT senderId, COUNT(1) AS num FROM \`msghistory\` WHERE isRead = 0 AND targetId = ${userId} GROUP BY senderId `);
unreadList.forEach((item) => {
unreadMap[`id${item.senderId}`] = item.num;
});
const rs = [];
for (const i in list) {
var item = list[i];
item.lastMsg = msgMap[`id${item.lastMsgId}`];
item.businessLicense = businessLicenseMap[`id${item.businessLicense_id}`];
item.friend = userMap[`id${item.toId}`];
item.unreadCount = unreadMap[`id${item.toId}`] || 0;
rs.push(item);
}
return rs;
}
async countUnread(userId) {
debugger;
const unreadList = await this.userDao.customQuery(`SELECT COUNT(1) AS num FROM \`msghistory\` WHERE isRead = 0 AND targetId = ${userId}`);
let count = 0;
if (unreadList && unreadList.length > 0) {
count = unreadList[0].num || 0;
}
return count;
}
}
module.exports = MsgNoticeService;
...@@ -21,10 +21,10 @@ module.exports = function (app) { ...@@ -21,10 +21,10 @@ module.exports = function (app) {
const invokeObj = System.getObject(`api.${classPath}`); const invokeObj = System.getObject(`api.${classPath}`);
if (invokeObj.doexec) { if (invokeObj.doexec) {
p = invokeObj.doexec.apply(invokeObj, params); p = invokeObj.doexec.apply(invokeObj, params);
p.then((r) => {
res.end(JSON.stringify(r));
});
} }
p.then((r) => {
res.end(JSON.stringify(r));
});
}); });
app.post('/api/:gname/:qname/:method', (req, res) => { app.post('/api/:gname/:qname/:method', (req, res) => {
let classPath = req.params.qname; let classPath = req.params.qname;
......
...@@ -40,9 +40,9 @@ module.exports = function (app) { ...@@ -40,9 +40,9 @@ module.exports = function (app) {
const invokeObj = system.getObject(`web.${classPath}`); const invokeObj = system.getObject(`web.${classPath}`);
if (invokeObj.doexec) { if (invokeObj.doexec) {
p = invokeObj.doexec.apply(invokeObj, params); p = invokeObj.doexec.apply(invokeObj, params);
p.then((r) => {
res.end(JSON.stringify(r));
});
} }
p.then((r) => {
res.end(JSON.stringify(r));
});
}); });
}; };
...@@ -45,15 +45,15 @@ class MsgHandler { ...@@ -45,15 +45,15 @@ class MsgHandler {
const invokeObj = system.getObject(`web.${msg.pkgname}.${msg.cls}`); const invokeObj = system.getObject(`web.${msg.pkgname}.${msg.cls}`);
if (invokeObj[msg.method]) { if (invokeObj[msg.method]) {
p = invokeObj[msg.method].apply(invokeObj, [msg.data]); p = invokeObj[msg.method].apply(invokeObj, [msg.data]);
p.then((r) => {
fn(r);
}).then(() => {
console.log('call success');
})
.catch((err) => {
console.log(err);
});
} }
p.then((r) => {
fn(r);
}).then(() => {
console.log('call success');
})
.catch((err) => {
console.log(err);
});
}); });
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment