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 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
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)
  
  const {
    email,
    captcha,
    scene
  } = params
  
  if (!(Object.values(EMAIL_SCENE).includes(scene))) {
    throw {
      errCode: ERROR.INVALID_PARAM
    }
  }
  
  await verifyCaptcha.call(this, {
    scene: 'send-email-code',
    captcha
  })
  
  // -- 测试代码
  require('../../lib/utils/verify-code')
    .setEmailVerifyCode.call(this, {
      email,
      code: '123456',
      expiresIn: 180,
      scene
    })
  return {
    errCode: 'uni-id-invalid-mail-template',
    errMsg: `已启动测试模式,直接使用:123456作为邮箱验证码即可。\n如果是正式项目,需自行实现发送邮件的相关功能`
  }
  // -- 测试代码
  
  
  //发送邮件--需自行实现
}