模拟枚举
2019-09-25 本文已影响0人
royluck
function isThisType(val){
for(let key in this){
if(this[key] === val){
return true
}
}
return false
}
const LoginType = {
USER_MINI_PROGRAM:100,
USER_EMAIL:101,
USER_MOBILE:102,
ADMIN_EMAIL:200,
isThisType
}
const ArtType = {
MOVIE:100,
MUSIC:200,
SENTENCE:300,
BOOK:400,
isThisType
}
module.exports = {
LoginType,
ArtType
}
使用:
router.post('/', async (ctx) => {
const v = await new TokenValidator().validate(ctx)
let token;
switch (v.get('body.type')) {
case LoginType.USER_EMAIL:
token = await emailLogin(v.get('body.account'),
v.get('body.secret'))
break
case LoginType.USER_MINI_PROGRAM:
token = await WXManager.codeToToken(v.get('body.account'))
break
case LoginType.ADMIN_EMAIL:
break
default:
throw new global.errs.ParameterException('没有相应的处理函数')
}
ctx.body = {
token
}
})