lrf 2 anni fa
commit
d4dafcc06e

+ 1 - 0
.eslintignore

@@ -0,0 +1 @@
+coverage

+ 4 - 0
.eslintrc

@@ -0,0 +1,4 @@
+{
+  "extends": "eslint-config-egg",
+  "root": true
+}

+ 46 - 0
.github/workflows/nodejs.yml

@@ -0,0 +1,46 @@
+# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
+# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
+
+name: Node.js CI
+
+on:
+  push:
+    branches:
+      - main
+      - master
+  pull_request:
+    branches:
+      - main
+      - master
+  schedule:
+    - cron: '0 2 * * *'
+
+jobs:
+  build:
+    runs-on: ${{ matrix.os }}
+
+    strategy:
+      fail-fast: false
+      matrix:
+        node-version: [16, 18]
+        os: [ubuntu-latest, windows-latest, macos-latest]
+
+    steps:
+    - name: Checkout Git Source
+      uses: actions/checkout@v2
+
+    - name: Use Node.js ${{ matrix.node-version }}
+      uses: actions/setup-node@v1
+      with:
+        node-version: ${{ matrix.node-version }}
+
+    - name: Install Dependencies
+      run: npm i
+
+    - name: Continuous Integration
+      run: npm run ci
+
+    - name: Code Coverage
+      uses: codecov/codecov-action@v1
+      with:
+        token: ${{ secrets.CODECOV_TOKEN }}

+ 15 - 0
.gitignore

@@ -0,0 +1,15 @@
+logs/
+npm-debug.log
+yarn-error.log
+node_modules/
+*-lock.json
+*-lock.yaml
+yarn.lock
+coverage/
+.idea/
+run/
+.DS_Store
+*.sw*
+*.un~
+typings/
+.nyc_output/

+ 33 - 0
README.md

@@ -0,0 +1,33 @@
+# service-email
+
+邮件服务
+
+## QuickStart
+
+<!-- add docs here for user -->
+
+see [egg docs][egg] for more detail.
+
+### Development
+
+```bash
+$ npm i
+$ npm run dev
+$ open http://localhost:7001/
+```
+
+### Deploy
+
+```bash
+$ npm start
+$ npm stop
+```
+
+### npm scripts
+
+- Use `npm run lint` to check code style.
+- Use `npm test` to run unit test.
+- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.
+
+
+[egg]: https://eggjs.org

+ 48 - 0
app/controller/email.js

@@ -0,0 +1,48 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+const assert = require('assert');
+const nodemailer = require('nodemailer');
+const path = require('path');
+const fs = require('fs');
+
+// 发邮件
+class EmailController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    const { sender } = this.app.config;
+    this.sender = sender;
+  }
+  async sendEmail() {
+    const { template, receiver, params } = this.ctx.request.body;
+    assert(template, '缺少模板信息');
+    assert(receiver, '缺少接收人信息');
+    const text = this.getTemplate(template, params);
+    const config = {
+      host: 'smtp.163.com',
+      port: 465,
+      secure: true,
+      auth: this.sender,
+    };
+    const mailOptions = {
+      from: `"天恩活泉" <${this.sender.user}>`, // 邮件来源
+      to: receiver, // 邮件发送到哪里,多个邮箱使用逗号隔开
+      subject: '天恩活泉-邮箱绑定', // 邮件主题
+      html: text, // html类型的邮件正文
+    };
+    const transporter = nodemailer.createTransport(config);
+    transporter.sendMail(mailOptions, (error, info) => {
+      if (error) console.log(error);
+      else console.log(info);
+    });
+    this.ctx.ok();
+  }
+
+  getTemplate(template, params) {
+    const p = path.resolve('template', `${template}.js`);
+    const tl = require(p);
+    const f = tl(params);
+    return f;
+  }
+}
+module.exports = CrudController(EmailController, {});

+ 12 - 0
app/controller/home.js

@@ -0,0 +1,12 @@
+'use strict';
+
+const { Controller } = require('egg');
+
+class HomeController extends Controller {
+  async index() {
+    const { ctx } = this;
+    ctx.body = 'hi, egg';
+  }
+}
+
+module.exports = HomeController;

+ 12 - 0
app/router.js

@@ -0,0 +1,12 @@
+'use strict';
+
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const { router, controller } = app;
+  const { routePrefix, cluster } = app.config;
+  router.get('/', controller.home.index);
+  console.log(`${routePrefix}/sendEmail`);
+  router.post(`${routePrefix}/sendEmail`, controller.email.sendEmail);
+};

+ 64 - 0
config/config.default.js

