contact.js
7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var express = require('express');
var session = require('express-session');
const nodemailer = require('nodemailer');
var router = express.Router();
const EAMIL_USER = "service@leadstecgz.com";
const EAMIL_TO = ["sales@list.leadstecgz.com","lawrence@leadstec.com.hk"];
const EMAIL_PASSWORD ="Lt12345678";
//const EMAIL_SMTP_SERVER = "smtp.qiye.aliyun.com";
const EMAIL_SMTP_SERVER = "smtp.exmail.qq.com";
const axios = require('axios');
//验证码部分
var app = express();
var svgCaptcha = require('svg-captcha');
//设置过期时间等参数,过期时间为1分钟
router.use(session({secret: 'keyboard cat', resave: false, saveUninitialized: true, cookie: {maxAge: 60000}}));
router.get('/api/getCaptcha', function(req, res, next) {
//return getCaptcha();
var captcha = svgCaptcha.create({
// 翻转颜色
inverse: false,
// 字体大小
fontSize: 36,
// 噪声线条数
noise: 2,
// 宽度
width: 80,
// 高度
height: 30,
});
// 保存到session,忽略大小写
req.session.contactUsCaptcha = captcha.text.toLowerCase();
res.setHeader('Content-Type', 'image/svg+xml');
res.write(String(captcha.data));
res.end();
});
//负责发邮件和发erp等,另外需要两三个api,负责将参数处理
//需要看如何在node post数据
router.post('/sendCustomerInfoToEmailAndErp', async function (req, res) {
console.log("in sendCustomerInfoToEmailAndErp: ");
var result = {};
// 三种情况:1.官网,有验证码,都发送
// 2.谷歌,无验证码且token符合,都发送,标题需更改
// 3.来自tvp,无验证码且data符合,只发送erp
if (!req.body || (!req.body.captcha && req.body.google_key != "token leadstecgz-lt12345678" && req.headers.origin != "https://www.leadstecgz.com")) {
console.log("No captcha!");
res.send("No captcha!");
return;
} else {
req.body.interested = "TVP";
if (req.body.google_key == "token leadstecgz-lt12345678") {
// 发送google输入的内容
// 需要获取json中column_id为对应字段的string_value值,再写入
console.log("From google");
var userColumnData = req.body['user_column_data'];
if (!userColumnData || !(userColumnData instanceof Array)) {
console.log("No property user_column_data!");
res.send("No property user_column_data!");
return;
}
userColumnData.forEach(function(item){
var columnName = item["column_name"];
if (columnName == 'Full Name') {
req.body.name = item["string_value"];
} else if (columnName == 'Company Name') {
req.body.company = item["string_value"];
} else if (columnName == 'User Phone') {
req.body.phone = item["string_value"];
} else if (columnName == 'User Email') {
req.body.email = item["string_value"];
}
});
req.body.message = "From google ads." ;
req.body.subject = "From Google Ads" ;
console.log("Google body: "+JSON.stringify(req.body));
} else if (req.body.captcha){
console.log("From Leadstec");
var captcha = req.body.captcha.toLowerCase();
req.body.interested = "Adobe";
req.body.subject = "Leadstec 官网留言";
result.mailStatus = {};
if (captcha != req.session.contactUsCaptcha) {
result.mailStatus.EmailErrcode = 40001;
result.mailStatus.EmailErrmsg = "Wrong captcha";
res.send(result);
return;
}
}
var bodyStatus = contentVerification(req.body);
console.log("Verification stats: "+JSON.stringify(bodyStatus));
if (bodyStatus.code != 40000) {
result.statusCode = bodyStatus.code;
result.statusMsg = bodyStatus.message;
res.send(result);
} else {
if (req.body.isTvp) {
console.log("From TVP");
} else{
console.log("Not From TVP");
result.mailStatus = await emailSend(req.body);
}
result.erpStatus = await erpSend(req.body);
console.log("result: "+JSON.stringify(result));
res.send(result);
}
}
});
//目前未确定返回码用法
function contentVerification(body) {
var status = {};
status.code = 40000;
console.log("In contentVerification");
var bodyText = JSON.stringify(body).toLowerCase();
if(bodyText.indexOf("sex") >= 0 || bodyText.indexOf("dating") >= 0) {
status.code = 40002;
status.message = "Illegal text";
return status;
}
var email = body.email;
var emailExp = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
if (!emailExp.test(email)) {
status.code = 40003;
status.message = "Wrong Email";
return status;
}
var phone = body.phone;
var phoneExp = /(\w+?)(\d+)\b$/;
if (!phoneExp.test(phone)) {
status.code = 40004;
status.message = "Wrong number";
return status;
}
return status;
}
async function emailSend(body) {
let mailOpts, smtpTrans;
smtpTrans = nodemailer.createTransport({
host: EMAIL_SMTP_SERVER,
port: 465,
secure: true,
auth: {
user: EAMIL_USER,
pass: EMAIL_PASSWORD
}
});
mailOpts = {
from: EAMIL_USER,
to: EAMIL_TO,
subject: body.subject,
text: `Name:${body.name} \n Company:${body.company} \n Phone:${body.phone} \n Email:${body.email} \n Messages: ${body.message}`
};
var mailResult = {};
var sendingMail = function (mailOpts) {
return new Promise((resolve,reject)=>{
smtpTrans.sendMail(mailOpts, function (error, response) {
if (error) {
mailResult.EmailErrcode = 40003;
mailResult.EmailErrmsg = "contact failed:"+error.message;
reject();
} else {
mailResult.EmailErrcode = 0;
mailResult.EmailErrmsg = "ok";
resolve();
}
});
});
};
await sendingMail(mailOpts);
console.log("mailResult"+JSON.stringify(mailResult));
return mailResult;
}
async function erpSend(body) {
var lead_name = body.name;
var company_name = body.company;
var email_id = body.email;
var phone = body.phone;
var interested = body.interested;
var notes = body.message;
var data = {
lead_name: lead_name,
company_name: company_name,
email_id: email_id,
phone: phone,
status: "Lead",
interested: interested,
organization_lead: 1,
notes: notes
};
if (body.isTvp) {
data.territory = "Hong Kong";
}
//注意header要与data分开放
var erpResult = {};
await axios.post('https://erp.leadstecgz.com/api/resource/Lead', data, {
timeout: 1000 * 600,
headers: {'Authorization': 'token 9c264725c0d1c08:fdbf83cd1a03e1c','Content-Type':'application/json'} //设置header信息
}).then((res) => {
console.log(`Status: ${res.status}`);
console.log('Body: ', res.data);
erpResult.ErpErrcode = res.status;
erpResult.ErpErrmsg = res.body;
}).catch((err) => {
erpResult.ErpErrmsg = err;
console.error("err: "+err);
});
console.log("erpResult"+JSON.stringify(erpResult));
return erpResult;
}
module.exports = router;