thinkjs之頁(yè)面跳轉(zhuǎn)同步異步操作
對(duì)于剛?cè)胧謙hinkjs項(xiàng)目的新手來(lái)說(shuō),時(shí)常會(huì)犯的一個(gè)錯(cuò)誤就是“混用”各種代碼邏輯,比如:我們經(jīng)常在做后臺(tái)管理系統(tǒng)的時(shí)候用到的登錄框,

其實(shí)它原本是有一個(gè)路由專門存放自己的代碼邏輯,而在點(diǎn)擊提交按鈕的時(shí)候,要達(dá)到的效果便是賬號(hào)密碼正確的時(shí)候,正常跳轉(zhuǎn)頁(yè)面,而錯(cuò)誤的時(shí)候給出提示;為了發(fā)現(xiàn)問(wèn)題,就先把源代碼貼出來(lái)吧:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用戶登錄</title>
</head>
<style>
*{ margin:0px; padding:0px; list-style:none;}
body,html{ height:100%;font:12px/1.5 \5FAE\8F6F\96C5\9ED1,tahoma,arial,sans-serif;}
html{ background:url(/static/img/bg.gif) repeat-x;}
body{ background:url(/static/img/ftbg.png) 0 bottom repeat-x;}
.main{ background:url(/static/img/mbg.png) no-repeat center bottom;position: absolute;width:100%;height:500px;top:50%;
margin-left:0;margin-top:-290px; z-index:99}
.loginbox{ width:410px; height:375px;background:url(/static/img/borderbg.png); position: absolute; left:50%; top:50%; margin-left:-200px; margin-top:-200px; border-radius:8px;-moz-border-radius: 8px; -webkit-border-radius:8px; z-index:999;}
.loginbg{ width:310px;padding:40px; margin:0 auto; margin-top:10px; background-color:#fff; border-radius:8px;-moz-border-radius: 8px; -webkit-border-radius:8px;}
.loginbox h3{ font-size:18px; font-weight:normal; color:#333; padding-bottom:15px; text-align:center;}
.loginbox input{ width:260px; height:46px; border:1px solid #dbdbdb; padding:0 5px; font-size:14px; color:#666;border-radius:5px rgba(0,0,0,0.5);-moz-border-radius: 5px; -webkit-border-radius:5px; padding-left:45px; line-height:46px;}
.loginbox ul li{ padding:15px 0; position:relative;}
.loginbox .user{ background:url(/static/img/lgicon.png) 0 0 no-repeat; display:inline-block; position:absolute; width:19px; height:20px; left:15px; top:27px;}
.loginbox .pwd{ background:url(/static/img/lgicon.png) 0 bottom no-repeat; display:inline-block; position:absolute; width:19px; height:22px; left:15px; top:27px;}
.loginbox input.lgbtn{ width:312px; background-color:#f86c6b; border:0px; color:#fff; font-size:18px; font-family:\5FAE\8F6F\96C5\9ED1;line-height:46px; text-align:center; cursor:pointer; text-indent:0px; padding:0px;}
.main h2{ margin-top:-40px; font-size:30px; text-align:center; color:#fff; font-weight:normal;}
.footer{ position:fixed; z-index:9; bottom:0px; text-align:center; color:#666; width:100%; padding-bottom:20px; font-size:14px;}
</style>
<body>
<div class="main">
<h2>用戶登錄</h2>
<div class="loginbox">
<div class="loginbg">
<h3>用戶登錄</h3>
<form id="fm" action="/index/login" method="post">
<ul>
<li><span class="user" ></span><input type="text" name="name" required="true" value=""></li>
<li><span class="pwd" ></span><input type="password" name="pwd" required="true" value=""><span style="color: red;position: absolute;top: 70px;left: 10px" id="msg">{{msg}}</span></li>
<li><input type="submit" value="登錄" class="lgbtn"/></li>
</ul>
</form>
</div>
</div>
</div>
<!--<div class="footer">陜西鋼谷電子商務(wù)股份有限公司 版權(quán)所有2016</div>-->
</body>
</html>
頁(yè)面效果:

而正常的后臺(tái)處理邏輯也便是:
'use strict';
/**
* author: xxx
* create: 2017-02-05
* update: 2017-02-05
* desc: 登錄controller
*/
import Base from './base.js';
import cf from '../../common/config/config';
export default class extends Base {
indexAction() {//登錄頁(yè)面
//auto render template file index_index.html
return this.display();
};
/**
* 登錄方法
* @returns {*}
*/
async loginAction() {
let result = await this.model('admin').where({name: this.post().name, pwd: think.md5(this.post().pwd)}).select();
if (result&&result.length > 0) {
if(result[0].state==1){
let adminrole= await this.model('adminroles').where({id:result[0].rids}).select();
if(adminrole&&adminrole[0].state!=1){
this.assign('msg', '該用戶的身份已經(jīng)被禁用或刪除,請(qǐng)聯(lián)系管理員!');
return this.display("index");//錯(cuò)誤信息渲染至登錄頁(yè)面
}else{
let acresult = await this.model('adminaction').where({rid: result[0].rids}).field('action').select();//查詢?cè)摍?quán)限id的集合
result[0]['actions'] = acresult;//把集合賦予session
await this.session(cf.sessionKey, result[0]);
await this.model('adminlog').add({uid: result[0].id, createtime: new Date().getTime() / 1000, ip: this.ip()})//添加登錄日志
return this.redirect('/main');//跳轉(zhuǎn)main路由(主要是修改頁(yè)面顯示url)
}
}else{
this.assign('msg', '該用戶已經(jīng)被停用或刪除,請(qǐng)聯(lián)系管理員!');
return this.display("index");//錯(cuò)誤信息渲染至登錄頁(yè)面
}
} else {
this.assign('msg', '用戶名或密碼錯(cuò)誤!');
return this.display("index");//錯(cuò)誤信息渲染至登錄頁(yè)面
}
}
/**
* 退出方法
* @returns {promise|*|void|PreventPromise}
*/
async loginoutAction() {
await this.session();//清除session
return this.redirect('/');//跳轉(zhuǎn)登錄頁(yè)面
}
}
原本這樣處理下來(lái)的代碼算是最簡(jiǎn)潔的方式。但是對(duì)于新手來(lái)說(shuō),因?yàn)樵趀asyui官網(wǎng)上看到的demo比較多,于是在不太清楚各個(gè)之間的區(qū)別時(shí),就容易出現(xiàn)“互相冗雜”在一起的現(xiàn)象,于是就出現(xiàn)了這樣的情況:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用戶登錄</title>
<style>
.form-group {
margin-bottom: 30px;
}
.form-group > label {
float: left;
width: 80px;
}
.form-group > input {
float: right;
}
h1 {
text-align: center;
margin-bottom: 50px;
}
</style>
<link rel="stylesheet" href="/static/js/jquery-easyui/themes/default/easyui.css">
<link rel="stylesheet" href="/static/js/jquery-easyui/themes/icon.css">
<!--easyui js-->
<script src="/static/js/jquery-easyui/jquery.min.js"></script>
<script src="/static/js/jquery-easyui/jquery.easyui.min.js"></script>
<script src="/static/js/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<div>
<div style="width:400px;height:400px;margin: 200px auto ;border: 2px solid #9cc8f7;border-radius: 10px;padding:20px 0 0 10px"
id="login1" buttons="#dlg-buttons">
<h1>用戶登錄</h1>
<form id="ff1" method="post" url="/index/login">
<div class="form-group">
<label>用戶名:</label>
<input class="easyui-textbox" name="name" style="width:300px" data-options="required:true">
</div>
<div class="form-group">
<label>密碼:</label>
<input class="easyui-textbox" type="password" name="pwd" style="width:300px"
data-options="required:true">
</div>
</form>
<div id="dlg-buttons">
<!--<a href="javascript:submitForm()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">提交</a>-->
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" iconCls="icon-ok"
plain="true">提交</a>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" iconCls="icon-cancel"
plain="true">取消</a>
</div>
<!--<b id="msg" style="display: none;"></b>-->
{{msg}}
</div>
</div>
<script>
function submitForm() {
jQuery.ajax({
url: "/index/login",
async: false,
method:"POST",
data:{
name:"123",
pwd:"123"
}
});
}
function clearForm() {
jQuery('#ff1').form('clear');
}
</script>
</body>
</html>
后臺(tái)的處理邏輯:
'use strict';
import Base from './base.js';
export default class extends Base {
/**
* index action
* @return {Promise} []
*/
indexAction(){
//auto render template file index_index.html
return this.display();
}
async loginAction(){
// return this.redirect('/login');
console.log(this.post());
let name=this.post().name;
let pwd=this.post().pwd;
let model=this.model('user');
let data = await model.where({name:name,pwd:pwd}).find();
if(!think.isEmpty(data)){
console.log("http://////////");
return this.redirect('/login888');
// return this.json({'succ':true});
}else{
this.assign('msg','賬號(hào)或者密碼錯(cuò)誤!');
return this.display('index');
// return this.json({'succ':false,'msg':'賬號(hào)或者密碼錯(cuò)誤!'});
}
}
}
而這樣處理的結(jié)果卻是:

出現(xiàn)了瀏覽器自身報(bào)錯(cuò):此方法已被棄用。新手因?yàn)榻佑|thinkjs的并不是很多,所以時(shí)常會(huì)混淆其中,以為這樣很正確,其實(shí)在瀏覽器自身的js運(yùn)行機(jī)制中,該方法是行不通的。因此建議初接觸thinkjs的小伙伴們,在寫頁(yè)面跳轉(zhuǎn)的邏輯,比如用到redirect或assign渲染時(shí),前臺(tái)就不要使用ajax提交;而后臺(tái)用json返回時(shí),就不要使用sumbit()提交。而這種非常隱蔽的問(wèn)題,一般初學(xué)者也不會(huì)意識(shí)到問(wèn)題存在哪里,因此還是需要小伙伴們多多看看相關(guān)的教程,增長(zhǎng)自己的經(jīng)驗(yàn)。
相關(guān)文章
[JS源碼]超長(zhǎng)文章自動(dòng)分頁(yè)(客戶端版)
[JS源碼]超長(zhǎng)文章自動(dòng)分頁(yè)(客戶端版)...2007-01-01
javascript中使用正則計(jì)算中文長(zhǎng)度的例子
這篇文章主要介紹了javascript中使用正則計(jì)算中文長(zhǎng)度的例子,需要的朋友可以參考下2014-04-04
javascript瀏覽器窗口之間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了javascript瀏覽器窗口之間傳遞數(shù)據(jù)的方法,實(shí)例分析了父窗口與子窗口之間傳遞參數(shù)的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01
詳解JavaScript如何實(shí)現(xiàn)四種常用排序
這篇文章主要為大家介紹了如何利用JavaScript實(shí)現(xiàn)四個(gè)常用的排序:插入排序、交換排序、選擇排序和歸并排序,文中利用動(dòng)圖詳細(xì)介紹了實(shí)現(xiàn)過(guò)程,需要的可以參考一下2022-02-02
微信小程序tab切換可滑動(dòng)切換導(dǎo)航欄跟隨滾動(dòng)實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序tab切換可滑動(dòng)切換導(dǎo)航欄跟隨滾動(dòng)實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
使用JavaScript實(shí)現(xiàn)一個(gè)拖拽縮放效果
這篇文章主要介紹了如何使用JS實(shí)現(xiàn)一個(gè)這樣的拖拽縮放效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
基于JS實(shí)現(xiàn)限時(shí)搶購(gòu)倒計(jì)時(shí)間表代碼
本文給大家分享一段簡(jiǎn)單的代碼基于js實(shí)現(xiàn)限時(shí)搶購(gòu)倒計(jì)時(shí)間表功能,非常不錯(cuò),代碼簡(jiǎn)單易懂,需要的的朋友參考下吧2017-05-05
微信小程序使用for循環(huán)動(dòng)態(tài)渲染頁(yè)面操作示例
這篇文章主要介紹了微信小程序使用for循環(huán)動(dòng)態(tài)渲染頁(yè)面操作,結(jié)合實(shí)例形式分析了微信小程序使用for語(yǔ)句獲取data數(shù)據(jù)渲染頁(yè)面相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
深入了解Hybrid App技術(shù)的相關(guān)知識(shí)
這篇文章主要介紹了深入了解Hybrid App技術(shù)的相關(guān)知識(shí),Hybrid App(混合模式移動(dòng)應(yīng)用)是指介于web-app、native-app這兩者之間的app,兼具" Native App良好用戶交互體驗(yàn)的優(yōu)勢(shì) "和" Web App跨平臺(tái)開(kāi)發(fā)的優(yōu)勢(shì) ",需要的朋友可以參考下2019-07-07

