🚧 save work

This commit is contained in:
Song
2020-09-13 23:50:16 +08:00
parent 24a91b6013
commit 615ff93d6b
13 changed files with 191 additions and 42 deletions
+9
View File
@@ -0,0 +1,9 @@
const knex = require("knex");
const environment = process.env.NODE_ENV || "development";
const configuration = require("../knexfile")[environment];
const db = knex(configuration);
module.exports = {
db: db,
};
+33
View File
@@ -0,0 +1,33 @@
function formatTime(format) {
if (format === undefined) format = "yyyy-MM-dd hh:mm:ss";
const date = new Date();
const o = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
S: date.getMilliseconds(),
};
if (/(y+)/.test(format)) {
format = format.replace(
RegExp.$1,
(date.getFullYear() + "").substr(4 - RegExp.$1.length)
);
}
for (let k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
}
}
return format;
}
module.exports = {
formatTime,
};
+36
View File
@@ -0,0 +1,36 @@
const axios = require("axios");
module.exports = {
requestToken: function (app) {
let token = "";
axios
.get(
`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${process.env.APP_ID}&secret=${process.env.APP_SECRET}`
)
.then((res) => {
if (res.data && res.data.access_token) {
console.log("Token requested.");
token = res.data.access_token;
app.locals.access_token = token;
process.env.access_token = token;
} else {
console.error(res.data);
}
});
return token;
},
pushWeChatMessage: function (description, link) {
// Reference: https://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl
let access_token = process.env.access_token;
let request_data = {
touser: process.env.OPEN_ID,
template_id: process.env.TEMPLATE_ID,
};
request_data.data = { text: { value: description } };
return axios.post(
`https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${access_token}`,
request_data
);
},
};