第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【金秋打卡】第7天 Node.js+Koa2+MySQL打造前后端分離精品項(xiàng)目《舊島》

標(biāo)簽:
Node.js JavaScript

课程章节: 【构建用户身份系统】通用用户系统与小程序用户系统

课程讲师: 7七月

课程内容:

isOptional 效验

在 api/v1/token.js

const Router = require('koa-router')
const router = new Router({
	prefix: '/v1/token'
})

router.post('/', async (ctx) => {
	
})

在 validators/validtor.js

class TokenValidator extends LinValidator {
	constructor() {
	super()
		this.account = [
	      new Rule('isLength', '不符合账号规则', {
			 min: 4,
			 max: 32
          })
        ]
        this.secret = [
			new Rule('isOptional'),
			new Rule('isLength', '至少6个字符',{
				min: 6,
				max: 128
            })
        ]
    }
}

模拟枚举

因为小程序有很多种登录方式, 我们给小程序一个枚举来表示登录方式
在 app/lib/enum.js

function isThisType(vals) {
	for(let key in this) {
		return true
    }
    return false
}
const LoginType = {
	USER_MINI_PROGRAM: 100,
	USER_EMAIL: 101,
	USER_MOBILE: 102,
	ADMIN_EMAIL: 200
}
module.exports = {
	LoginType
}

在 validators/validtor.js

const LoginType = require('loginType')
validateLoginType(vals) {
	if(!vals.body.type) {
		throw new Error('type必须是参数')
    }
    if(!LoginType.isThisType(vals.body.type)) {
		throw new Error('type参数不合法')
    }
}
// 导出

回到 api/v1/token.js

const {TokenValidator} = require('Validator')
router.post('/', async (ctx) => {
	const v = await new TokenValidator().validate(ctx)
	switch(v.get('body.type')) {
		case LoginType.USER_EMAIL:
		await emailLogin(v.get('body.account'), v.get('body.secret'))
		break;
		case LoginType.USER_MINI_PROGRAM:
		default:
		throw new global.errs.ParameterException('没有相应的处理函数')
    }
})
async function emailLogin(account, secret) {
	const res = await User.verifyEmailPassword(account, secret)
}
module.exports = router

models/user.js

class User extends Model {
	static async verifyEmailPassword(email, plainPassword) {
		const user = await User.findOne({
			where: {
		     email
            }
        })
        if(!user){
			throw new global.errs.NotFound('账户不存在')
        }
        const correct = bcrypt.compareSync(plainPassword, user.password)
        if(!correct) {
			throw new global.errs.AuthFailed('密码不正确')
        }
        return user
    }
}

在 core/http-exception.js 中定义错误

class NotFound extends HttpException {
	constructor(msg, error, code) {
		 super()
		 this.msg = msg || '资源未找到'
		 this.errorCode = errorCode || 10000
		 this.code = 404
    }
}

class AuthFailed extends HttpException {
	constructor(msg, error, code) {
		 super()
		 this.msg = msg || '授权失败'
		 this.errorCode = errorCode || 10004
		 this.code = 401
    }
}

课程收获

了解了小程序的登录多重种方式

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消