Пример #1
0
def login():
    ctx = {'code': 1, 'msg': "成功",'data': {}}
    nickname = request.form.get("nickname")


    gender = request.form.get("gender")
    avatarurl = request.form.get("avatarur")
    code = request.form.get("code")

    if not code or len(code) <1:
        ctx['code']  = -1
        ctx['msg'] = '获取openid出错'
        return jsonify(ctx)
    opneid  =MemberService.getOpenId(code)

    if not opneid:
        ctx['code'] = -1
        ctx['msg'] = 'code无效'
        return jsonify(ctx)
    app_id = current_app.config.get('APP_ID')
    app_secret= current_app.config.get('app_secret')
    ctx['data']['nickname'] = nickname

    url =  'https://api.weixin.qq.com/sns/jscode2session?appid=%swx4503a4f6af272145&secret=%s&js_code=%s&grant_type=authorization_code'%(code,app_id,app_secret)
    response = request.get(url)
    opneid = response.json().get('openid')

    oauthmemberbind = OauthMemberBind.query.filter_by(opneid=opneid).first()
    #存数据库

    if not oauthmemberbind:
        member = Member()
        member.nickname = nickname
        member.avatar =avatarurl
        member.gender =gender
        member.salt =MemberService.getSalt()
        db.session.add(member)
        db.session.commit()

        oauthmemberbind=  OauthMemberBind
        oauthmemberbind.openid = opneid
        oauthmemberbind.client_type = 'wx'
        oauthmemberbind.type = 1
        oauthmemberbind.member_id = member.id
        db.session.add(oauthmemberbind)
        db.session.commit()
        token = MemberService.geneAuthCode(member)
        ctx['data']['token'] = '%s#%s'%(member.id,token)
        return jsonify(ctx)

    else:
        member = Member.query.get(oauthmemberbind.member_id)
        token = MemberService.geneAuthCode(member)
        ctx['data']['token'] = '%s#%s' % (member.id, token)
        return jsonify(ctx)
Пример #2
0
def login():
    res = {'code': 1, 'msg': '成功', 'data': {}}
    nickName = request.form.get('nickName')
    avataUrl = request.form.get('avataUrl')
    gender = request.form.get('gender')
    code = request.form.get('code')
    print(nickName, avataUrl, gender, code)

    if len(code) < 1:
        res['code'] = -1
        res['msg'] = 'code有误'
        return jsonify(res)

    if not all([nickName, avataUrl, gender, code]):
        res['code'] = -1
        res['msg'] = '参数有误'
        return jsonify(res)

    # 获取open_id
    open_id = memberService.getOpenid(code)
    if not open_id:
        res['code'] = -1
        res['msg'] = '获取open_id出错'
        return jsonify(res)

    #存数据库去重
    oauthMemberBind = OauthMemberBind.query.filter_by(openid=open_id).first()
    if not oauthMemberBind:
        member = Member()
        member.nickname = nickName
        member.avatar = avataUrl
        member.gender = gender
        member.salt = memberService.getSalt()

        db.session.add(member)
        db.session.commit()

        Oauth_member_bind = OauthMemberBind()
        Oauth_member_bind.openid = open_id
        Oauth_member_bind.client_type = 'wx'
        Oauth_member_bind.type = 1
        Oauth_member_bind.member_id = member.id

        db.session.add(Oauth_member_bind)
        db.session.commit()

    member = Member.query.get(oauthMemberBind.member_id)
    #生成前端所需要的token
    token = "%s#%s" % (memberService.geneAuthCode(member), member.id)
    res['data']['token'] = token
    return jsonify(res)