def login(): data = request.get_json() if 'phone' not in data or 'password' not in data: return "信息缺失" phone = data['phone'] password = data['password'] # 判断电话号码是否为空 if phone is None: return "The phone number is empty!" # 判断密码是否为空 if password is None: return "The password is empty!" user = User() user.set_phone(phone) user.set_password(password) try: user = UserDAO().retrieve(user) except: return "Server Failure!" # 用户不存在 if user is None: result = return_status(-1) return jsonify(result) # 授权 result = Auth.authorize(user) return jsonify(result)
def retrieve(self, user): retrieve_user = None if user.get_user_id() is None: sql = 'select * from user where phone = "%s" and password = "******"' % ( user.get_phone(), user.get_password()) else: sql = 'select * from user where user_id = %s' % ( user.get_user_id()) connection = pymysql.connect(host=self.__db_host, user=self.__db_admin, password=self.__db_password, database=self.__db, port=self.__port, charset=self.__charset) cursor = connection.cursor() try: cursor.execute(sql) result = cursor.fetchone() if result is not None: retrieve_user = User() retrieve_user.set_user_id(result[0]) retrieve_user.set_phone(result[1]) retrieve_user.set_password(result[2]) except: traceback.print_exc() finally: connection.close() cursor.close() return retrieve_user
def register(): data = request.get_json() if 'phone' not in data or 'password' not in data: return "信息缺失" phone = data['phone'] password = data['password'] # 判断电话号码是否为空 if phone is None: return "The phone number is empty!" # 判断密码是否为空 if password is None: return "The password is empty!" # 检测手机是否已经使用 phone_is_used = verify_phone(phone) if phone_is_used: result = return_status(-1) # 手机号码被使用 return jsonify(result) # 检测手机格式是否正确 phone_format_false = verify_phone_format(phone) if phone_format_false: result = return_status(-2) # 手机格式不正确 return jsonify(result) user = User() user.set_phone(phone) user.set_password(password) try: user_dao = UserDAO() user_dao.add(user) result = return_status(0) return jsonify(result) # 注册成功 except: return "Server failure!"