thinkjs之页面跳转同步异步操作


Posted in Javascript onFebruary 05, 2017

对于刚入手thinkjs项目的新手来说,时常会犯的一个错误就是“混用”各种代码逻辑,比如:我们经常在做后台管理系统的时候用到的登录框,

thinkjs之页面跳转同步异步操作

其实它原本是有一个路由专门存放自己的代码逻辑,而在点击提交按钮的时候,要达到的效果便是账号密码正确的时候,正常跳转页面,而错误的时候给出提示;为了发现问题,就先把源代码贴出来吧:

<!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">陕西钢谷电子商务股份有限公司 版权所有2016</div>-->
</body>
</html>

页面效果:

thinkjs之页面跳转同步异步操作

而正常的后台处理逻辑也便是:

'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() {//登录页面
 //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', '该用户的身份已经被禁用或删除,请联系管理员!');
   return this.display("index");//错误信息渲染至登录页面
   }else{
   let acresult = await this.model('adminaction').where({rid: result[0].rids}).field('action').select();//查询该权限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');//跳转main路由(主要是修改页面显示url)
   }
  }else{
   this.assign('msg', '该用户已经被停用或删除,请联系管理员!');
   return this.display("index");//错误信息渲染至登录页面
  }
  } else {
  this.assign('msg', '用户名或密码错误!');
  return this.display("index");//错误信息渲染至登录页面
  }
 }
 /**
 * 退出方法
 * @returns {promise|*|void|PreventPromise}
 */
 async loginoutAction() {
  await this.session();//清除session
  return this.redirect('/');//跳转登录页面
 }
}

原本这样处理下来的代码算是最简洁的方式。但是对于新手来说,因为在easyui官网上看到的demo比较多,于是在不太清楚各个之间的区别时,就容易出现“互相冗杂”在一起的现象,于是就出现了这样的情况:

<!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>

后台的处理逻辑:

'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("//////////");
 return this.redirect('/login888');
 // return this.json({'succ':true});
 }else{
 this.assign('msg','账号或者密码错误!');
 return this.display('index');
 // return this.json({'succ':false,'msg':'账号或者密码错误!'});
 }
 }
}

而这样处理的结果却是:

thinkjs之页面跳转同步异步操作

出现了浏览器自身报错:此方法已被弃用。新手因为接触thinkjs的并不是很多,所以时常会混淆其中,以为这样很正确,其实在浏览器自身的js运行机制中,该方法是行不通的。因此建议初接触thinkjs的小伙伴们,在写页面跳转的逻辑,比如用到redirect或assign渲染时,前台就不要使用ajax提交;而后台用json返回时,就不要使用sumbit()提交。而这种非常隐蔽的问题,一般初学者也不会意识到问题存在哪里,因此还是需要小伙伴们多多看看相关的教程,增长自己的经验。

Javascript 相关文章推荐
Mootools 1.2教程(3) 数组使用简介
Sep 14 Javascript
JQuery 技巧和窍门整理(8个)
Apr 22 Javascript
Js数组的操作push,pop,shift,unshift等方法详细介绍
Dec 28 Javascript
Jquery多选框互相内容交换的实例代码
Jul 04 Javascript
点击button获取text内容并改变样式的js实现
Sep 09 Javascript
浅谈javascript alert和confirm的美化
Dec 15 Javascript
jQuery树插件zTree使用方法详解
May 02 jQuery
JavaScript new对象的四个过程实例浅析
Jul 31 Javascript
vue开发环境配置跨域的方法步骤
Jan 16 Javascript
详解如何写出一个利于扩展的vue路由配置
May 16 Javascript
详解vue 中 scoped 样式作用域的规则
Sep 14 Javascript
小程序实现侧滑删除功能
Jun 25 Javascript
js实现5秒倒计时重新发送短信功能
Feb 05 #Javascript
解决微信内置浏览器返回上一页强制刷新问题方法
Feb 05 #Javascript
js仿小米手机上下滑动效果
Feb 05 #Javascript
简单实现js无缝滚动效果
Feb 05 #Javascript
完美的js图片轮换效果
Feb 05 #Javascript
jQuery中ztree 点击文本框弹出下拉框的实例代码
Feb 05 #Javascript
使用JavaScript判断用户输入的是否为正整数(两种方法)
Feb 05 #Javascript
You might like
一个ORACLE分页程序,挺实用的.
2006/10/09 PHP
在VS2008中编译MYSQL5.1.48的方法
2010/07/03 PHP
奉献出一个封装的curl函数 便于调用(抓数据专用)
2013/07/22 PHP
php实现微信企业号支付个人的方法详解
2017/07/26 PHP
jQuery JSON的解析方式分享
2011/04/05 Javascript
Jquery下:nth-child(an+b)的使用注意
2011/05/28 Javascript
laytpl 精致巧妙的JavaScript模板引擎
2014/08/29 Javascript
在HTML代码中使用JavaScript代码的例子
2014/10/16 Javascript
jQuery+AJAX实现网页无刷新上传
2015/02/22 Javascript
jQuery实现Div拖动+键盘控制综合效果的方法
2015/03/10 Javascript
再JavaScript的jQuery库中编写动画效果的指南
2015/08/13 Javascript
JavaScript实现仿新浪微博大厅和腾讯微博首页滚动特效源码
2015/09/15 Javascript
js针对ip地址、子网掩码、网关的逻辑性判断
2016/01/06 Javascript
关于不同页面之间实现参数传递的几种方式讨论
2017/02/13 Javascript
JS实现简易刻度时钟示例代码
2017/03/11 Javascript
使用express+multer实现node中的图片上传功能
2018/02/02 Javascript
对Vue beforeRouteEnter 的next执行时机详解
2018/08/25 Javascript
vue-cli 项目打包完成后运行文件路径报错问题
2019/07/19 Javascript
Emberjs 通过 axios 下载文件的方法
2019/09/03 Javascript
[01:38]DOTA2第二届亚洲邀请赛中国区预选赛出线战队晋级之路
2017/01/17 DOTA
python如何实现远程控制电脑(结合微信)
2015/12/21 Python
详解Python 2.6 升级至 Python 2.7 的实践心得
2017/04/27 Python
Python读取MRI并显示为灰度图像实例代码
2018/01/03 Python
python 实现倒排索引的方法
2018/12/25 Python
python 梯度法求解函数极值的实例
2019/07/10 Python
Python pandas库中的isnull()详解
2019/12/26 Python
Python接口测试get请求过程详解
2020/02/28 Python
python报错: 'list' object has no attribute 'shape'的解决
2020/07/15 Python
python实现不同数据库间数据同步功能
2021/02/25 Python
优秀党务工作者事迹材料
2014/05/07 职场文书
办公室文员岗位职责范本
2014/06/12 职场文书
重大事项社会稳定风险评估方案
2014/06/15 职场文书
2014大学班主任工作总结
2014/11/08 职场文书
2015年教师工作总结范文
2015/03/31 职场文书
党员带头倡议书
2015/04/29 职场文书
2016年小学六一儿童节活动总结
2016/04/06 职场文书