完成登录和注册,完成注册界面

This commit is contained in:
xianya 2024-12-29 20:33:18 +08:00
parent 31c2725eaa
commit 2a8ed8f1a3
5 changed files with 339 additions and 206 deletions

View File

@ -15,5 +15,7 @@ add_executable(o
sqlite3.c sqlite3.c
login.c login.c
ui.c ui.c
include/ui.h) signin.c
include/ui.h
)
target_link_libraries(o "${LIBRARY_PATH}/${LIBRARY_NAME}") target_link_libraries(o "${LIBRARY_PATH}/${LIBRARY_NAME}")

View File

@ -9,7 +9,9 @@
#ifndef FUNCTION_H #ifndef FUNCTION_H
#define FUNCTION_H #define FUNCTION_H
int Login(); void Login(char *username,char*password,int *recall,char *errmsg);
void Signin(const char *username,const char*password,int *recall,char *errmsg);
void myexit(); void myexit();
#endif //FUNCTION_H #endif //FUNCTION_H

56
login.c
View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include<string.h>
#include "./include/function.h"
#include "./include/ui.h"
void Login(char *username,char*password,int *recall,char *errmsg){
sqlite3 *db;
int rc;
// 打开数据库
rc = sqlite3_open("./data/msg.db", &db);
if (rc) {
strcpy(errmsg,"Error:database error.");
*recall=-1;
return;
}
// SQL 查询语句:查找用户及其密码
const char *sql = "SELECT user, password FROM users WHERE user = ?;";
sqlite3_stmt *stmt;
// 编译 SQL 语句
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
strcpy(errmsg,"Error:database error.");
*recall=-1;
sqlite3_close(db);
return;
}
// 绑定查询参数
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
// 查找到用户,获取密码
const char *db_password = (const char *)sqlite3_column_text(stmt, 1);
// 如果密码匹配
if (db_password && strcmp(db_password, password) == 0) {
*recall=0;
} else {
strcpy(errmsg,"Incorrect username or password.");
*recall=-1;
}
} else {
strcpy(errmsg,"Incorrect username or password.");
*recall=-1;
}
// 绑定查询参数
// 清理资源
sqlite3_finalize(stmt);
sqlite3_close(db);
return;
}

63
signin.c Normal file
View File

@ -0,0 +1,63 @@
//
// Created by zhang on 2024/12/29.
//
#include <stdio.h>
#include <string.h>
#include "./include/function.h"
#include "./include/sqlite3.h"
void Signin(const char *username,const char*password,int *recall,char *errmsg){
sqlite3 *db;
char *errMsg = 0;
int rc;
// 打开数据库,如果数据库文件不存在则创建它
rc = sqlite3_open("./data/msg.db", &db);
if (rc) {
strcpy(errmsg,"Error:database error.");
*recall=-1;
return;
}
const char *sql_create_table =
"CREATE TABLE IF NOT EXISTS users "
"("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"user TEXT UNIQUE,"
"password TEXT"
");";
// 执行 SQL 语句
rc = sqlite3_exec(db, sql_create_table, 0, 0, &errMsg);
if (rc != SQLITE_OK) {
strcpy(errmsg,"Error:database error.");
sqlite3_free(errMsg);
sqlite3_close(db);
*recall=-1;return;
}
const char *sql_insert="INSERT INTO users (user, password) VALUES (?, ?);";
sqlite3_stmt *insert;
rc = sqlite3_prepare_v2(db, sql_insert, -1, &insert, 0);
if (rc != SQLITE_OK) {
strcpy(errmsg,"Error:database error.");
*recall=-1;
sqlite3_close(db);
return;
}
// 将第一个参数 (name) 绑定到 SQL 语句中的第一个 "?"
sqlite3_bind_text(insert, 1, username, -1, SQLITE_STATIC);
// 将第二个参数 (email) 绑定到 SQL 语句中的第二个 "?"
sqlite3_bind_text(insert, 2, password, -1, SQLITE_STATIC);
rc = sqlite3_exec(db,insert,0,0,&errMsg);
if (rc != SQLITE_OK) {
strcpy(errmsg,errMsg);
sqlite3_free(errMsg);
sqlite3_finalize(insert);
sqlite3_close(db);
*recall=-1;return;
}
sqlite3_close(db);
*recall=0;
return;
}

278
ui.c
View File

