邓淳远、崔鹏宇、翁思扬组云计算期末项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.3 KiB

  1. from flask import request, Flask, jsonify, Blueprint
  2. import json
  3. from APP.view.database import db_session
  4. from APP.view.model import Users
  5. bp_auth = Blueprint("bp_auth", __name__,url_prefix="/bp_auth")
  6. @bp_auth.route('/login', methods=['POST', 'GET'])
  7. def login():
  8. if request.method == 'POST':
  9. datas = json.loads(request.get_data())
  10. phonenum = datas['phonenum']
  11. password = datas['password']
  12. name = ''
  13. msg = ''
  14. userlist = db_session.query(Users)
  15. flag = 0
  16. for user in userlist:
  17. if user.phonenum == phonenum:
  18. if user.password == password:
  19. flag = 1
  20. name = user.name
  21. else:
  22. flag = 2
  23. break
  24. if flag == 1:
  25. return jsonify({'code': 1, 'name': name, 'phonenum': phonenum})
  26. else:
  27. if phonenum != '' and password != '':
  28. if flag == 0:
  29. msg = "手机号还未注册!"
  30. return jsonify({'code': 0, 'msg': msg})
  31. else:
  32. msg = "密码错误!"
  33. return jsonify({'code': 2, 'msg': msg})
  34. else:
  35. msg = "请输入完整登录信息!"
  36. return jsonify({'code': 3, 'msg': msg})
  37. else:
  38. msg = "提交失败,请重新登录!"
  39. return jsonify({'code': 4, 'msg': msg})
  40. @bp_auth.route('/register', methods=['POST', 'GET'])
  41. def register():
  42. if request.method == 'POST':
  43. datas = json.loads(request.get_data())
  44. phonenum = datas['phonenum']
  45. name = datas['name']
  46. password = datas['password']
  47. password1 = datas['password1']
  48. msg = ''
  49. userlist = db_session.query(Users)
  50. flag = 1
  51. for user in userlist:
  52. if user.phonenum == phonenum:
  53. flag = 0
  54. break
  55. if flag == 0:
  56. msg = "手机号已被注册!"
  57. return jsonify({'code': 0, 'msg': msg})
  58. else:
  59. if phonenum != '' and password != '':
  60. if password == password1:
  61. user = Users(phonenum=phonenum, name=name, password=password)
  62. db_session.add(user)
  63. db_session.commit()
  64. return jsonify({'code': 1, 'name': name, 'phonenum': phonenum})
  65. else:
  66. msg = "两次输入密码不一致!"
  67. return jsonify({'code': 2, 'msg': msg})
  68. else:
  69. msg = "请输入完整登录信息!"
  70. return jsonify({'code': 3, 'msg': msg})
  71. else:
  72. msg = "提交失败,请重新注册!"
  73. return jsonify({'code': 4, 'msg': msg})
  74. @bp_auth.route('/forget', methods=['POST', 'GET'])
  75. def forget():
  76. if request.method == 'POST':
  77. datas = json.loads(request.get_data())
  78. phonenum = datas['phonenum']
  79. name = datas['name']
  80. password = datas['password']
  81. password1 = datas['password1']
  82. msg = ''
  83. userlist = db_session.query(Users)
  84. flag = 0
  85. for user in userlist:
  86. if user.phonenum == phonenum:
  87. if user.name == name:
  88. user.password = password
  89. db_session.commit()
  90. flag = 1
  91. else:
  92. flag = 2
  93. break
  94. if flag == 0:
  95. msg = "手机号还未注册!"
  96. return jsonify({'code': 0, 'msg': msg})
  97. else:
  98. if phonenum != '' and password != '':
  99. if password == password1:
  100. if flag == 1:
  101. msg = "修改密码成功!"
  102. return jsonify({'code': 1, 'name': name, 'phonenum': phonenum})
  103. else:
  104. msg = "用户名输入错误!"
  105. return jsonify({'code': 2, 'msg': msg})
  106. else:
  107. msg = "两次输入密码不一致!"
  108. return jsonify({'code': 3, 'msg': msg})
  109. else:
  110. msg = "请输入完整修改密码信息!"
  111. return jsonify({'code': 4, 'msg': msg})
  112. else:
  113. msg = "提交失败,请重新修改密码!"
  114. return jsonify({'code': 5, 'msg': msg})