34 Star 179 Fork 59

DCloud / uni-id

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
send-sms-code.js 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
import {
getSmsCode
} from '../../share/index'
import {
PublicErrorCode
} from '../utils/config'
export default async function ({
mobile,
code,
type,
templateId
}) {
if (!mobile) {
return {
code: PublicErrorCode.PARAM_REQUIRED,
messageValues: {
param: this.t('mobile')
}
}
}
if (!code) {
code = getSmsCode()
}
if (!type) {
return {
code: PublicErrorCode.PARAM_REQUIRED,
messageValues: {
param: this.t('verify-code-type')
}
}
}
const config = this._getConfig()
let smsConfig = config && config.service && config.service.sms
if (!smsConfig) {
throw new Error(this.t('config-param-required', {
param: 'service.sms'
}))
}
smsConfig = Object.assign({
codeExpiresIn: 300
}, smsConfig)
const {
name,
smsKey,
smsSecret,
codeExpiresIn
} = smsConfig
let action
switch (type) {
case 'login':
action = this.t('login')
break
default:
action = this.t('verify-mobile')
break
}
try {
const data = {
name,
code,
action,
expMinute: '' + Math.round(codeExpiresIn / 60)
}
if (name) {
data.name = name
}
await uniCloud.sendSms({
smsKey,
smsSecret,
phone: mobile,
templateId: templateId || 'uniID_code',
data
})
const setCodeRes = await this.setVerifyCode({
mobile,
code,
expiresIn: codeExpiresIn,
type
})
if (setCodeRes.code >= 0) {
return setCodeRes
}
return {
code: 0,
msg: ''
}
} catch (e) {
console.error(e)
return {
code: 50301
}
}
}
1
https://gitee.com/dcloud/uni-id.git
git@gitee.com:dcloud/uni-id.git
dcloud
uni-id
uni-id
master

搜索帮助