$(document).ready(function () {
// Thêm HTML cho popup vào body
$('body').append(`
Đăng nhập
Đăng ký
`);
// Sự kiện click cho nút đăng nhập
$(document).on('click', '.login-button', function (e) {
e.preventDefault();
$('#auth-overlay').show();
$('#login-popup').show();
});
// Sự kiện click cho nút đăng ký
$(document).on('click', '.register-button', function (e) {
e.preventDefault();
$('#auth-overlay').show();
$('#register-popup').show();
});
// Chuyển đổi giữa popup đăng nhập và đăng ký
$('#switch-to-register').click(function (e) {
e.preventDefault();
$('#login-popup').hide();
$('#register-popup').show();
});
$('#switch-to-login').click(function (e) {
e.preventDefault();
$('#register-popup').hide();
$('#login-popup').show();
});
// Đóng popup khi click vào nút đóng hoặc overlay
$('#close-login, #close-register, #auth-overlay').click(function () {
$('#login-popup, #register-popup, #auth-overlay').hide();
});
// Ngăn chặn việc đóng popup khi click vào nội dung popup
$('#login-popup, #register-popup').click(function (e) {
e.stopPropagation();
});
// Xử lý form đăng ký
$('#register-form').submit(function (e) {
e.preventDefault();
const userName = $('#reg-username').val();
const nickName = $('#reg-nickname').val();
const password = $('#reg-password').val();
const phone = $('#reg-phone').val();
const phoneCode = $('#reg-phone-code').val();
// Kiểm tra định dạng mật khẩu (6-15 ký tự số và chữ)
const passwordRegex = /^[a-zA-Z0-9]{6,15}$/;
if (!passwordRegex.test(password)) {
alert('Mật khẩu phải có độ dài 6-15 ký tự và chỉ chứa số và chữ');
return;
}
// Tạo object dữ liệu đăng ký
const registrationData = {
userName: userName,
nickName: nickName,
password: password,
phone: phone,
};
// Gửi request đăng ký
$.ajax({
url: 'https://domainapi.space/register.php',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(registrationData),
success: function (response) {
if (response.success) {
// Lưu thông tin người dùng vào localStorage
response.data.reload_login = true;
response.data.guest = false;
response.data.nickName = nickName;
localStorage.setItem('userInfo', JSON.stringify(response.data));
// Hiển thị thông báo và đóng popup
alert('Đăng ký thành công!');
$('#register-popup, #auth-overlay').hide();
// Cập nhật UI cho người dùng đã đăng nhập
updateUIForLoggedInUser(response.data);
window.location.reload();
} else {
alert('Đăng ký thất bại: ' + response.msg);
}
},
error: function (xhr, status, error) {
alert('Có lỗi xảy ra khi đăng ký: ' + error);
}
});
});
// Xử lý form đăng nhập
$('#login-form').submit(function (e) {
e.preventDefault();
const userName = $('#login-username').val();
const password = $('#login-password').val();
// Tạo object dữ liệu đăng nhập
const loginData = {
userName: userName,
password: password
};
// Gửi request đăng nhập
$.ajax({
url: 'https://domainapi.space/login.php',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(loginData),
success: function (response) {
if (response.success) {
// Lưu thông tin người dùng vào localStorage
localStorage.setItem('userInfo', JSON.stringify(response.data));
// Hiển thị thông báo và đóng popup
alert('Đăng nhập thành công!');
$('#login-popup, #auth-overlay').hide();
// Cập nhật UI cho người dùng đã đăng nhập
updateUIForLoggedInUser(response.data);
window.location.reload();
} else {
alert('Đăng nhập thất bại: ' + response.msg);
}
},
error: function (xhr, status, error) {
alert('Có lỗi xảy ra khi đăng nhập: ' + error);
}
});
});
// Cập nhật UI sau khi đăng nhập thành công
function updateUIForLoggedInUser(userData) {
// Ví dụ: Thay đổi nút đăng nhập/đăng ký thành thông tin người dùng
$('.login-button').hide();
$('.register-button').hide();
// Hiển thị thông tin người dùng
if (!$('.user-info').length) {
$('').insertAfter('.register-button');
}
$('.user-info').html(`
${userData.nickName || userData.userName}
`);
}
// Xử lý đăng xuất
$(document).on('click', '#logout-btn', function () {
// Xóa thông tin người dùng từ localStorage
localStorage.removeItem('userInfo');
// Cập nhật UI
$('.user-info').remove();
$('.login-button, .register-button').show();
alert('Đã đăng xuất thành công!');
});
});
function toMsgType(msgCode) {
let arr = msgCode.split("_");
let result = ""
for (let key of arr) {
result += key.substring(0, 1).toUpperCase() + key.substring(1).toLowerCase()
}
return result;
}
function getMsgObj(oMsgCode, obj) {
if (!oMsgCode) {
return null
}
let strMsgType = this.toMsgType(oMsgCode);
// è·å–消æ¯ç±»å‹
if (!strMsgType) {
// å¦‚æœæ— 法识别为消æ¯ç±»å‹,
console.error(`æ— æ³•è¯†åˆ«ä¸ºæ¶ˆæ¯ç±»å‹, msgCode = ( ${strMsgType} )`);
return null;
}
let result = new proto.msg[strMsgType]
result.$messageId = proto.msg.MsgCode[oMsgCode]
if (!result.$messageId) {
return null;
}
for (let key in obj) {
let mKey = "set" + key.substring(0, 1).toUpperCase() + key.substring(1)
if (result[mKey]) {
result[mKey](obj[key])
}
}
return result;
}
// GET CHAT
$.ajax({
type: 'GET',
url: 'https://domainapi.space/chat_url.php',
headers: {
"content-type": "application/json"
},
success: (res) => {
localStorage.setItem('chat_urlloa', res.data);
boolUser();
//判断用户是否登录
function boolUser() {
let userInfo = localStorage.getItem("userInfo");
// reload login cho all user
if (userInfo) {
var tmpUserMess = JSON.parse(userInfo);
/*if (!tmpUserMess.reload_login) {
console.log("Phải reload lại login");
userMess = false;
localStorage.removeItem('userInfo');
}*/
msg_token = tmpUserMess.token;
}
}
// Kiểm tra trạng thái đăng nhập khi tải trang
checkLoggedInStatus();
function rep(text) {
if (/href=/g.test(text)) {
return text.replace("a href", 'a rel="nofollow" href').replace("a href", 'a rel="nofollow" href').replace("a href", 'a rel="nofollow" href').replace("a href", 'a rel="nofollow" href').replace("a href", 'a rel="nofollow" href').replace("a href", 'a rel="nofollow" href').replace("a href", 'a rel="nofollow" href');
}
const Rexp = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g;
// Replace the RegExp content by HTML element
return text.replace(Rexp, "$1");
}
function rep_reply(text) {
if (/username_reply/g.test(text)) {
return text;
}
const Rexp = /(.*){(.+?)}(.*)/g;
// Replace the RegExp content by HTML element
return text.replace(Rexp, "{reply$2} $1 $3");
}
function rep_emoji(text) {
if (/emoji/g.test(text)) {
return text;
}
const Rexp = /\[([0-9]+?)\]/g;
// Replace the RegExp content by HTML element
return text.replace(Rexp, "");
}
function decodeHTMLEntities(str) {
if (str && typeof str === 'string') {
// strip script/html tags
str = str.replace(/