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 '' reg_ip = req['reg_ip'] if 'reg_ip' in req else request.remote_addr ''' 判断是否已经测试过,注册了直接返回一些信息 ''' 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.reg_ip = reg_ip model_member.updated_time = model_member.created_time = getCurrentDate( ) db.session.add(model_member) db.session.commit() # 放入user_info table里 model_user = UserInfo() model_user.id = model_member.id model_user.nickname = nickname model_user.sex = sex model_user.reg_ip = reg_ip db.session.add(model_user) 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, 'openid': openid} return jsonify(resp)
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: # 判断code是否合法 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)
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)
def login(): resp = {'code': 200, 'msg': '操作成功', 'data': {}} req = request.values 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 '' 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) # content = r.text # # app.logger.info(content) # {"session_key":"VZQjfF1ebiHhyTHgm4LfFg==","openid":"omXHE5AoiI3c8AlSF2e8IGv7tdq8"} # res = json.loads(content) # <class 'dict'> # openid = res['openid'] if 'openid' in res else '' # 封装了代码 openid = MemberService.getWeChatOppenid(code) if not openid: resp['code'] = -1 resp['msg'] = '授权失败1' return jsonify(resp) bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first() if bind_info: # 如果根据 openid 能查到信息 则 用户已经授权过 直接返回用户信息即可 member_info = Member.query.filter_by(id=bind_info.member_id).first() resp['code'] = 200 resp['msg'] = '已经存在,无需重新授权' resp['data'] = {'nickname': member_info.nickname} return jsonify(resp) # 走到这一步 说明 用户 没授权过 则 新生产一个 member_info = Member() member_info.nickname = nickname member_info.sex = sex member_info.avatar = avatar member_info.salt = MemberService.geneSalt() member_info.updated_time = member_info.created_time = geneTime() db.session.add(member_info) db.session.commit() model_bind = OauthMemberBind() model_bind.openid = openid model_bind.member_id = member_info.id model_bind.type = 1 model_bind.updated_time = model_bind.created_time = geneTime() model_bind.extra = '' db.session.add(model_bind) db.session.commit() # resp['data'] = {'nickname': model_bind.openid} token = "{0}#{1}".format(MemberService.geneAuthCode(member_info), member_info.id) resp['data'] = {"token": token} return jsonify(resp)
def login(): resp = {'code': 200, 'msg': '操作成功', 'data': {}} req = request.values # 得到前端的数据 # app.logger.info(req) code = req['code'] if 'code' in req else '' # 获取code! if not code or len(code) < 1: resp['code'] = -1 resp['msg'] = "需要code" return jsonify(resp) # 向微信发送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 0 avatar = req['avatarUrl'] if 'avatarUrl' in req else '' # 从绑定表中通过openID查找是否存在,即是否已经注册 bind_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first() if not bind_info: # 没有绑定 # 如果没有绑定,则需要注册到member和关联表中 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() # 则获取详细信息 # 这里是第一次登陆注册,发票据!!!! # app.logger.info(member_info.id) token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id) resp['data'] = {'token': token} return jsonify(resp)
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)
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)
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) # 根据拿到的code(临时登录凭证),加上小程序id,密匙,可以拿到用户的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 '' # print(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.reg_ip = request.remote_addr model_member.salt = MemberService.gene_salt() 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 token = "%s#%s" % ( MemberService.gene_auth_code(member_info), member_info.id) # token信息 resp['data'] = {'token': token} return jsonify(resp)
def login(): resp = {'code': 200, 'msg': '操作成功', 'data': {}} req = request.values code = req['code'] if 'code' in req else '' if not code: resp['code'] = -1 resp['msg'] = '需要code' return jsonify(resp) # 通过前端传回来的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 '' # 判断是否已经绑定了openid,注册了直接返回一些信息 oauth_info = OauthMemberBind.query.filter_by(openid=openid, type=1).first() if not oauth_info: # 会员信息 model_member = Member() model_member.nickname = nickname model_member.sex = sex model_member.avatar = avatar model_member.salt = MemberService.geneSalt() model_member.reg_ip = request.remote_addr model_member.updated_time = model_member.created_time = getCurrentDate( ) db.session.add(model_member) db.session.commit() # 授权信息 model_oauth = OauthMemberBind() model_oauth.member_id = model_member.id model_oauth.type = 1 model_oauth.extra = '' model_oauth.openid = openid model_oauth.updated_time = model_oauth.created_time = getCurrentDate() db.session.add(model_oauth) db.session.commit() # oauth_info = model_oauth member_info = Member.query.filter_by(id=oauth_info.member_id).first() token = "%s#%s" % (MemberService.geneAuthCode(member_info), member_info.id) resp['data'] = {'token': token} return jsonify(resp)
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)
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'] = 'weixin api is wrong' 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.geneSalet() model_member.created_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.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)
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)
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)
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)
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)
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 not 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 '' # 是否已经注册过 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.setSalt() 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.setAuthcode(member_info), member_info.id) resp['data'] = {'token': token} return jsonify(resp)
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)
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)
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)
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错误
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)
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)
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)