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: 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 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)
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)