send-email-code.js 1.4 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
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
const {
  verifyCaptcha
} = require('../../lib/utils/captcha')
const {
  EMAIL_SCENE
} = require('../../common/constants')
const {
  ERROR
} = require('../../common/error')
/**
 * 发送邮箱验证码,可用于登录、注册、绑定邮箱、修改密码等操作
 * @tutorial
 * @param {Object} params
 * @param {String} params.email     邮箱
 * @param {String} params.captcha   图形验证码
 * @param {String} params.scene     使用场景
 * @returns
 */
module.exports = async function (params = {}) {
  const schema = {
    email: 'email',
    captcha: 'string',
    scene: 'string'
  }
  this.middleware.validate(params, schema)
26

DCloud_JSON's avatar
DCloud_JSON 已提交
27 28 29 30 31
  const {
    email,
    captcha,
    scene
  } = params
32

DCloud_JSON's avatar
DCloud_JSON 已提交
33 34 35 36 37
  if (!(Object.values(EMAIL_SCENE).includes(scene))) {
    throw {
      errCode: ERROR.INVALID_PARAM
    }
  }
38

DCloud_JSON's avatar
DCloud_JSON 已提交
39 40 41 42
  await verifyCaptcha.call(this, {
    scene: 'send-email-code',
    captcha
  })
43

DCloud_JSON's avatar
DCloud_JSON 已提交
44
  // -- 测试代码
45
  await require('../../lib/utils/verify-code')
DCloud_JSON's avatar
DCloud_JSON 已提交
46 47 48 49 50 51 52 53 54 55 56
    .setEmailVerifyCode.call(this, {
      email,
      code: '123456',
      expiresIn: 180,
      scene
    })
  return {
    errCode: 'uni-id-invalid-mail-template',
    errMsg: `已启动测试模式,直接使用:123456作为邮箱验证码即可。\n如果是正式项目,需自行实现发送邮件的相关功能`
  }
  // -- 测试代码
57 58


DCloud_JSON's avatar
DCloud_JSON 已提交
59 60
  //发送邮件--需自行实现
}