#include "RunHttpPost_loginByFingerprintDat.h"

#include "../user/config.h"
extern config *pconfig;

RunHttpPost_loginByFingerprintDat::RunHttpPost_loginByFingerprintDat(InteractiveTask *task, QMutex *mutex)
    :RunAbstract(task, mutex)
{

}

void RunHttpPost_loginByFingerprintDat::run()
{
    QByteArray json;
    QByteArray outData = InteractiveHttp::http_post_loginByFingerprintDat();

    if (!outData.isEmpty())
    {
        // 解析是否登录成功
        QJsonParseError error;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(outData, &error);
        if (error.error == QJsonParseError::NoError)
        {
            if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
            {
                QJsonObject jsonObjRoot = jsonDoc.object();
                if(jsonObjRoot.contains("code"))
                {
                    if(jsonObjRoot.value("code").toInt() == 200)
                    {
                        if(jsonObjRoot.contains("token"))
                        {
                            QJsonValue value = jsonObjRoot.value("token");
                            if(value.type() == QJsonValue::String)
                            {
                                InteractiveHttp::strToken = value.toString();
                                QString username = "未知用户";
                                if(jsonObjRoot.contains("nickName"))
                                {
                                    QJsonValue name = jsonObjRoot.value("nickName");
                                    if(name.type() == QJsonValue::String)
                                    {
                                        username = name.toString();
                                    }
                                }
                                pconfig->username = username;

                                // 这里为了保证原有逻辑，需调用pro类
                                json = QString("{\"stat\":0,\"str\":\"登陆成功\",\"name\":\"%1\",\"outh\": 0}").arg(username).toUtf8();
                            }
                            else
                            {
                                json = QString("{\"stat\":-3,\"str\":\"登陆失败\",\"name\":\"\",\"outh\": 0}").toUtf8();
                            }
                        }
                    }
                    else
                    {
                        // json = QString("{\"stat\":-3,\"str\":\"登陆失败\",\"name\":\"\",\"outh\": 0}").toUtf8();
                        json = QString("{\"stat\":-2,\"str\":\"%1\",\"name\":\"\",\"outh\": 0}").arg(jsonObjRoot.value("msg").toString()).toUtf8();
                    }
                }
            }
        }
        else
        {
            json = QString("{\"stat\":-3,\"str\":\"服务器返回数据解析错误\",\"name\":\"\",\"outh\": 0}").toUtf8();
        }
    }

    setRespond(QString(json));
}
