| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813 |
- #include "tcontroller.h"
- #include "qglobal.h"
- #include "qvariant.h"
- #include "teachertr.h"
- #include "tmodel.h"
- #include <QDir>
- #include <QJsonArray>
- #include <QJsonObject>
- #include <QMimeDatabase>
- #include "appevent.h"
- #include "qjsonwebtoken.h"
- #include <cwf/filemanager.h>
- #include <cwf/sqlquery.h>
- static CWF::SqlDatabaseStorage storage("QSQLITE", "localhost", "data.db", "", "");
- static const QLatin1String tokenSecret("mydirtysecret");
- static QJsonObject getJwtToken(qint64 userid)
- {
- QJsonWebToken token;
- const QDateTime time = QDateTime::currentDateTime();
- int expDate = 7;
- //当前时间
- token.setSecret(tokenSecret);
- token.appendClaim("iat", QString::number(time.toSecsSinceEpoch()));
- // 过期时间
- token.appendClaim("exp", QString::number(time.addDays(expDate).toSecsSinceEpoch()));
- token.appendClaim("userId", QString::number(userid));
- return QJsonObject{{"access_token", token.getToken()},
- {"access_expire", time.addDays(expDate).toSecsSinceEpoch()},
- {"refresh_after", time.addDays(expDate / 2).toSecsSinceEpoch()}};
- }
- static QStringList listFiles(const QString &path, const QString &basePath)
- {
- QDir dir(path);
- QStringList fileList;
- // 获取所有文件
- QFileInfoList files = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
- foreach (QFileInfo file, files) {
- // 计算相对路径并添加到 fileList 中
- QString relativePath = dir.relativeFilePath(file.absoluteFilePath());
- fileList.append(relativePath);
- }
- // 递归获取子目录中的文件
- QFileInfoList dirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
- foreach (QFileInfo subDir, dirs) {
- // 递归获取子目录的文件列表
- QStringList subDirFiles = listFiles(subDir.absoluteFilePath(), basePath);
- // 计算子目录相对于 basePath 的相对路径
- QString relativeSubDirPath = dir.relativeFilePath(subDir.absoluteFilePath());
- // 将子目录中的文件相对路径添加到 fileList 中
- foreach (QString subFile, subDirFiles) {
- fileList.append(relativeSubDirPath + "/" + subFile);
- }
- }
- return fileList;
- }
- void ConfigController::doGet(CWF::Request &request, CWF::Response &response) const
- {
- // IP 地址是自己配置
- static QStringList filter = {"serverIP", "serverPort", "webSocketIP", "webSocketPort"};
- QJsonObject responseConfig;
- const QJsonObject object = AppEvent::instance()->config();
- for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
- const QString key = it.key();
- if (!filter.contains(key)) {
- responseConfig.insert(key, it.value());
- }
- }
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("ok")},
- {"code", 200},
- {"data", responseConfig}});
- }
- void LoginController::doPost(CWF::Request &request, CWF::Response &response) const
- {
- // 获取服务器参数信息
- // qDebug() << request.getSession().getId();
- // qDebug() << request.getRequestURI() << request.getBody();
- QJsonParseError error;
- QJsonDocument jdc = QJsonDocument::fromJson(request.getBody(), &error);
- QJsonObject jsonObject = jdc.object();
- QJsonObject::const_iterator it = jsonObject.constBegin();
- QJsonObject::const_iterator end = jsonObject.constEnd();
- QMap<QByteArray, QByteArray> par;
- while (it != end) {
- par.insert(it.key().toUtf8(), it.value().toString().toUtf8());
- it++;
- }
- UserModel user(storage);
- request.fillQObject(&user, par, false);
- if (user.getName() == "123" && (user.getSwID() == "123" || user.getExamineeNumber() == "123")) {
- QJsonObject UserResponse{{"id", 0}, {"name", "123"}, {"JwtToken", getJwtToken(0)}};
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("Login succeeded")},
- {"code", 200},
- {"data", UserResponse}});
- return;
- }
- CWF::SqlQueryManager queryManager(storage);
- queryManager.select("*", user.getTableName()).where("name=?");
- queryManager.prepare();
- queryManager.addBindValue(user.getName());
- queryManager.exec();
- QJsonArray jsonArray = queryManager.toJson();
- if (jsonArray.isEmpty()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("user does not exist")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- // QString name;
- // QString phone;
- // QString swID;
- // QString state;
- // QString examineeNumber;
- for (const QJsonValue &json : jsonArray) {
- if (json.isObject()) {
- QJsonObject obj = json.toObject();
- bool isok = false;
- if (obj.contains("swID")) {
- const QString swID = user.getSwID();
- if (obj["swID"] == user.getSwID() && !swID.isEmpty()) {
- isok = true;
- }
- }
- if (obj.contains("examineeNumber")) {
- const QString examineeNumber = user.getExamineeNumber();
- if (obj["examineeNumber"] == examineeNumber && !examineeNumber.isEmpty()) {
- isok = true;
- }
- }
- if (isok) {
- QJsonObject UserResponse{{"id", obj["id"]},
- {"name", obj["name"]},
- {"JwtToken", getJwtToken(obj["id"].toInt())}};
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("Login succeeded")},
- {"code", 200},
- {"data", UserResponse}});
- // 修改登录状态
- obj["state"] = 1;
- // 更新在线状态
- user.buildFromJson(obj);
- user.save();
- AppEvent::instance()->loginUser(obj["id"].toInt());
- return;
- }
- }
- }
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Login failed")},
- {"code", 500},
- {"data", QJsonValue()}});
- }
- void UserInfoController::doGet(CWF::Request &request, CWF::Response &response) const
- {
- const QString authorization = request.getHttpParser().getHeaderField("Authorization");
- if (authorization.isEmpty()) {
- response.write(
- QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("LoginOut failed, no find user")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(authorization.mid(7), tokenSecret);
- if (!token.isValid()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("unauthorized")},
- {"code", response.SC_UNAUTHORIZED},
- {"data", QJsonValue()}});
- return;
- }
- QString userid = token.claim("userId");
- bool isok = false;
- qint64 id = userid.toLongLong(&isok, 10);
- if (id == 0) {
- QJsonObject userInfo;
- userInfo.insert("username", "123");
- userInfo.insert("maxTime", 0);
- userInfo.insert("checkinNumber", "--");
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("LoginOut succeeded")},
- {"code", 200},
- {"data", userInfo}});
- return;
- }
- UserModel user(storage);
- CWF::SqlQueryManager queryManager(storage);
- queryManager.select("*", user.getTableName()).where("id=?");
- queryManager.prepare();
- queryManager.addBindValue(id);
- queryManager.exec();
- QJsonArray jsonArray = queryManager.toJson();
- if (jsonArray.isEmpty()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("user does not exist")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- for (const QJsonValue &json : jsonArray) {
- if (json.isObject()) {
- QJsonObject obj = json.toObject();
- QJsonObject userInfo;
- userInfo.insert("username", obj.value("name"));
- userInfo.insert("maxTime", obj.value("maxTime"));
- userInfo.insert("checkinNumber", obj.value("checkinNumber")); // 1 - name
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("LoginOut succeeded")},
- {"code", 200},
- {"data", userInfo}});
- return;
- }
- }
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Login failed")},
- {"code", 500},
- {"data", QJsonValue()}});
- }
- void LoginOutController::doPost(CWF::Request &request, CWF::Response &response) const
- {
- const QString authorization = request.getHttpParser().getHeaderField("Authorization");
- if (authorization.isEmpty()) {
- response.write(
- QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("LoginOut failed, no find user")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(authorization.mid(7), tokenSecret);
- if (!token.isValid()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("unauthorized")},
- {"code", response.SC_UNAUTHORIZED},
- {"data", QJsonValue()}});
- return;
- }
- QString userid = token.claim("userId");
- bool isok = false;
- qint64 id = userid.toLongLong(&isok, 10);
- if (id == 0) {
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("LoginOut succeeded")},
- {"code", 200},
- {"data", QJsonValue()}});
- return;
- }
- UserModel user(storage);
- CWF::SqlQueryManager queryManager(storage);
- queryManager.select("*", user.getTableName()).where("id=?");
- queryManager.prepare();
- queryManager.addBindValue(id);
- queryManager.exec();
- QJsonArray jsonArray = queryManager.toJson();
- if (jsonArray.isEmpty()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("user does not exist")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- for (const QJsonValue &json : jsonArray) {
- if (json.isObject()) {
- QJsonObject obj = json.toObject();
- // 修改登录状态
- obj["state"] = 0;
- // 更新在线状态
- user.buildFromJson(obj);
- user.save();
- AppEvent::instance()->loginOutUser(obj["id"].toInt());
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("LoginOut succeeded")},
- {"code", 200},
- {"data", QJsonValue()}});
- return;
- }
- }
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Login failed")},
- {"code", 500},
- {"data", QJsonValue()}});
- }
- void ExamsController::doPost(CWF::Request &request, CWF::Response &response) const
- {
- const QString authorization = request.getHttpParser().getHeaderField("Authorization");
- QString examContent;
- if (!authorization.isEmpty()) {
- // response.write(
- // QJsonObject{{"status", false},
- // {"message", TeacherServer::Tr::tr("exam content failed, no find user")},
- // {"code", 500},
- // {"data", QJsonValue()}});
- // return;
- QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(authorization.mid(7), tokenSecret);
- if (!token.isValid()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("unauthorized")},
- {"code", response.SC_UNAUTHORIZED},
- {"data", QJsonValue()}});
- return;
- }
- QString userid = token.claim("userId");
- bool isok = false;
- qint64 id = userid.toLongLong(&isok, 10);
- ///////////// 获取个人用户的考题
- CWF::SqlQuery query(storage);
- QJsonObject status = query.exec(QString("SELECT * FROM user WHERE id = %1").arg(id));
- if (status["success"].toBool()) {
- const QJsonArray array = query.toJson();
- if (!array.isEmpty()) {
- QJsonObject object = array[0].toObject();
- examContent = object["examTest"].toString();
- }
- }
- }
- UserModel user(storage);
- ExamsQuestionModel exams(storage);
- // 个人考试内容是空 获取默认选择的考试内容
- if (examContent.isEmpty()) {
- CWF::SqlQuery query(storage);
- QJsonObject status = query.exec(QString("SELECT * FROM exams_question WHERE isSelect = 1"));
- if (status["success"].toBool()) {
- const QJsonArray array = query.toJson();
- if (!array.isEmpty()) {
- QJsonObject object = array[0].toObject();
- examContent = object["name"].toString();
- }
- }
- }
- // 获取考试内容
- CWF::SqlQueryManager queryManager(storage);
- queryManager.select("*", exams.getTableName()).where("name = ?");
- queryManager.prepare();
- queryManager.addBindValue(examContent);
- queryManager.exec();
- QJsonArray jsonArray = queryManager.toJson();
- if (jsonArray.isEmpty()) {
- response.write(
- QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Failed to obtain exam content")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- for (const QJsonValue &json : jsonArray) {
- if (json.isObject()) {
- QJsonObject obj = json.toObject();
- const QString dir = obj["fileDir"].toString();
- const QStringList files = listFiles(dir, dir);
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("ok")},
- {"code", 200},
- {"data", QJsonArray::fromStringList(files)}});
- }
- }
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Failed to obtain exam content")},
- {"code", 500},
- {"data", QJsonValue()}});
- }
- void ExamsFilesController::doGet(CWF::Request &request, CWF::Response &response) const
- {
- const QString authorization = request.getHttpParser().getHeaderField("Authorization");
- QString examContent;
- if (!authorization.isEmpty()) {
- QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(authorization.mid(7), tokenSecret);
- if (!token.isValid()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("unauthorized")},
- {"code", response.SC_UNAUTHORIZED},
- {"data", QJsonValue()}});
- return;
- }
- QString userid = token.claim("userId");
- bool isok = false;
- qint64 id = userid.toLongLong(&isok, 10);
- ///////////// 获取个人用户的考题
- CWF::SqlQuery query(storage);
- QJsonObject status = query.exec(QString("SELECT * FROM user WHERE id = %1").arg(id));
- if (status["success"].toBool()) {
- const QJsonArray array = query.toJson();
- if (!array.isEmpty()) {
- QJsonObject object = array[0].toObject();
- examContent = object["examTest"].toString();
- }
- }
- }
- UserModel user(storage);
- ExamsQuestionModel exams(storage);
- // 个人考试内容是空 获取默认选择的考试内容
- if (examContent.isEmpty()) {
- CWF::SqlQuery query(storage);
- QJsonObject status = query.exec(QString("SELECT * FROM exams_question WHERE isSelect = 1"));
- if (status["success"].toBool()) {
- const QJsonArray array = query.toJson();
- if (!array.isEmpty()) {
- QJsonObject object = array[0].toObject();
- examContent = object["name"].toString();
- }
- }
- }
- QString fileName = request.getParameter("file");
- // 获取考试内容
- CWF::SqlQueryManager queryManager(storage);
- queryManager.select("*", exams.getTableName()).where("name = ?");
- queryManager.prepare();
- queryManager.addBindValue(examContent);
- queryManager.exec();
- QJsonArray jsonArray = queryManager.toJson();
- if (jsonArray.isEmpty()) {
- response.write(
- QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Failed to obtain exam content")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- QString dir;
- for (const QJsonValue &json : jsonArray) {
- if (json.isObject()) {
- QJsonObject obj = json.toObject();
- dir = obj["fileDir"].toString();
- break;
- }
- }
- // 获取扩展类型
- auto getMimeType = [](const QString &fileName) {
- QMimeDatabase mimeDatabase;
- QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileName);
- return mimeType.name();
- };
- const QString path = dir + "/" + fileName;
- QFile file(path);
- if (file.open(QIODevice::ReadOnly)) {
- QString extention = CWF::FileManager::fileExtention(fileName).toLower();
- response.addHeader(CWF::HTTP::CONTENT_DISPOSITION,
- "filename=" + QUrl::toPercentEncoding(fileName));
- response.addHeader(CWF::HTTP::CONTENT_TYPE, getMimeType(fileName).toUtf8());
- response.write(file.readAll());
- } else {
- response.sendJsonError(0, file.errorString().toUtf8());
- }
- }
- void UploadedFilesController::doPost(CWF::Request &request, CWF::Response &response) const
- {
- const QString authorization = request.getHttpParser().getHeaderField("Authorization");
- if (authorization.isEmpty()) {
- response.write(
- QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("LoginOut failed, no find user")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(authorization.mid(7), tokenSecret);
- if (!token.isValid()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("unauthorized")},
- {"code", response.SC_UNAUTHORIZED},
- {"data", QJsonValue()}});
- return;
- }
- QString userid = token.claim("userId");
- bool isok = false;
- qint64 id = userid.toLongLong(&isok, 10);
- if (id == 0) {
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("file uploaded successfully")},
- {"code", 200},
- {"data", QJsonValue()}});
- return;
- }
- UserModel user(storage);
- CWF::SqlQueryManager queryManager(storage);
- queryManager.select("*", user.getTableName()).where("id=?");
- queryManager.prepare();
- queryManager.addBindValue(id);
- queryManager.exec();
- QJsonArray jsonArray = queryManager.toJson();
- if (jsonArray.isEmpty()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("user does not exist")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- for (const QJsonValue &json : jsonArray) {
- if (json.isObject()) {
- QJsonObject obj = json.toObject();
- // 更新在线状态
- user.buildFromJson(obj);
- user.save();
- AppEvent::instance()->loginOutUser(obj["id"].toInt());
- }
- }
- const QMultiMap<QByteArray, QByteArray> &files = request.getUploadedFiles();
- if (user.getId() > 0) {
- QVariant answerDir = AppEvent::instance()->configValue("answerDir");
- const QString path = answerDir.toString();
- QDir dir = path;
- if (!dir.exists(path)) {
- dir.mkpath(path);
- }
- for (auto it = files.begin(); it != files.end(); ++it) {
- QString fileName = user.getAnswerFileName();
- if (fileName.isEmpty()) {
- fileName = QString("%1_%2.zip").arg(user.getName()).arg(user.getExamineeNumber());
- }
- const QString filePath = dir.absoluteFilePath(fileName);
- QFile file(filePath);
- if (!file.open(QIODevice::WriteOnly)) {
- qWarning() << "Failed to open file for writing:" << file.errorString() << filePath;
- }
- file.write(it.value());
- file.close();
- }
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("file uploaded successfully")},
- {"code", 200},
- {"data", QJsonValue()}});
- return;
- }
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Failed to uploaded files")},
- {"code", 500},
- {"data", QJsonValue()}});
- }
- void ExamsAnswerTimeController::doPost(CWF::Request &request, CWF::Response &response) const
- {
- const QString authorization = request.getHttpParser().getHeaderField("Authorization");
- if (authorization.isEmpty()) {
- response.write(
- QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("answer Time failed, no find user login")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(authorization.mid(7), tokenSecret);
- if (!token.isValid()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("unauthorized")},
- {"code", response.SC_UNAUTHORIZED},
- {"data", QJsonValue()}});
- return;
- }
- QString userid = token.claim("userId");
- bool isok = false;
- qint64 id = userid.toLongLong(&isok, 10);
- if (id == 0) {
- response.write(
- QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("Update Answer Time successfully")},
- {"code", 200},
- {"data", QJsonValue()}});
- return;
- }
- QJsonParseError error;
- QJsonDocument jdc = QJsonDocument::fromJson(request.getBody(), &error);
- QJsonObject jsonObject = jdc.object();
- qint64 time = 0;
- if (jsonObject.contains("answerTime")) {
- time = jsonObject["answerTime"].toVariant().toLongLong();
- }
- UserModel user(storage);
- CWF::SqlQueryManager queryManager(storage);
- queryManager.select("*", user.getTableName()).where("id=?");
- queryManager.prepare();
- queryManager.addBindValue(id);
- queryManager.exec();
- QJsonArray jsonArray = queryManager.toJson();
- if (jsonArray.isEmpty()) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("user does not exist")},
- {"code", 500},
- {"data", QJsonValue()}});
- return;
- }
- for (const QJsonValue &json : jsonArray) {
- if (json.isObject()) {
- QJsonObject obj = json.toObject();
- // 修改登录状态
- obj["answerTime"] = time;
- // 更新在线状态
- user.buildFromJson(obj);
- user.save();
- AppEvent::instance()->loginOutUser(obj["id"].toInt());
- {
- int examID = 0;
- QString sql = QString(R"__(
- SELECT eq.id
- FROM user u
- JOIN exams_question eq
- ON (
- (u.examTest IS NULL OR u.examTest = '') AND eq.isSelect = 1
- OR (u.examTest IS NOT NULL AND u.examTest <> '' AND eq.name = u.examTest)
- )
- WHERE u.id = %1;
- )__")
- .arg(id);
- CWF::SqlQuery query(storage);
- QJsonObject status = query.exec(sql);
- if (status["success"].toBool()) {
- const QJsonArray array = query.toJson();
- if (!array.isEmpty()) {
- QJsonObject object = array[0].toObject();
- examID = object["id"].toInt();
- }
- }
- const QString examNumber = AppEvent::instance()->examNumber();
- const QString examRoom = AppEvent::instance()->examRoom();
- QString fileName = user.getAnswerFileName();
- if (fileName.isEmpty()) {
- fileName = QString("%1_%2.zip").arg(user.getName()).arg(user.getExamineeNumber());
- }
- ExamsTestModel examsTestModel(storage);
- examsTestModel.setUserId(id);
- examsTestModel.setExamsId(examID);
- examsTestModel.setEndTime(time);
- examsTestModel.setExamRoom(examRoom);
- examsTestModel.setExamSessions(examNumber);
- examsTestModel.setAnswerFileName(fileName);
- examsTestModel.save();
- qDebug() << " AppEvent::instance()->examsTestUpdate();";
- AppEvent::instance()->examsTestUpdate();
- }
- response.write(
- QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("Update Answer Time successfully")},
- {"code", 200},
- {"data", QJsonValue()}});
- return;
- }
- }
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Update Answer Time Failed")},
- {"code", 500},
- {"data", QJsonValue()}});
- }
- void TableDataController::doPost(CWF::Request &request, CWF::Response &response) const
- {
- QJsonParseError error;
- QJsonDocument jdc = QJsonDocument::fromJson(request.getBody(), &error);
- if (error.error != QJsonParseError::NoError) {
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("json param error")},
- {"code", 500},
- {"data", error.errorString()}});
- return;
- }
- QJsonObject jsonObject = jdc.object();
- int pageNum = jsonObject.value("pageNum").toInt(1);
- int pageSize = jsonObject.value("pageSize").toInt(20);
- QString examNumber = jsonObject.value("examNumber").toString("");
- QString examRoom = jsonObject.value("examRoom").toString("");
- int offset = (pageNum - 1) * pageSize;
- QStringList wheleList;
- if (!examNumber.isEmpty()) {
- wheleList += QString("e.exam_sessions='%1'").arg(examNumber);
- }
- if (!examRoom.isEmpty()) {
- wheleList += QString("e.exam_room='%1'").arg(examRoom);
- }
- QString whele;
- if (!wheleList.isEmpty()) {
- whele += "WHERE ";
- whele += wheleList.join(" AND ");
- }
- QString sql = QString(R"__(
- SELECT
- s.name,
- s.swID,
- s.examineeNumber,
- s.checkinNumber,
- s.groupName,
- s.answerFileName,
- s.school,
- e.end_time AS exam_time,
- e.exam_room,
- e.exam_sessions,
- eq.name AS examName
- FROM
- user s
- JOIN
- exams_test e ON s.id = e.user_id
- JOIN
- exams_question eq ON e.exams_id = eq.id
- %1
- ORDER BY
- e.end_time ASC
- LIMIT %2, %3;
- )__")
- .arg(whele)
- .arg(offset)
- .arg(pageSize);
- CWF::SqlQuery query(storage);
- QJsonObject status = query.exec(sql);
- if (status["success"].toBool()) {
- const QJsonArray array = query.toJson();
- response.write(QJsonObject{{"status", true},
- {"message", TeacherServer::Tr::tr("Table data ")},
- {"code", 200},
- {"data", array}});
- return;
- }
- qDebug().noquote().nospace() << status << sql;
- response.write(QJsonObject{{"status", false},
- {"message", TeacherServer::Tr::tr("Get Table Data Failed")},
- {"code", 500},
- {"data", QJsonValue()}});
- }
- void TableDataExamRoomController::doGet(CWF::Request &, CWF::Response &response) const
- {
- const QString examNumber = AppEvent::instance()->examNumber();
- const QString examRoom = AppEvent::instance()->examRoom();
- QJsonObject object;
- object.insert("examNumber", examNumber);
- object.insert("examRoom", examRoom);
- response.write(QJsonObject{{"status", true},
- {"message", "Get Table Data ok"},
- {"code", 200},
- {"data", object}});
- }
|