177 lines
6.9 KiB
JavaScript
177 lines
6.9 KiB
JavaScript
|
const dateFormat = require('dateformat');
|
|||
|
const Tools_MYSQLDBClass = require('../line-cost-js/Tools_MYSQLDBClass');
|
|||
|
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
|
|||
|
|
|||
|
/** LINENotify */
|
|||
|
class LINENotifyClass {
|
|||
|
constructor() {
|
|||
|
this.Tools_MYSQLDB = new Tools_MYSQLDBClass();
|
|||
|
}
|
|||
|
|
|||
|
async LINENotify_Receive(data) {
|
|||
|
let postdata = {
|
|||
|
'grant_type': 'authorization_code',
|
|||
|
'code': data.code,
|
|||
|
'redirect_uri': process.env.LINENotify_redirect_uri,
|
|||
|
'client_id': process.env.LINENotify_client_id,
|
|||
|
'client_secret': process.env.LINENotify_client_secret
|
|||
|
};
|
|||
|
let Response = await this.Get_token(postdata);
|
|||
|
let Responsedata = JSON.parse(Response);
|
|||
|
if (Responsedata.status === 200) {
|
|||
|
let access_token = Responsedata.access_token;
|
|||
|
|
|||
|
let datetime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
|
|||
|
let state = data.state.split("$");
|
|||
|
let userId = state[0];
|
|||
|
let displayName = state[1];
|
|||
|
let Query = `INSERT INTO \`UserData\` (UserData.datetime, UserData.userId, UserData.displayName, UserData.LINENotify) VALUES ('${datetime}', '${userId}', '${displayName}', '${access_token}') ON DUPLICATE KEY UPDATE UserData.LINENotify = '${access_token}';`;
|
|||
|
await this.Tools_MYSQLDB.Query(Query);
|
|||
|
|
|||
|
let message = "\\n阿巴阿巴";
|
|||
|
this.Send(access_token, message)
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
// 傳入 resolve 與 reject,表示資料成功與失敗
|
|||
|
// resolve("連接已經完成");
|
|||
|
let html = `
|
|||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|||
|
<html>
|
|||
|
<head>
|
|||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|||
|
</head>
|
|||
|
<body>
|
|||
|
<h1>連接已經完成</h1>
|
|||
|
<script type="text/javascript">
|
|||
|
window.onload = function() {
|
|||
|
// window.opener = null;
|
|||
|
window.open("https://lin.ee/VQnmldh","_self");
|
|||
|
// window.close();
|
|||
|
};
|
|||
|
</script>
|
|||
|
</body>
|
|||
|
</html>
|
|||
|
`;
|
|||
|
resolve(html);
|
|||
|
});
|
|||
|
} else {
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
// 傳入 resolve 與 reject,表示資料成功與失敗
|
|||
|
resolve("連接失敗");
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 取得表
|
|||
|
* @param Url Url
|
|||
|
* @param arrange 是否需要整理
|
|||
|
*/
|
|||
|
GetData(Url) {
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
var xhr = new XMLHttpRequest();
|
|||
|
xhr.onreadystatechange = function () {
|
|||
|
if (xhr.readyState === 4) {
|
|||
|
if (xhr.status >= 200 && xhr.status < 400) {
|
|||
|
var response = xhr.responseText;
|
|||
|
resolve(response);
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
xhr.open("GET", Url, true);
|
|||
|
xhr.send();
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 取得表
|
|||
|
* @param Url Url
|
|||
|
* @param data data
|
|||
|
*/
|
|||
|
PostData(Url, data) {
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
var xhr = new XMLHttpRequest();
|
|||
|
xhr.withCredentials = true;
|
|||
|
xhr.onreadystatechange = function () {
|
|||
|
if (xhr.readyState === 4) {
|
|||
|
if (xhr.status >= 200 && xhr.status < 400) {
|
|||
|
var response = xhr.responseText;
|
|||
|
resolve(response);
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
xhr.open("Post", Url);
|
|||
|
// xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
|||
|
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
|||
|
xhr.setRequestHeader("requestverificationtoken", "aA2VWAXiewD1fyMrVhoSmdZAOhNa5YYPdnBxyLU-MlbWdnLHNKtMOkF1xtcZN3KCLW3RrFYLRCPm0DtgZW8sBIQXzxA1:wSKMTfFjGh65R1J0CDU_e91xfxHoqFwPomyXUVEz6Lm3itbHmDMjj11vCCoU4FD-QZjd690_4GD79luvLDTrb6aEFeU1");
|
|||
|
let encodedData = this.encodeFormData(data);
|
|||
|
xhr.send(encodedData);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Get_token
|
|||
|
* @param data data
|
|||
|
*/
|
|||
|
Get_token(data) {
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
let url = 'https://notify-bot.line.me/oauth/token';
|
|||
|
var xhr = new XMLHttpRequest();
|
|||
|
xhr.withCredentials = true;
|
|||
|
xhr.onreadystatechange = function () {
|
|||
|
if (xhr.readyState === 4) {
|
|||
|
if (xhr.status >= 200 && xhr.status < 400) {
|
|||
|
var response = xhr.responseText;
|
|||
|
resolve(response);
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
xhr.open("Post", url);
|
|||
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|||
|
let encodedData = this.encodeFormData(data);
|
|||
|
xhr.send(encodedData);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Send
|
|||
|
* @param data data
|
|||
|
*/
|
|||
|
Send(access_token, data) {
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
let url = 'https://notify-api.line.me/api/notify';
|
|||
|
var xhr = new XMLHttpRequest();
|
|||
|
xhr.withCredentials = true;
|
|||
|
xhr.onreadystatechange = function () {
|
|||
|
if (xhr.readyState === 4) {
|
|||
|
if (xhr.status >= 200 && xhr.status < 400) {
|
|||
|
var response = xhr.responseText;
|
|||
|
resolve(response);
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
// let encodedData = this.encodeFormData(data);
|
|||
|
let newline = encodeURI("\\n");
|
|||
|
let encodedData = "%0D%0A" + encodeURI(data).replace(newline, "%0D%0A");
|
|||
|
xhr.open("POST", `${url}?message=${encodedData}`);
|
|||
|
access_token = access_token ? access_token : "S32BS5DulNLWFjlp1if24yn2SXMaEGyzmSRl75kfCXv";
|
|||
|
xhr.setRequestHeader("Authorization", `Bearer ${access_token}`);
|
|||
|
xhr.send();
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
encodeFormData(data) {
|
|||
|
if (!data) return ""; // Always return a string
|
|||
|
var pairs = []; // To hold name=value pairs
|
|||
|
for (var name in data) { // For each name
|
|||
|
if (!data.hasOwnProperty(name)) continue; // Skip inherited
|
|||
|
if (typeof data[name] === "function") continue; // Skip methods
|
|||
|
var value = data[name].toString(); // Value as string
|
|||
|
name = encodeURIComponent(name.replace(" ", "+")); // Encode name
|
|||
|
value = encodeURIComponent(value.replace(" ", "+")); // Encode value
|
|||
|
pairs.push(name + "=" + value); // Remember name=value pair
|
|||
|
}
|
|||
|
return pairs.join('&'); // Return joined pairs separated with &
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
module.exports = LINENotifyClass
|