Beispiel #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)
    # app.logger.info(req)
    # 使用code 向微信官方获取openid
    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 ''
    '''
       判断是否已经测试过,注册了直接返回一些信息
    '''
    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)
        try:
            db.session.commit()
        except Exception as e:
            print(e)
            resp['code'] = -1
            resp['msg'] = "提交数据库出错"
            db.session.rollback()

        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)
Beispiel #2
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)
  
  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 ''

  # 判断是否已经绑定过,如果已经绑定则直接返回一些信息
  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.reg_ip = ''
    model_member.salt = MemberService.geneSalt()
    model_member.updated_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.client_type = 'weixin'
    model_bind.openid = openid
    model_bind.updated_time = model_bind.updated_time = getCurrentDate()
    model_bind.extra = ''
    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)
Beispiel #3
0
def login():
    if request.method == 'GET':
        pass
    else:
        resp = {'code':200, 'msg':'操作成功', 'data':{}}
        req = request.json
        app.logger.info(req)
        code = req['code'] if 'code' in req else ''
        # 013tK8KN1cKzh61YDBKN1vS2KN1tK8Kg
        if not code or len(code) < 1:
            resp['code'] = -1
            resp['msg'] = '需要code'
            return jsonify(resp)

        openid = MemberService.getWeChatOpenId(code)
        # oesTl5ZSMGOOY6lvlZijsxmPtowU
        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 ''
        '''
            判断是否已经注册过,注册了直接返回一些信息
        '''
        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)
Beispiel #4
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 用户唯一标识
    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['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 = 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()
    #进行md5加密
    token = "%s#%s" %(MemberService.geneAuthCode(member_info),member_info.id)
    resp['data'] = {"token":token,"nickname":nickname}

    return jsonify(resp)
Beispiel #5
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)

    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['avatarUrl'] if 'avatarUrl' in req else ''
    """
    判断是否已经注册过,注册了直接返回一些信息 
    """
    bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first()
    app.logger.error(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.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)
Beispiel #6
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'] = "請重新登錄"
        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['avatarUrl'] if 'avatarUrl' in req else ''
    '''
    判斷是否已經註冊,if so 直接返回登錄頁面
    '''

    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)
Beispiel #7
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)

    openid = MemberService.getWeChatOpenId(code)
    if openid is None:
        resp['code'] = -1
        resp['msg'] = "调用接口出错"
        return jsonify(resp)

    nickname = req['nickName'] if 'nickName' in req else ''
    gender = req['gender'] if 'gender' in req else 0
    avatar = 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.gender = gender
        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_member.updated_time = model_member.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)
Beispiel #8
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)

    url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code"\
        .format(app.config['MINA_APP']['appid'],app.config['MINA_APP']['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 resp else 0
    avatar = req['avatarUrl'] if 'avatarUrl' in req else ''

    bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first()
    if bind_info:
        model_member = Member()
        model_member.nickname = nickname
        model_member.sex = sex
        model_member.avatar = avatar
        model_member.salt = ''
        model_member.updated_time = model_member.created_time = datetime.datetime.now(
        )
        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 = datetime.datetime.now(
        )
        db.session.add(model_bind)
        db.session.commit()

        bind_info = model_bind

        member_info = Member.query.filter_by(id=bind_info.member_id).first()
        resp['data'] = {'nickname': member_info.nickname}
        return jsonify(resp)
Beispiel #9
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)

    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["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 = sex
        model_member.avatar = avatar
        model_member.salt = MemberService.geneSalt()
        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.openid = openid
        model_bind.extra = ""
        model_bind.created_time = 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)
