All files / koa/app/controller/common sends.js

12.9% Statements 4/31
0% Branches 0/14
0% Functions 0/2
12.9% Lines 4/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 845x 5x                                   5x                                                                                       5x                                        
const aliyunSMS = require('../../../libs/aliyun-sms');
const mail = require('../../../libs/mail');
 
/**
 * @api {post} common/sendEmail 发送邮件
 * @apiDescription 作者:shanzm
 *
 * @apiGroup common
 * @apiName sendEmail
 * @apiSampleRequest common/sendEmail
 * @apiPermission user
 *
 * @apiParam {String} to 收件人邮箱
 * @apiParam {String} subject 标题
 * @apiParam {String} content 内容
 *
 * @apiUse Header
 * @apiUse Response
 */
exports.sendEmail = async(ctx, next) => {
  let body = ctx.request.body
  try {
    let msg
    if (!body.to) {
      msg = '收件人不能为空'
    }
    if (!body.subject) {
      msg = '标题不能为空'
    }
    if (!body.content) {
      msg = '内容不能为空'
    }
    if (msg) {
      return ctx.resMid.resBody(ctx, 422, msg)
    }
    let doc = {
      to: body.to,
      subject: body.subject,
      content: body.content,
      desc: body.subject,
    };
    await mail.sendMail(doc)
    ctx.resMid.resBody(ctx, '', '发送成功')
  } catch (err) {
    ctx.throw(err)
  }
}
 
/**
 * @api {post} common/sendMessage 发送短信
 * @apiDescription 作者:shanzm
 *
 * @apiGroup common
 * @apiName sendMessage
 * @apiSampleRequest common/sendMessage
 * @apiPermission user
 *
 * @apiParam {String} phone 手机号码
 * @apiParam {String} content 内容
 *
 * @apiUse Header
 * @apiUse Response
 */
exports.sendMessage = async(ctx, next) => {
  let body = ctx.request.body
  try {
    let msg
    let phone = body.phone
    let content = body.content
    if (!phone) {
      msg = '手机号码不能为空'
    }
    if (!content) {
      msg = '内容不能为空'
    }
    if (msg) {
      return ctx.resMid.resBody(ctx, 422, msg)
    }
    await aliyunSMS.messageNotify(phone, content)
    ctx.resMid.resBody(ctx, '', '发送成功')
  } catch (err) {
    ctx.throw(err)
  }
}