10 changed files with 142 additions and 246 deletions
			
			
		@ -1,189 +0,0 @@ | 
				
			|||||
'use strict'; | 
					 | 
				
			||||
const Hex = require('crypto-js/enc-hex'); | 
					 | 
				
			||||
const MD5 = require('crypto-js/md5'); | 
					 | 
				
			||||
const moment = require('moment'); | 
					 | 
				
			||||
const uuid = require('uuid'); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
async function login(ctx, next) { | 
					 | 
				
			||||
    const transaction = await ctx.fs.dc.orm.transaction(); | 
					 | 
				
			||||
    try { | 
					 | 
				
			||||
        const models = ctx.fs.dc.models; | 
					 | 
				
			||||
        const params = ctx.request.body; | 
					 | 
				
			||||
        let password = Hex.stringify(MD5(params.password)); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
        const userRes = await models.User.findOne({ | 
					 | 
				
			||||
            where: { | 
					 | 
				
			||||
                username: params.username, | 
					 | 
				
			||||
                password: password, | 
					 | 
				
			||||
                delete: false, | 
					 | 
				
			||||
            }, | 
					 | 
				
			||||
            attributes: { exclude: ['password'] }, | 
					 | 
				
			||||
            include: [{ | 
					 | 
				
			||||
                attributes: ["resourceId"], | 
					 | 
				
			||||
                model: models.UserResource | 
					 | 
				
			||||
            }] | 
					 | 
				
			||||
        }); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
        if (!userRes) { | 
					 | 
				
			||||
            ctx.status = 400; | 
					 | 
				
			||||
            ctx.body = { | 
					 | 
				
			||||
                "message": "账号或密码错误" | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
        } else if (!userRes.enable) { | 
					 | 
				
			||||
            ctx.status = 400; | 
					 | 
				
			||||
            ctx.body = { message: "该用户已被禁用" } | 
					 | 
				
			||||
        } else { | 
					 | 
				
			||||
            const token = uuid.v4(); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
            let userRslt = Object.assign(userRes.dataValues, { | 
					 | 
				
			||||
                authorized: true, | 
					 | 
				
			||||
                token: token, | 
					 | 
				
			||||
                userResources: userRes.userResources.map(r => r.resourceId), | 
					 | 
				
			||||
            }); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
            await models.UserToken.create({ | 
					 | 
				
			||||
                token: token, | 
					 | 
				
			||||
                userInfo: userRslt, | 
					 | 
				
			||||
                expired: moment().add(30, 'days').format() | 
					 | 
				
			||||
            }); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
            ctx.status = 200; | 
					 | 
				
			||||
            ctx.body = userRslt; | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
        await transaction.commit(); | 
					 | 
				
			||||
    } catch (error) { | 
					 | 
				
			||||
        await transaction.rollback(); | 
					 | 
				
			||||
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); | 
					 | 
				
			||||
        ctx.status = 400; | 
					 | 
				
			||||
        ctx.body = { | 
					 | 
				
			||||
            "message": "登录失败" | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
} | 
					 | 
				
			||||
 | 
					 | 
				
			||||
/** | 
					 | 
				
			||||
 * 微信小程序登录 | 
					 | 
				
			||||
 * @@requires.body {phone-手机号, password-密码} ctx  | 
					 | 
				
			||||
 */ | 
					 | 
				
			||||
async function wxLogin(ctx, next) { | 
					 | 
				
			||||
    const transaction = await ctx.fs.dc.orm.transaction(); | 
					 | 
				
			||||
    try { | 
					 | 
				
			||||
        const models = ctx.fs.dc.models; | 
					 | 
				
			||||
        const params = ctx.request.body; | 
					 | 
				
			||||
        let password = Hex.stringify(MD5(params.password)); | 
					 | 
				
			||||
        const userRes = await models.User.findOne({ | 
					 | 
				
			||||
            where: { | 
					 | 
				
			||||
                phone: params.phone, | 
					 | 
				
			||||
                password: password, | 
					 | 
				
			||||
                delete: false, | 
					 | 
				
			||||
            }, | 
					 | 
				
			||||
            attributes: { exclude: ['password'] } | 
					 | 
				
			||||
        }); | 
					 | 
				
			||||
        if (!userRes) { | 
					 | 
				
			||||
            ctx.status = 400; | 
					 | 
				
			||||
            ctx.body = { message: "手机号或密码错误" } | 
					 | 
				
			||||
        } else if (!userRes.enable) { | 
					 | 
				
			||||
            ctx.status = 400; | 
					 | 
				
			||||
            ctx.body = { message: "该用户已被禁用" } | 
					 | 
				
			||||
        } else { | 
					 | 
				
			||||
            const token = uuid.v4(); | 
					 | 
				
			||||
            //获取用户关注区域信息
 | 
					 | 
				
			||||
            const departmentRes = await models.Department.findOne({ where: { id: userRes.departmentId } }); | 
					 | 
				
			||||
            let attentionRegion = departmentRes; | 
					 | 
				
			||||
            while (attentionRegion.dependence && attentionRegion.type != 1) { | 
					 | 
				
			||||
                const departmentParent = await models.Department.findOne({ where: { id: attentionRegion.dependence } }); | 
					 | 
				
			||||
                attentionRegion = { | 
					 | 
				
			||||
                    ...departmentParent.dataValues, | 
					 | 
				
			||||
                    nextRegin: attentionRegion | 
					 | 
				
			||||
                } | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
            //获取用户权限信息
 | 
					 | 
				
			||||
            const resourceRes = await models.UserResource.findAll({ | 
					 | 
				
			||||
                where: { | 
					 | 
				
			||||
                    userId: userRes.id | 
					 | 
				
			||||
                }, | 
					 | 
				
			||||
                include: [{ | 
					 | 
				
			||||
                    model: models.Resource, | 
					 | 
				
			||||
                    attributes: ['code', 'name'], | 
					 | 
				
			||||
                }], | 
					 | 
				
			||||
                attributes: [] | 
					 | 
				
			||||
            }); | 
					 | 
				
			||||
            let userRslt = Object.assign({ | 
					 | 
				
			||||
                authorized: true, | 
					 | 
				
			||||
                token: token, | 
					 | 
				
			||||
                ...userRes.dataValues | 
					 | 
				
			||||
            }); | 
					 | 
				
			||||
            await models.UserToken.create({ | 
					 | 
				
			||||
                token: token, | 
					 | 
				
			||||
                userInfo: userRslt, | 
					 | 
				
			||||
                expired: moment().add(30, 'day').format('YYYY-MM-DD HH:mm:ss') | 
					 | 
				
			||||
            }, { transaction: transaction }); | 
					 | 
				
			||||
            ctx.status = 200; | 
					 | 
				
			||||
            ctx.body = Object.assign({ | 
					 | 
				
			||||
                ...userRslt, | 
					 | 
				
			||||
                userRegionType: departmentRes.type,//1-市级,2-区县级,3-乡镇级,4-村级
 | 
					 | 
				
			||||
                attentionRegion: attentionRegion, | 
					 | 
				
			||||
                resources: resourceRes.map(r => r.resource) | 
					 | 
				
			||||
            }); | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
        await transaction.commit(); | 
					 | 
				
			||||
    } catch (error) { | 
					 | 
				
			||||
        await transaction.rollback(); | 
					 | 
				
			||||
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); | 
					 | 
				
			||||
        ctx.status = 400; | 
					 | 
				
			||||
        ctx.body = { | 
					 | 
				
			||||
            "message": "登录失败" | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
} | 
					 | 
				
			||||
 | 
					 | 
				
			||||
async function logout(ctx) { | 
					 | 
				
			||||
    try { | 
					 | 
				
			||||
        const { token, code } = ctx.request.body; | 
					 | 
				
			||||
        const models = ctx.fs.dc.models; | 
					 | 
				
			||||
 | 
					 | 
				
			||||
        await models.UserToken.destroy({ | 
					 | 
				
			||||
            where: { | 
					 | 
				
			||||
                token: token, | 
					 | 
				
			||||
            }, | 
					 | 
				
			||||
        }); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
        ctx.status = 204; | 
					 | 
				
			||||
    } catch (error) { | 
					 | 
				
			||||
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); | 
					 | 
				
			||||
        ctx.status = 400; | 
					 | 
				
			||||
        ctx.body = { | 
					 | 
				
			||||
            "message": "登出失败" | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
} | 
					 | 
				
			||||
 | 
					 | 
				
			||||
/** | 
					 | 
				
			||||
 * 微信小程序登出 | 
					 | 
				
			||||
 * @request.body {token-用户登录Token} ctx  | 
					 | 
				
			||||
 */ | 
					 | 
				
			||||
async function wxLogout(ctx) { | 
					 | 
				
			||||
    try { | 
					 | 
				
			||||
        const { token } = ctx.request.body; | 
					 | 
				
			||||
        const models = ctx.fs.dc.models; | 
					 | 
				
			||||
        await models.UserToken.destroy({ | 
					 | 
				
			||||
            where: { | 
					 | 
				
			||||
                token: token, | 
					 | 
				
			||||
            }, | 
					 | 
				
			||||
        }); | 
					 | 
				
			||||
        ctx.status = 204; | 
					 | 
				
			||||
    } catch (error) { | 
					 | 
				
			||||
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); | 
					 | 
				
			||||
        ctx.status = 400; | 
					 | 
				
			||||
        ctx.body = { | 
					 | 
				
			||||
            "message": "登出失败" | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
} | 
					 | 
				
			||||
 | 
					 | 
				
			||||
module.exports = { | 
					 | 
				
			||||
    login, | 
					 | 
				
			||||
    wxLogin, | 
					 | 
				
			||||
    logout, | 
					 | 
				
			||||
    wxLogout | 
					 | 
				
			||||
}; | 
					 | 
				
			||||
@ -0,0 +1,68 @@ | 
				
			|||||
 | 
					'use strict'; | 
				
			||||
 | 
					const moment = require('moment') | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					async function edit (ctx, next) { | 
				
			||||
 | 
					    const transaction = await ctx.fs.dc.orm.transaction(); | 
				
			||||
 | 
					    try { | 
				
			||||
 | 
					        const models = ctx.fs.dc.models; | 
				
			||||
 | 
					        const { userId } = ctx.fs.api | 
				
			||||
 | 
					        const data = ctx.request.body; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        // 或取其他服务信息
 | 
				
			||||
 | 
					        const nvrData = { | 
				
			||||
 | 
					            channelCount: 8, | 
				
			||||
 | 
					            port: 8080, | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        if (data.id) { | 
				
			||||
 | 
					            // 修改
 | 
				
			||||
 | 
					            const storageData = Object.assign({}, data, nvrData) | 
				
			||||
 | 
					            await models.Nvr.update(storageData, { | 
				
			||||
 | 
					                where: { | 
				
			||||
 | 
					                    id: data.id | 
				
			||||
 | 
					                }, | 
				
			||||
 | 
					                transaction | 
				
			||||
 | 
					            }) | 
				
			||||
 | 
					        } else { | 
				
			||||
 | 
					            // 添加
 | 
				
			||||
 | 
					            const storageData = Object.assign({}, data, nvrData, { | 
				
			||||
 | 
					                createTime: moment().format(), | 
				
			||||
 | 
					                createUserId: userId, | 
				
			||||
 | 
					                delete: false, | 
				
			||||
 | 
					            }) | 
				
			||||
 | 
					            await models.Nvr.create(storageData, { transaction }) | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        await transaction.commit(); | 
				
			||||
 | 
					        ctx.status = 204; | 
				
			||||
 | 
					    } catch (error) { | 
				
			||||
 | 
					        await transaction.rollback(); | 
				
			||||
 | 
					        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); | 
				
			||||
 | 
					        ctx.status = 400; | 
				
			||||
 | 
					        ctx.body = {} | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					async function del (ctx, next) { | 
				
			||||
 | 
					    try { | 
				
			||||
 | 
					        const models = ctx.fs.dc.models; | 
				
			||||
 | 
					        const { nvrId } = ctx.params | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        await models.Nvr.destroy({ | 
				
			||||
 | 
					            where: { | 
				
			||||
 | 
					                id: nvrId | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					        }) | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        ctx.status = 204; | 
				
			||||
 | 
					    } catch (error) { | 
				
			||||
 | 
					        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); | 
				
			||||
 | 
					        ctx.status = 400; | 
				
			||||
 | 
					        ctx.body = {} | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					module.exports = { | 
				
			||||
 | 
					    edit, | 
				
			||||
 | 
					    del, | 
				
			||||
 | 
					}; | 
				
			||||
@ -1,32 +0,0 @@ | 
				
			|||||
'use strict'; | 
					 | 
				
			||||
 | 
					 | 
				
			||||
const auth = require('../../controllers/auth'); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
module.exports = function (app, router, opts) { | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * @api {Post} login 登录. | 
					 | 
				
			||||
     * @apiVersion 1.0.0 | 
					 | 
				
			||||
     * @apiGroup Auth | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    app.fs.api.logAttr['POST/login'] = { content: '登录', visible: true }; | 
					 | 
				
			||||
    router.post('/login', auth.login); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * @api {POST} wxLogin 微信小程序登录.(使用手机号、密码登录) | 
					 | 
				
			||||
     * @apiVersion 1.0.0 | 
					 | 
				
			||||
     * @apiGroup Auth | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    app.fs.api.logAttr['POST/wxLogin'] = { content: '微信小程序登录', visible: true }; | 
					 | 
				
			||||
    router.post('/wxLogin', auth.wxLogin); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    app.fs.api.logAttr['PUT/logout'] = { content: '登出', visible: false }; | 
					 | 
				
			||||
    router.put('/logout', auth.logout); | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * @api {PUT} wxLogout 微信小程序登出 | 
					 | 
				
			||||
     * @apiVersion 1.0.0 | 
					 | 
				
			||||
     * @apiGroup Auth | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    app.fs.api.logAttr['PUT/wxLogout'] = { content: '登出', visible: false }; | 
					 | 
				
			||||
    router.put('/wxLogout', auth.wxLogout); | 
					 | 
				
			||||
}; | 
					 | 
				
			||||
@ -0,0 +1,11 @@ | 
				
			|||||
 | 
					'use strict'; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					const nvr = require('../../controllers/nvr'); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					module.exports = function (app, router, opts) { | 
				
			||||
 | 
					    app.fs.api.logAttr['POST/nvr'] = { content: '添加/修改nvr', visible: false }; | 
				
			||||
 | 
					    router.post('/nvr', nvr.edit); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    app.fs.api.logAttr['DEL/nvr'] = { content: '删除nvr', visible: false }; | 
				
			||||
 | 
					    router.del('/nvr/:nvrId', nvr.del); | 
				
			||||
 | 
					}; | 
				
			||||
@ -0,0 +1,29 @@ | 
				
			|||||
 | 
					'use strict'; | 
				
			||||
 | 
					const redis = require("ioredis") | 
				
			||||
 | 
					const moment = require('moment') | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					module.exports = async function factory (app, opts) { | 
				
			||||
 | 
					    let client = new redis(opts.redis.port, opts.redis.host); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    client.on("error", function (err) { | 
				
			||||
 | 
					        app.fs.logger.error('info', '[FS-AUTH-REDIS]', 'redis connect error.'); | 
				
			||||
 | 
					        console.error("Error :", err); | 
				
			||||
 | 
					        process.exit(-1); | 
				
			||||
 | 
					    }); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    client.on('connect', function () { | 
				
			||||
 | 
					        console.log(`redis connect success ${opts.redis.host + ':' + opts.redis.port}`); | 
				
			||||
 | 
					    }) | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    // 自定义方法
 | 
				
			||||
 | 
					    async function hdelall (key) { | 
				
			||||
 | 
					        const obj = await client.hgetall(key); | 
				
			||||
 | 
					        const hkeys = Object.keys(obj) | 
				
			||||
 | 
					        await client.hdel(key, hkeys) | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    app.redis = client | 
				
			||||
 | 
					    app.redisTools = { | 
				
			||||
 | 
					        hdelall, | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue