HepuLogin.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include <iostream>
  3. #include <rapidjson/document.h>
  4. #include <rapidjson/rapidjson.h>
  5. #include <rapidjson/stringbuffer.h>
  6. #include <rapidjson/writer.h>
  7. using namespace std;
  8. class HepuLogin
  9. {
  10. private:
  11. public:
  12. const string cmd = "userLogin";
  13. string token;
  14. string username;
  15. string password;
  16. HepuLogin(){};
  17. ~HepuLogin(){};
  18. void objectToJson(string& str){
  19. rapidjson::StringBuffer strBuf;
  20. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  21. this->objectToJson(writer);
  22. str = strBuf.GetString();
  23. }
  24. void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
  25. writer.StartObject();
  26. writer.Key("cmd");
  27. writer.String(cmd.c_str());
  28. writer.Key("param");
  29. writer.StartObject();
  30. writer.Key("token");
  31. writer.String(token.c_str());
  32. writer.Key("username");
  33. writer.String(username.c_str());
  34. writer.Key("password");
  35. writer.String(password.c_str());
  36. writer.EndObject();
  37. writer.EndObject();
  38. }
  39. bool jsonToObject(std::string& json){
  40. rapidjson::Document doc;
  41. doc.Parse(json.c_str());
  42. if(!doc.IsObject()){
  43. return false;
  44. }
  45. const auto end = doc.MemberEnd();
  46. if(end == doc.FindMember("cmd") || !doc["cmd"].IsString()){
  47. return false;
  48. }else{
  49. if(cmd != doc["cmd"].GetString())
  50. return false;
  51. }
  52. if(end == doc.FindMember("param") || !doc["param"].IsObject()) {
  53. return false;
  54. }else{
  55. if(end == doc.FindMember("token") || !doc["token"].IsString()) {
  56. return false;
  57. }else{
  58. token = doc["token"].GetString();
  59. }
  60. if(end == doc.FindMember("username") || !doc["username"].IsString()) {
  61. return false;
  62. }else{
  63. username = doc["username"].GetString();
  64. }
  65. if(end == doc.FindMember("password") || !doc["password"].IsString()) {
  66. return false;
  67. }else{
  68. password = doc["password"].GetString();
  69. }
  70. }
  71. return true;
  72. }
  73. };