|
| 1 | +#include "apiwidget.h" |
| 2 | +#include "ui_apiwidget.h" |
| 3 | + |
| 4 | +#include <QJsonDocument> |
| 5 | +#include <QJsonObject> |
| 6 | +#include <QFile> |
| 7 | +#include <QFileInfo> |
| 8 | +#include <QDebug> |
| 9 | + |
| 10 | + |
| 11 | +APIWidget::APIWidget(QWidget *parent) |
| 12 | + : QWidget(parent) |
| 13 | + , ui(new Ui::APIWidget) |
| 14 | + , m_url("http://api.fanyi.baidu.com/api/trans/vip/translate") |
| 15 | +{ |
| 16 | + ui->setupUi(this); |
| 17 | + setWindowTitle("API设置"); |
| 18 | + int status=0; |
| 19 | + if(readConfig(status)){ |
| 20 | + if(status == 1){ |
| 21 | + ui->textEdit_appid->setPlainText(m_appid); |
| 22 | + ui->textEdit_key->setPlainText(m_key); |
| 23 | + }else if(status == 0){ |
| 24 | + //默认配置不输出 |
| 25 | + ; |
| 26 | + }else{ |
| 27 | + //读取文件状态错误 |
| 28 | + ; |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +APIWidget::~APIWidget() |
| 34 | +{ |
| 35 | + delete ui; |
| 36 | +} |
| 37 | + |
| 38 | +void APIWidget::on_pushButton_clicked() |
| 39 | +{ |
| 40 | + m_appid = ui->textEdit_appid->toPlainText().toUtf8(); |
| 41 | + m_key = ui->textEdit_key->toPlainText().toUtf8() ; |
| 42 | + // 配置文件读取标志位 |
| 43 | + m_configReadFlag = true; |
| 44 | + QJsonObject jsonObj; |
| 45 | + jsonObj.insert("appid",m_appid); |
| 46 | + jsonObj.insert("key",m_key); |
| 47 | + QJsonDocument jsonDoc(jsonObj); |
| 48 | + //将json对象转换成字符串 |
| 49 | + QByteArray data=jsonDoc.toJson(); |
| 50 | + QFile file("config.json"); |
| 51 | + file.open(QIODevice::WriteOnly); |
| 52 | + file.write(data); |
| 53 | + file.close(); |
| 54 | + this->hide(); |
| 55 | +} |
| 56 | + |
| 57 | +bool APIWidget::readConfig(int &status){ |
| 58 | + //首先判断是否存在配置文件 |
| 59 | + QFileInfo fileInfo("config.json"); |
| 60 | + if(fileInfo.isFile()){ |
| 61 | + qDebug() << "文件存在,读取文件中配置"; |
| 62 | + QFile loadFile("config.json"); |
| 63 | + |
| 64 | + if(!loadFile.open(QIODevice::ReadOnly)){ |
| 65 | + qDebug() << "could't open projects json"; |
| 66 | + status = -1; |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + QByteArray allData = loadFile.readAll(); |
| 71 | + loadFile.close(); |
| 72 | + |
| 73 | + QJsonParseError json_error; |
| 74 | + QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error)); |
| 75 | + |
| 76 | + if(json_error.error != QJsonParseError::NoError) |
| 77 | + { |
| 78 | + qDebug() << "json error!"; |
| 79 | + status = -1; |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + QJsonObject rootObj = jsonDoc.object(); |
| 84 | + m_appid = rootObj.value("appid").toString(); |
| 85 | + m_key = rootObj.value("key").toString(); |
| 86 | + status = 1; |
| 87 | + return true; |
| 88 | + }else{ |
| 89 | + qDebug() << "文件不存在,读取默认配置"; |
| 90 | + m_appid = " ";//默认配置 |
| 91 | + m_key = " "; |
| 92 | + status = 0; |
| 93 | + return true; |
| 94 | + } |
| 95 | +} |
| 96 | +QString APIWidget::getAppid(){ |
| 97 | + return m_appid; |
| 98 | +} |
| 99 | +QString APIWidget::getKey(){ |
| 100 | + return m_key; |
| 101 | +} |
| 102 | +QString APIWidget::getUrl(){ |
| 103 | + return m_url; |
| 104 | +} |
0 commit comments