Commit 9035c153 by 庄冰

redis

parent 9f00c122
......@@ -56,7 +56,7 @@ class Template extends APIBase {
// opResult = await this.templateinfoSve.editTemplateTdk(pobj);
// break;
case "getTemplateAndLinkInfo"://根据链接参数获取模板链接信息
opResult = await this.templatelinkSve.getTemplateAndLinkInfo(pobj);
opResult = await this.templatelinkSve.getTemplateAndLinkInfo2(pobj);
break;
case "submitFormRecord"://提交表单记录
var a=0;
......
......@@ -130,6 +130,7 @@ class TemplateinfoService extends ServiceBase {
updateObj["form_id"] = ab.form_id;
}
await this.dao.update(updateObj);
this.delRedisInfoByLinkByTempId(templateInfo.id);
return system.getResultSuccess();
}
/**
......@@ -157,6 +158,8 @@ class TemplateinfoService extends ServiceBase {
return system.getResultFail(-300,"未知模板");
}
await this.dao.update({id:templateInfo.id,is_enabled:ab.is_enabled});
// 获取模板对应模板链接,并删除其redis缓存
this.delRedisInfoByLinkByTempId(templateInfo.id);
return system.getResultSuccess();
}
/**
......@@ -197,6 +200,8 @@ class TemplateinfoService extends ServiceBase {
}
updateObj.id = templateInfo.id;
await this.dao.update(updateObj);
// 获取模板对应模板链接,并删除其redis缓存
this.delRedisInfoByLinkByTempId(templateInfo.id);
return system.getResultSuccess();
}
......@@ -230,45 +235,36 @@ class TemplateinfoService extends ServiceBase {
// 更新模板
let obj = {template_content: ab.template_content};
if(ab.form_id){
obj["form_id"] = ab.form_id;
}
await template.update(obj);
// 获取模板对应模板链接,并删除其redis缓存
let template_id = template.id;
console.log("template_id is " + template_id);
let tls = await this.templatelinkDao.findAll({"template_id": template_id});
console.log("tls.length: " + tls.length);
tls.forEach(async (tl) => {
console.log(tl);
let obj = {
template_id: tl.template_id,
channel_code: tl.channel_code,
else_channel_param: tl.else_channel_param,
business_type_code: tl.business_type_code,
lauch_type_code: tl.lauch_type_code,
marketing_subject_code: tl.marketing_subject_code
};
let shaStr = sha256(JSON.stringify(obj));
await this.redisClient.delete(shaStr);
});
this.delRedisInfoByLinkByTempId(template.id);
return system.getResultSuccess();
//根据模板编码获取模板信息
// var templateInfo = await this.dao.model.findOne({
// where:{code:ab.code},raw:true
// });
// if(!templateInfo || !templateInfo.id){
// return system.getResultFail(-300,"未知模板");
// }
// if(templateInfo.is_enabled===1){
// return system.getResultFail(-301,"该模板正在使用中,不能执行此操作");
// }
// var updateObj = {id:templateInfo.id,template_content:ab.template_content};
// if(ab.form_id){
// updateObj["form_id"] = ab.form_id;
// }
// await this.dao.update(updateObj);
// return system.getResultSuccess();
}
//清除模板缓存数据
async delRedisInfoByLinkByTempId(template_id){
try {
let tls = await this.templatelinkDao.findAll({"template_id": template_id});
tls.forEach(async (tl) => {
let obj = {
is_enabled:1,
template_id: tl.template_id?tl.template_id.toString():"",
channel_code: tl.channel_code,
else_channel_param: tl.else_channel_param,
business_type_code: tl.business_type_code,
lauch_type_code: tl.lauch_type_code,
marketing_subject_code: tl.marketing_subject_code
};
let shaStr = sha256(JSON.stringify(obj));
shaStr = "templink_"+template_id+"_"+shaStr;
await this.redisClient.delete(shaStr);
});
} catch (e) {
console.log(e)
}
}
}
module.exports = TemplateinfoService;
\ No newline at end of file
......@@ -189,6 +189,9 @@ class TemplatelinkService extends ServiceBase {
}
ab = checkres;
await this.dao.update(ab);
if(ab.code){
this.delRedisInfoByLinkCode(ab.code);
}
return system.getResultSuccess();
}
......@@ -248,6 +251,7 @@ class TemplatelinkService extends ServiceBase {
}
}
await this.dao.update({id:linkinfo.id,is_enabled:ab.is_enabled});
this.delRedisInfoByLinkCode(linkinfo.code);
return system.getResultSuccess();
}
/**
......@@ -413,49 +417,48 @@ class TemplatelinkService extends ServiceBase {
let linkinfo; // 模板链接信息
let addObj; // 储存访问信息
linkObj = {
template_id:ab.template_id,
is_enabled:1,
template_id:ab.template_id?ab.template_id.toString():"",
channel_code:ab.channel_code,
else_channel_param:ab.else_channel_param,
business_type_code:ab.business_type_code,
lauch_type_code:ab.lauch_type_code,
marketing_subject_code:ab.marketing_subject_code
};
// linkObj = {...ab};
// delete linkObj.device;
console.log(JSON.stringify(linkObj),"222222222222222222222222222222222");
shaStr = await sha256(JSON.stringify(linkObj));
shaStr = "templink_"+ab.template_id+"_"+shaStr;
rtn = await this.redisClient.get(shaStr); // 先试图从redis读取数据
//---- 从redis中读取到数据
if (!!rtn) {
if (rtn) {
rtnObj = JSON.parse(rtn);
linkinfo = rtnObj["linkinfo"];
console.log("========================================================\n从redis中读取数据\n=============================================================");
//---- 未从redis中读取到数据
} else {
// 获取模板链接信息
linkinfo = await this.dao.model.findOne({
attributes:[
"code",
"name",
"channel_name",
"business_type_name",
"lauch_type_name",
"marketing_subject_name"
],
where:linkObj,raw:true
});
if(!linkinfo || !linkinfo.id){
return system.getResultFail(-300,"未知链接");
}
if(!linkinfo.is_enabled || linkinfo.is_enabled!==1){
return system.getResultFail(-301,"无效链接,该链接未投放");
if(!linkinfo){
return system.getResultFail(-300,"未知链接或该链接未投放");
}
// 模板链接存在,获取模板信息
let tempObj = {id:ab.template_id}; // 用于查询templateinfo的条件
let tempObj = {id:ab.template_id,is_enabled:1,}; // 用于查询templateinfo的条件
let templateinfo = await this.templateinfoDao.model.findOne({ // 查询结果
attributes:["title","keyword","describe","pic_url","template_content"],
where:tempObj,raw:true
});
if(!templateinfo || !templateinfo.id){
return system.getResultFail(-400,"未知模板");
}
if(!templateinfo.is_enabled || templateinfo.is_enabled!==1){
return system.getResultFail(-401,"无效模板,该模板未启用");
if(!templateinfo){
return system.getResultFail(-400,"未知模板或该模板未启用");
}
rtnObj = { // 组合模板链接和模板信息
templateinfo,
linkinfo
......@@ -474,9 +477,41 @@ class TemplatelinkService extends ServiceBase {
marketing_subject_name:linkinfo.marketing_subject_name,
client_ip:pobj.clientIp
};
await this.browsingrecordsDao.create(addObj);//添加链接浏览记录
this.browsingrecordsDao.create(addObj);//添加链接浏览记录
return system.getResultSuccess(rtnObj);
}
//清除模板链接缓存数据
async delRedisInfoByLinkCode(code){
try {
var ab = await this.dao.model.findOne({
where:{code:code},raw:true
});
if(ab){
var linkObj = {
is_enabled:1,
template_id:ab.template_id?ab.template_id.toString():"",
channel_code:ab.channel_code,
else_channel_param:ab.else_channel_param,
business_type_code:ab.business_type_code,
lauch_type_code:ab.lauch_type_code,
marketing_subject_code:ab.marketing_subject_code
};
// is_enabled:1,
// template_id:ab.template_id,
// channel_code:ab.channel_code,
// else_channel_param:ab.else_channel_param,
// business_type_code:ab.business_type_code,
// lauch_type_code:ab.lauch_type_code,
// marketing_subject_code:ab.marketing_subject_code
console.log(JSON.stringify(linkObj),"111111111111111111111111111111");
var shaStr = await sha256(JSON.stringify(linkObj));
shaStr = "templink_"+ab.template_id+"_"+shaStr;
await this.redisClient.delete(shaStr);
}
} catch (e) {
console.log(e)
}
}
}
module.exports = TemplatelinkService;
\ No newline at end of file
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