Skip to content
关注公众号,获取新课通知

获取用户相关信息


控制器:app/controller/user.js

js
	// 获取用户相关信息
    async user_info() {
        const { ctx, service, app } = this;
        let currentUser = ctx.authUser;

        ctx.validate({
            user_id: {
                required: true,
                desc: "用户id",
                type: "int"
            }
        });

        let user_id = ctx.query.user_id;

        let res = await service.user.getUserInfo(user_id);

        let fensCount = 0;

        let followCount = 0;

        if (res) {
            fensCount = await service.user.getFensCount(user_id);
            followCount = await service.user.getFollowCount(user_id);
        }

        let follow = false;

        if (currentUser) {
            follow = await service.user.isFollow(currentUser.id, user_id);
        }

        ctx.apiSuccess({
            user: res,
            fensCount,
            followCount,
            follow
        });
    }

服务:app/service/user.js

js
// 用户相关信息
   async getUserInfo(user_id) {
       return await this.app.model.User.findOne({
           where: {
               id: user_id
           },
           attributes: {
               exclude: ['password']
           }
       });
   }

路由:app/router.js

js
// 获取用户相关信息
router.get("/user/user_info", controller.user.user_info);