'use strict';

const Controller = require('egg').Controller;
const uuidv4 = require('uuid/v4');
class UserController extends Controller {


    async auth() {

        const { ctx, app } = this;
        const code = ctx.request.body.code;
        if (!code) {
            ctx.failed('error code');
        }

        //请求微信授权 获取openid
        const wx_auth_ret = await ctx.service.course.v1.user.requestWxAuth(code);
        const openid = wx_auth_ret.openid;//获取openid

        //检查是否已授权过 是否已入用户表
        let user = await ctx.classModel.V1.CourseUser.one({ where: { openid, is_deleted: 0 } });
        if (!user || !user.uuid) {
            const uuid = uuidv4();
            user = await ctx.classModel.V1.CourseUser.create({ uuid, openid });
            // user.uuid = uuid;
        }
        console.info(user);
        //存储缓存标识
        const user_uuid = user.uuid;
        const key = 'course_user_session_' + user_uuid;
        await app.memcache.set(key, { user_uuid, openid }, 86400 * 180);
        const auth_token = ctx.helper.md5(openid + user_uuid + 'jbwl');
        ctx.set('uuid', key);
        ctx.set('auth_token', auth_token);

        const result = { uuid: user_uuid, auth_token };

        ctx.success({ result });

    }

    async registerUserInfo() {

        const { ctx } = this;
        const uuid = ctx.userUuid;
        const input_params = ctx.request.body;
        const { avatar, nickname, province, country, sex, city } = input_params;

        //查找用户是否存在并更新
        const user = ctx.classModel.V1.CourseUser.one({ where: { uuid } });
        await ctx.classModel.V1.CourseUser.edit({ params: { avatar, nickname, sex }, where: { uuid } });
        let bindphone = 0;
        if (user.phone) {
            bindphone = 1;
        }
        const result = { bindphone };

        ctx.success({ result });
    }

    /**
     * 获取baby信息
     */
    async getBabyInfo() {

        const { ctx } = this;
        const result = await ctx.service.course.v1.user.getBabyInfo();

        ctx.success({ result });
    }

    /**
     * 保存baby信息
     */
    async saveBabyInfo() {

        const { ctx } = this;
        const input_params = ctx.request.body;
        const result = await ctx.service.course.v1.user.saveBabyInfo(input_params);

        ctx.success({ result });
    }

    /**
     * 删除baby信息
     */
    async delBabyInfo() {

        const { ctx } = this;
        const result = await ctx.service.course.v1.user.delBabyInfo();

        ctx.success({ result });
    }

    /**
     * 用户收藏机构列表
     */
    async getCollectInstitutions() {

        const { ctx } = this;
        let input_params = ctx.request.body;
        input_params = Object.assign(input_params, ctx.request.query);
        const result = await ctx.service.course.v1.user.getCollectInstitutions(input_params);

        ctx.success({ result });
    }

    /**
     * 收藏机构
     */
    async collectInstitution() {

        const { ctx } = this;
        const institution_id = ctx.request.body.institution_id;
        if (!institution_id) {
            ctx.failed('error institution_id');
        }
        const result = await ctx.service.course.v1.user.collectInstitution(institution_id);

        ctx.success({ result });
    }

    /**
    * 取消收藏机构
    */
    async delCollectInstitution() {

        const { ctx } = this;
        const institution_id = ctx.request.body.institution_id;
        if (!institution_id) {
            ctx.failed('error institution_id');
        }
        const result = await ctx.service.course.v1.user.delCollectInstitution(institution_id);

        ctx.success({ result });
    }




}

module.exports = UserController;
