SamsaraDao/static/js/getIsUser.js

143 lines
4.7 KiB
JavaScript
Raw Normal View History

2025-04-19 15:31:22 +08:00
let userInfo = window.localStorage.getItem('userInfo');
userInfo = JSON.parse(userInfo);
if (userInfo) {
let parentEle = document.getElementById('userLogo');
let newChild = document.createElement('img');
document.getElementById('isLogin').style.display = 'none';
document.getElementById('isLogin1').style.display = 'none';
newChild.src = userInfo.avatar;
parentEle.appendChild(newChild);
let arr = userInfo.nickname;
if(arr.length <= 8){
document.getElementById('userName').innerText = arr;
}else{
document.getElementById('userName').innerText = arr.substring(0, 8) + "...";
}
}else{
document.getElementById('isshouother').style.display = 'none';
}
2025-05-07 13:42:14 +08:00
let nowPath = window.location.pathname;
console.log(nowPath,'当前路径');
2025-05-09 18:28:03 +08:00
if(!userInfo && !nowPath.includes('login.html') && !nowPath.includes('signup.html') && nowPath.includes('trading.html')){
2025-04-19 15:31:22 +08:00
let language = window.localStorage.getItem("languageNow");
let message = language == 'zh' ? '請先登入或註冊' : 'Please log in or register first';
const html ="<div id='customizeBox' class='customize-message-box'><p>" + message + "</p></div>"
document.body.innerHTML += html;
const item = document.getElementById('customizeBox');
item.classList.add('show')
setTimeout(()=>{
window.location.href = "login.html";
item.classList.remove('show')
},2000)
}
baseUrl = "https://demo.samsaradao.com/"
if(window.location.host === 'www.samsaradao.com'){
baseUrl = "https://www.samsaradao.com/"
}
2025-05-07 13:42:14 +08:00
2025-05-09 18:28:03 +08:00
let nowToken = "";
if(window.localStorage.getItem("userInfo")){
nowToken = JSON.parse(window.localStorage.getItem("userInfo")).token
}
2025-05-07 13:42:14 +08:00
function publikRequesFunction(api,type,params){
return new Promise((resolve, reject) => {
$.ajax({
url:baseUrl + api,
type: type,
dataType: "json",
headers:{
2025-05-09 18:28:03 +08:00
token: nowToken,
2025-05-07 13:42:14 +08:00
},
data: params,
success(res) {
resolve(res)
},
error(rej){
console.log(rej,'错误请求');
if(rej.status === 401){
2025-05-07 18:19:14 +08:00
let language = window.localStorage.getItem("languageNow");
let message = language == 'zh' ? '請重新登入' : 'Please log in again';
const html ="<div id='customizeBox' class='customize-message-box'><p>"+message+"</p></div>"
document.body.innerHTML += html
const item = document.getElementById('customizeBox');
item.classList.add('show')
setTimeout(()=>{
const item = document.getElementById('customizeBox')
item.classList.remove('show')
},2000)
2025-05-07 13:42:14 +08:00
window.localStorage.clear();
window.location.href = 'login.html'
}else{
reject("failed")
}
}
});
})
}
2025-04-19 15:31:22 +08:00
function signOutPage(){
$.ajax({
url:baseUrl+"api/user/logout",
type: "post",
dataType: "json",
data:{
token:JSON.parse(window.localStorage.getItem("userInfo")).token,
},
success(res) {
if(res.code === 1){
console.log(res,'aaaasss');
window.localStorage.removeItem("userInfo");
setTimeout(()=>{
window.location.href = "index.html";
},1000)
}
},
error: function(rej){
alert("failed")
}
});
}
customClick(false)
document.addEventListener('DOMContentLoaded', () => {
let tradingcenter = document.getElementById('isOpenTrading');
if (tradingcenter) {
tradingcenter.addEventListener('click', ()=>{
customClick(true)
});
}
let links = document.querySelectorAll('#isshouother a');
links.forEach((link , index) => {
if(index > 2 && index < (links.length /2 - 1)){
link.addEventListener('click', function(event) {
event.preventDefault();
customClick(true)
});
}
});
});
function customClick(isShow){
const item = document.getElementById('customizeBox')
let language = window.localStorage.getItem("languageNow");
let message = language == 'zh' ? '功能開放中' : 'Function open';
if(!isShow){
const html ="<div id='customizeBox' class='customize-message-box'><p>"+message+"</p></div>"
document.body.innerHTML += html
}else{
item.classList.add('show')
}
setTimeout(()=>{
const item = document.getElementById('customizeBox')
item.classList.remove('show')
},3000)
}