dbcontrol.js
4.55 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
//----------------low db-----------------------------------
const request = require('request');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync')
const fs = require('fs');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
const token = require('../config/key');
const TOKEN = token.token; // 사용자 토큰
//var USER_ID = ' '; // LINE 메세지 유저 아이디
//---------------------------------------------------------
// db 초기화
function start_db(USER_ID){
const adapter = new FileSync(USER_ID + '.json');
const db = low(adapter);
db.defaults({cart : []}).write();
}
// 장바구니 동기화
function synchronization(USER_ID, prd_img, prd_name, prd_price, prd_link){
const adapter = new FileSync(USER_ID + '.json');
const db = low(adapter);
db.defaults({cart : []}).write();
// 중복 있는지 검사하는 코드를 if구문에 넣기 (prd_link 기반으로)
// 입력받은 정보를 db에 저장
db.get('cart')
.push({
PRD_IMG : prd_img,
PRD_NAME : prd_name,
PRD_PRICE : prd_price,
PRD_LINK : prd_link
}).write();
}
// db에서 목록 검색 후 삭제
function deleting(USER_ID, prd_link){
const adapter = new FileSync(USER_ID + '.json');
const db = low(adapter);
db.defaults({cart : []}).write();
db.get('cart')
.remove({PRD_LINK : prd_link})
.write(); // prd_link 참조 삭제
}
// db에서 USER_ID 기반 장바구니 조회
function viewing(USER_ID){
const adapter = new FileSync(USER_ID + '.json');
const db = low(adapter);
db.defaults({cart : []}).write();
var datalist = new Array();
datalist = db.get('cart').value(); // [ { } ] 배열속 객체형태로 리턴
//미완성 - 메세지로 내역 push하는 기능 업데이트 요망
}
//================== Just replying specific messages needed ===================================
async function replying(replyToken, sp_message){ // sp_message is message(string) that depends on the situation that user selects
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":sp_message // replying message
}
]
}
},(error, response, body) => {
// console.log(body)
});
}
//================== Just pushing specific messages needed ===================================
async function pushing(USER_ID, sp_message){ //push function
request.post(
{
url: TARGET_URL_2,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"to": `${USER_ID}`,
"messages":[
{
"type": "text",
"text": sp_message // replying message
}
]
}
},(error, response, body) => {
//console.log(body)
});
}
//================== 장바구니 목록 전체 출력 ===================================
async function viewall(img_link, msg1, msg2, msg3){ //push function
request.post(
{
url: TARGET_URL_2,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"to": `${USER_ID}`,
"messages":[
{
"type":"image",
"originalContentUrl": img_link,
"previewImageUrl": img_link
},
{
"type": "text",
"text": "상품명: " + msg1 // replying message
},
{
"type": "text",
"text": "가격: " + msg2 + " 원" // replying message
},
{
"type": "text",
"text": "링크: " + msg3 // replying message
}
]
}
},(error, response, body) => {
//console.log(body)
});
}
module.exports.start_db = start_db;
module.exports.synchronization = synchronization;
module.exports.deleting = deleting;
module.exports.viewing = viewing;