Beispiel #10
0
def login():
    ret = {'code': 200, 'msg': '操作成功~~', 'data': {}}
    req = request.values
    code = req['code'] if 'code' in req else ''
    if not code or len(code) < 1:
        ret['code'] = -1
        ret['msg'] = '非法传值'
        return jsonify(ret)

    openid = MemberService.getWeChatOpenId(code)
    if openid is None:
        ret['code'] = -1
        ret['msg'] = '调用微信出错'
        return jsonify(ret)

    # 判断是否已经绑定
    bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first()
    if not bind_info:
        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_obj = Member()
        member_obj.nickname = nickname
        member_obj.sex = sex
        member_obj.avatar = avatar
        member_obj.salt = MemberService.geneSalt()
        member_obj.created_time = member_obj.updated_time = get_current_time()
        db.session.add(member_obj)
        db.session.commit()

        bind_obj = OauthMemberBind()
        bind_obj.member_id = member_obj.id
        bind_obj.type = 1
        bind_obj.openid = openid
        bind_obj.extra = ''
        bind_obj.created_time = bind_obj.updated_time = get_current_time()
        db.session.add(bind_obj)
        db.session.commit()

        bind_info = bind_obj

    member_info = Member.query.filter_by(id=bind_info.member_id).first()
    token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id)
    ret['data'] = {'token': token}
    return jsonify(ret)
Beispiel #11
0
def login():
    res = {'code': 200, 'msg': '登录成功', 'data': {}}
    req_data = request.values
    code = req_data['code'] if 'code' in req_data else ''
    if not code or len(code) < 1:
        res['code'] = -1
        res['msg'] = '需要code'
        return jsonify(res)
    openid = MemberService.get_wechat_openid(code)
    nickname = req_data['nickName'] if 'nickName' in req_data else ''
    sex = req_data['gender'] if 'gender' in req_data else 0
    avatar = req_data['avatarUrl'] if 'avatarUrl' in req_data else ''

    # 根据openid查询用户是否已经绑定注册过
    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.updated_time = model_member.created_time = get_current_time()
        model_member.salt = MemberService.get_salt()
        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 = get_current_time()
        db.session.add(model_bind)
        db.session.commit()

        bind_info = model_bind
    # 登录成功后设置一个token返回
    member_info = Member.query.filter_by(id=bind_info.member_id).first()
    res['data']['token'] = f'{MemberService.get_member_code(member_info)}#{member_info.id}'
    return jsonify(res)
Beispiel #12
0
def login():
    resp = {'code': 200, 'msg': '操作成功', 'data': {}}
    req = request.values

    # 获取req中的code进而获取openid值
    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
    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['avatarUrl'] if 'avatarUrl' in req else ''

    # 打印日志调试
    app.logger.info(nickname)
    app.logger.info(sex)
    app.logger.info(avatar)
    '''
    判断用户是否已经注册,如果注册了直接返回一些信息
    '''
    bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first()
    if not bind_info:
        # 如果oauth表查不到用户信息,需要进行信息注册存进数据库
        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
        bind_info = model_bind

    # 查询用户信息,直接返回
    member_info = Member.query.filter_by(id=bind_info.member_id).first()

    # 操作完成返回给用户token数据
    token = '%s#%s' % (MemberService.geneAuthCode(member_info), member_info.id)
    resp['data'] = {'token': token}
    return jsonify(resp)
Beispiel #13
0
def login():
    resp = {'code': 200, 'msg': '操作成功~', 'data': {}}
    req = request.values
    code = req['code'] if 'code' in req else ''
    platform_id = int(req['platform_id']
                      ) if 'platform_id' in req and req['platform_id'] else 0
    community_id = int(
        req['community_id']
    ) if 'community_id' in req and req['community_id'] else 0
    community_name = req['community_name'] if 'community_name' in req else ''
    stamp = req['stamp'] if 'stamp' in req else None

    if not code or len(code) < 1:
        resp['code'] = -1
        resp['msg'] = "需要code"
        return jsonify(resp)
    print(code)
    openid = MemberService.getWeChatOpenId(code, stamp=stamp)
    print(openid)
    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['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.platform_id = platform_id
        model_member.community_id = community_id
        model_member.community_name = community_name
        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,
        'id': member_info.id,
        'platform_id': member_info.platform_id
    }
    return jsonify(resp)
