|
var querystring = require('querystring');
|
|
var {getToken,customeId,httprequest}=require("./httprequest")
|
var {postFiles}=require("./httppost");
|
var FormData=require("form-data")
|
var fs=require("fs");
|
var temp_category_id=5738
|
function addTemplate(title,content,callBackFunc,errCallBack){
|
var url="http://edmapi.rushmail.com/api.php?module=wc-template&action=template-add";
|
var data={token: getToken(),customer_id:customeId,category_id:temp_category_id,
|
name:title+new Date(),mode:"editor",subject:title,tpl_content:content,tpl_file:""}
|
var post_data = querystring.stringify(data)
|
httprequest(url,post_data,function(ret){
|
if(ret.code=="success"){
|
console.log("添加模板成功")
|
var tempId=ret.status;
|
if(callBackFunc)
|
callBackFunc(tempId)
|
|
}else{
|
console.log("添加模板失败",ret.message)
|
if(errCallBack){
|
errCallBack(ret.message)
|
}
|
}
|
},errCallBack);
|
}
|
function addTemplateFiles(tempId,req,callBackFunc,errCallBackFunc){
|
|
|
var formData=new FormData();
|
|
formData.append("attachment",files);
|
formData.append("token",getToken());
|
formData.append("customer_id",customeId);
|
formData.append("template_id",tempId);
|
|
|
|
|
}
|
/**
|
* 上传模板文件
|
* @param {*} tempId 模板ID
|
* @param {*} files 文件列表
|
* @param {*} callBackFunc 回调
|
*/
|
function addTemplateFile(tempId,files,callBackFunc,errCallBack)
|
{
|
var postURL="/api.php?module=wc-template&action=attachment-upload-system"
|
var options={
|
host:"edmapi.rushmail.com",
|
port:"80",
|
path:postURL,
|
method:"POST"
|
}
|
//token: getToken(),customer_id:customeId
|
var fileDataInfo=[{urlKey:"token", urlValue: getToken()},{urlKey:"customer_id",urlValue:customeId},
|
{urlKey:"template_id",urlValue:tempId}];
|
|
var countFiles=files.length;
|
for(var file of files){
|
if(!fs.existsSync(file)){
|
console.log(file+"不存在!")
|
if(errCallBack){
|
errCallBack(file+"不存在!")
|
}
|
return false;
|
}
|
var fileLists=[{urlKey:"attachment",urlValue:file}];
|
var uploadFileIndex=0;
|
postFiles(options,fileDataInfo,fileLists,function(e){
|
if(e.code=="success"){
|
uploadFileIndex++
|
if(callBackFunc && uploadFileIndex==countFiles){
|
callBackFunc()
|
}
|
}else{
|
console.log("上传文件失败!",e.message);
|
if(errCallBack){
|
errCallBack("上传文件失败")
|
}
|
}
|
},errCallBack);
|
}
|
}
|
|
exports.addTemplate=addTemplate;
|
exports.addTemplateFile=addTemplateFile;
|