重构js
This commit is contained in:
+205
-199
@@ -17,13 +17,15 @@
|
|||||||
<script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js"></script> <!-- 图标相关-->
|
<script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js"></script> <!-- 图标相关-->
|
||||||
<script src="/static/js/mind/c_m_m.js"></script>
|
<script src="/static/js/mind/c_m_m.js"></script>
|
||||||
<script>
|
<script>
|
||||||
const qr_websocket = {
|
|
||||||
websocket: null,
|
|
||||||
uuid: (new Date().getTime().toString(16) + Math.random().toString(16).substr(2)).substr(2, 16)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const DefaultView = function () {
|
||||||
|
|
||||||
|
}
|
||||||
|
DefaultView.prototype = {
|
||||||
|
websocket: null,
|
||||||
|
uuid: (new Date().getTime().toString(16) + Math.random().toString(16).substr(2)).substr(2, 16),
|
||||||
/**界面初始化*/
|
/**界面初始化*/
|
||||||
function view_init() {
|
view_init: function () {
|
||||||
const uid = $('#uid').html()
|
const uid = $('#uid').html()
|
||||||
// 未登录
|
// 未登录
|
||||||
if (!uid || isNaN(uid)) {
|
if (!uid || isNaN(uid)) {
|
||||||
@@ -33,195 +35,12 @@
|
|||||||
$('#login_register').hide()
|
$('#login_register').hide()
|
||||||
$('#logout_a').show()
|
$('#logout_a').show()
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
get_uu_id: function () {
|
||||||
function get_uu_id() {
|
|
||||||
return (new Date().getTime().toString(16) + Math.random().toString(16).substr(2)).substr(2, 16)
|
return (new Date().getTime().toString(16) + Math.random().toString(16).substr(2)).substr(2, 16)
|
||||||
}
|
|
||||||
|
|
||||||
$(function () {
|
|
||||||
/**界面初始化*/
|
|
||||||
view_init()
|
|
||||||
|
|
||||||
// 监听modal窗口事件
|
|
||||||
$('#exampleModal').on('show.bs.modal', function (event) {
|
|
||||||
// console.log(qr_websocket)
|
|
||||||
// console.log('打开');
|
|
||||||
const ws = new WebSocket(`ws://${window.location.host}/ws/qr/?${qr_websocket.uuid}`)
|
|
||||||
ws.onmessage = function (e) {
|
|
||||||
console.log('接收到websocket消息:' + e.data)
|
|
||||||
}
|
|
||||||
qr_websocket.websocket = ws
|
|
||||||
});
|
|
||||||
$('#exampleModal').on('hidden.bs.modal', function (event) {
|
|
||||||
// console.log('关闭');
|
|
||||||
qr_websocket.websocket.close()
|
|
||||||
});
|
|
||||||
|
|
||||||
/**点击 扫码登录 */
|
|
||||||
$('#s_login_a').click(function () {
|
|
||||||
$('.modal-body').hide();
|
|
||||||
$('.qr_login_group').show();
|
|
||||||
set_modal_title('登录');
|
|
||||||
})
|
|
||||||
|
|
||||||
// 点击注册控件
|
|
||||||
$('#register_a').click(function () {
|
|
||||||
$('.modal-body').hide();
|
|
||||||
$('.register_group').show();
|
|
||||||
set_modal_title('注册');
|
|
||||||
})
|
|
||||||
|
|
||||||
// 用户名密码登录
|
|
||||||
$('#p_login_a').click(function () {
|
|
||||||
$('.modal-body').hide();
|
|
||||||
$('.pw_login_group').show();
|
|
||||||
set_modal_title('登录');
|
|
||||||
})
|
|
||||||
|
|
||||||
/** 设置窗口名称 */
|
|
||||||
function set_modal_title(title) {
|
|
||||||
$('#exampleModal .modal-title').html(title);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 注册按钮 */
|
|
||||||
$('#register_button').click(function () {
|
|
||||||
const userName = $('#userName').val();
|
|
||||||
const inputPassword = $('#inputPassword').val();
|
|
||||||
const inputPassword_ = $('#inputPassword_').val();
|
|
||||||
|
|
||||||
if (!userName) {
|
|
||||||
reg_alert('请输入用户名')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!inputPassword) {
|
|
||||||
reg_alert('请输入密码')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 校验输入是否一致
|
|
||||||
const validate = validate_pw_eq(this, inputPassword, inputPassword_)
|
|
||||||
if (validate) {
|
|
||||||
$.ajax({
|
|
||||||
type: "post",
|
|
||||||
url: "register",
|
|
||||||
data: {userName: userName, inputPassword: inputPassword},
|
|
||||||
dataType: "json",
|
|
||||||
success: function (data) {
|
|
||||||
// 注册成功
|
|
||||||
if (data.code === 0) {
|
|
||||||
clear_reg_info()
|
|
||||||
reg_alert(data.msg, 'alert-primary')
|
|
||||||
setTimeout(function () {
|
|
||||||
$('#p_login_a').click()
|
|
||||||
}, 1500)
|
|
||||||
} else {
|
|
||||||
// 注册失败
|
|
||||||
reg_alert(data.msg)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
async: false
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// validate end
|
|
||||||
});
|
|
||||||
|
|
||||||
/** 清除注册时输入内容 */
|
|
||||||
function clear_reg_info() {
|
|
||||||
$('#userName').val('');
|
|
||||||
$('#inputPassword').val('');
|
|
||||||
$('#inputPassword_').val('');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**校验密码是否输入一致*/
|
|
||||||
function validate_pw_eq(obj, inputPassword, inputPassword_) {
|
|
||||||
if (inputPassword !== inputPassword_) {
|
|
||||||
reg_alert('两次密码输入不一样,请确认!')
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 注册提示 */
|
|
||||||
function reg_alert(text, class_str) {
|
|
||||||
common_alert_(text, class_str)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 登录提示 */
|
|
||||||
function login_alert(text, class_str) {
|
|
||||||
common_alert_(text, class_str)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**登录按钮*/
|
|
||||||
$('#login_button').click(function () {
|
|
||||||
const userName_login = $('#userName_login').val();
|
|
||||||
const inputPassword_login = $('#inputPassword_login').val();
|
|
||||||
if (!userName_login || !inputPassword_login) {
|
|
||||||
login_alert('请输入用户名和密码')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
$.ajax({
|
|
||||||
type: "post",
|
|
||||||
url: "login",
|
|
||||||
headers: {"X-CSRFToken": $.cookie("csrftoken")},
|
|
||||||
data: {username: userName_login, password: inputPassword_login},
|
|
||||||
dataType: "json",
|
|
||||||
success: function (data) {
|
|
||||||
// 登录成功
|
|
||||||
if (data.code === 0) {
|
|
||||||
login_alert(data.msg, 'alert-primary')
|
|
||||||
$('#exampleModal').modal('hide')
|
|
||||||
// $('#login_register').hide()
|
|
||||||
// fetch_mind()
|
|
||||||
setTimeout(function () {
|
|
||||||
location.reload();
|
|
||||||
}, 500)
|
|
||||||
} else {
|
|
||||||
// 登录失败
|
|
||||||
login_alert(data.msg)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async: false
|
|
||||||
});
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
/**新建导图*/
|
|
||||||
$('#new_mind_btn').click(function () {
|
|
||||||
const mind_name = $('#mindName').val()
|
|
||||||
if (!mind_name) {
|
|
||||||
common_alert_('请输入导图名称')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const post_data = {'mind_name': mind_name}
|
|
||||||
$.ajax({
|
|
||||||
type: "post",
|
|
||||||
url: "create",
|
|
||||||
headers: {"X-CSRFToken": $.cookie("csrftoken")},
|
|
||||||
data: post_data,
|
|
||||||
dataType: "json",
|
|
||||||
success: function (resp) {
|
|
||||||
fetch_mind()
|
|
||||||
$('#newMindModal').modal('hide')
|
|
||||||
// 新页面打开
|
|
||||||
setTimeout(function () {
|
|
||||||
const id = resp.data
|
|
||||||
window.open('detail?id=' + id)
|
|
||||||
}, 500)
|
|
||||||
},
|
|
||||||
async: false
|
|
||||||
});
|
|
||||||
})
|
|
||||||
|
|
||||||
/**如果已经登录则查询列表*/
|
|
||||||
fetch_mind()
|
|
||||||
// js end
|
|
||||||
})
|
|
||||||
|
|
||||||
/**注销登录*/
|
/**注销登录*/
|
||||||
function logout_() {
|
logout_: function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: "post",
|
type: "post",
|
||||||
url: "logout",
|
url: "logout",
|
||||||
@@ -236,10 +55,10 @@
|
|||||||
},
|
},
|
||||||
async: false
|
async: false
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
|
|
||||||
/**获得导图*/
|
/**获得导图*/
|
||||||
function fetch_mind(page, page_size) {
|
fetch_mind: function (page, page_size) {
|
||||||
|
const me = this
|
||||||
if (!page) {
|
if (!page) {
|
||||||
page = 1
|
page = 1
|
||||||
}
|
}
|
||||||
@@ -263,7 +82,7 @@
|
|||||||
let idx = 0
|
let idx = 0
|
||||||
let row = $(`<div class="row"></row>`)
|
let row = $(`<div class="row"></row>`)
|
||||||
results.forEach(function (item) {
|
results.forEach(function (item) {
|
||||||
const uuid = get_uu_id()
|
const uuid = me.get_uu_id()
|
||||||
const content = `
|
const content = `
|
||||||
<div class="card col-3" style="margin: 6px;" id='card_${uuid}'>
|
<div class="card col-3" style="margin: 6px;" id='card_${uuid}'>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
@@ -293,10 +112,9 @@
|
|||||||
},
|
},
|
||||||
async: false
|
async: false
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
|
|
||||||
/**删除导图*/
|
/**删除导图*/
|
||||||
function del_mind(mind_id, card_id) {
|
del_mind: function (mind_id, card_id) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: "delete",
|
type: "delete",
|
||||||
url: "delete",
|
url: "delete",
|
||||||
@@ -313,7 +131,195 @@
|
|||||||
},
|
},
|
||||||
async: false
|
async: false
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
/** 清除注册时输入内容 */
|
||||||
|
clear_reg_info: function () {
|
||||||
|
$('#userName').val('');
|
||||||
|
$('#inputPassword').val('');
|
||||||
|
$('#inputPassword_').val('');
|
||||||
|
},
|
||||||
|
/**校验密码是否输入一致*/
|
||||||
|
validate_pw_eq: function (obj, inputPassword, inputPassword_) {
|
||||||
|
if (inputPassword !== inputPassword_) {
|
||||||
|
reg_alert('两次密码输入不一样,请确认!')
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
/** 注册提示 */
|
||||||
|
reg_alert: function (text, class_str) {
|
||||||
|
common_alert_(text, class_str)
|
||||||
|
},
|
||||||
|
/** 登录提示 */
|
||||||
|
login_alert: function (text, class_str) {
|
||||||
|
common_alert_(text, class_str)
|
||||||
|
},
|
||||||
|
/** 设置窗口名称 */
|
||||||
|
set_modal_title: function (title) {
|
||||||
|
$('#exampleModal .modal-title').html(title);
|
||||||
|
},
|
||||||
|
init_pagination: function (count, page_size) {
|
||||||
|
|
||||||
|
},
|
||||||
|
jq_listener_init: function () {
|
||||||
|
const me = this
|
||||||
|
/**监听modal窗口事件*/
|
||||||
|
$('#exampleModal').on('show.bs.modal', function (event) {
|
||||||
|
const ws = new WebSocket(`ws://${window.location.host}/ws/qr/?${me.uuid}`)
|
||||||
|
ws.onmessage = function (e) {
|
||||||
|
console.log('接收到websocket消息:' + e.data)
|
||||||
|
}
|
||||||
|
me.websocket = ws
|
||||||
|
});
|
||||||
|
$('#exampleModal').on('hidden.bs.modal', function (event) {
|
||||||
|
// console.log('关闭');
|
||||||
|
me.websocket.close()
|
||||||
|
});
|
||||||
|
|
||||||
|
/**点击 扫码登录 */
|
||||||
|
$('#s_login_a').click(function () {
|
||||||
|
$('.modal-body').hide();
|
||||||
|
$('.qr_login_group').show();
|
||||||
|
me.set_modal_title('登录');
|
||||||
|
})
|
||||||
|
|
||||||
|
// 点击注册控件
|
||||||
|
$('#register_a').click(function () {
|
||||||
|
$('.modal-body').hide();
|
||||||
|
$('.register_group').show();
|
||||||
|
me.set_modal_title('注册');
|
||||||
|
})
|
||||||
|
|
||||||
|
// 用户名密码登录
|
||||||
|
$('#p_login_a').click(function () {
|
||||||
|
$('.modal-body').hide();
|
||||||
|
$('.pw_login_group').show();
|
||||||
|
me.set_modal_title('登录');
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
/** 注册按钮 */
|
||||||
|
$('#register_button').click(function () {
|
||||||
|
const userName = $('#userName').val();
|
||||||
|
const inputPassword = $('#inputPassword').val();
|
||||||
|
const inputPassword_ = $('#inputPassword_').val();
|
||||||
|
|
||||||
|
if (!userName) {
|
||||||
|
me.reg_alert('请输入用户名')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inputPassword) {
|
||||||
|
me.reg_alert('请输入密码')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验输入是否一致
|
||||||
|
const validate = me.validate_pw_eq(this, inputPassword, inputPassword_)
|
||||||
|
if (validate) {
|
||||||
|
$.ajax({
|
||||||
|
type: "post",
|
||||||
|
url: "register",
|
||||||
|
data: {userName: userName, inputPassword: inputPassword},
|
||||||
|
dataType: "json",
|
||||||
|
success: function (data) {
|
||||||
|
// 注册成功
|
||||||
|
if (data.code === 0) {
|
||||||
|
me.clear_reg_info()
|
||||||
|
me.reg_alert(data.msg, 'alert-primary')
|
||||||
|
setTimeout(function () {
|
||||||
|
$('#p_login_a').click()
|
||||||
|
}, 1500)
|
||||||
|
} else {
|
||||||
|
// 注册失败
|
||||||
|
me.reg_alert(data.msg)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// validate end
|
||||||
|
});
|
||||||
|
|
||||||
|
/**登录按钮*/
|
||||||
|
$('#login_button').click(function () {
|
||||||
|
const userName_login = $('#userName_login').val();
|
||||||
|
const inputPassword_login = $('#inputPassword_login').val();
|
||||||
|
if (!userName_login || !inputPassword_login) {
|
||||||
|
me.login_alert('请输入用户名和密码')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
$.ajax({
|
||||||
|
type: "post",
|
||||||
|
url: "login",
|
||||||
|
headers: {"X-CSRFToken": $.cookie("csrftoken")},
|
||||||
|
data: {username: userName_login, password: inputPassword_login},
|
||||||
|
dataType: "json",
|
||||||
|
success: function (data) {
|
||||||
|
// 登录成功
|
||||||
|
if (data.code === 0) {
|
||||||
|
me.login_alert(data.msg, 'alert-primary')
|
||||||
|
$('#exampleModal').modal('hide')
|
||||||
|
setTimeout(function () {
|
||||||
|
location.reload();
|
||||||
|
}, 500)
|
||||||
|
} else {
|
||||||
|
// 登录失败
|
||||||
|
me.login_alert(data.msg)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async: false
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
/**新建导图*/
|
||||||
|
$('#new_mind_btn').click(function () {
|
||||||
|
debugger
|
||||||
|
const mind_name = $('#mindName').val()
|
||||||
|
if (!mind_name) {
|
||||||
|
common_alert_('请输入导图名称')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const post_data = {'mind_name': mind_name}
|
||||||
|
$.ajax({
|
||||||
|
type: "post",
|
||||||
|
url: "create",
|
||||||
|
headers: {"X-CSRFToken": $.cookie("csrftoken")},
|
||||||
|
data: post_data,
|
||||||
|
dataType: "json",
|
||||||
|
success: function (resp) {
|
||||||
|
if (resp.code === 0) {
|
||||||
|
me.fetch_mind()
|
||||||
|
$('#newMindModal').modal('hide')
|
||||||
|
// 新页面打开
|
||||||
|
setTimeout(function () {
|
||||||
|
const id = resp.data
|
||||||
|
window.open('detail?id=' + id)
|
||||||
|
}, 500)
|
||||||
|
} else {
|
||||||
|
common_alert_(resp.msg)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async: false
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// prototype end
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$(function () {
|
||||||
|
const view = new DefaultView()
|
||||||
|
/**初始化*/
|
||||||
|
view.view_init()
|
||||||
|
view.jq_listener_init()
|
||||||
|
|
||||||
|
/**如果已经登录则查询列表*/
|
||||||
|
view.fetch_mind()
|
||||||
|
window.mindView = view
|
||||||
|
// js end
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
<!--script end-->
|
<!--script end-->
|
||||||
</head>
|
</head>
|
||||||
@@ -325,7 +331,7 @@
|
|||||||
<ul class="navbar-nav px-3">
|
<ul class="navbar-nav px-3">
|
||||||
<li class="nav-item text-nowrap">
|
<li class="nav-item text-nowrap">
|
||||||
<a id="login_register" class="nav-link" href="#" data-toggle="modal" data-target="#exampleModal">登录/注册</a>
|
<a id="login_register" class="nav-link" href="#" data-toggle="modal" data-target="#exampleModal">登录/注册</a>
|
||||||
<a id="logout_a" class="nav-link" href="javascript:void(0)" onclick="logout_()" style="display: none;">注销</a>
|
<a id="logout_a" class="nav-link" href="javascript:void(0)" onclick="mindView.logout_()" style="display: none;">注销</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
|||||||
Reference in New Issue
Block a user