{"size": 2342, "ext": "js", "max_stars_repo_path": "server/src/service/admin.js", "max_stars_repo_name": "kakingchan/XPCMS", "max_stars_repo_stars_event_min_datetime": "2019-09-24T06:42:36.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-21T02:56:29.000Z", "max_issues_repo_path": "server/src/service/admin.js", "max_issues_repo_name": "kakingchan/XPCMS", "max_issues_repo_issues_event_min_datetime": "2019-10-04T18:46:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T22:59:56.000Z", "max_forks_repo_path": "server/src/service/admin.js", "max_forks_repo_name": "kakingchan/XPCMS", "max_forks_repo_forks_event_min_datetime": "2019-09-27T11:28:10.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-01T03:22:54.000Z"}
{"max_stars_count": 217, "max_issues_count": 18, "max_forks_count": 69, "avg_line_length": 25.4565217391, "max_line_length": 66, "alphanum_fraction": 0.5764304014}
import adminSchema from '../db/schema/admin'
import bcrypt from 'bcrypt'

export const getAdmins = async () => {
    let originResult = await adminSchema.lrange(0, -1)
    if(originResult && originResult.length) { 
        let result = originResult.map(item => JSON.parse(item))
        return result
    }else {
        return []
    }
}

export const findAdmin = async (name) => {
    let admins = await getAdmins()
    if(admins.length) {
        let result = admins.filter(item => item.username === name)
        return result
    }else {
        return []
    }
    
}

export const addAdmin = async (query) => {
    let admin = await findAdmin(query.username)
    if(admin.length) {
        return null
    }else {
        // 生成加密hash密码
        let hash = bcrypt.hashSync(query.pwd, 8)
        query.pwd = hash
        query.role = +query.role
        let result = await adminSchema.lpush(query)
        return result
    }
}

export const editAdmin = async (query) => {
    let admins = await getAdmins()
    let index = -1
    admins.forEach((item, i) => {
        if(item.username === query.prevName) {
            index = i
            return
        }
    })
    
    if(admins[index].pwd !== query.pwd) {
        // 生成加密hash密码
        let hash = bcrypt.hashSync(query.pwd, 8)
        query.pwd = hash
    }

    query.role = +query.role
    let result = await adminSchema.lset(index, query)
    return result
}

export const delAdmin = async (name) => {
    let admins = await getAdmins()
    let delIndex = -1
    admins.forEach((item, i) => {
        if(item.username === name) {
            delIndex = i
            return
        }
    })
    await adminSchema.lset(delIndex, 'del')
    return await adminSchema.lrem(1, 'del')
}

export const loginAdmin = async (query) => {
    let admin = await findAdmin(query.username)
    if(admin.length) {
        let { pwd, username, role } = admin[0]
        let match = bcrypt.compareSync(query.pwd, pwd)
        return match ? { username, role } : null
    }else {  // 用户不存在
        return null
    }
}

// 初始化管理员数据，目前先放在这个位置
export const initAdmin = async (query) => {
    let admins = await adminSchema.lrange(0, -1)
    if(!admins || !admins.length) {
        let hash = bcrypt.hashSync(query.pwd, 8)
        query.pwd = hash
        return await adminSchema.lpush(query)
    }
}