contact.js
6.07 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
var express = require('express');
var session = require('express-session');
const nodemailer = require('nodemailer');
var router = express.Router();
const EAMIL_USER = "service@leadstecgz.com";
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();
});
/* Post Contact Form. */
router.post('/sendmail', function (req, res) {
var captcha = req.body.captcha;
var bodyText = JSON.stringify(req.body).toLowerCase();
var result = {};
if (captcha != req.session.contactUsCaptcha) {
result.errcode = 40001;
result.errmsg = "wrong captcha";
res.send(result);
} else if(bodyText.indexOf("sex") >= 0 || bodyText.indexOf("dating") >= 0) {
result.errcode = 40002;
result.errmsg = "illegal text";
res.send(result);
} else {
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: "sales@leadstec.com",
subject: 'Leadstec 官网留言',
/*text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`*/
text: `Name:${req.body.name} \n Company:${req.body.company} \n Phone:${req.body.phone} \n Email:${req.body.email} \n Messages: ${req.body.message}`
};
smtpTrans.sendMail(mailOpts, function (error, response) {
if (error) {
result.errcode = 40003;
result.errmsg = "contact failed:"+error.message;
res.send(result);
} else {
result.errcode = 0;
result.errmsg = "ok";
res.send(result);
}
});
}
});
//负责发邮件和发erp等,另外需要两三个api,负责将参数处理
//需要看如何在node post数据
//注意邮箱目前还是自己的邮箱
router.post('/sendCustomerInfoToEmailAndErp', function (req, res, next) {
console.log("in sendCustomerInfoToEmailAndErp: ");
//无验证码则log google内容
if (req.body.captcha==null) {
console.log("none captcha");
console.log("google header: "+req.headers);
console.log("google body: "+req.body);
res.send("none captcha");
} else {
var captcha = req.body.captcha;
var bodyText = JSON.stringify(req.body).toLowerCase();
var result = {};
if (captcha != req.session.contactUsCaptcha) {
result.errcode = 40001;
result.errmsg = "wrong captcha";
res.send(result);
} else if(bodyText.indexOf("sex") >= 0 || bodyText.indexOf("dating") >= 0) {
result.errcode = 40002;
result.errmsg = "illegal text";
res.send(result);
} else {
//发送邮件和erp
emailSend(req.body);
erpSend(req.body);
}
}
});
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: "sales@leadstec.com",
subject: 'Leadstec 官网留言',
text: `Name:${body.name} \n Company:${body.company} \n Phone:${body.phone} \n Email:${body.email} \n Messages: ${body.message}`
};
smtpTrans.sendMail(mailOpts, function (error, response) {
if (error) {
result.errcode = 40003;
result.errmsg = "contact failed:"+error.message;
res.send(result);
} else {
result.errcode = 0;
result.errmsg = "ok";
res.send(result);
}
});
}
function erpSend(body) {
var lead_name = body.name;
var company_name = body.company;
var email_id = body.email;
var mobile_no = body.phone;
var status = "Opportunity";
var data = {
lead_name: lead_name,
company_name: company_name,
email_id: email_id,
mobile_no: mobile_no,
status: "Opportunity"
};
/*var data = {access_token:'41_chmBK5GZ4RvJg6y-M23qLC39LcLEWvu_B3w4k-0ySYaRrLH9iOKZbH5ss-aCQPwxp-CbIf2BqEnkbFd38rtXmz7GmsUknQSCB6uQ3TAglyj2Mezhld0_CALqtZvupa8DwGXVlH0j0y6SZPnpABLaAAAFGQ'}
console.log("in erpSend"+JSON.stringify(data));
axios.post('https://api.weixin.qq.com/cgi-bin/user/get', JSON.stringify({
params: data,
})).then((res) => {
console.log(`Status1111: ${res.status}`);
console.log('Body1111: ', res.data);
}).catch((err) => {
//目前报错Request failed with status code 403
console.error("err1111"+err);
});*/
//注意header要与data分开放
axios.post('http://erp.leadstecgz.com/api/resource/Lead', data, {
headers: {'Authorization': 'token 9c264725c0d1c08:fdbf83cd1a03e1c','Content-Type':'application/json'}//设置header信息
}).then((res) => {
console.log(`Status: ${res.status}`);
console.log('Body: ', res.data);
}).catch((err) => {
console.error("err: "+err);
});
}
module.exports = router;