bindschoolinfo.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "bindschoolinfo.h"
  2. #include <QNetworkRequest>
  3. #include <QNetworkReply>
  4. #include <QJsonObject>
  5. #include <QJsonParseError>
  6. #include <QJsonArray>
  7. #include <QSettings>
  8. #include "../Util/Util.h"
  9. extern wstring g_strConfigPath;
  10. CBindSchoolInfo::CBindSchoolInfo(QWidget *parent)
  11. : CBaseDialog(parent)
  12. {
  13. ui.setupUi(base_ui.widget_content);
  14. base_ui.widget_sysbutton->setMinVisible(false);
  15. base_ui.label_title->setText(QString::fromStdWString(L"绑定学校信息"));
  16. setFixedSize(700, 400);
  17. setWindowFlags(Qt::FramelessWindowHint);
  18. connect(ui.pushButton_modify, SIGNAL(clicked()), this, SLOT(slot_clickButton()));
  19. connect(ui.pushButton_ok, SIGNAL(clicked()), this, SLOT(slot_clickButton()));
  20. connect(ui.pushButton_canncel, SIGNAL(clicked()), this, SLOT(slot_clickButton()));
  21. connect(ui.comboBox_school, SIGNAL(currentIndexChanged(int)), this, SLOT(slot_comboxIndexChange(int)));
  22. RequestSchoolInfo();
  23. CheckBindSchool();
  24. }
  25. CBindSchoolInfo::~CBindSchoolInfo()
  26. {
  27. }
  28. void CBindSchoolInfo::RequestSchoolInfo()
  29. {
  30. WCHAR server_url[MAX_PATH] = { 0 };
  31. GetPrivateProfileString(L"USER", L"server_url", L"", server_url, MAX_PATH, g_strConfigPath.c_str());
  32. QString url(QString::fromStdWString(server_url));
  33. url.append("/teacher/third/school?schoolName=""");
  34. QNetworkRequest request;
  35. request.setUrl(url);
  36. request.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("application/json;charset=utf-8"));
  37. request.setRawHeader("Authorization", "Basic ZnhiLXNoOmVDbGo0TlRoSHlMY05USE0wZWl3ZWpVUExCcGFlZQ==");//Authorization身份验证
  38. QNetworkAccessManager manager;
  39. manager.setNetworkAccessible(QNetworkAccessManager::Accessible);
  40. QNetworkReply* reply = manager.get(request);
  41. QEventLoop eventLoop;
  42. QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
  43. eventLoop.exec();
  44. QByteArray bytes = reply->readAll(); // bytes
  45. QString response = QString::fromUtf8(bytes);
  46. QJsonParseError json_error;
  47. QJsonDocument jsonDoc = QJsonDocument::fromJson(bytes, &json_error);
  48. QString message;
  49. if (jsonDoc.isObject())
  50. {
  51. QJsonObject json_obj = jsonDoc.object();
  52. if (json_obj.contains("code") && json_obj.value("code").toInt() == 200)
  53. {
  54. if (json_obj.contains("data") && json_obj.value("data").isArray())
  55. {
  56. QJsonArray json_array = json_obj.value("data").toArray(); // 转数组
  57. for (int i = 0; i < json_array.count(); i++)
  58. {
  59. QJsonObject json_obj_school = json_array[i].toObject();
  60. school_info _info;
  61. _info.schoolId = json_obj_school.value("schoolId").toInt();
  62. _info.schoolName = json_obj_school.value("schoolName").toString();
  63. _info.schoolArea = json_obj_school.value("provinceName").toString();
  64. _info.schoolArea.append(json_obj_school.value("cityName").toString());
  65. _info.schoolArea.append(json_obj_school.value("countyName").toString());
  66. if (json_obj_school.contains("managerList") && json_obj_school.value("managerList").isArray())
  67. {
  68. QJsonArray json_array_manager = json_obj_school.value("managerList").toArray();
  69. for (int j = 0; j < json_array_manager.count(); j++)
  70. {
  71. QJsonObject json_obj_manager = json_array_manager[j].toObject();
  72. QString _managerInfo;
  73. _managerInfo.append(json_obj_manager.value("userName").toString());
  74. _managerInfo.append("-");
  75. _managerInfo.append(json_obj_manager.value("phone").toString());
  76. _info.manager.push_back(_managerInfo);
  77. }
  78. }
  79. m_vctschool.push_back(_info);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. void CBindSchoolInfo::CheckBindSchool()
  86. {
  87. WCHAR school_id[MAX_PATH] = { 0 };
  88. GetPrivateProfileString(L"USER", L"school_id", L"", school_id, MAX_PATH, g_strConfigPath.c_str());
  89. if (wcslen(school_id) == 0)
  90. {
  91. ui.comboBox_school->hide();
  92. ui.pushButton_ok->hide();
  93. ui.pushButton_canncel->hide();
  94. ui.label_schoolname->hide();
  95. }
  96. else
  97. {
  98. ui.comboBox_school->hide();
  99. ui.label_unbind->hide();
  100. ui.pushButton_ok->hide();
  101. ui.pushButton_canncel->hide();
  102. WCHAR school_name[MAX_PATH] = { 0 };
  103. GetPrivateProfileString(L"USER", L"school_name", L"", school_name, MAX_PATH, g_strConfigPath.c_str());
  104. WCHAR school_area[MAX_PATH] = { 0 };
  105. GetPrivateProfileString(L"USER", L"school_area", L"", school_area, MAX_PATH, g_strConfigPath.c_str());
  106. ui.label_schoolname->setText(QString::fromStdWString(school_name));
  107. ui.label_school_id->setText(QString::fromStdWString(school_id));
  108. ui.label_schoolarea->setText(QString::fromStdWString(school_area));
  109. }
  110. }
  111. void CBindSchoolInfo::slot_comboxIndexChange(int index)
  112. {
  113. ui.label_schoolarea->setText("");
  114. QVariant qvt = ui.comboBox_school->itemData(index);
  115. int _school_id = qvt.toInt();
  116. for (auto& iter : m_vctschool)
  117. {
  118. if (_school_id == iter.schoolId)
  119. {
  120. ui.label_schoolname->setText(iter.schoolName);
  121. ui.label_school_id->setText(QString::number(iter.schoolId));
  122. ui.label_schoolarea->setText(iter.schoolArea);
  123. break;
  124. }
  125. }
  126. }
  127. void CBindSchoolInfo::slot_clickButton()
  128. {
  129. QObject* sender = this->sender();
  130. if (sender == ui.pushButton_modify)
  131. {
  132. ui.comboBox_school->show();
  133. ui.pushButton_ok->show();
  134. ui.pushButton_canncel->show();
  135. ui.label_schoolname->hide();
  136. ui.pushButton_modify->hide();
  137. ui.label_unbind->hide();
  138. ui.comboBox_school->clear();
  139. for (auto& iter : m_vctschool)
  140. {
  141. QString strName(QString::number(iter.schoolId));
  142. strName.append("-");
  143. strName.append(iter.schoolName);
  144. strName.append("-");
  145. strName.append(iter.schoolArea);
  146. ui.comboBox_school->addItem(strName, iter.schoolId);
  147. }
  148. }
  149. else if (sender == ui.pushButton_ok)
  150. {
  151. if (m_vctschool.size() == 0)
  152. {
  153. MessageBox(NULL, L"没有可绑定的学校", L"提示", MB_OK);
  154. return;
  155. }
  156. QSettings setting(QString::fromStdWString(g_strConfigPath), QSettings::IniFormat);
  157. QVariant qvt = ui.comboBox_school->currentData();
  158. int _school_id = qvt.toInt();
  159. for (auto& iter : m_vctschool)
  160. {
  161. if (_school_id == iter.schoolId)
  162. {
  163. WritePrivateProfileString(L"USER", L"school_id", std::to_wstring(iter.schoolId).c_str(), g_strConfigPath.c_str());
  164. WritePrivateProfileString(L"USER", L"school_name", iter.schoolName.toStdWString().c_str(), g_strConfigPath.c_str());
  165. WritePrivateProfileString(L"USER", L"school_area", iter.schoolArea.toStdWString().c_str(), g_strConfigPath.c_str());
  166. break;
  167. }
  168. }
  169. done(Accepted);
  170. }
  171. else if (sender == ui.pushButton_canncel)
  172. {
  173. close();
  174. }
  175. }