Commit d33aceba by dong

fix20220307

parent 895df745
class WxConfig(object):
_wx_conf = {
'AppID': 'wxb44559f2cbe1efcf',
'AppSecret': 'b81f33dfc82e40122dfe4ea6269dd46a'
'AppID': 'wx94b4c8efa79f741d',
'AppSecret': 'cf4e8aec799e4695945b186e75375ef4'
}
@classmethod
......
......@@ -369,6 +369,130 @@ def login_bypwd():
return jsonify(code=RET.OK, msg="登录成功", data={"token": token, "flag": user.flag})
# 微信授权
@api_user.route("/LoginByvx", methods=["POST"])
def login_byvx():
'''
用户验证码登录,用户存在,直接登陆,不存在就后台注册
:return:
'''
# 参数获取与校验
req_dict = request.get_json()
code = req_dict.get('code') # 微信登录code
# 校验参数完整性
if not all([code]):
return jsonify(code=RET.PARAMERR, msg="参数不完整")
app_id = WxConfig.get_wx_app_id()
app_secret = WxConfig.get_wx_app_secret()
url = u'https://api.weixin.qq.com/sns/oauth2/access_token'
params = {
'appid': app_id,
'secret': app_secret,
'code': code,
'grant_type': 'authorization_code'
}
res = requests.get(url, params=params).json()
user_info_url = u'https://api.weixin.qq.com/sns/userinfo'
params1 = {
'access_token': res.get("access_token"),
'openid': res.get("openid")
}
uinfo = requests.get(user_info_url, params=params1).json()
# nickname = uinfo.get('nickname').encode('iso8859-1').decode('utf-8')
try:
openid = uinfo["openid"]
unionid = uinfo["unionid"]
except:
return None
try:
user = User.query.filter_by(vxopenid=openid, vxunionid=unionid).first()
if user:
# 若成功保存登录状态
token = create_token(user.id)
return jsonify(code=RET.OK, msg="登录成功", token=token, flag=user.flag)
except Exception as e:
current_app.logger.error(e)
return jsonify(code=RET.DBERR, msg="appid,secreat异常")
time = datetime.now()
current_app.logger.error(
'++++++++++++++++++++++++++++登录日志>>>{}:{}通过使用微信登录成功了!+++++++++++++++++++++++=++'.format(time, user))
return jsonify(code=RET.USERERR, msg="授权成功", user_info=uinfo)
# vx登陆后绑定手机号
@api_user.route("/Binding", methods=["POST"])
def binding():
'''
绑定已有账号
:return:
'''
req_dict = request.get_json()
openid = req_dict.get("openid") # vxopenid
unionid = req_dict.get("unionid") # vxunionid
mobile = req_dict.get('mobile') # 手机号
sms_code = req_dict.get("sms_code") # 验证码
# 校验参数完整性
if not all([openid, unionid, mobile, sms_code]):
return jsonify(code=RET.PARAMERR, msg="参数不完整")
# 校验手机号格式
if not re.match(r"1[23456789]\d{9}$", mobile):
return jsonify(code=RET.PARAMERR, msg="手机号格式错误")
# 获取短信验证码
try:
real_sms_code = redis_store.get('sms_code_{}'.format(mobile)).decode()
except Exception as e:
current_app.logger.error(e)
return jsonify(code=RET.DBERR, msg="redis数据库异常")
# 获取用户
try:
user = User.query.filter_by(mobile=mobile).first()
except Exception as e:
current_app.logger.error(e)
return jsonify(code=RET.DBERR, msg="获取用户信息失败")
# 判断用户填写短信验证码是否一致
if real_sms_code != sms_code:
return jsonify(code=RET.DATAERR, msg="短信验证码错误")
# 删除redis中的短信验证码,防止重复校验
try:
redis_store.delete("sms_code_{}".format(mobile))
except Exception as e:
current_app.logger.error(e)
try:
if user:
user.vxopenid = openid
user.vxunionid = unionid
db.session.commit()
if user.status != 1:
jsonify(code=RET.OK, msg="绑定成功,当前用户被禁止登录,请联系管理员")
else:
user = User(name=mobile, mobile=mobile, vxopenid=openid, vxunionid=unionid, flag=1, status=1)
# user.password = password
db.session.add(user)
db.session.commit()
except Exception as e:
# 表示操作失败,回滚数据库操作
db.session.rollback()
current_app.logger.error(e)
return jsonify(code=RET.DBERR, msg="数据库异常")
# 若成功保存登录状态
token = create_token(user.id)
return jsonify(code=RET.OK, msg="绑定成功,登录成功", token=token, flag=user.flag)
# # 异步邮箱发送信息
# def send_async_email(mail, app, msg):
# with app.app_context():
......@@ -443,128 +567,9 @@ def login_bypwd():
# ''''''
#
#
# # 微信授权
# @api_user.route("/login_byvx", methods=["POST"])
# def login_byvx():
# '''
# 用户验证码登录,用户存在,直接登陆,不存在就后台注册
# :return:
# '''
# # 参数获取与校验
# req_dict = request.get_json()
# code = req_dict.get('code') # 微信登录code
#
# # 校验参数完整性
# if not all([code]):
# return jsonify(code=RET.PARAMERR, msg="参数不完整")
#
# app_id = WxConfig.get_wx_app_id()
# app_secret = WxConfig.get_wx_app_secret()
#
# url = u'https://api.weixin.qq.com/sns/oauth2/access_token'
# params = {
# 'appid': app_id,
# 'secret': app_secret,
# 'code': code,
# 'grant_type': 'authorization_code'
# }
# res = requests.get(url, params=params).json()
#
# user_info_url = u'https://api.weixin.qq.com/sns/userinfo'
# params1 = {
# 'access_token': res.get("access_token"),
# 'openid': res.get("openid")
# }
# uinfo = requests.get(user_info_url, params=params1).json()
# # nickname = uinfo.get('nickname').encode('iso8859-1').decode('utf-8')
# try:
# openid = uinfo["openid"]
# unionid = uinfo["unionid"]
# except:
# return None
#
# try:
# user = User.query.filter_by(vxopenid=openid, vxunionid=unionid).first()
# if user:
# # 若成功保存登录状态
# token = create_token(user.id)
# return jsonify(code=RET.OK, msg="登录成功", token=token, flag=user.flag)
# except Exception as e:
# current_app.logger.error(e)
# return jsonify(code=RET.DBERR, msg="appid,secreat异常")
#
# time = datetime.now()
# current_app.logger.error(
# '++++++++++++++++++++++++++++登录日志>>>{}:{}通过使用微信登录成功了!+++++++++++++++++++++++=++'.format(time, user))
#
# return jsonify(code=RET.USERERR, msg="授权成功", user_info=uinfo)
#
#
# # vx登陆后绑定手机号
# @api_user.route("/binding", methods=["POST"])
# def binding():
# '''
# 绑定已有账号
# :return:
# '''
# req_dict = request.get_json()
# openid = req_dict.get("openid") # vxopenid
# unionid = req_dict.get("unionid") # vxunionid
# mobile = req_dict.get('mobile') # 手机号
# sms_code = req_dict.get("sms_code") # 验证码
#
# # 校验参数完整性
# if not all([openid, unionid, mobile, sms_code]):
# return jsonify(code=RET.PARAMERR, msg="参数不完整")
#
# # 校验手机号格式
# if not re.match(r"1[23456789]\d{9}$", mobile):
# return jsonify(code=RET.PARAMERR, msg="手机号格式错误")
#
# # 获取短信验证码
# try:
# real_sms_code = redis_store.get('sms_code_{}'.format(mobile)).decode()
# except Exception as e:
# current_app.logger.error(e)
# return jsonify(code=RET.DBERR, msg="redis数据库异常")
#
# # 获取用户
# try:
# user = User.query.filter_by(mobile=mobile).first()
# except Exception as e:
# current_app.logger.error(e)
# return jsonify(code=RET.DBERR, msg="获取用户信息失败")
#
# # 判断用户填写短信验证码是否一致
# if real_sms_code != sms_code:
# return jsonify(code=RET.DATAERR, msg="短信验证码错误")
# # 删除redis中的短信验证码,防止重复校验
# try:
# redis_store.delete("sms_code_{}".format(mobile))
# except Exception as e:
# current_app.logger.error(e)
#
# try:
# if user:
# user.vxopenid = openid
# user.vxunionid = unionid
# db.session.commit()
# if user.status != 1:
# jsonify(code=RET.OK, msg="绑定成功,当前用户被禁止登录,请联系管理员")
# else:
# user = User(name=mobile, mobile=mobile, vxopenid=openid, vxunionid=unionid, flag=1, status=1)
# # user.password = password
# db.session.add(user)
# db.session.commit()
# except Exception as e:
# # 表示操作失败,回滚数据库操作
# db.session.rollback()
# current_app.logger.error(e)
# return jsonify(code=RET.DBERR, msg="数据库异常")
#
# # 若成功保存登录状态
# token = create_token(user.id)
# return jsonify(code=RET.OK, msg="绑定成功,登录成功", token=token, flag=user.flag)
#
#
# # 后台用户账号密码
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment