#include "stdafx.h"
|
#include "Respond.h"
|
#include "MySQLDB.h"
|
|
#include <iostream>
|
#include <time.h>
|
|
using namespace std;
|
|
Respond::Respond()
|
{
|
}
|
|
|
Respond::~Respond()
|
{
|
}
|
|
//¸¨Öúº¯Êý
|
bool Respond::business_login(LoginReq * req, SOCKET client)
|
{
|
cout << "µÇ¼ÇëÇóÒµÎñµÄ´¦Àí..." << endl;
|
// µÇ¼ÏìÓ¦
|
|
// ǰÖõĴ¦Àí£ºÏÈÑéÖ¤Ò»ÏÂÊý¾ÝµÄ׼ȷÐÔ£¬Ñé֤ͨ¹ý£¬ÔÙÍùÏÂÒ»²½
|
|
// µÚÒ»²½£ºÏȰÑÇëÇóµÄÊý¾ÝÄóöÀ´£¬×é×°Ò»ÏÂSQLÓï¾ä¡¾±ê×¼¸ñʽ¡¿
|
char sql[512] = { 0 };
|
|
//½«¸ñʽ»¯Êý¾ÝдÈëÒ»¸ö×Ö·û´®»º³åÇø
|
sprintf_s(sql, "select id,user_name,status,admin,history,log_search,map_change,attend_manage,device_manage,version_manage,notify_manage,image_input,monitor_back from user_info a,role_info b where a.user_name ='%s' and a.password = '%s' and a.role_id = b.id;", req->user_name,req->password);
|
|
//ʵÀý»¯Ò»¸ö¶ÔÏó
|
MySQLDB my_DB;
|
|
//·µ»Ø½á¹ûµÄ¡°Êý¾Ý½áºÏÌ塱
|
vector<vector<string>> result;
|
size_t rows = my_DB.execSQL(sql, result);
|
cout << "rows:" << rows << endl;
|
//Ò»¸öµÇ¼ÏìÓ¦½á¹¹Ì壬¸ù¾Ýresult,Ìî³ä½á¹¹Ì壬·¢ËÍ
|
LoginRes res;
|
|
//³õÖµ
|
res.per = { 0 };
|
|
|
if (rows > 0)//´ú±íÓÐÊý¾Ý¼¯
|
{
|
//È¡µÃÓû§ID
|
res.emp_id = stoi(result[0][0]);
|
strcpy_s(res.user_name, result[0][1].c_str());
|
//½«resultÊý×éÖеÚ0ÐеÚ2ÁÐλÖõÄ×Ö·û´®ÄÚÈÝת»»ÎªÕûÊý
|
res.status = stoi(result[0][2]);
|
//ȨÏÞ¸³Öµ
|
res.per.admin = stoi(result[0][3]);
|
res.per.admin = stoi(result[0][4]);
|
res.per.log_search = stoi(result[0][5]);
|
res.per.map_change = stoi(result[0][6]);
|
res.per.attend_manage = stoi(result[0][7]);
|
res.per.device_manage = stoi(result[0][8]);
|
res.per.version_manage = stoi(result[0][9]);
|
res.per.notify_manage = stoi(result[0][10]);
|
res.per.image_input = stoi(result[0][11]);
|
res.per.monitor_back = stoi(result[0][12]);
|
}
|
else
|
{
|
//ûÓÐÊý¾Ý¼¯
|
strcpy_s(res.user_name, "");
|
|
res.status = 0;
|
res.emp_id = 0;
|
}
|
|
int send_len = 0;
|
//·¢Ë͵ǼÏìÓ¦°ü
|
send_len = send(client, (char *)&res, res.head.len, 0);
|
|
cout << "send len :" << send_len << endl;
|
cout << "·¢Ë͵ǼÏìÓ¦°ü£¬µÇ¼ҵÎñ´¦ÀíÍê³É£¡" << endl;
|
|
//test
|
cout << "user_status :" << res.status << endl;
|
cout << "some part user_power :" << res.per.admin << res.per.admin << res.per.image_input << endl;
|
|
return false;
|
}
|
|
bool Respond::business_register(RegisterReq * req, SOCKET client)
|
{
|
//ÏÈÓÃÒ»¸öcharÊý×飬°Ñ·ÃÎÊÊý¾Ý¿âµÄÓï¾ä×¼±¸ºÃ
|
char sql[512] = { 0 };
|
|
/*
|
¾¹ýÐÉÌ£¬¿Í»§¶ËQt½çÃæ¡¤Óû§ÐÅÏ¢µÄ½á¹¹Ìå
|
|
struct User {
|
QString username; //ok
|
QString password; //ok
|
QString email; //ok
|
QString phone; //ok
|
QString department; //ok
|
QString permission; //default¡¾×¢²áĬÈÏÖ»ÓÐ×î»ù±¾µÄȨÏÞ¡¿
|
QString status; //default¡¾×¢²áĬÈÏ״̬1¡¿
|
QDate registerDate; //default¡¾×¢²áĬÈϵ±Ç°Ê±¼ä¡¿
|
};
|
*/
|
|
// »ñÈ¡µ±Ç°Ê±¼ä²¢¸ñʽ»¯Îª×Ö·û´®
|
char current_time[20];
|
time_t now = time(NULL);
|
struct tm tm_info; // ¸ÄΪջ±äÁ¿¶ø²»ÊÇÖ¸Õë
|
|
// ʹÓð²È«µÄ localtime_s Ìæ´ú localtime
|
errno_t err = localtime_s(&tm_info, &now);
|
if (err == 0) {
|
strftime(current_time, sizeof(current_time), "%Y-%m-%d %H:%M:%S", &tm_info);
|
}
|
else {
|
// ´íÎó´¦Àí
|
strcpy_s(current_time, sizeof(current_time), "1970-01-01 00:00:00");
|
}
|
cout << "µ±Ç°²Ù×÷ʱ¼ä£º" << current_time;
|
|
//¸ñʽ»¯ÊäÈëµ½×Ö·û´®¡¾Äѵ㣬дÈëÊý¾Ý¿âµÄÁ÷³Ì¡¿
|
sprintf_s(sql, "INSERT INTO user_info (user_name, password, tel, email, department, status, register_time, role_id) VALUES('%s', '%s', '%s', '%s', '%s', 1, '%s', 2);", req->user_name, req->password, req->telephone, req->email, req->department,current_time);
|
|
//ʵÀý»¯Ò»¸ö¶ÔÏó
|
MySQLDB my_DB;
|
|
//·¢ËÍ×¢²áÏìÓ¦°ü
|
RegisterRes res;
|
|
//Êý¾Ý¿â·â×°µÄ²åÈ뺯Êý
|
uint64_t user_id = 0;
|
user_id = my_DB.exec_insSQL(sql, res.res_info);
|
|
//Ìî³äÏìÓ¦½á¹¹Ìå
|
strcpy_s(res.user_name, req->user_name);
|
res.per = { 0 };
|
res.emp_id = user_id;//¿ÉÒÔͨ¹ý¼ì²éid£¬ÅжÏÊÇ·ñ³É¹¦×¢²á
|
res.status = 1;
|
|
|
/*
|
ÐèÒª¼ÓÃÜ
|
*/
|
|
|
int send_len = 0;
|
//·¢ËÍ×¢²áÏìÓ¦°ü
|
send_len = send(client, (char *)&res, res.head.len, 0);
|
|
cout << "send len :" << send_len << endl;
|
cout << "·¢ËÍ×¢²áÏìÓ¦°ü£¬×¢²áÒµÎñ´¦ÀíÍê³É£¡" << endl;
|
|
//test
|
cout << "user_status :" << res.status << endl;
|
cout << "some part user_power :" << res.per.admin << res.per.admin << res.per.image_input << endl;
|
cout << "user_info :" << res.status << endl;
|
cout << "Óû§Ãû£º" << res.user_name << " ¹¤ºÅ£º" << res.emp_id << " ״̬£º" << res.status << endl;
|
cout << "´íÎóÐÅÏ¢£º[" << res.res_info << "/]" << endl;
|
|
return false;
|
}
|