123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #include "bindschoolinfo.h"
- #include "../Util/Util.h"
- #include <QNetworkRequest>
- #include <QNetworkReply>
- #include <QJsonObject>
- #include <QJsonParseError>
- #include <QJsonArray>
- #include <QSettings>
- #include <QCompleter>
- extern wstring g_strConfigPath;
- CBindSchoolInfo::CBindSchoolInfo(QWidget *parent)
- : CBaseDialog(parent)
- {
- ui.setupUi(base_ui.widget_content);
- base_ui.widget_sysbutton->setMinVisible(false);
- base_ui.label_title->setText(QString::fromStdWString(L"绑定学校信息"));
- setFixedSize(700, 400);
- setWindowFlags(Qt::FramelessWindowHint);
- connect(ui.pushButton_modify, SIGNAL(clicked()), this, SLOT(slot_clickButton()));
- connect(ui.pushButton_ok, SIGNAL(clicked()), this, SLOT(slot_clickButton()));
- connect(ui.pushButton_canncel, SIGNAL(clicked()), this, SLOT(slot_clickButton()));
- connect(ui.comboBox_school, SIGNAL(currentIndexChanged(int)), this, SLOT(slot_comboxIndexChange(int)));
- connect(ui.comboBox_school, &QComboBox::editTextChanged, this, [=](QString strContext){
- QCompleter * pCompleter = ui.comboBox_school->completer();
- int nRow = pCompleter->completionCount();
- //无法从前50个学校中获取时,调用后端接口获取
- if (nRow == 0)
- {
- m_vctschool.clear();
- RequestSchoolInfo(strContext);
- for (auto& iter : m_vctschool)
- {
- QString strName(QString::number(iter.schoolId));
- strName.append("-");
- strName.append(iter.schoolName);
- strName.append("-");
- strName.append(iter.schoolArea);
- ui.comboBox_school->addItem(strName, iter.schoolId);
- }
- pCompleter->popup();
- }
- });
- RequestSchoolInfo();
- CheckBindSchool();
- }
- CBindSchoolInfo::~CBindSchoolInfo()
- {
- }
- void CBindSchoolInfo::RequestSchoolInfo(QString strSchoolName)
- {
- WCHAR server_url[MAX_PATH] = { 0 };
- GetPrivateProfileString(L"USER", L"server_url", L"", server_url, MAX_PATH, g_strConfigPath.c_str());
- QString url(QString::fromStdWString(server_url));
- if (strSchoolName.isEmpty())
- strSchoolName = "";
- QString strUrl = QString("/teacher/third/school?schoolName=%1").arg(strSchoolName);
- url.append(strUrl.toStdString().c_str());
- QNetworkRequest request;
- request.setUrl(url);
- request.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("application/json;charset=utf-8"));
- request.setRawHeader("Authorization", "Basic ZnhiLXNoOmVDbGo0TlRoSHlMY05USE0wZWl3ZWpVUExCcGFlZQ==");//Authorization身份验证
- QNetworkAccessManager manager;
- manager.setNetworkAccessible(QNetworkAccessManager::Accessible);
- QNetworkReply* reply = manager.get(request);
- QEventLoop eventLoop;
- QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
- eventLoop.exec();
- QByteArray bytes = reply->readAll(); // bytes
- QString response = QString::fromUtf8(bytes);
- QJsonParseError json_error;
- QJsonDocument jsonDoc = QJsonDocument::fromJson(bytes, &json_error);
- QString message;
- if (jsonDoc.isObject())
- {
- QJsonObject json_obj = jsonDoc.object();
- if (json_obj.contains("code") && json_obj.value("code").toInt() == 200)
- {
- if (json_obj.contains("data") && json_obj.value("data").isArray())
- {
- QJsonArray json_array = json_obj.value("data").toArray(); // 转数组
- for (int i = 0; i < json_array.count(); i++)
- {
- QJsonObject json_obj_school = json_array[i].toObject();
- school_info _info;
- _info.schoolId = json_obj_school.value("schoolId").toInt();
- _info.schoolName = json_obj_school.value("schoolName").toString();
- _info.schoolArea = json_obj_school.value("provinceName").toString();
- _info.schoolArea.append(json_obj_school.value("cityName").toString());
- _info.schoolArea.append(json_obj_school.value("countyName").toString());
- if (json_obj_school.contains("managerList") && json_obj_school.value("managerList").isArray())
- {
- QJsonArray json_array_manager = json_obj_school.value("managerList").toArray();
- for (int j = 0; j < json_array_manager.count(); j++)
- {
- QJsonObject json_obj_manager = json_array_manager[j].toObject();
- QString _managerInfo;
- _managerInfo.append(json_obj_manager.value("userName").toString());
- _managerInfo.append("-");
- _managerInfo.append(json_obj_manager.value("phone").toString());
- _info.manager.push_back(_managerInfo);
- }
- }
- m_vctschool.push_back(_info);
- }
- }
- }
- }
- }
- void CBindSchoolInfo::CheckBindSchool()
- {
- WCHAR school_id[MAX_PATH] = { 0 };
- GetPrivateProfileString(L"USER", L"school_id", L"", school_id, MAX_PATH, g_strConfigPath.c_str());
- if (wcslen(school_id) == 0)
- {
- ui.comboBox_school->hide();
- ui.pushButton_ok->hide();
- ui.pushButton_canncel->hide();
- ui.label_schoolname->hide();
- }
- else
- {
- ui.comboBox_school->hide();
- ui.label_unbind->hide();
- ui.pushButton_ok->hide();
- ui.pushButton_canncel->hide();
- WCHAR school_name[MAX_PATH] = { 0 };
- GetPrivateProfileString(L"USER", L"school_name", L"", school_name, MAX_PATH, g_strConfigPath.c_str());
- WCHAR school_area[MAX_PATH] = { 0 };
- GetPrivateProfileString(L"USER", L"school_area", L"", school_area, MAX_PATH, g_strConfigPath.c_str());
- ui.label_schoolname->setText(QString::fromStdWString(school_name));
- ui.label_school_id->setText(QString::fromStdWString(school_id));
- ui.label_schoolarea->setText(QString::fromStdWString(school_area));
- }
- }
- void CBindSchoolInfo::slot_comboxIndexChange(int index)
- {
- ui.label_schoolarea->setText("");
- QVariant qvt = ui.comboBox_school->itemData(index);
- int _school_id = qvt.toInt();
- for (auto& iter : m_vctschool)
- {
- if (_school_id == iter.schoolId)
- {
- ui.label_schoolname->setText(iter.schoolName);
- ui.label_school_id->setText(QString::number(iter.schoolId));
- ui.label_schoolarea->setText(iter.schoolArea);
- break;
- }
- }
- }
- void CBindSchoolInfo::slot_clickButton()
- {
- QObject* sender = this->sender();
- if (sender == ui.pushButton_modify)
- {
- ui.comboBox_school->show();
- ui.pushButton_ok->show();
- ui.pushButton_canncel->show();
- ui.label_schoolname->hide();
- ui.pushButton_modify->hide();
- ui.label_unbind->hide();
- ui.comboBox_school->clear();
- for (auto& iter : m_vctschool)
- {
- QString strName(QString::number(iter.schoolId));
- strName.append("-");
- strName.append(iter.schoolName);
- strName.append("-");
- strName.append(iter.schoolArea);
- ui.comboBox_school->addItem(strName, iter.schoolId);
- }
- //增加下拉框自动补全
- ui.comboBox_school->setEditable(true);
- QCompleter * pCompleter = new QCompleter(ui.comboBox_school->model());
- pCompleter->setFilterMode(Qt::MatchContains);
- ui.comboBox_school->setCompleter(pCompleter);
- }
- else if (sender == ui.pushButton_ok)
- {
- if (m_vctschool.size() == 0)
- {
- MessageBox(NULL, L"没有可绑定的学校", L"提示", MB_OK);
- return;
- }
- QSettings setting(QString::fromStdWString(g_strConfigPath), QSettings::IniFormat);
- QVariant qvt = ui.comboBox_school->currentData();
- int _school_id = qvt.toInt();
- for (auto& iter : m_vctschool)
- {
- if (_school_id == iter.schoolId)
- {
- WritePrivateProfileString(L"USER", L"school_id", std::to_wstring(iter.schoolId).c_str(), g_strConfigPath.c_str());
- WritePrivateProfileString(L"USER", L"school_name", iter.schoolName.toStdWString().c_str(), g_strConfigPath.c_str());
- WritePrivateProfileString(L"USER", L"school_area", iter.schoolArea.toStdWString().c_str(), g_strConfigPath.c_str());
- break;
- }
- }
- done(Accepted);
- }
- else if (sender == ui.pushButton_canncel)
- {
- close();
- }
- }
|