@ -36,7 +36,8 @@ struct bottom *creat_bottom(WINDOW *win,int x,int y,int width,int height,char *t
} }
bool click_bottom(MEVENT event, struct bottom bottom, int sx, int sy) { bool click_bottom(MEVENT event, struct bottom bottom, int sx, int sy) {
return event.x-sx>= bottom.x && event.x-sx < bottom.x + bottom.width && event.y-sy >= bottom.y && event.y-sy < bottom.y + bottom.height; return event.x - sx >= bottom.x && event.x - sx < bottom.x + bottom.width && event.y - sy >= bottom.y &&
event.y - sy < bottom.y + bottom.height;
} }
int Welcome_ui() { int Welcome_ui() {
@ -70,15 +71,24 @@ int Welcome_ui() {
if (nc_getmouse(&event) == OK) { if (nc_getmouse(&event) == OK) {
//mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate); //mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate);
if (click_bottom(event, *login, start_x, start_y)) { if (click_bottom(event, *login, start_x, start_y)) {
free(login);free(signin);free(exit);endwin(); free(login);
free(signin);
free(exit);
endwin();
return 2; return 2;
} }
if (click_bottom(event, *signin, start_x, start_y)) { if (click_bottom(event, *signin, start_x, start_y)) {
free(login);free(signin);free(exit);endwin(); free(login);
free(signin);
free(exit);
endwin();
return 3; return 3;
} }
if (click_bottom(event, *exit, start_x, start_y)) { if (click_bottom(event, *exit, start_x, start_y)) {
free(login);free(signin);free(exit);endwin(); free(login);
free(signin);
free(exit);
endwin();
return 0; return 0;
} }
} }
@ -86,7 +96,9 @@ int Welcome_ui() {
} }
} }
void draw_login_window(WINDOW *win,struct bottom *login,struct bottom *signin,struct bottom *user,struct bottom *psword,struct bottom *exit) { void
draw_login_window(WINDOW *win, struct bottom *login, struct bottom *signin, struct bottom *user, struct bottom *psword,
struct bottom *exit) {
// 清空窗口并绘制边框 // 清空窗口并绘制边框
wclear(win); wclear(win);
box(win, 0, 0); box(win, 0, 0);
@ -108,6 +120,29 @@ void draw_login_window(WINDOW *win,struct bottom *login,struct bottom *signin,st
wrefresh(win); // 刷新窗口 wrefresh(win); // 刷新窗口
} }
void
draw_signin_window(WINDOW *win, struct bottom *signin, struct bottom *user, struct bottom *psword,
struct bottom *exit) {
// 清空窗口并绘制边框
wclear(win);
box(win, 0, 0);
// 标题
mvwprintw(win, 1, (WIN_WIDTH - 10) / 2, "Signin");
// 用户名输入框
mvwprintw(win, 3, 4, "Username:");
// 密码输入框
mvwprintw(win, 6, 4, "Password:");
//绘制剩余内容
draw_bottom(win, signin);
draw_bottom(win, exit);
draw_bottom(win, user);
draw_bottom(win, psword);
wrefresh(win); // 刷新窗口
}
void get_input(WINDOW *win, int y, int x, char *buffer, int max_len, struct bottom *bot, int sx, int sy) { void get_input(WINDOW *win, int y, int x, char *buffer, int max_len, struct bottom *bot, int sx, int sy) {
MEVENT event; MEVENT event;
int ch, pos = 0; int ch, pos = 0;
@ -129,8 +164,7 @@ void get_input(WINDOW *win, int y, int x, char *buffer, int max_len,struct botto
else test[pos] = '*'; else test[pos] = '*';
draw_bottom(win, bot); draw_bottom(win, bot);
wrefresh(win); wrefresh(win);
} } else if (ch == KEY_MOUSE) {
else if(ch== KEY_MOUSE){
if (nc_getmouse(&event) == OK) { if (nc_getmouse(&event) == OK) {
if (!click_bottom(event, *bot, sx, sy)) { if (!click_bottom(event, *bot, sx, sy)) {
return; return;
@ -139,8 +173,11 @@ void get_input(WINDOW *win, int y, int x, char *buffer, int max_len,struct botto
} }
} }
} }
int Login_ui() { int Login_ui() {
MEVENT event; MEVENT event;
char *errmsg[30];
memset(errmsg, 0, 30);
int start_y = (LINES - WIN_HEIGHT) / 2; int start_y = (LINES - WIN_HEIGHT) / 2;
int start_x = (COLS - WIN_WIDTH) / 2; int start_x = (COLS - WIN_WIDTH) / 2;
WINDOW *login_win = newwin(WIN_HEIGHT, WIN_WIDTH, start_y, start_x); WINDOW *login_win = newwin(WIN_HEIGHT, WIN_WIDTH, start_y, start_x);
@ -154,28 +191,54 @@ int Login_ui(){
struct bottom *exit = creat_bottom(login_win, 10, 25, 5, 1, "Exit"); struct bottom *exit = creat_bottom(login_win, 10, 25, 5, 1, "Exit");
struct bottom *users = creat_bottom(login_win, 4, 4, 16, 1, "[______________]"); struct bottom *users = creat_bottom(login_win, 4, 4, 16, 1, "[______________]");
struct bottom *psword = creat_bottom(login_win, 7, 4, 16, 1, "[______________]"); struct bottom *psword = creat_bottom(login_win, 7, 4, 16, 1, "[______________]");
psword->private=1;users->private=0; psword->private = 1;
users->private = 0;
int willdraw = 1;
while (1) { while (1) {
draw_login_window(login_win,login,signin,users,psword,exit); if (willdraw)draw_login_window(login_win, login, signin, users, psword, exit);
int rc = wgetch(login_win); int rc = wgetch(login_win);
if (rc == KEY_MOUSE) { if (rc == KEY_MOUSE) {
if (nc_getmouse(&event) == OK) { if (nc_getmouse(&event) == OK) {
//mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate); //mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate);
if (willdraw)
if (click_bottom(event, *login, start_x, start_y)) { if (click_bottom(event, *login, start_x, start_y)) {
free(login);free(signin);free(exit);free(users);free(psword);endwin(); int recall;
Login(username, password, &recall, errmsg);
if (recall != 0) {
wclear(login_win);
mvwprintw(login_win, 5, 5, "%s", errmsg);
exit->x=5;exit->y=8;
draw_bottom(login_win, exit);
wrefresh(login_win);
willdraw=0;
continue;
} }
}
if (willdraw)
if (click_bottom(event, *signin, start_x, start_y)) { if (click_bottom(event, *signin, start_x, start_y)) {
free(login);free(signin);free(exit);free(users);free(psword);endwin(); free(login);
free(signin);
free(exit);
free(users);
free(psword);
endwin();
return 3; return 3;
} }
if (click_bottom(event, *exit, start_x, start_y)) { if (click_bottom(event, *exit, start_x, start_y)) {
free(login);free(signin);free(exit);free(users);free(psword);endwin(); free(login);
return 1; free(signin);
free(exit);
free(users);
free(psword);
endwin();
return willdraw==1?1:2;
} }
if (willdraw)
if (click_bottom(event, *users, start_x, start_y)) { if (click_bottom(event, *users, start_x, start_y)) {
get_input(login_win, 4, 6, username, 15, users, start_x, start_y); get_input(login_win, 4, 6, username, 15, users, start_x, start_y);
continue; continue;
} }
if (willdraw)
if (click_bottom(event, *psword, start_x, start_y)) { if (click_bottom(event, *psword, start_x, start_y)) {
get_input(login_win, 7, 6, password, 15, psword, start_x, start_y); get_input(login_win, 7, 6, password, 15, psword, start_x, start_y);
continue; continue;
@ -186,128 +249,75 @@ int Login_ui(){
} }
int Signin_ui() { int Signin_ui() {
MEVENT event;
char *errmsg[100];
memset(errmsg, 0, 30);
int start_y = (LINES - WIN_HEIGHT) / 2;
int start_x = (COLS - WIN_WIDTH) / 2;
WINDOW *signin_win = newwin(WIN_HEIGHT, WIN_WIDTH, start_y, start_x);
keypad(signin_win, TRUE);
char *username[15]; //储存用户名
memset(username, 0, 15); // 清空缓冲区
char *password[15]; //储存密码
memset(password, 0, 15); // 清空缓冲区
struct bottom *signin = creat_bottom(signin_win, 10, 4, 5, 1, "Signin");
struct bottom *exit = creat_bottom(signin_win, 10, 25, 5, 1, "Exit");
struct bottom *users = creat_bottom(signin_win, 4, 4, 16, 1, "[______________]");
struct bottom *psword = creat_bottom(signin_win, 7, 4, 16, 1, "[______________]");
psword->private = 0;
users->private = 0;
int willdraw = 1;
while (1) {
if (willdraw)draw_signin_window(signin_win, signin, users, psword, exit);
int rc = wgetch(signin_win);
if (rc == KEY_MOUSE) {
if (nc_getmouse(&event) == OK) {
//mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate);
if (willdraw)
if (click_bottom(event, *signin, start_x, start_y)) {
int recall;
Signin(username,password,&recall,errmsg);
if (recall != 0) {
wclear(signin_win);
mvwprintw(signin_win, 5, 5, "%s", errmsg);
exit->x=5;exit->y=8;
draw_bottom(signin_win, exit);
wrefresh(signin_win);
willdraw=0;
continue;
}
else{
wclear(signin_win);
mvwprintw(signin_win, 5, 5, "Accepted");
exit->x=5;exit->y=8;
draw_bottom(signin_win, exit);
wrefresh(signin_win);
willdraw=0;
continue;
}
}
if (click_bottom(event, *exit, start_x, start_y)) {
free(signin);
free(exit);
free(users);
free(psword);
endwin();
return willdraw==1?1:3;
}
if (willdraw)
if (click_bottom(event, *users, start_x, start_y)) {
get_input(signin_win, 4, 6, username, 15, users, start_x, start_y);
continue;
}
if (willdraw)
if (click_bottom(event, *psword, start_x, start_y)) {
get_input(signin_win, 7, 6, password, 15, psword, start_x, start_y);
continue;
}
}
}
}
} }
//void draw_button(WINDOW *win, int x, int y, const char *label, bool is_active) {
// if (is_active) {
// wattron(win, A_REVERSE); // 激活按钮时反显
// }
// mvwprintw(win, y, x + (BUTTON_WIDTH - strlen(label)) / 2, "%s", label);
// if (is_active) {
// wattroff(win, A_REVERSE); // 反显结束
// }
// box(win, 0, 0); // 画按钮的边框
// wrefresh(win);
//}
//void draw_login_window() {
// // 绘制登录窗口
// clear();
// box(stdscr, 0, 0);
//
// // 标题
// mvprintw(2, 20, "Login Window");
//
// // 用户名
// mvprintw(USERNAME_Y, USERNAME_X - 10, "Username: ");
// mvprintw(PASSWORD_Y, PASSWORD_X - 10, "Password: ");
//
// // 输入框
// mvprintw(USERNAME_Y, USERNAME_X, "____________________________");
// mvprintw(PASSWORD_Y, PASSWORD_X, "____________________________");
//
// // 按钮
// mvprintw(LOGIN_BTN_Y, LOGIN_BTN_X, "[ Login ]");
// mvprintw(REGISTER_BTN_Y, REGISTER_BTN_X, "[ Register ]");
//
// refresh();
//}
//void handle_mouse_click(MEVENT event) {
// // 处理鼠标点击事件
// if (event.bstate & BUTTON1_CLICKED) {
// // 判断是否点击了登录按钮
// if (event.x >= LOGIN_BTN_X && event.x <= LOGIN_BTN_X + 9 &&
// event.y == LOGIN_BTN_Y) {
// printw("\nLogin button clicked!\n");
// refresh();
// }
// // 判断是否点击了注册按钮
// else if (event.x >= REGISTER_BTN_X && event.x <= REGISTER_BTN_X + 11 &&
// event.y == REGISTER_BTN_Y) {
// printw("\nRegister button clicked!\n");
// refresh();
// }
// }
//}
void clear_input_area(int x, int y, int len) {
// 清除输入框区域
for (int i = 0; i < len; i++) {
mvaddch(y, x + i, ' ');
}
}
//void Login_ui() {
// initscr(); // 初始化屏幕
// cbreak(); // 禁用行缓冲
// noecho(); // 不显示输入的字符
// keypad(stdscr, TRUE); // 启用键盘功能(例如箭头键)
// mousemask(ALL_MOUSE_EVENTS, NULL); // 启用所有鼠标事件
// curs_set(0); // 隐藏光标
//
// draw_login_window(); // 绘制登录窗口
//
// char username[MAX_INPUT_LEN + 1] = "";
// char password[MAX_INPUT_LEN + 1] = "";
// int ch;
// MEVENT event;
//
// while (1) {
// ch = getch(); // 获取输入
//
// if (ch == KEY_MOUSE) { // 检查是否是鼠标事件
// if (nc_getmouse(&event) == OK) {
// handle_mouse_click(event); // 处理鼠标点击事件
// }
// }
//
// // 处理键盘输入(用户输入用户名和密码)
// if (ch == 10) { // 回车键
// printw("\n登录成功\n");
// break;
// }
//
// // 如果是在用户名或密码框中输入
// if (ch == KEY_BACKSPACE || ch == 127) { // 退格键
// // 删除输入框中的最后一个字符
// if (strlen(username) > 0) {
// username[strlen(username) - 1] = '\0';
// clear_input_area(USERNAME_X, USERNAME_Y, MAX_INPUT_LEN);
// mvprintw(USERNAME_Y, USERNAME_X, "%s", username);
// }
// } else if (isprint(ch)) { // 只接受可打印的字符
// if (strlen(username) < MAX_INPUT_LEN) {
// username[strlen(username)] = ch;
// username[strlen(username) + 1] = '\0';
// mvprintw(USERNAME_Y, USERNAME_X, "%s", username);
// }
// }
//
// refresh();
// }
//
// endwin(); // 结束 ncurses 模式
// return;
//}
// 用于绘制按钮的函数