1. add login api
2. add get channel list api
This commit is contained in:
@@ -44,11 +44,96 @@ const chkObject = function (key = '', type = '', empty = false) {
|
||||
return true
|
||||
}
|
||||
|
||||
const resObject = (key = null, message = null, msgCode = null) => {
|
||||
/**
|
||||
* parse number
|
||||
* @param {any} v input number
|
||||
* @param {number} def default number
|
||||
* @param {number} min min number
|
||||
* @param {number} max max number
|
||||
* @return {number}
|
||||
*/
|
||||
const toInt = (v, def = 0, min = null, max = null) => {
|
||||
if (!isFinite(def)) def = 0
|
||||
if (typeof def === 'string') def = parseInt(def)
|
||||
min = isFinite(min) ? (typeof min === 'string' ? parseInt(min) : min) : null
|
||||
max = isFinite(max) ? (typeof max === 'string' ? parseInt(max) : max) : null
|
||||
if (!isFinite(v)) return def
|
||||
v = parseInt(v)
|
||||
if (min !== null && v < min) v = min
|
||||
if (max !== null && v > max) v = max
|
||||
return v
|
||||
}
|
||||
|
||||
/**
|
||||
* response object
|
||||
* @param {string} key response key
|
||||
* @param {string|object} message response object or message
|
||||
* @param {number} msgCode message code
|
||||
*/
|
||||
const resObject = (key = null, message = null, msgCode = null) => {
|
||||
if (key === null || typeof key !== 'string' || key.trim().length === 0) key = 'InternalError'
|
||||
key = key.trim()
|
||||
let resObj = _.cloneDeep(resMessage[key] || {})
|
||||
|
||||
if (msgCode !== null && isFinite(msgCode)) {
|
||||
resObj.obj.msgCode = toInt(msgCode)
|
||||
}
|
||||
|
||||
if (typeof message === 'string' && message.trim().length > 0) {
|
||||
resObj.obj.message = message.trim()
|
||||
} else if (typeof message === 'object') {
|
||||
resObj.obj = message
|
||||
}
|
||||
return resObj
|
||||
}
|
||||
|
||||
/**
|
||||
* API Error cls
|
||||
*/
|
||||
class APIError extends Error {
|
||||
set apiMsg (txt = '') {
|
||||
if (typeof txt !== 'string') txt = null
|
||||
this._apiMsg = txt
|
||||
}
|
||||
get apiMsg () {
|
||||
return this._apiMsg
|
||||
}
|
||||
|
||||
set msgCode (code = 0) {
|
||||
this._code = toInt(code, 0)
|
||||
}
|
||||
get msgCode () {
|
||||
return this._code
|
||||
}
|
||||
|
||||
set resKey (txt = '') {
|
||||
if (typeof txt !== 'string' || txt.trim().length === 0) txt = 'InternalError'
|
||||
this._resKey = txt
|
||||
}
|
||||
get resKey () {
|
||||
return this._resKey
|
||||
}
|
||||
}
|
||||
|
||||
const genError = (key = null, message = null, msgCode = null) => {
|
||||
let cls = new APIError()
|
||||
cls.apiMsg = message
|
||||
cls.msgCode = msgCode
|
||||
cls.resKey = key
|
||||
return cls
|
||||
}
|
||||
|
||||
const checkSession = async (c, n) => {
|
||||
if (!('session' in c) || !('user' in c.session) || !('loginType' in c.session)) throw genError('NotLogin')
|
||||
|
||||
return n()
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
chkObject,
|
||||
resObject
|
||||
resObject,
|
||||
toInt,
|
||||
APIError,
|
||||
genError,
|
||||
checkSession
|
||||
}
|
||||
|
||||
@@ -22,6 +22,13 @@ module.exports = {
|
||||
message: 'login first'
|
||||
}
|
||||
},
|
||||
Forbidden: {
|
||||
status: 403,
|
||||
obj: {
|
||||
status: 403,
|
||||
message: 'Forbidden'
|
||||
}
|
||||
},
|
||||
NotFound: {
|
||||
status: 404,
|
||||
obj: {
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
const config = require('@config/index')
|
||||
const bcrypt = require('bcrypt')
|
||||
const {
|
||||
toInt
|
||||
} = require('@libs/route-utils')
|
||||
|
||||
/**
|
||||
* hash password func
|
||||
* @param {string} pass password string
|
||||
* @return {string}
|
||||
*/
|
||||
const hashPassword = async (pass = '') => {
|
||||
if (typeof pass !== 'string' || pass.trim().length === 0) return null
|
||||
let saltRound = toInt(config.salt_round, 1, 1)
|
||||
// gen salt
|
||||
let salt = await new Promise((resolve, reject) => {
|
||||
bcrypt.genSalt(saltRound, (err, saltStr) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
resolve(saltStr)
|
||||
})
|
||||
})
|
||||
|
||||
let passHash = new Promise((resolve, reject) => {
|
||||
bcrypt.hash(pass, salt, (err, hash) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
resolve(hash)
|
||||
})
|
||||
})
|
||||
|
||||
return passHash
|
||||
}
|
||||
|
||||
/**
|
||||
* compare password and hash
|
||||
* @param {string} password password string
|
||||
* @param {string} hash hash string
|
||||
* @return {boolean}
|
||||
*/
|
||||
const comparePassword = async (password = '', hash = '') => {
|
||||
if (typeof password !== 'string' || password.trim().length === 0) return null
|
||||
if (typeof hash !== 'string' || hash.trim().length === 0) return null
|
||||
let check = new Promise((resolve, reject) => {
|
||||
bcrypt.compare(password, hash, (err, res) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
resolve(res)
|
||||
})
|
||||
})
|
||||
|
||||
return !!check
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hashPassword,
|
||||
comparePassword
|
||||
}
|
||||
Reference in New Issue
Block a user