remove `alicloud/pop-core` keep simple

pull/687/head
wuwenjing 3 years ago
parent a2f2253221
commit 57a76e6129

@ -52,7 +52,6 @@
"update-language-files": "cd extra/update-language-files && node index.js && eslint ../../src/languages/**.js --fix" "update-language-files": "cd extra/update-language-files && node index.js && eslint ../../src/languages/**.js --fix"
}, },
"dependencies": { "dependencies": {
"@alicloud/pop-core": "^1.7.10",
"@fortawesome/fontawesome-svg-core": "~1.2.36", "@fortawesome/fontawesome-svg-core": "~1.2.36",
"@fortawesome/free-regular-svg-icons": "~5.15.4", "@fortawesome/free-regular-svg-icons": "~5.15.4",
"@fortawesome/free-solid-svg-icons": "~5.15.4", "@fortawesome/free-solid-svg-icons": "~5.15.4",

@ -1,6 +1,8 @@
const NotificationProvider = require("./notification-provider"); const NotificationProvider = require("./notification-provider");
const { DOWN, UP } = require("../../src/util"); const { DOWN, UP } = require("../../src/util");
const Core = require("@alicloud/pop-core"); const { default: axios } = require("axios");
const Crypto = require("crypto");
const qs = require("qs");
class AliyunSMS extends NotificationProvider { class AliyunSMS extends NotificationProvider {
name = "AliyunSMS"; name = "AliyunSMS";
@ -9,50 +11,86 @@ class AliyunSMS extends NotificationProvider {
let okMsg = "Sent Successfully."; let okMsg = "Sent Successfully.";
try { try {
var client = new Core({
accessKeyId: notification.accessKeyId,
accessKeySecret: notification.secretAccessKey,
endpoint: "https://dysmsapi.aliyuncs.com",
apiVersion: "2017-05-25",
});
var params = {
PhoneNumbers: notification.phonenumber,
TemplateCode: notification.templateCode,
SignName: notification.signName,
TemplateParam: JSON.stringify({
name: "",
time: "",
status: "",
msg: msg,
}),
};
if (heartbeatJSON != null) { if (heartbeatJSON != null) {
params.TemplateParam = JSON.stringify({ var msgBody = JSON.stringify({
name: monitorJSON["name"], name: monitorJSON["name"],
time: heartbeatJSON["time"], time: heartbeatJSON["time"],
status: this.statusToString(heartbeatJSON["status"]), status: this.statusToString(heartbeatJSON["status"]),
msg: heartbeatJSON["msg"], msg: heartbeatJSON["msg"],
}); });
if (this.sendSms(notification, msgBody)) {
return okMsg;
}
} else {
var msgBody = JSON.stringify({
name: "",
time: "",
status: "",
msg: msg,
});
if (this.sendSms(notification, msgBody)) {
return okMsg;
}
}
} catch (error) {
this.throwGeneralAxiosError(error);
}
} }
var requestOption = { async sendSms(notification, msgbody) {
method: "POST", var params = {
PhoneNumbers: notification.phonenumber,
TemplateCode: notification.templateCode,
SignName: notification.signName,
TemplateParam: msgbody,
AccessKeyId: notification.accessKeyId,
Format: "JSON",
SignatureMethod: "HMAC-SHA1",
SignatureVersion: "1.0",
SignatureNonce: Math.random().toString(),
Timestamp: new Date().toISOString(),
Action: "SendSms",
Version: "2017-05-25",
}; };
await client.request("SendSms", params, requestOption).then( params.Signature = this.sign(params, notification.secretAccessKey);
(result) => { var config = {
console.log(JSON.stringify(result)); method: "POST",
return okMsg; url: "http://dysmsapi.aliyuncs.com/",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
}, },
(ex) => { data: qs.stringify(params),
console.log(ex); };
var result = await axios(config);
if (result.data.Message == "OK") {
return true;
} }
); return false;
} catch (error) { }
this.throwGeneralAxiosError(error);
/** Aliyun request sign */
sign(param, AccessKeySecret) {
var param2 = {},
data = [];
var oa = Object.keys(param).sort();
for (var i = 0; i < oa.length; i++) {
var key = oa[i];
param2[key] = param[key];
} }
for (var key in param2) {
data.push(`${encodeURIComponent(key)}=${encodeURIComponent(param2[key])}`);
}
var StringToSign = `POST&${encodeURIComponent("/")}&${encodeURIComponent(data.join("&"))}`;
return Crypto
.createHmac("sha1", `${AccessKeySecret}&`)
.update(Buffer.from(StringToSign))
.digest("base64");
} }
statusToString(status) { statusToString(status) {

@ -16,6 +16,7 @@
<input id="signName" v-model="$parent.notification.signName" type="text" class="form-control" required> <input id="signName" v-model="$parent.notification.signName" type="text" class="form-control" required>
<div class="form-text"> <div class="form-text">
<p>Sms template must contain parameters: <br> <code>${name} ${time} ${status} ${msg}</code></p>
<i18n-t tag="p" keypath="Read more:"> <i18n-t tag="p" keypath="Read more:">
<a href="https://help.aliyun.com/document_detail/101414.html" target="_blank">https://help.aliyun.com/document_detail/101414.html</a> <a href="https://help.aliyun.com/document_detail/101414.html" target="_blank">https://help.aliyun.com/document_detail/101414.html</a>
</i18n-t> </i18n-t>

Loading…
Cancel
Save