ts-glitter 21.8.3 → 21.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lowcode/Entry.js +1 -1
- package/lowcode/Entry.ts +1 -1
- package/lowcode/backend-manager/bg-dialog.js +342 -100
- package/lowcode/backend-manager/bg-dialog.ts +432 -145
- package/lowcode/backend-manager/bg-widget.js +4 -4
- package/lowcode/backend-manager/bg-widget.ts +5 -5
- package/lowcode/cms-plugin/POS-setting.js +18 -6
- package/lowcode/cms-plugin/POS-setting.ts +22 -10
- package/lowcode/cms-plugin/pos-pages/payment-page.js +9 -2
- package/lowcode/cms-plugin/pos-pages/payment-page.ts +11 -3
- package/lowcode/cms-plugin/shopping-discount-setting.js +148 -92
- package/lowcode/cms-plugin/shopping-discount-setting.ts +162 -111
- package/lowcode/cms-plugin/shopping-product-stock.js +58 -45
- package/lowcode/cms-plugin/shopping-product-stock.ts +66 -49
- package/lowcode/cms-plugin/stock-stores.js +55 -0
- package/lowcode/cms-plugin/stock-stores.ts +59 -0
- package/lowcode/css/editor.css +8 -9
- package/lowcode/editor/basic-component.js +0 -10
- package/lowcode/editor/basic-component.ts +1 -11
- package/lowcode/glitter-base/route/article.js +50 -51
- package/lowcode/glitter-base/route/article.ts +102 -103
- package/lowcode/glitter-base/route/recommend.js +1 -0
- package/lowcode/glitter-base/route/recommend.ts +141 -123
- package/lowcode/glitterBundle/plugins/html-render.js +2 -0
- package/lowcode/glitterBundle/plugins/html-render.ts +1 -0
- package/lowcode/jspage/function-page/setting_editor.js +1 -1
- package/lowcode/jspage/function-page/setting_editor.ts +1 -1
- package/lowcode/jspage/function-page/tool-setting.js +0 -1
- package/lowcode/jspage/function-page/tool-setting.ts +0 -1
- package/lowcode/jspage/main.js +6 -1
- package/lowcode/jspage/main.ts +6 -1
- package/lowcode/modules/image-library-clone.ts +2 -0
- package/lowcode/modules/image-library.js +118 -155
- package/lowcode/modules/image-library.ts +134 -197
- package/lowcode/official_view_component/official/component.js +1 -1
- package/lowcode/official_view_component/official/component.ts +1 -1
- package/lowcode/public-components/banner/first-banner.js +85 -50
- package/lowcode/public-components/banner/first-banner.ts +96 -59
- package/lowcode/public-components/layout-plugin/social-links-01.js +19 -19
- package/lowcode/public-components/layout-plugin/social-links-01.ts +35 -35
- package/package.json +1 -1
- package/src/api-public/controllers/article.js +11 -0
- package/src/api-public/controllers/article.js.map +1 -1
- package/src/api-public/controllers/article.ts +13 -0
- package/src/api-public/controllers/recommend.js +1 -0
- package/src/api-public/controllers/recommend.js.map +1 -1
- package/src/api-public/controllers/recommend.ts +99 -89
- package/src/api-public/services/checkout-event.js +81 -43
- package/src/api-public/services/checkout-event.js.map +1 -1
- package/src/api-public/services/checkout-event.ts +93 -38
- package/src/api-public/services/post.js +7 -17
- package/src/api-public/services/post.js.map +1 -1
- package/src/api-public/services/recommend.d.ts +1 -0
- package/src/api-public/services/recommend.js +12 -2
- package/src/api-public/services/recommend.js.map +1 -1
- package/src/api-public/services/recommend.ts +375 -354
- package/src/api-public/services/shopping.d.ts +1 -0
- package/src/api-public/services/shopping.js +4 -2
- package/src/api-public/services/shopping.js.map +1 -1
- package/src/api-public/services/shopping.ts +7 -2
- package/src/services/saas-table-check.js +1 -1
- package/src/services/saas-table-check.js.map +1 -1
- package/src/services/saas-table-check.ts +1 -1
|
@@ -5,408 +5,429 @@ import { Shopping } from './shopping.js';
|
|
|
5
5
|
import { saasConfig } from '../../config.js';
|
|
6
6
|
|
|
7
7
|
export class Recommend {
|
|
8
|
-
|
|
8
|
+
public app: string;
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
public token?: IToken;
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
constructor(app: string, token?: IToken) {
|
|
13
|
+
this.app = app;
|
|
14
|
+
this.token = token;
|
|
15
|
+
}
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
const percentage = (numerator / denominator) * 100;
|
|
22
|
-
return `${percentage.toFixed(decimalPlaces)}%`;
|
|
17
|
+
calculatePercentage(numerator: number, denominator: number, decimalPlaces: number = 2): string {
|
|
18
|
+
if (denominator === 0) {
|
|
19
|
+
return `0%`;
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
21
|
+
const percentage = (numerator / denominator) * 100;
|
|
22
|
+
return `${percentage.toFixed(decimalPlaces)}%`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async getLinkList(query: {
|
|
26
|
+
code?: string;
|
|
27
|
+
status?: boolean;
|
|
28
|
+
page: number;
|
|
29
|
+
limit: number;
|
|
30
|
+
user_id?: string;
|
|
31
|
+
no_detail?: boolean;
|
|
32
|
+
id_list?: string;
|
|
33
|
+
}) {
|
|
34
|
+
try {
|
|
35
|
+
query.page = query.page ?? 0;
|
|
36
|
+
query.limit = query.limit ?? 50;
|
|
37
|
+
|
|
38
|
+
let search = ['1=1'];
|
|
39
|
+
if (query?.code) {
|
|
40
|
+
search.push(`(code = "${query.code}")`);
|
|
41
|
+
}
|
|
42
|
+
if (query?.status) {
|
|
43
|
+
search.push(`(JSON_EXTRACT(content, '$.status') = ${query.status})`);
|
|
44
|
+
}
|
|
45
|
+
if (query?.user_id) {
|
|
46
|
+
search.push(`(JSON_EXTRACT(content, '$.recommend_user.id') = ${query.user_id})`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const idStr = query.id_list
|
|
50
|
+
? query.id_list
|
|
51
|
+
.split(',')
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
.map(id => db.escape(id))
|
|
54
|
+
.join(',')
|
|
55
|
+
: '';
|
|
56
|
+
|
|
57
|
+
if (query.id_list && idStr) {
|
|
58
|
+
search.push(`(id in (${idStr}))`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const links = await db.query(
|
|
62
|
+
`SELECT * FROM \`${this.app}\`.t_recommend_links WHERE ${search.join(' AND ')}
|
|
43
63
|
${query.page !== undefined && query.limit !== undefined ? `LIMIT ${query.page * query.limit}, ${query.limit}` : ''};
|
|
44
64
|
`,
|
|
45
|
-
|
|
46
|
-
|
|
65
|
+
[]
|
|
66
|
+
);
|
|
47
67
|
|
|
48
|
-
|
|
49
|
-
|
|
68
|
+
const total = await db.query(
|
|
69
|
+
`SELECT count(*) as c FROM \`${this.app}\`.t_recommend_links WHERE ${search.join(' AND ')};
|
|
50
70
|
`,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
71
|
+
[]
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const shopping = new Shopping(this.app, this.token);
|
|
75
|
+
|
|
76
|
+
if (!query.no_detail) {
|
|
77
|
+
const orderList = await shopping.getCheckOut({
|
|
78
|
+
page: 0,
|
|
79
|
+
limit: 999999,
|
|
80
|
+
distribution_code: links.map((data: any) => data.code).join(','),
|
|
81
|
+
});
|
|
82
|
+
const monitors = await (async () => {
|
|
83
|
+
if (links.length === 0) {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
return await db.query(
|
|
87
|
+
`SELECT id, mac_address, base_url
|
|
68
88
|
FROM \`${saasConfig.SAAS_NAME}\`.t_monitor
|
|
69
89
|
WHERE app_name = "${this.app}"
|
|
70
90
|
AND base_url in (${links.map((data: any) => `"/shopnex/distribution/${data.content.link}"`).join(',')})
|
|
71
91
|
`,
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
const monitor = monitors.filter((d: any) => d.base_url === `/shopnex/distribution/${data.content.link}`);
|
|
86
|
-
|
|
87
|
-
const monitorLength = monitor.length;
|
|
88
|
-
const macAddrSize = new Set(monitor.map((item: any) => item.mac_address)).size;
|
|
89
|
-
const totalOrders = orders.filter((order: any) => {
|
|
90
|
-
return order.status === 1;
|
|
91
|
-
}).length;
|
|
92
|
-
|
|
93
|
-
const totalPrice = orders.reduce((sum: number, order: any) => {
|
|
94
|
-
if (order.status === 1) {
|
|
95
|
-
return sum + order.orderData.total - order.orderData.shipment_fee;
|
|
96
|
-
}
|
|
97
|
-
return sum;
|
|
98
|
-
}, 0);
|
|
99
|
-
|
|
100
|
-
data.orders = totalOrders;
|
|
101
|
-
data.click_times = monitorLength;
|
|
102
|
-
data.mac_address_count = macAddrSize;
|
|
103
|
-
data.conversion_rate = this.calculatePercentage(totalOrders, monitor.length, 1);
|
|
104
|
-
data.total_price = totalPrice;
|
|
105
|
-
data.sharing_bonus = 0;
|
|
106
|
-
if (data.content.lineItems) {
|
|
107
|
-
let idArray: any[] = data.content.lineItems.map((item: any) => {
|
|
108
|
-
return item.id
|
|
109
|
-
});
|
|
110
|
-
orders.map((order: any) => {
|
|
111
|
-
order.orderData.lineItems.forEach((item: any) => {
|
|
112
|
-
if (idArray.includes(item.id) && order.status === 1) {
|
|
113
|
-
console.log(`item.sale_price=>`,item.sale_price);
|
|
114
|
-
console.log(`item.count=>`,item.count);
|
|
115
|
-
console.log(`data.content.share_value=>`,data.content.share_value);
|
|
116
|
-
data.sharing_bonus += Math.floor((item.sale_price * item.count * parseFloat(data.content.share_value)) / 100);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
}
|
|
92
|
+
[]
|
|
93
|
+
);
|
|
94
|
+
})();
|
|
95
|
+
|
|
96
|
+
for (const data of links) {
|
|
97
|
+
const orders = orderList.data.filter((d: any) => {
|
|
98
|
+
try {
|
|
99
|
+
return d.orderData.distribution_info.code === data.code;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
return false;
|
|
122
102
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
`,
|
|
136
|
-
[data.code]
|
|
137
|
-
);
|
|
138
|
-
if (getLinks.length > 0) {
|
|
139
|
-
return { result: false, message: '此分銷代碼已被建立' };
|
|
103
|
+
});
|
|
104
|
+
const monitor = monitors.filter((d: any) => d.base_url === `/shopnex/distribution/${data.content.link}`);
|
|
105
|
+
|
|
106
|
+
const monitorLength = monitor.length;
|
|
107
|
+
const macAddrSize = new Set(monitor.map((item: any) => item.mac_address)).size;
|
|
108
|
+
const totalOrders = orders.filter((order: any) => {
|
|
109
|
+
return order.status === 1;
|
|
110
|
+
}).length;
|
|
111
|
+
|
|
112
|
+
const totalPrice = orders.reduce((sum: number, order: any) => {
|
|
113
|
+
if (order.status === 1) {
|
|
114
|
+
return sum + order.orderData.total - order.orderData.shipment_fee;
|
|
140
115
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
116
|
+
return sum;
|
|
117
|
+
}, 0);
|
|
118
|
+
|
|
119
|
+
data.orders = totalOrders;
|
|
120
|
+
data.click_times = monitorLength;
|
|
121
|
+
data.mac_address_count = macAddrSize;
|
|
122
|
+
data.conversion_rate = this.calculatePercentage(totalOrders, monitor.length, 1);
|
|
123
|
+
data.total_price = totalPrice;
|
|
124
|
+
data.sharing_bonus = 0;
|
|
125
|
+
if (data.content.lineItems) {
|
|
126
|
+
let idArray: any[] = data.content.lineItems.map((item: any) => {
|
|
127
|
+
return item.id;
|
|
128
|
+
});
|
|
129
|
+
orders.map((order: any) => {
|
|
130
|
+
order.orderData.lineItems.forEach((item: any) => {
|
|
131
|
+
if (idArray.includes(item.id) && order.status === 1) {
|
|
132
|
+
console.log(`item.sale_price=>`, item.sale_price);
|
|
133
|
+
console.log(`item.count=>`, item.count);
|
|
134
|
+
console.log(`data.content.share_value=>`, data.content.share_value);
|
|
135
|
+
data.sharing_bonus += Math.floor(
|
|
136
|
+
(item.sale_price * item.count * parseFloat(data.content.share_value)) / 100
|
|
137
|
+
);
|
|
145
138
|
}
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
{
|
|
150
|
-
code: data.code,
|
|
151
|
-
content: JSON.stringify(data),
|
|
152
|
-
},
|
|
153
|
-
]);
|
|
154
|
-
return { result: true, data: links };
|
|
155
|
-
} catch (error) {
|
|
156
|
-
throw exception.BadRequestError('ERROR', 'Recommend postLink Error: ' + error, null);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
157
142
|
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return { data: links, total: total[0].c };
|
|
146
|
+
} catch (error) {
|
|
147
|
+
throw exception.BadRequestError('ERROR', 'Recommend getLinkList Error: ' + error, null);
|
|
158
148
|
}
|
|
149
|
+
}
|
|
159
150
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
151
|
+
async postLink(data: any) {
|
|
152
|
+
try {
|
|
153
|
+
data.token && delete data.token;
|
|
154
|
+
const getLinks = await db.query(
|
|
155
|
+
`SELECT * FROM \`${this.app}\`.t_recommend_links WHERE code = ?;
|
|
165
156
|
`,
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
},
|
|
176
|
-
id,
|
|
177
|
-
]);
|
|
178
|
-
return { result: true, data: links };
|
|
179
|
-
} catch (error) {
|
|
180
|
-
throw exception.BadRequestError('ERROR', 'Recommend putLink Error: ' + error, null);
|
|
157
|
+
[data.code]
|
|
158
|
+
);
|
|
159
|
+
if (getLinks.length > 0) {
|
|
160
|
+
return { result: false, message: '此分銷代碼已被建立' };
|
|
161
|
+
}
|
|
162
|
+
if (data.recommend_status === 'new' && data.recommend_user && data.recommend_user.id === 0) {
|
|
163
|
+
const register = await this.postUser(data.recommend_user);
|
|
164
|
+
if (!register.result) {
|
|
165
|
+
return { result: false, message: '信箱已被建立' };
|
|
181
166
|
}
|
|
167
|
+
data.recommend_user.id = register.data.insertId;
|
|
168
|
+
}
|
|
169
|
+
const links = await db.query(`INSERT INTO \`${this.app}\`.t_recommend_links SET ?`, [
|
|
170
|
+
{
|
|
171
|
+
code: data.code,
|
|
172
|
+
content: JSON.stringify(data),
|
|
173
|
+
},
|
|
174
|
+
]);
|
|
175
|
+
return { result: true, data: links };
|
|
176
|
+
} catch (error) {
|
|
177
|
+
throw exception.BadRequestError('ERROR', 'Recommend postLink Error: ' + error, null);
|
|
182
178
|
}
|
|
179
|
+
}
|
|
183
180
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
181
|
+
async putLink(id: string, data: any) {
|
|
182
|
+
try {
|
|
183
|
+
data.token && delete data.token;
|
|
184
|
+
const getLinks = await db.query(
|
|
185
|
+
`SELECT * FROM \`${this.app}\`.t_recommend_links WHERE code = ? AND id <> ?;
|
|
188
186
|
`,
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
throw exception.BadRequestError('ERROR', 'Recommend toggleLink Error: ' + error, null);
|
|
205
|
-
}
|
|
187
|
+
[data.code, id]
|
|
188
|
+
);
|
|
189
|
+
if (getLinks.length > 0) {
|
|
190
|
+
return { result: false, message: '此分銷代碼已被建立' };
|
|
191
|
+
}
|
|
192
|
+
const links = await db.query(`UPDATE \`${this.app}\`.t_recommend_links SET ? WHERE (id = ?);`, [
|
|
193
|
+
{
|
|
194
|
+
code: data.code,
|
|
195
|
+
content: JSON.stringify(data),
|
|
196
|
+
},
|
|
197
|
+
id,
|
|
198
|
+
]);
|
|
199
|
+
return { result: true, data: links };
|
|
200
|
+
} catch (error) {
|
|
201
|
+
throw exception.BadRequestError('ERROR', 'Recommend putLink Error: ' + error, null);
|
|
206
202
|
}
|
|
203
|
+
}
|
|
207
204
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
205
|
+
async toggleLink(id: string) {
|
|
206
|
+
try {
|
|
207
|
+
const getLinks = await db.query(
|
|
208
|
+
`SELECT * FROM \`${this.app}\`.t_recommend_links WHERE id = ?;
|
|
209
|
+
`,
|
|
210
|
+
[id]
|
|
211
|
+
);
|
|
212
|
+
if (getLinks.length === 0) {
|
|
213
|
+
return { result: false, message: '此分銷連結不存在' };
|
|
214
|
+
}
|
|
215
|
+
const content = getLinks[0].content;
|
|
216
|
+
content.status = !content.status;
|
|
217
|
+
const links = await db.query(`UPDATE \`${this.app}\`.t_recommend_links SET ? WHERE (id = ?);`, [
|
|
218
|
+
{
|
|
219
|
+
content: JSON.stringify(content),
|
|
220
|
+
},
|
|
221
|
+
id,
|
|
222
|
+
]);
|
|
223
|
+
return { result: true, data: links };
|
|
224
|
+
} catch (error) {
|
|
225
|
+
throw exception.BadRequestError('ERROR', 'Recommend toggleLink Error: ' + error, null);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async deleteLink(data: any) {
|
|
230
|
+
try {
|
|
231
|
+
data.token && delete data.token;
|
|
232
|
+
if (data.id && data.id.length > 0) {
|
|
233
|
+
const links = await db.query(
|
|
234
|
+
`DELETE FROM \`${this.app}\`.t_recommend_links
|
|
214
235
|
WHERE id in (${data.id.join(',')});
|
|
215
236
|
`,
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
237
|
+
[]
|
|
238
|
+
);
|
|
239
|
+
return { result: true, data: links };
|
|
240
|
+
}
|
|
241
|
+
return { result: false, message: '刪除失敗' };
|
|
242
|
+
} catch (error) {
|
|
243
|
+
throw exception.BadRequestError('ERROR', 'Recommend putUser Error: ' + error, null);
|
|
224
244
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async getUserList(query: { limit: number; page: number; search?: string; searchType?: string; orderBy?: string }) {
|
|
248
|
+
try {
|
|
249
|
+
query.page = query.page ?? 0;
|
|
250
|
+
query.limit = query.limit ?? 50;
|
|
251
|
+
|
|
252
|
+
let search = ['1=1'];
|
|
253
|
+
if (query.search) {
|
|
254
|
+
switch (query.searchType) {
|
|
255
|
+
case 'phone':
|
|
256
|
+
search.push(`(JSON_EXTRACT(content, '$.phone') like '%${query.search}%')`);
|
|
257
|
+
break;
|
|
258
|
+
case 'name':
|
|
259
|
+
search.push(`(JSON_EXTRACT(content, '$.name') like '%${query.search}%')`);
|
|
260
|
+
break;
|
|
261
|
+
case 'email':
|
|
262
|
+
default:
|
|
263
|
+
search.push(`(email like '%${query.search}%')`);
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
let orderBy = 'id DESC';
|
|
269
|
+
if (query.orderBy) {
|
|
270
|
+
orderBy = (() => {
|
|
271
|
+
switch (query.orderBy) {
|
|
272
|
+
case 'name':
|
|
273
|
+
return `JSON_EXTRACT(content, '$.name')`;
|
|
274
|
+
case 'created_time_asc':
|
|
275
|
+
return `created_time`;
|
|
276
|
+
case 'created_time_desc':
|
|
277
|
+
return `created_time DESC`;
|
|
278
|
+
default:
|
|
279
|
+
return `id DESC`;
|
|
280
|
+
}
|
|
281
|
+
})();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const data = await db.query(
|
|
285
|
+
`SELECT * FROM \`${this.app}\`.t_recommend_users
|
|
265
286
|
WHERE ${search.join(' AND ')}
|
|
266
287
|
ORDER BY ${orderBy}
|
|
267
288
|
${query.page !== undefined && query.limit !== undefined ? `LIMIT ${query.page * query.limit}, ${query.limit}` : ''};
|
|
268
289
|
`,
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
290
|
+
[]
|
|
291
|
+
);
|
|
292
|
+
const total = await db.query(
|
|
293
|
+
`SELECT count(id) as c FROM \`${this.app}\`.t_recommend_users
|
|
273
294
|
WHERE ${search.join(' AND ')}
|
|
274
295
|
`,
|
|
275
|
-
|
|
276
|
-
|
|
296
|
+
[]
|
|
297
|
+
);
|
|
277
298
|
|
|
278
|
-
|
|
279
|
-
|
|
299
|
+
const allOrders = await db.query(
|
|
300
|
+
`SELECT * FROM \`${this.app}\`.t_checkout WHERE orderData->>'$.distribution_info' is not null;
|
|
280
301
|
`,
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
302
|
+
[]
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
if (data.length === 0) {
|
|
306
|
+
return {
|
|
307
|
+
data: [],
|
|
308
|
+
total: 0,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
let n = 0;
|
|
313
|
+
await new Promise<void>(resolve => {
|
|
314
|
+
data.map(async (user: any) => {
|
|
315
|
+
db.query(
|
|
316
|
+
`SELECT * FROM \`${this.app}\`.t_recommend_links
|
|
296
317
|
WHERE (JSON_EXTRACT(content, '$.recommend_user.id') = ${user.id});
|
|
297
318
|
`,
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
const totalList = results.map((result: any) => {
|
|
309
|
-
const code = result.code;
|
|
310
|
-
const content = result.content;
|
|
311
|
-
const total = orders.reduce((sum: number, order: any) => {
|
|
312
|
-
if (order.status === 1 && order.orderData.distribution_info.code === code) {
|
|
313
|
-
return sum + order.orderData.total - order.orderData.shipment_fee;
|
|
314
|
-
}
|
|
315
|
-
return sum;
|
|
316
|
-
}, 0);
|
|
317
|
-
return { code, total, content };
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
user.sharing_bonus = totalList.reduce((sum: number, obj: any) => {
|
|
321
|
-
return sum + Math.floor((obj.total * parseFloat(obj.content.share_value)) / 100);
|
|
322
|
-
}, 0);
|
|
323
|
-
user.total_price = totalList.reduce((sum: number, obj: any) => sum + obj.total, 0);
|
|
324
|
-
user.links = results.length;
|
|
325
|
-
n++;
|
|
326
|
-
if (n === data.length) {
|
|
327
|
-
resolve();
|
|
328
|
-
}
|
|
329
|
-
});
|
|
330
|
-
});
|
|
319
|
+
[]
|
|
320
|
+
).then(results => {
|
|
321
|
+
const orders = allOrders.filter((order: any) => {
|
|
322
|
+
try {
|
|
323
|
+
return order.orderData.distribution_info.recommend_user.id === user.id;
|
|
324
|
+
} catch (error) {
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
331
327
|
});
|
|
332
328
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
329
|
+
const totalList = results.map((result: any) => {
|
|
330
|
+
const code = result.code;
|
|
331
|
+
const content = result.content;
|
|
332
|
+
const total = orders.reduce((sum: number, order: any) => {
|
|
333
|
+
if (order.status === 1 && order.orderData.distribution_info.code === code) {
|
|
334
|
+
return sum + order.orderData.total - order.orderData.shipment_fee;
|
|
335
|
+
}
|
|
336
|
+
return sum;
|
|
337
|
+
}, 0);
|
|
338
|
+
return { code, total, content };
|
|
339
|
+
});
|
|
341
340
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
);
|
|
351
|
-
if (getUsers.length > 0) {
|
|
352
|
-
return { result: false, message: '信箱已被建立' };
|
|
341
|
+
user.sharing_bonus = totalList.reduce((sum: number, obj: any) => {
|
|
342
|
+
return sum + Math.floor((obj.total * parseFloat(obj.content.share_value)) / 100);
|
|
343
|
+
}, 0);
|
|
344
|
+
user.total_price = totalList.reduce((sum: number, obj: any) => sum + obj.total, 0);
|
|
345
|
+
user.links = results.length;
|
|
346
|
+
n++;
|
|
347
|
+
if (n === data.length) {
|
|
348
|
+
resolve();
|
|
353
349
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
return {
|
|
355
|
+
data: data,
|
|
356
|
+
total: total[0].c,
|
|
357
|
+
};
|
|
358
|
+
} catch (error) {
|
|
359
|
+
throw exception.BadRequestError('ERROR', 'Recommend getUserList Error: ' + error, null);
|
|
364
360
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async postUser(data: any) {
|
|
364
|
+
try {
|
|
365
|
+
data.token && delete data.token;
|
|
366
|
+
data.id !== undefined && delete data.id;
|
|
367
|
+
const getUsers = await db.query(
|
|
368
|
+
`SELECT * FROM \`${this.app}\`.t_recommend_users WHERE email = ?;
|
|
371
369
|
`,
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
throw exception.BadRequestError('ERROR', 'Recommend putUser Error: ' + error, null);
|
|
387
|
-
}
|
|
370
|
+
[data.email]
|
|
371
|
+
);
|
|
372
|
+
if (getUsers.length > 0) {
|
|
373
|
+
return { result: false, message: '信箱已被建立' };
|
|
374
|
+
}
|
|
375
|
+
const user = await db.query(`INSERT INTO \`${this.app}\`.t_recommend_users SET ?`, [
|
|
376
|
+
{
|
|
377
|
+
email: data.email,
|
|
378
|
+
content: JSON.stringify(data),
|
|
379
|
+
},
|
|
380
|
+
]);
|
|
381
|
+
return { result: true, data: user };
|
|
382
|
+
} catch (error) {
|
|
383
|
+
throw exception.BadRequestError('ERROR', 'Recommend postUser Error: ' + error, null);
|
|
388
384
|
}
|
|
385
|
+
}
|
|
389
386
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
387
|
+
async putUser(id: string, data: any) {
|
|
388
|
+
try {
|
|
389
|
+
data.token && delete data.token;
|
|
390
|
+
const getUsers = await db.query(
|
|
391
|
+
`SELECT * FROM \`${this.app}\`.t_recommend_users WHERE email = ? AND id <> ?;
|
|
392
|
+
`,
|
|
393
|
+
[data.email, id]
|
|
394
|
+
);
|
|
395
|
+
if (getUsers.length > 0) {
|
|
396
|
+
return { result: false, message: '信箱已被建立' };
|
|
397
|
+
}
|
|
398
|
+
const user = await db.query(`UPDATE \`${this.app}\`.t_recommend_users SET ? WHERE (id = ?);`, [
|
|
399
|
+
{
|
|
400
|
+
email: data.email,
|
|
401
|
+
content: JSON.stringify(data),
|
|
402
|
+
},
|
|
403
|
+
id,
|
|
404
|
+
]);
|
|
405
|
+
return { result: true, data: user };
|
|
406
|
+
} catch (error) {
|
|
407
|
+
throw exception.BadRequestError('ERROR', 'Recommend putUser Error: ' + error, null);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async deleteUser(data: any) {
|
|
412
|
+
try {
|
|
413
|
+
data.token && delete data.token;
|
|
414
|
+
if (data.id && data.id.length > 0) {
|
|
415
|
+
await db.query(
|
|
416
|
+
`DELETE FROM \`${this.app}\`.t_recommend_links
|
|
396
417
|
WHERE JSON_EXTRACT(content, '$.recommend_user.id') in (${data.id.join(',')});
|
|
397
418
|
`,
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
419
|
+
[]
|
|
420
|
+
);
|
|
421
|
+
const user = await db.query(
|
|
422
|
+
`DELETE FROM \`${this.app}\`.t_recommend_users WHERE (id in (${data.id.join(',')}));
|
|
402
423
|
`,
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
424
|
+
[]
|
|
425
|
+
);
|
|
426
|
+
return { result: true, data: user };
|
|
427
|
+
}
|
|
428
|
+
return { result: false, message: '刪除失敗' };
|
|
429
|
+
} catch (error) {
|
|
430
|
+
throw exception.BadRequestError('ERROR', 'Recommend putUser Error: ' + error, null);
|
|
411
431
|
}
|
|
432
|
+
}
|
|
412
433
|
}
|