Beispiel #14
0
def login():
    resp = {'code': 200, 'msg': 'success', 'data': {}}  #建立一个空临时数据库
    req = request.values  #获取又小程序发来的数据
    code = req['code'] if 'code' in req else ''  #把code从数据中摘出
    if not code or len(code) < 1:  #判断一下code的有效性
        resp['code'] = -1
        resp['msg'] = 'need code'
        return jsonify(resp)
    nickname = req[
        'nickName'] if 'nickName' in req else ''  #提取出来用户的数据用于第三步的database 数据录入
    sex = req['gender'] if 'gender' in req else 0
    avatar = req['avatarUrl'] if 'avatarUrl' in req else ''
    #app.logger.info(nickname, sex, avatar)

    # 2 发送上面的code和小程序自己的ID和key到腾讯服务器去获得OpenID
    #url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code"\
    #    .format(app.config['MINA_APP']['appid'],app.config['MINA_APP']['appkey'],code)  #向腾讯服务器发送appid 和 appkey并获取他提供的OpenID
    #r = requests.get(url)         #获取腾讯服务器的data
    #res = json.loads(r.text)
    #openid= res['openid']         #将openID拿出来
    #app.logger.info(openid)
    #上面方法由于重复利用已经被搬到 common.libs.member.MemberService中了
    openid = MemberService.getWeChatOpenId(code)
    if openid is None:
        resp['code'] = -1
        resp['msg'] = 'no openid'
        return jsonify(resp)

    #3 获取了openid ,用于本地数据库的注册,提前在common/models/member中建立了数据库
    bind_info = OauthMemberBind.query.filter_by(openid=openid,
                                                type=1).first()  #查找用户是否已经注册
    #app.logger.info(bind_info)
    # 如果有注册
    if bind_info == None:
        # 如果没有注册
        model_member = Member()
        model_member.nickname = nickname
        model_member.sex = sex
        model_member.avatar = avatar
        model_member.salt = MemberService.geneSalt()  #在lib里导入的方法
        model_member.update_time = model_member.create_time = getCurrentDate()
        db.session.add(model_member)
        db.session.commit()

        model_bind = OauthMemberBind()
        model_bind.member_id = model_member.id  #两个表share same memberId
        model_bind.type = 1
        model_bind.openid = openid
        model_bind.extra = ''
        model_bind.update_time = model_bind.create_time = getCurrentDate()
        db.session.add(model_bind)
        db.session.commit()
        resp['data'] = {'nikename': nickname}

        bind_info = model_bind  #防止出现NoneType没有.member_id的属性的问题,因为两个表都有member_id,而且都是一样的

    #4 反馈前方的token值
    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)
Beispiel #15
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)

    # # 下面通过code 获取用户的一些基本信息,以及+ appid  获得用户唯一的openid标致!
    # # 方便进行  用户是否已经注册的!  数据库比照的判定!所以会员 一定要有自己的数据库!
    # # 发送 get请求
    # url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code".format( app.config['MINA_APP']['appid'], app.config['MINA_APP']['appkey'], code)
    # # 安装request扩展
    # r = requests.get( url )
    # res = json.loads( r.text )
    # app.logger.info( "The result RES is:{0} and The result R.Text is:{1} ".format( res, r.text ))
    # openid = res['openid']                  # 获取用户唯一表示

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

    # 区别前端请求 和  后端向微信服务器的 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 ''
    '''
        判断用户是否已经  注册过
    '''
    bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first()
    if bind_info:  # 已经注册过
        member_info = Member.query.filter_by(id=bind_info.member_id).first()
        # resp['code'] = -1
        resp['msg'] = "抱歉!您已绑定"
        #resp['data'] = { 'nickname': member_info.nickname }
        # 产生 token 表明已经绑定了!
        token = "%s#%s" % (MemberService.geneAuthCode(member_info),
                           member_info.id)
        resp['data'] = {'token': token}
        return jsonify(resp)

    # 数据库中没有: 则进行注册  并写入数据库
    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.updated_time = model_member.created_time = getCurrentDate()
    db.session.add(model_member)
    db.session.commit()

    # app.logger.info( res + "\n\ntest" + "\b\n分行 进行式!" )

    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()
    #resp['data'] = { 'nickname': nickname }                  # 注意此处重复了!————所以可以进行代码的优化操作!
    # 代码优化———— 当用户注册之后  不需要再首先进入 带登录界面!
    # 产生 token 表明已经绑定了!
    token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id)
    resp['data'] = {'token': token}
    return jsonify(resp)  # 必须有 json的返回数据!因为 有request。否则会报 500/300错误
