示例#1
0
def login():
    resp = {'code': 200, 'msg': "操作成功", "data": {}}
    req = request.values

    code = req['code']
    app.logger.info(len(code))
    app.logger.info(code)
    if code == None or len(code) < 10:
        resp['code'] = -1
        resp['msg'] = "需要code"
        app.logger.info("need code")
        return jsonify(resp)
    openid = MemberService.getWeChatOpenId(code)
    if openid == None:
        resp['code'] = -1
        resp['msg'] = "微信调用登录接口失败"
        app.logger.info("微信调用登录接口失败")
        return jsonify(resp)
    resp["openid"] = openid
    nickname = req['nickName']
    sex = req['gender']
    avatar = req['avatarUrl']

    bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first()
    app.logger.debug("bind_info")
    app.logger.debug(bind_info)
    if not bind_info:
        model_member = Member()
        model_member.nickname = nickname
        model_member.sex = sex
        model_member.avatar = avatar
        model_member.salt = MemberService.genSalt()
        model_member.created_time = model_member.updated_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.extra = ''
        model_bind.openid = openid
        model_bind.updated_time = model_bind.created_time = getCurrentDate()
        db.session.add(model_bind)
        db.session.commit()

        db.session.flush()

        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)
示例#2
0
def login():
    resp = {'code': 200, 'msg': '操作成功~', 'data': {}}
    req = request.values
    # app.logger.info( req )
    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 ''
    avatar = req['avatarUrl'] if 'avatarUrl' in req else ''
    # app.logger.info( req )

    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 = 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 = 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)
示例#3
0
def login():
    prompt = {'code': 200, "msg": "登陆成功", "data": {}}
    req = request.values
    code = req['code'] if 'code' in req else ''
    if not code or len(code) < 1:
        prompt['code'] = -1
        prompt['msg'] = "未接收到code!"
        return jsonify(prompt)

    url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code" \
        .format(app.config['MINA']['appid'], app.config['MINA']['appkey'], code)
    r = requests.get(url)
    res = json.loads(r.text)
    openid = res['openid']

    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 ''

    member_info = Member.query.filter_by(openid=openid).first()
    if member_info:
        if member_info.status != 1:
            prompt['code'] = -1
            prompt['msg'] = "用户已失效!"
            return jsonify(prompt)
        token = "%s-%s" % (member_info.id, member_info.status)
        prompt['data'] = {"token": token}
        return jsonify(prompt)

    model_member = Member()
    model_member.nickname = nickname
    model_member.sex = sex
    model_member.avatar = avatar
    model_member.openid = openid
    model_member.purview = 1
    db.session.add(model_member)
    db.session.commit()

    token = "%s-%s" % (member_info.id, member_info.status)
    prompt['data'] = {"token": token}
    return jsonify(prompt)