邓淳远、崔鹏宇、翁思扬组云计算期末项目
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.

131 lines
4.6 KiB

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