Ejemplo n.º 1
0
def login():
    resp = {'code': 200, 'msg': '操作成功', 'data': {}}  # 定义全局变量,操作成功返回信息
    req = request.values
    code = req['code'] if 'code' in req else ''
    if not code or len(code) < 1:
        resp['code'] = -1
        resp['msg'] = "需要code"
        return jsonify(resp)

    ## 通过code 可以获得用户的一些基本信息。获得的方法分到了公共方法里面
    openid = MemberService.getWeChatOpenId(code)
    if openid is None:  # 如果用户的请求里面拿到的code没有 openid(用户唯一标识)
        resp['code'] = -1
        resp['msg'] = "调用微信出错"
        return jsonify(resp)

    nickname = req['nickName'] if 'nickName' in req else ''
    sex = req['gender'] if 'gender' in req else 0  # 性别
    avatar = req['avatarUrl'] if 'avatarUrl' in req else ''  # 头像链接

    ## 建立数据库,确认这个openid是不是唯一的
    """
    判断是否已经注册过了,注册了直接返回一些信息
    """
    bind_info = OauthMemberBind.query.filter_by(
        openid=openid, type=1).first()  # type=1:信息来源表示是微信用户

    if not bind_info:  # 没有信息,即没注册。进行注册
        model_member = Member()
        model_member.nickname = nickname
        model_member.sex = sex
        model_member.avatar = avatar
        model_member.salt = MemberService.geneSalt()  # 秘钥
        model_member.created_time = getCurrentData()
        model_member.updated_time = getCurrentData()
        db.session.add(model_member)
        db.session.commit()

        # 建立绑定关系
        model_bind = OauthMemberBind()
        model_bind.member_id = model_member.id
        model_bind.type = 1  # 信息来源1,
        model_bind.openid = openid
        model_bind.extra = ''
        model_bind.created_time = getCurrentData()
        model_bind.updated_time = getCurrentData()
        db.session.add(model_bind)
        db.session.commit()

        bind_info = model_bind  # 将新的变量赋值给 bind_info,

    member_info = Member.query.filter_by(
        id=bind_info.member_id).first()  # 如果注册过了。会员信息 = 会员id (上面绑定用户的id)

    token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id
                       )  # 将token(加密字符串),返回给前台处理
    resp['data'] = {'token': token}  # 返回给前台的数据
    return jsonify(resp)
Ejemplo n.º 2
0
def login():
    resp = {'code': 200, 'msg': 'success', 'data': {}}
    req = request.values
    code = req['code'] if 'code' in req else ''
    if not code or len(code) < 1:
        resp['code'] = -1
        resp['msg'] = "需要code"
        return jsonify(resp)

    openid = MemberService.getWeChatOpenId(code)
    if openid is None:
        resp['code'] = -1
        resp['msg'] = "调用微信出错,请重新调用"
        return jsonify(resp)

    nickname = req['nickName'] if 'nickName' in req else ""
    sex = req['gender'] if 'gender' in req else 0
    avatar = req['avatar'] if 'avatar' in req else ""
    """
    判断是否注册过,如果注册过直接返回
    """
    bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first()
    if not bind_info:
        model_member = Member()
        model_member.nickname = nickname
        model_member.sex = sex
        model_member.avatar = avatar
        model_member.salt = MemberService.geneSalt()
        model_member.updated_time = getCurrentDate()
        model_member.created_time = getCurrentDate()
        db.session.add(model_member)
        db.session.commit()

        model_bind = OauthMemberBind()
        model_bind.member_id = model_member.id
        model_bind.type = 1
        model_bind.openid = openid
        model_bind.extra = ''
        model_bind.updated_time = getCurrentDate()
        model_bind.created_time = getCurrentDate()
        db.session.add(model_bind)
        db.session.commit()

        bind_info = model_bind

    member_info = Member.query.filter_by(id=bind_info.member_id).first()
    token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id)
    resp['data'] = {'token': token}
    return jsonify(resp)
Ejemplo n.º 3
0
def login():
    resp = {'code': 200, 'msg': "操作成功"}
    req = request.values
    code = req['code'] if 'code' in req else ''
    if not code or len(code) < 1:
        resp['code'] = -1
        resp['msg'] = '需要code'
        return jsonify(resp)

    openid = MemberService.getOpenId(code)

    nickname = req['nickName'] if 'nickName' in req else ''
    gender = req['gender'] if 'gender' in req else ''
    avatarUrl = req['avatarUrl'] if 'avatarUrl' in req else ''
    '''
        判断是否注册过
    '''
    bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first()
    if not bind_info:
        model_member = Member()
        model_member.nickname = nickname
        model_member.sex = gender
        model_member.avatar = avatarUrl
        model_member.salt = MemberService.geneSalt()
        model_member.updated_time = getCurrentDate()
        model_member.created_time = getCurrentDate()

        db.session.add(model_member)
        db.session.commit()

        model_bind = OauthMemberBind()
        model_bind.member_id = model_member.id
        model_bind.type = 1
        model_bind.openid = openid
        model_bind.extra = ''
        model_bind.created_time = getCurrentDate()
        model_bind.updated_time = getCurrentDate()

        db.session.add(model_bind)
        db.session.commit()

        bind_info = model_bind

    member_info = Member.query.filter_by(id=bind_info.member_id).first()

    token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id)
    resp['data'] = {'token': token}

    return jsonify(resp)