void TalkWindowShell::getEmployeeIDs(QStringList& employeesList)
{
QSqlQueryModel queryModel;
queryModel.setQuery("SELECT employeeID FROM tab_employees WHERE status = 1");
//返回模型的总行数(员工的总数)
int employeesNum = queryModel.rowCount();
QModelIndex index;
for (int i = 0; i < employeesNum; ++i)
{
index = queryModel.index(i, 0);//行,列
employeesList <<queryModel.data(index).toString();
}
}
bool TalkWindowShell::createJSFile(QStringList& employeesList)
{
//读取txt文件数据
QString strFileTxt = "Resources/MainWindow/MsgHtml/msgtmpl.txt";
QFile fileRead(strFileTxt);
QString strFile;
if (fileRead.open(QIODevice::ReadOnly))
{
strFile = fileRead.readAll();
fileRead.close();
}
else
{
QMessageBox::information(this,
QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("读取 msgtmpl.txt 失败!"));
return false;
}
//替换 (external0,appendHtml0用作自己发信息使用)
QFile fileWrite("Resources/MainWindow/MsgHtml/msgtmpl.js");
if (fileWrite.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
//更新空值
QString strSourceInitNull = "var external = null;";
//更新初始化
QString strSourceInit = "external = channel.objects.external;";
//更新newWebChannel
QString strSourceNew =
"new QWebChannel(qt.webChannelTransport,\
function(channel) {\
external = channel.objects.external;\
}\
);\
";
//更新追加recvHtml,脚本中有双引号无法直接进行赋值,采用读文件方式
QString strSourceRecvHtml;
QFile fileRecvHtml("Resources/MainWindow/MsgHtml/recvHtml.txt");
if (fileRecvHtml.open(QIODevice::ReadOnly))
{
strSourceRecvHtml = fileRecvHtml.readAll();
fileRecvHtml.close();
}
else
{
QMessageBox::information(this,
QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("读取 recvHtml.txt 失败!"));
return false;
}
//保存替换后的脚本
QString strReplaceInitNull;
QString strReplaceInit;
QString strReplaceNew;
QString strReplaceRecvHtml;
for (int i = 0; i < employeesList.length(); i++)
{
//编辑替换后的空值
QString strInitNull = strSourceInitNull;
strSourceInit.replace("external",QString("external_1%").arg(employeesList.at(i)));
strReplaceInitNull += strInitNull;
strReplaceInitNull += "\n";
//编辑替换后的初始值
QString strInit = strSourceInit;
strInit.replace("external", QString("external_1%").arg(employeesList.at(i)));
strReplaceInit += strInit;
strReplaceInit += "\n";
//编辑替换后的 newWebChannel
QString strNew = strSourceNew;
strNew.replace("external", QString("external_1%").arg(employeesList.at(i)));
strReplaceNew += strNew;
strReplaceNew += "\n";
//编辑替换后的 recvHtml
QString strRecvHtml = strSourceRecvHtml;
strReplaceNew.replace("external", QString("external_1%").arg(employeesList.at(i)));
strRecvHtml.replace("recvHtml",QString("recvHtml_1%").arg(employeesList.at(i)));
strReplaceRecvHtml += strRecvHtml;
strReplaceRecvHtml += "\n";
}
strFile.replace(strSourceInitNull,strReplaceInitNull);
strFile.replace(strSourceInit,strReplaceInit);
strFile.replace(strSourceNew,strReplaceNew);
strFile.replace(strSourceRecvHtml,strReplaceRecvHtml);
QTextStream stream(&fileWrite);
stream << strFile;
fileWrite.close();
return true;
}
else
{
QMessageBox::information(this,
QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("写 msgtmpl.js 失败!"));
return false;
}
}
更新js文件
最新推荐文章于 2024-11-13 01:18:00 发布