-
Notifications
You must be signed in to change notification settings - Fork 1
/
MikeCRM.ts
149 lines (127 loc) · 4 KB
/
MikeCRM.ts
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
import 'ts-polyfill/lib/es2019-object';
import { Object as LCObject, Query } from 'leanengine';
import { JSDOM } from 'jsdom';
class UserTemp extends LCObject {}
const number_pattern = /[\d\.]+/,
ID_pattern = /[\w\-]+/;
interface OrderItem {
name?: string;
count: number;
price: number;
}
interface Order {
id: string;
paymentId: string;
list: OrderItem[];
sum: OrderItem;
}
export default class MikeCRM {
static parseMeta(box: Element) {
const [id, creator, location, date] = Array.from(
box.querySelectorAll('td > span'),
({ textContent }) => textContent.trim()
);
return {
id: +number_pattern.exec(id)[0],
creator,
location,
date: new Date(date)
};
}
static parseOrder(box: Element): Order {
const [orderId, orderList] = Array.from(
box.querySelector('tbody').children
);
const list = Array.from(
orderList.querySelectorAll('tr'),
({
children: [
{ textContent: A },
{ textContent: B },
{ textContent: C }
]
}) => ({
name: A.trim(),
count: +number_pattern.exec(B)[0],
price: +number_pattern.exec(C)[0]
})
);
return {
id: ID_pattern.exec(orderId.querySelector('p').textContent)[0],
paymentId: ID_pattern.exec(
orderId.querySelector('p:last-child').textContent
)[0],
list,
sum: list.reduce(
(sum, { count, price }) => {
(sum.count += count), (sum.price += price);
return sum;
},
{ count: 0, price: 0 }
)
};
}
static parseMail(HTML: string) {
const {
window: { document }
} = new JSDOM(HTML);
const body = [
...document.querySelectorAll(
'.mk_mailBody > td > table > tbody > tr'
)
];
const fields = body.slice(-4)[0].querySelectorAll('tbody tr');
if (body.length > 6) var order = this.parseOrder(body[2]);
return {
...this.parseMeta(body[1]),
title: document
.querySelector('.mk_mailTitle + p > span')
.textContent.trim(),
order,
fields: Object.fromEntries(
Array.from(fields, ({ children: [A, B] }) => {
const content = B.children[0]
? Array.from(B.children, ({ textContent }) =>
textContent.trim()
).filter(Boolean)
: B.textContent.trim();
return [
A.textContent.trim(),
content.length > 1 ? content : content[0]
];
})
)
};
}
id: number;
date: Date;
location: string;
creator: string;
title: string;
fields: any;
order: Order;
constructor(HTML: string) {
Object.assign(this, MikeCRM.parseMail(HTML));
}
async saveUser() {
const { title, fields, order, ...meta } = this;
console.group(title);
console.log(meta);
console.log(fields);
console.log(order);
console.groupEnd();
const data = {
username: fields['姓名'],
gender: fields['性别'],
mobilePhoneNumber: fields['手机'],
GitHub: fields['GitHub ID'],
company: fields['公司/组织'],
title: fields['职位'],
workYear: parseInt(fields['工作经验'] as string)
};
const user = await new Query('UserTemp')
.equalTo('mobilePhoneNumber', data.mobilePhoneNumber)
.first();
return !user ? new UserTemp().save(data) : user.set(data).save();
}
}