zhibin před 6 roky
rodič
revize
892663aa9e
7 změnil soubory, kde provedl 161 přidání a 0 odebrání
  1. 14 0
      app.js
  2. 12 0
      config/sys.json
  3. 15 0
      package.json
  4. 59 0
      router/error.js
  5. 17 0
      router/index.js
  6. 0 0
      router/share.js
  7. 44 0
      util/pubtool.js

+ 14 - 0
app.js

@@ -0,0 +1,14 @@
+const Koa = require('koa');
+const static = require('koa-static');
+const path = require('path');
+const router = require('./router');
+const sys = require('./config/sys.json');
+
+const app = new Koa();
+app.use(router.routes());
+app.use(router.allowedMethods());
+app.use(static(path.join(__dirname, sys.static_dir), { handle: true }));
+
+app.listen(8000, () => {
+  console.log(`${sys.pro_name} is starting at port ${sys.port}`);
+});

+ 12 - 0
config/sys.json

@@ -0,0 +1,12 @@
+{
+  "static_dir": "/static",
+  "mail": {
+    "service": "qq",
+    "account": "854556519@qq.com",
+    "pass": "gjoiykrnepbabebi"
+  },
+  "port": 8000,
+  "pro_name": "WeChat",
+  "err_sendee": "854556519@qq.com",
+  "debug": false
+}

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "wechat",
+  "version": "1.0.0",
+  "description": "微信分享",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "http://192.168.0.115:3000/bill/WeChat.git"
+  },
+  "author": "",
+  "license": "ISC"
+}

+ 59 - 0
router/error.js

@@ -0,0 +1,59 @@
+const mailSend = require('../util/mailSend');
+const sys = require('../config/sys');
+
+let api, adminMail;
+
+class OwnError extends Error {
+  constructor(msg = '', code = 400, additional = undefined) {
+    super();
+    this.msg = msg;
+    this.code = code;
+    this.additional = additional;
+  }
+}
+
+function err(msg, code, additional) {
+  throw (new OwnError(msg, code, additional));
+}
+
+async function error(ctx, next) {
+  try {
+    ctx[api] = err;
+    await next()
+  } catch (e) {
+    let body;
+
+    if (e instanceof OwnError) {
+      ctx.status = e.code
+      body = {
+        msg: e.msg,
+        content: e.additional
+      }
+    } else {
+      ctx.status = 500
+      body = { code: 500, msg: '系统发生错误,请稍后再试!' };
+
+      if (!sys.debug && adminMail) {
+        await mailSend({
+          to: adminMail,
+          subject: '4维时代错误提醒',
+          html: `<h1>${e.message}</h1><div>${e.stack ? e.stack.replace(/\n/ig, '<br>') : e}</div>`
+        });
+      } else {
+        throw e;
+      }
+    }
+
+    ctx.body = body;
+  }
+}
+
+
+function init(_api = 'error', _adminMail) {
+  api = _api;
+  adminMail = _adminMail;
+
+  return error;
+}
+
+module.exports = exports = init;

+ 17 - 0
router/index.js

@@ -0,0 +1,17 @@
+const Router = require('koa-router');
+const error = require('./error');
+const sys = require('../config/sys.json');
+const router = new Router();
+
+
+const roter_modulars = [
+  require('./share')
+];
+
+router.use(error('error', sys.err_sendee));
+
+for (modular of roter_modulars) {
+  router.use(modular.path || '/', modular.router.routes(), modular.router.allowedMethods());
+}
+
+module.exports = router;

+ 0 - 0
router/share.js


+ 44 - 0
util/pubtool.js

@@ -0,0 +1,44 @@
+const crypto = require('crypto');
+
+
+/**
+ * 生成指定位数的随机字符串
+ * @param digit number 要生成随机字符串的位数
+ * @return string 随机字符串
+ */
+function randomChar(digit = 4) {
+  let strs = [];
+  
+  do {
+    let str = Math.random().toString(16).substr(2);
+    if (str.length >= digit) {
+      strs.push(str.substr(0, digit));
+      digit = 0;
+    } else {
+      strs.push(str);
+      digit -= str.length
+    }
+  } while(digit);
+
+  return strs.join('');
+}
+
+
+/**
+ * 用md5加密指定字符串
+ * @param encStr string 要加密的字符串
+ * @param pow number 要加密的次数
+ * @return string 加密后的字符串
+ */
+function hashEncryption(encStr, pow = 1) {
+  for (let i = 0; i < pow; i++) {
+    encStr = crypto.createHash('md5').update(encStr, 'utf8').digest('hex');
+  }
+  return encStr;
+}
+
+console.log(hashEncryption('admin', 2))
+
+module.exports = exports = {
+  randomChar, hashEncryption
+}