Beispiel #16
0
def login():
    """
    @api {post} /member/wx_login 小程序用户登录
    @apiVersion 1.0.0
    @apiGroup wxUser
    @apiName login
    @apiParam {String}  code      (必须)    微信返回的code
    @apiParam {String}  encryptedData      (必须)    微信返回的加密数据
    @apiParam {String}  iv      (必须)    微信返回的初始向量
    @apiParamExample {json} Request-Example:
    {
        code: "13970512239",
        encryptedData: "sdfasdfsdfewerwq23123",
        iv: "21asdasdaqwe"
    }
    @apiSuccess (回参) {String} token  用户token
    @apiSuccessExample {json} Success-Response:
    {
        "code":200,
        "msg":"操作成功",
        "data": {
            "token": "13813888888#1",
        }
    }
    @apiErrorExample {json} Error-Response:
    {
        "code":-1,
        "msg":"登录失败"
    }
    """
    resp = {'code': 200, 'msg': '操作成功~', 'data': {}}
    req = request.values
    code = req['code'] if 'code' in req else ''
    encryptedData = req['encryptedData'] if 'encryptedData' in req else ''
    iv = req['iv'] if 'iv' in req else ''
    if not code or len(code) < 1:
        resp['code'] = -1
        resp['msg'] = "需要code"
        return jsonify(resp)

    openid, session_key, unionid = getWeChatInfoByCode(code)
    if openid is None:
        resp['code'] = -1
        resp['msg'] = "调用微信出错"
        return jsonify(resp)

    try:
        wxapp = WXBizDataCrypt(app.config['MINA_APP']['appid'], session_key)
        user_info = wxapp.decrypt(encryptedData, iv)
        print(user_info)
        nickname = user_info['nickName'] if 'nickName' in user_info else ''
        sex = user_info['gender'] if 'gender' in user_info else 0
        avatar = user_info['avatarUrl'] if 'avatarUrl' in user_info else ''
        province = user_info['province'] if 'province' in user_info else ''
        city = user_info['city'] if 'city' in user_info else ''
    except Exception:
        resp['code'] = -1
        resp['msg'] = '获取用户信息失败,请重新登录'
        return jsonify(resp)

    #判断是否已经测试过,注册了直接返回一些信息
    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.province = province
        model_member.city = city
        model_member.salt = gen_salt()
        model_member.reg_ip = request.remote_addr
        model_member.updated_time = model_member.created_time = ()
        db.session.add(model_member)
        db.session.commit()

        model_bind = OauthMemberBind()
        model_bind.member_id = model_member.id
        model_bind.client_type = 'weixin'
        model_bind.type = 1  #weixin
        model_bind.openid = openid
        model_bind.unionid = unionid
        model_bind.session_key = session_key
        model_bind.extra = json.dumps(user_info)
        model_bind.updated_time = model_bind.created_time = get_str_now()
        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" % (gen_auth_code(member_info), member_info.id)
    resp['data'] = {'token': token}
    return jsonify(resp)