bindschoolinfo.cpp 7.2 KB

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