主页框架

This commit is contained in:
zifan 2023-09-20 16:00:14 +08:00
parent 86491da7cf
commit 689ff0a77c
6 changed files with 137 additions and 11 deletions

28
www/cgi-bin/c_mysql_user_pwd.c Normal file → Executable file
View File

@ -8,20 +8,23 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
int main() { int main()
{
// 连接到 MySQL 数据库 // 连接到 MySQL 数据库
MYSQL *conn; MYSQL *conn;
MYSQL_RES *res; MYSQL_RES *res;
MYSQL_ROW row; MYSQL_ROW row;
conn = mysql_init(NULL); conn = mysql_init(NULL);
if (conn == NULL) { if (conn == NULL)
{
fprintf(stderr, "mysql_init() failed\n"); fprintf(stderr, "mysql_init() failed\n");
exit(1); exit(1);
} }
if (mysql_real_connect(conn, "localhost", "root", "root", "router", 3306, if (mysql_real_connect(conn, "localhost", "root", "root", "router", 3306,
NULL, 0) == NULL) { NULL, 0) == NULL)
{
fprintf(stderr, "mysql_real_connect() failed\n"); fprintf(stderr, "mysql_real_connect() failed\n");
mysql_close(conn); mysql_close(conn);
exit(1); exit(1);
@ -33,7 +36,8 @@ int main() {
// 解析 JSON 数据 // 解析 JSON 数据
cJSON *p = cJSON_Parse(request_data); cJSON *p = cJSON_Parse(request_data);
if (p == NULL) { if (p == NULL)
{
fprintf(stderr, "Error parsing JSON data\n"); fprintf(stderr, "Error parsing JSON data\n");
mysql_close(conn); mysql_close(conn);
return 1; return 1;
@ -44,7 +48,8 @@ int main() {
cJSON *pwd = cJSON_GetObjectItemCaseSensitive(p, "pwd"); cJSON *pwd = cJSON_GetObjectItemCaseSensitive(p, "pwd");
if (user == NULL || pwd == NULL || !cJSON_IsString(user) || if (user == NULL || pwd == NULL || !cJSON_IsString(user) ||
!cJSON_IsString(pwd)) { !cJSON_IsString(pwd))
{
fprintf(stderr, fprintf(stderr,
"Error getting username and/or password from JSON data\n"); "Error getting username and/or password from JSON data\n");
cJSON_Delete(p); cJSON_Delete(p);
@ -60,7 +65,8 @@ int main() {
sprintf(query, "SELECT * FROM users WHERE username='%s' AND password='%s'", sprintf(query, "SELECT * FROM users WHERE username='%s' AND password='%s'",
username_from_frontend, password_from_frontend); username_from_frontend, password_from_frontend);
if (mysql_query(conn, query)) { if (mysql_query(conn, query))
{
fprintf(stderr, "SELECT query failed. Error: %s\n", mysql_error(conn)); fprintf(stderr, "SELECT query failed. Error: %s\n", mysql_error(conn));
cJSON_Delete(p); cJSON_Delete(p);
mysql_close(conn); mysql_close(conn);
@ -68,7 +74,8 @@ int main() {
} }
res = mysql_store_result(conn); res = mysql_store_result(conn);
if (res == NULL) { if (res == NULL)
{
fprintf(stderr, "mysql_store_result() failed\n"); fprintf(stderr, "mysql_store_result() failed\n");
cJSON_Delete(p); cJSON_Delete(p);
mysql_close(conn); mysql_close(conn);
@ -77,10 +84,13 @@ int main() {
cJSON *result_json = cJSON_CreateObject(); cJSON *result_json = cJSON_CreateObject();
if ((row = mysql_fetch_row(res))) { if ((row = mysql_fetch_row(res)))
{
cJSON_AddNumberToObject(result_json, "code", 0); cJSON_AddNumberToObject(result_json, "code", 0);
cJSON_AddStringToObject(result_json, "nickname", "admin"); cJSON_AddStringToObject(result_json, "nickname", "admin");
} else { }
else
{
cJSON_AddNumberToObject(result_json, "code", 1); cJSON_AddNumberToObject(result_json, "code", 1);
cJSON_AddStringToObject(result_json, "msg", "用户名或口令错误"); cJSON_AddStringToObject(result_json, "msg", "用户名或口令错误");
} }

0
www/cgi-bin/creat_table.sql Normal file → Executable file
View File

BIN
www/cgi-bin/login.c Executable file

Binary file not shown.

1
www/cgi-bin/tmp.txt Executable file
View File

@ -0,0 +1 @@
{"name":"disen","pwd":"123456"}disen12345

111
www/index.html Executable file
View File

@ -0,0 +1,111 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="zifan">
<meta name="keywords" content="物联网">
<title>主页</title>
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
justify-content: space-between;
}
#top {
height: 50px;
background-color: lightblue;
}
#foot {
height: 40px;
background-color: lightblue;
}
#main {
height: 100%;
display: flex;
flex-direction: row;
background-color: lightcyan;
}
#top {
display: flex;
flex-direction: row;
justify-content: space-between;
justify-items: center;
align-items: center;
padding: 5px;
border-bottom: 1px solid lightgray;
}
#logo>img {
width: 180px;
height: 45px;
}
#foot {
border-top: 1px solid lightgray;
padding: 10px 0;
}
#menu {
width: 300px;
border-right: 1px solid lightgray;
padding: 20px;
}
iframe {
outline: none;
border: none;
}
</style>
</head>
<body>
<div id="top">
<div id="logo">
<img src="/logo.png">
</div>
<div id="user_info"><span id="userName"></span>
<span class="logout" onclick="logout()">[登出]</span>
</div>
</div>
</div>
<div id="main">
<div id="menu">
<h3>路由配置</h3>
</div>
<div id="content" style="width: 100%;">
<iframe name="content_frame" width="100%" height="100%"></iframe>
</div>
</div>
<div id="foot" style="text-align: center;">
<b>版本所有@FLY-NOOB</b>
</div>
<script>
window.onload = function () {
if (localStorage.getItem("login_user") == null) {
open("/login.html", "_self");
} else {
let login_user_str = localStorage.getItem("login_user");
let login_user = JSON.parse(login_user_str);
userName.innerText = login_user.nickname;
}
}
function logout() {
localStorage.clear();
open("/login.html", "_self");
}
</script>
</body>
</html>

View File

@ -10,14 +10,18 @@
<style> <style>
html, html,
body { body {
display: flex;
height: 100%; height: 100%;
margin: 0; margin: 0;
padding: 0; padding: 0;
}
body {
display: flex;
flex-direction: column; flex-direction: column;
justify-content: space-between; justify-content: space-between;
} }
#top { #top {
height: 50px; height: 50px;
background-color: lightblue; background-color: lightblue;
@ -151,7 +155,7 @@
}).then(resp => resp.json()).then(data => { }).then(resp => resp.json()).then(data => {
console.log(data); console.log(data);
if (data.code == 0) { if (data.code == 0) {
window.localStorage.setItem("login_user", JSON.stringify(data.data)); window.localStorage.setItem("login_user", JSON.stringify(data));
open("/index.html", "_self"); open("/index.html", "_self");
} else { } else {
alert(data.msg); alert(data.msg);