Showing
2 changed files
with
0 additions
and
194 deletions
line.js
deleted
100644 → 0
1 | -var express = require('express'); | ||
2 | -const request = require('request'); | ||
3 | -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
4 | -const TOKEN = 'rQFBQXVZqeF1uFJSoPwloR8avp6Gkxssb2jNJdAFtfK' | ||
5 | - | ||
6 | -const fs = require('fs'); | ||
7 | -const path = require('path'); | ||
8 | -const HTTPS = require('https'); | ||
9 | -const domain = "localhost:80" | ||
10 | -const sslport = 23023; | ||
11 | -const bodyParser = require('body-parser'); | ||
12 | - | ||
13 | -let session = require('express-session'); | ||
14 | -const { text } = require('body-parser'); | ||
15 | -var app = express(); | ||
16 | -app.use(bodyParser.json()); | ||
17 | - | ||
18 | -var User = new Array(); | ||
19 | -/* | ||
20 | -app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }})) | ||
21 | -app.use(bodyParser.urlencoded({ extended: false })); | ||
22 | -*/ | ||
23 | -var db_config = require(__dirname+ '/cf.js'); | ||
24 | -var conn = db_config.init(); | ||
25 | - | ||
26 | -db_config.connect(conn); | ||
27 | - | ||
28 | -app.set('views', __dirname + '/views'); | ||
29 | -app.set('view engine', 'ejs'); | ||
30 | - | ||
31 | - | ||
32 | -var ordernum = 0; | ||
33 | -var sql = 'SELECT * FROM item_order'; | ||
34 | -conn.query(sql, function (err, rows, fields) { | ||
35 | - if(err) { | ||
36 | - console.log('query is not excuted. select fail...\n' + err) | ||
37 | - } | ||
38 | - else { | ||
39 | - ordernum = rows.length; | ||
40 | - } | ||
41 | -}); | ||
42 | -function SL(text){ | ||
43 | -for(let j =0; j < text.length; j++){ | ||
44 | - if(text[j] = '-'){ | ||
45 | - return [text.slice(0,j),text.slice(j+1)] | ||
46 | - } | ||
47 | - else return; | ||
48 | -} | ||
49 | -} | ||
50 | -function Message(Message){ | ||
51 | - request.post( | ||
52 | - { | ||
53 | - url: TARGET_URL, | ||
54 | - headers: { | ||
55 | - 'Authorization': `Bearer ${TOKEN}` | ||
56 | - }, | ||
57 | - json: { | ||
58 | - "replyToken":replyToken, | ||
59 | - "messages":[ | ||
60 | - { | ||
61 | - "type":"text", | ||
62 | - "text":Message | ||
63 | - } | ||
64 | - ] | ||
65 | - } | ||
66 | - },(error, response, body) => { | ||
67 | - console.log(body) | ||
68 | - }); | ||
69 | - return console.log(Message); | ||
70 | -} | ||
71 | - | ||
72 | - | ||
73 | -function purchase(z, text){ | ||
74 | - let found = false; | ||
75 | - if(text == '0'){ | ||
76 | - User[z].state = null; | ||
77 | - return; | ||
78 | - } | ||
79 | - var tx; | ||
80 | - if (! (tx = SL(text))) return Message("잘못된 입력입니다."); | ||
81 | - for( let i= 0; i < items.length; i++){ | ||
82 | - if(tx[0] == items[i].id){ | ||
83 | - found= true; | ||
84 | - var table = 'item_order'; | ||
85 | - var sql = `INSERT INTO `+ table + ` VALUES(?,?,?,?,?)`; | ||
86 | - var params = [ordernum++, items[i].id, tx[1], 0, User[z].id]; | ||
87 | - console.log(sql); | ||
88 | - conn.query(sql, params, function(err) { | ||
89 | - if(err) { | ||
90 | - console.log('query is not excuted. insert fail...\n' + err); | ||
91 | - Message('주문에 실패했습니다') | ||
92 | - return ; | ||
93 | - } | ||
94 | - else { | ||
95 | - console.log('주문성공') | ||
96 | - User[z].state = null; | ||
97 | - } | ||
98 | - }); | ||
99 | - | ||
100 | - } | ||
101 | - } | ||
102 | - if (!found){ | ||
103 | - Message("존재하지 않는 상품입니다"); | ||
104 | - return; | ||
105 | - } | ||
106 | -} | ||
107 | -var items = ''; | ||
108 | - | ||
109 | - | ||
110 | - | ||
111 | - | ||
112 | -app.post('/hook', function(req, res) { | ||
113 | - var eventObj = req.body.events[0]; | ||
114 | - var text = eventObj.message.text; | ||
115 | - var replyToken = eventObj.replyToken; | ||
116 | - var cur_user = eventObj.source.userId; | ||
117 | - console.log(req.body) | ||
118 | - console.log(cur_user); | ||
119 | - | ||
120 | - var Cus = false; | ||
121 | - var z = 0 | ||
122 | - for( z = 0; z < User.length; z++){ | ||
123 | - if(cur_user == User[z].id){ | ||
124 | - Cus = true; | ||
125 | - break; | ||
126 | - } | ||
127 | - } | ||
128 | - if( text == '회원가입') { | ||
129 | - for( var i = 0; i < User.length; i++){ | ||
130 | - if(cur_user == User[i]){ | ||
131 | - Message('회원가입이 이미 된 고객입니다') | ||
132 | - return; | ||
133 | - } | ||
134 | - } | ||
135 | - | ||
136 | - User.push([{"id" : cur_user},{"state" : null} ]); | ||
137 | - } | ||
138 | - if(!Cus) { | ||
139 | - Message('회원가입을 진행 후 쇼핑해주세요') | ||
140 | - } | ||
141 | - if(User[z].state != null){ | ||
142 | - switch(User[z].state){ | ||
143 | - case '상품구매': | ||
144 | - purchase(z,text) | ||
145 | - } | ||
146 | - } | ||
147 | - console.log('start'); | ||
148 | - if( text == '상품구매') { | ||
149 | - var Message ='번호-갯수를 입력하시면(ex:5번을 5개 구매 = 5-5) 구매를\n, 0을 누르시면 취소입니다.\n' | ||
150 | - User[z].state = '상품구매' | ||
151 | - for(let i=0;i<items.length;i++){ | ||
152 | - Message += items[i].id +'. '+ items[i].name + '\n' | ||
153 | - } | ||
154 | - Message(Message); | ||
155 | - | ||
156 | - | ||
157 | - } | ||
158 | -}); | ||
159 | - | ||
160 | -try { | ||
161 | - | ||
162 | -var sql = 'SELECT * FROM item'; | ||
163 | -conn.query(sql, function (err, rows, fields) { | ||
164 | - if(err) { | ||
165 | - console.log('query is not excuted. select fail...\n' + err) | ||
166 | - } | ||
167 | - else { | ||
168 | - items = rows; | ||
169 | - } | ||
170 | -}); | ||
171 | - const option = { | ||
172 | - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
173 | - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
174 | - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
175 | - }; | ||
176 | - HTTPS.createServer(option, app).listen(sslport, () => { | ||
177 | - console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
178 | - }); | ||
179 | - | ||
180 | - | ||
181 | - } catch (error) { | ||
182 | - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
183 | - console.log(error); | ||
184 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
shop1.sql
deleted
100644 → 0
-
Please register or login to post a comment