@@ -0,0 +1,64 @@
+/* eslint valid-jsdoc: "off" */
+
+'use strict';
+
+/**
+ * @param {Egg.EggAppInfo} appInfo app info
+ */
+module.exports = appInfo => {
+  /**
+   * built-in config
+   * @type {Egg.EggAppConfig}
+   **/
+  const config = (exports = {});
+
+  // use for cookie sign key, should change to your own and keep security
+  config.keys = appInfo.name + '_1665483162811_546';
+
+  // add your middleware config here
+  config.middleware = [];
+
+  // add your user config here
+  const userConfig = {
+    // myAppName: 'egg',
+  };
+  // redis设置
+  config.redis = {
+    client: {
+      port: 6379, // Redis port
+      host: '127.0.0.1', // Redis host
+      password: '123456',
+      db: 9,
+    },
+  };
+  // 进程设置
+  config.cluster = {
+    listen: {
+      port: 14002,
+    },
+  };
+  // 数据库设置
+  config.dbName = 'service-email';
+  config.mongoose = {
+    url: `mongodb://localhost:27017/${config.dbName}`,
+    options: {
+      user: 'admin',
+      pass: 'admin',
+      authSource: 'admin',
+      useNewUrlParser: true,
+      useCreateIndex: true,
+    },
+  };
+  // 邮件发送人
+  config.sender = {
+    user: 'myhope1977@163.com', // 邮箱账号
+    pass: 'RZGYKLOQUTRCNLEO', // 邮箱stmp授权码
+  };
+
+  // 路由设置
+  config.routePrefix = '/semail/api';
+  return {
+    ...config,
+    ...userConfig,
+  };
+};

+ 9 - 0
config/plugin.js

@@ -0,0 +1,9 @@
+'use strict';
+
+/** @type Egg.EggPlugin */
+module.exports = {
+  // had enabled by egg
+  // static: {
+  //   enable: true,
+  // }
+};

+ 49 - 0
package.json

@@ -0,0 +1,49 @@
+{
+  "name": "service-email",
+  "version": "1.0.0",
+  "description": "邮件服务",
+  "private": true,
+  "egg": {
+    "framework": "naf-framework-mongoose-free"
+  },
+  "dependencies": {
+    "egg": "^3",
+    "egg-scripts": "^2",
+    "lodash": "^4.17.21",
+    "moment": "^2.29.1",
+    "naf-framework-mongoose-free": "^0.0.37",
+    "nodemailer": "^6.7.8"
+  },
+  "devDependencies": {
+    "egg-bin": "^5",
+    "egg-ci": "^2",
+    "egg-mock": "^5",
+    "eslint": "^8",
+    "eslint-config-egg": "^12",
+    "jsonwebtoken": "^8.5.1"
+  },
+  "engines": {
+    "node": ">=16.0.0"
+  },
+  "scripts": {
+    "start": "egg-scripts start --daemon --title=egg-server-service-email",
+    "stop": "egg-scripts stop --title=egg-server-service-email",
+    "dev": "egg-bin dev",
+    "debug": "egg-bin debug",
+    "test": "npm run lint -- --fix && npm run test-local",
+    "test-local": "egg-bin test",
+    "cov": "egg-bin cov",
+    "lint": "eslint .",
+    "ci": "npm run lint && npm run cov"
+  },
+  "ci": {
+    "version": "16, 18",
+    "type": "github"
+  },
+  "repository": {
+    "type": "git",
+    "url": ""
+  },
+  "author": "lrf",
+  "license": "MIT"
+}

+ 7 - 0
template/bindEmail.js

@@ -0,0 +1,7 @@
+const _ = require('lodash');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+module.exports = params => {
+  const code = _.get(params, 'code');
+  if (!code) throw new BusinessError(ErrorCode.DATA_INVALID, '缺少模板需要的参数');
+  return `【邮箱绑定】验证码为:${code},用于平台用户信息验证,若非本人操作,请忽略此信息。`;
+};

+ 20 - 0
test/app/controller/home.test.js

@@ -0,0 +1,20 @@
+'use strict';
+
+const { app, assert } = require('egg-mock/bootstrap');
+
+describe('test/app/controller/home.test.js', () => {
+  it('should assert', async () => {
+    const pkg = require('../../../package.json');
+    assert(app.config.keys.startsWith(pkg.name));
+
+    // const ctx = app.mockContext({});
+    // yield ctx.service.xx();
+  });
+
+  it('should GET /', async () => {
+    return app.httpRequest()
+      .get('/')
+      .expect('hi, egg')
+      .expect(200);
+  });
+});