【JSON2WEB】10 基于 Amis 做个登录页面login.html

【JSON2WEB】01 WEB管理信息系统架构设计

【JSON2WEB】02 JSON2WEB初步UI设计

【JSON2WEB】03 go的模板包html/template的使用

【JSON2WEB】04 amis低代码前端框架介绍

【JSON2WEB】05 前端开发三件套 HTML CSS JavaScript 速成

【JSON2WEB】06 JSON2WEB前端框架搭建

【JSON2WEB】07 Amis可视化设计器CRUD增删改查

【JSON2WEB】08 Amis的事件和校验

【JSON2WEB】09 Amis-editor的代码移植到json2web


基于 Amis 做个登录页面 login.html ,用于验证用户名和密码的,验证成功后返回token,并保存token在 localStorage中。
参考视频教程,https://www.bilibili.com/video/BV1wu411Q7y3/?spm_id_from=333.788 ,Amis官方也没有视频教程,没有一点基础学起来很费劲啊。

1 创建登录页面 Login.html

1 新建登录页面

从官方文档 https://aisuda.bce.baidu.com/amis/zh-CN/docs/start/getting-started 拷贝hello.html,并修改后代码如下:

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8" />
  <title>amis demo</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
  <link rel="stylesheet" href="/sdk/sdk.css" />
  <link rel="stylesheet" href="/sdk/helper.css" />
  <link rel="stylesheet" href="/sdk/iconfont.css" />
  <!-- 这是默认主题所需的,如果是其他主题则不需要 -->
  <!-- 从 1.1.0 开始 sdk.css 将不支持 IE 11,如果要支持 IE11 请引用这个 css,并把前面那个删了 -->
  <!-- <link rel="stylesheet" href="sdk-ie11.css" /> -->
  <!-- 不过 amis 开发团队几乎没测试过 IE 11 下的效果,所以可能有细节功能用不了,如果发现请报 issue -->
  <style>
    html,
    body,
    .app-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>
  <div id="root" class="app-wrapper"></div>
  <script src="/sdk/sdk.js"></script>
  <script type="text/javascript">
    (function () {
      let amis = amisRequire('amis/embed');
      // 通过替换下面这个配置来生成不同页面
      let amisJSON = {
        type: 'page',
        title: '登录JSON2WEB',
        body: {
          type: 'form',
          title: '',
          mode: 'horizontal',
          api: {
            url: 'http://127.0.0.1:5217/token/generate-token?userid=$userId&passwd=$passWd',
            method: 'post',
            adaptor: function (payload) {
              console.log(payload);
              if (payload.status === 0) {
                localStorage.setItem('token', payload.data.token);
                // localStorage.clear(); location.href = '/login.html';
                return payload;
              }
            }
          },
          redirect: '/index.html',
          body: [
            {
              label: '用户名:',
              type: 'input-text',
              name: 'userId'
            },
            {
              label: '密码:',
              type: 'input-password',
              name: 'passWd'
            }
          ]
        }
      };
      let amisScoped = amis.embed('#root', amisJSON);
    })();
  </script>
</body>

</html>

1.2 核心就是 API的配置

在这里插入图片描述

1.3 页面预览

在这里插入图片描述

2 主页 index.html的代码

2.1 所有的API请求都带上token

token放在请求头里的Authorization,请求适配器代码如下:

requestAdaptor(api) {
            // Api前缀
            // if (api.url.indexOf("pages") == -1){
            //   api.url = "http://127.0.0.1:5217" + api.url;
            // }
            //api.url = "http://127.0.0.1:5217" + api.url;
            // token 认证            
            // api.headers['Authorization'] = "Bearer " + localStorage.getItem('token');
            api.headers['Authorization'] = localStorage.getItem('token');
            console.log("全局请求适配器", api);
            return api;
          },

2.2 所有的响应适配器

也就是没有token或过期等都请求不到后台的数据,代码如下:

// 全局 api 适配器。
          // 另外在 amis 配置项中的 api 也可以配置适配器,针对某个特定接口单独处理。
          responseAdaptor(payload, response) {
            console.log("全局响应适配器", response);
            if (response.state == 401) {
              localStorage.clear();
              location.href = '/';
            }
            return payload, response;
          },

2.3 退出代码

退出时清空token并跳转到登录页,用内嵌js代码实现如下?

 header: {
          type: 'tpl',
          inline: false,
          className: 'w-full',
          tpl: `<div class="flex justify-between"><div>顶部区域左侧</div><div><a href="#" οnclick="localStorage.clear(); location.href = '/';">退出</a></div></div>`
        },

2.4 首页全部代码

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>amis admin</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
  <!--  
  <link rel="stylesheet" title="default" href="https://cdn.jsdelivr.net/npm/amis@beta/sdk/sdk.css" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/amis@beta/sdk/helper.css" />
  <script src="https://cdn.jsdelivr.net/npm/amis@beta/sdk/sdk.js"></script> 
 -->

  <link rel="stylesheet" title="default" href="/sdk/sdk.css" />
  <link rel="stylesheet" title="default" href="/sdk/antd.css" />
  <!-- <link rel="stylesheet" title="default" href="/sdk/cxd.css" /> -->
  <link rel="stylesheet" href="/sdk/helper.css" />
  <script src="/sdk/sdk.js"></script>

  <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2"></script> -->
  <!-- <script src="https://cdn.jsdelivr.net/npm/history/umd/history.js"></script> -->
  <script src="/sdk/history.js"></script>
  <style>
    html,
    body,
    .app-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>
  <div id="root" class="app-wrapper"></div>
  <script>
    (function () {
      let amis = amisRequire('amis/embed');
      const match = amisRequire('path-to-regexp').match;

      // 如果想用 browserHistory 请切换下这处代码, 其他不用变
      //const history = History.createBrowserHistory();
      const history = History.createHashHistory();

      const app = {
        type: 'app',
        brandName: 'JSON2WEB',
        //logo: '/public/logo.png',
        logo: '/public/5217.jpg',
        header: {
          type: 'tpl',
          inline: false,
          className: 'w-full',
          tpl: `<div class="flex justify-between"><div>顶部区域左侧</div><div><a href="#" οnclick="localStorage.clear(); location.href = '/';">退出</a></div></div>`
        },
        // 如果想用 browserHistory 请切换下这处代码, 他不用变">退出登录</a></div></div>`
        footer: '<div class="p-2 text-center bg-light">版权没有,翻版不究!底部区域</div>',
        asideBefore: '<div class="p-2 text-center">菜单前面区域</div>',
        asideAfter: '<div class="p-2 text-center">菜单后面区域</div>',
        api: '/pages/site.json'
      };

      function normalizeLink(to, location = history.location) {
        to = to || '';

        if (to && to[0] === '#') {
          to = location.pathname + location.search + to;
        } else if (to && to[0] === '?') {
          to = location.pathname + to;
        }

        const idx = to.indexOf('?');
        const idx2 = to.indexOf('#');
        let pathname = ~idx
          ? to.substring(0, idx)
          : ~idx2
            ? to.substring(0, idx2)
            : to;
        let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
        let hash = ~idx2 ? to.substring(idx2) : location.hash;

        if (!pathname) {
          pathname = location.pathname;
        } else if (pathname[0] != '/' && !/^https?\:\/\//.test(pathname)) {
          let relativeBase = location.pathname;
          const paths = relativeBase.split('/');
          paths.pop();
          let m;
          while ((m = /^\.\.?\//.exec(pathname))) {
            if (m[0] === '../') {
              paths.pop();
            }
            pathname = pathname.substring(m[0].length);
          }
          pathname = paths.concat(pathname).join('/');
        }

        return pathname + search + hash;
      }

      function isCurrentUrl(to, ctx) {
        if (!to) {
          return false;
        }
        const pathname = history.location.pathname;
        const link = normalizeLink(to, {
          ...location,
          pathname,
          hash: ''
        });

        if (!~link.indexOf('http') && ~link.indexOf(':')) {
          let strict = ctx && ctx.strict;
          return match(link, {
            decode: decodeURIComponent,
            strict: typeof strict !== 'undefined' ? strict : true
          })(pathname);
        }

        return decodeURI(pathname) === link;
      }

      let amisInstance = amis.embed(
        '#root',
        app,
        {
          location: history.location
        },
        {
          // watchRouteChange: fn => {
          //   return history.listen(fn);
          // },
          requestAdaptor(api) {
            // Api前缀
            // if (api.url.indexOf("pages") == -1){
            //   api.url = "http://127.0.0.1:5217" + api.url;
            // }
            //api.url = "http://127.0.0.1:5217" + api.url;
            // token 认证            
            // api.headers['Authorization'] = "Bearer " + localStorage.getItem('token');
            api.headers['Authorization'] = localStorage.getItem('token');
            console.log("全局请求适配器", api);
            return api;
          },
          // 全局 api 适配器。
          // 另外在 amis 配置项中的 api 也可以配置适配器,针对某个特定接口单独处理。
          responseAdaptor(payload, response) {
            console.log("全局响应适配器", response);
            if (response.state == 401) {
              localStorage.clear();
              location.href = '/';
            }
            return payload, response;
          },
          updateLocation: (location, replace) => {
            location = normalizeLink(location);
            if (location === 'goBack') {
              return history.goBack();
            } else if (
              (!/^https?\:\/\//.test(location) &&
                location ===
                history.location.pathname + history.location.search) ||
              location === history.location.href
            ) {
              // 目标地址和当前地址一样,不处理,免得重复刷新
              return;
            } else if (/^https?\:\/\//.test(location) || !history) {
              return (window.location.href = location);
            }

            history[replace ? 'replace' : 'push'](location);
          },
          jumpTo: (to, action) => {
            if (to === 'goBack') {
              return history.goBack();
            }

            to = normalizeLink(to);

            if (isCurrentUrl(to)) {
              return;
            }

            if (action && action.actionType === 'url') {
              action.blank === false
                ? (window.location.href = to)
                : window.open(to, '_blank');
              return;
            } else if (action && action.blank) {
              window.open(to, '_blank');
              return;
            }

            if (/^https?:\/\//.test(to)) {
              window.location.href = to;
            } else if (
              (!/^https?\:\/\//.test(to) &&
                to === history.pathname + history.location.search) ||
              to === history.location.href
            ) {
              // do nothing
            } else {
              history.push(to);
            }
          },
          isCurrentUrl: isCurrentUrl,
          // theme: 'cxd'
          theme: 'cxd'
        }
      );

      history.listen(state => {
        amisInstance.updateProps({
          location: state.location || state
        });
      });
    })();
  </script>
</body>

</html>

3 后端REST2SQL修改

3.1 所有REST请求都要验证token

token从请求头Authorization获取。

switch req["RESTorSQL"] {
	case "REST":
		// token有效性验证
		tokenString := req["Authorization"].(string)
		if vToken(w, tokenString) != 200 {
			return
		}

token验证函数:

// token 验证函数
func vToken(w http.ResponseWriter, tokenString string) int {
	tokenmap := make(map[string]interface{})
	tokenmap = token.ValidateTokenHandler(w, tokenString)
	if tokenmap["status"] != http.StatusOK {
		// 抛出错误信息
		httpResWriter(w,tokenmap)
		return 401
	}
	return 200
}

主要内容就是抛出token错误信息,并设置返回代码为 401

4 nodejs路由配置

// 定义路由以加载不同的页面
app.get('/', (req, res) => {
  res.sendFile(path.resolve(__dirname, 'login.html'));
});

app.get('/index.html', function (req, res) {
  res.sendFile(path.join(__dirname, 'index.html'));
});

http://localhost:3000/ 请求的就是登录页面 login.html
http://localhost:3000/index.html请求的是 index.html

5 实操演练

Step1 登录

http://localhost:3000/
在这里插入图片描述

Step 2 提交跳转

点【提交】按钮,登录成功跳转到主页index.html,可以查看token

在这里插入图片描述

Step 3 用户管理

在这里插入图片描述
可查询到信息,并可以crud操作。

Setp 4 退出

点主页右上角的【退出】按钮,可退出主页,并跳转到登录页,并清除了token
在这里插入图片描述

Step 5 直接在访问index.html

页面可以打开请求不到api的数据。
在这里插入图片描述
没有token或token过期都请求不到数据


脑子不好用,搞一点都记录一下,方便自己查询,好记性不如烂笔头。
本文完。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/475951.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

免费原型图设计工具,先收藏这5个!

在当今快速发展的数字世界中&#xff0c;原型图设计工具无疑是设计师不可或缺的工具。高效易用的工具不仅可以使设计过程更加顺畅&#xff0c;还可以有效提高设计质量。在这个竞争激烈的市场中&#xff0c;有许多优秀的免费原型图设计工具可供选择。以下是我们选择的 5 个免费原…

【机器学习-07】逻辑回归(Logistic Regression)的介绍和python实现

Logistic Regression 虽然被称为回归&#xff0c;但其实际上是分类模型&#xff0c;并常用于二分类。主要用来表示某件事情发生的可能性&#xff0c;因此因变量的范围在 0 和 1 之间。Logistic Regression 因其简单、可并行化、可解释强深受工业界喜爱。例如&#xff0c;探讨引…

进程优先级

文章目录 一、进程优先级二、查看进程优先级以及修改优先级 一、进程优先级 cpu资源少量&#xff0c;只有一个然而在操作系统中存在很多进程&#xff0c;这些进程访问cpu资源时他们相互之间是存在竞争关系。cpu为了保他们之间的公平&#xff0c;也就存在了优先级&#xff01;那…

使用Go语言创建HTTP服务器并展示网页

使用Go语言创建一个简单的服务器时可以先建立一个项目根目录&#xff0c;随后在根目录中建立一个用于存放静态文件&#xff08;HTML/CSS/JavaScript&#xff09;的文件夹 GGboy&#xff0c;接下来输入命令初始化Go模块 go mod init GGboy // 项目名称是 GGboy 在出现 go.mod 文…

32-Java拦截过滤器模式 ( Intercepting Filter )

Java拦截过滤器模式 实现范例 拦截过滤器模式&#xff08;Intercepting Filter Pattern&#xff09;用于对应用程序的请求或响应做一些预处理/后处理定义过滤器&#xff0c;并在把请求传给实际目标应用程序之前应用在请求上过滤器可以做认证/授权/记录日志&#xff0c;或者跟踪…

Codeforces Round 496 (Div. 3)

目录 A. Tanya and Stairways B. Delete from the Left C. Summarize to the Power of Two D. Polycarp and Div 3 E. Median on Segments F. Berland and the Shortest Paths A. Tanya and Stairways 简单性质题 我们找到性质&#xff0c;如果这个数大于等于后面的数就…

网工内推 | 云计算工程师,HCIE认证优先,最高18k*14薪

01 杭州中港科技有限公司 招聘岗位&#xff1a;云计算工程师 职责描述&#xff1a; 1、承担云计算相关工程交付、业务上云及售前测试&#xff0c;从事虚拟化、桌面云、存储、服务器、数据中心、大数据、相关产品的工程项目交付或协助项目交付。 2、承担云计算维护工程师职责&…

mac安装rust开发环境,使用brew安装和全局配置

mac下使用brew可以一键安装环境&#xff1a; brew install rustup 安装完成执行&#xff1a; rustup-init 按照提示配置即可&#xff1a; 出现&#xff1a; 想要全局生效&#xff1a; echo export PATH"$HOME/.cargo/bin:$PATH" >> ~/.bash_profile source…

代码随想录算法训练营第day53|1143.最长公共子序列 、 1035.不相交的线、 53. 最大子序和 动态规划

目录 1143.最长公共子序列 1035.不相交的线 53. 最大子序和 1143.最长公共子序列 力扣题目链接(opens new window) 给定两个字符串 text1 和 text2&#xff0c;返回这两个字符串的最长公共子序列的长度。 一个字符串的 子序列 是指这样一个新的字符串&#xff1a;它是由原…

Windows 设置多显示器显示

Windows 设置多显示器显示 1. Windows 7 设置 HDMI 输出2. Windows 11 设置多显示器显示References 1. Windows 7 设置 HDMI 输出 2. Windows 11 设置多显示器显示 ​​​ References [1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

深度学习_20_卷积中的填充与步幅

如果图片本身比较小&#xff0c;卷积之后输出也会很小&#xff0c;那么可以在图片与卷积核相乘之前先填充一下&#xff0c;让输出为预期大小 一般填充后输入&#xff0c;输出相同 当图片比较大的时候&#xff0c;如果利用卷积核去得到我们想要的大小的话&#xff0c;得用到多层…

javaSwing日记管理系统

一、简介 使用 Java Swing 开发日记管理系统 在今天的博客中&#xff0c;我将向您介绍如何使用 Java Swing 开发一个简单而功能强大的日记管理系统。这个系统将具有登录、注册、找回密码、写日志以及切换主题等功能。我们将使用 MySQL 数据库来存储用户信息和日记内容。 二、…

ShardingSphere+JPA+Druid实现分表操作

要在SpringBoot项目中实现分表操作&#xff0c;本文使用的是ShardingSphereJPADruid实现。过程中出现问题记录一下。 准备MySQL数据库表 这里准备的是一张主表test_cost&#xff0c;两张从表test_cost_0和test_cost_1&#xff0c;结构需要相同&#xff0c;主表只是声明了表结构…

python异常:pythonIOError异常python打开文件异常

1.python读取不存在的文件时&#xff0c;抛出异常 通过 open()方法以读“r”的方式打开一个 abc.txt 的文件&#xff08;该文件不存在&#xff09;&#xff0c;执行 open()打开一个不存在的文件时会抛 IOError 异常&#xff0c;通过 Python 所提供的 try...except...语句来接收…

基于springBoot 整合JavaMail的网站邮件通知功能实现

JDK版本&#xff1a;jdk17 IDEA版本&#xff1a;IntelliJ IDEA 2022.1.3 SpringBoot 版本&#xff1a;v2.5.7 文章目录 一、关于邮件发送的基本概念1.1 邮件发送1.1.1 SMTP协议 1.2 邮件接收1.2.1 POP3协议1.2.2 IMAP协议 二、准备工作2.1 注册邮箱2.1 获取登录授权码 三、开发…

走进jvm之垃圾回收器篇

这里我想首先说明一下&#xff0c;虽然我们经常会拿垃圾回收器来做比较&#xff0c;虽然想挑选一个最好的收集器出来&#xff0c;但是目前也没有说哪一款收集器是完美的&#xff0c;更不存在万能的收集器&#xff0c;我们也只是对收集器选择最适合场景的一个收集器。 那么作者将…

Springboot+Vue前后端分离的在线图书商城(书城)系统

项目介绍 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本图书商城管理系统就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间内处理完毕庞大的数据…

UE snap02 解析ASCII文本文件

UE snap02 解析ASCII文本文件 示例数据data.dat 11389477.2714892 3364559.73645693 0 11389471.5162524 3364567.8860295 0 11389471.5162524 3365813.09618369 0 11388329.6082659 3366184.85895869 0 11388320.4775297 3366197.78833087 0 11388270.6882384 3366214.84811…

OpenAI Sora文生视频模型技术报告中英全文

Video generation models as world simulators 视频生成模型作为世界模拟器 We explore large-scale training of generative models on video data. Specifically, we train text-conditional diffusion models jointly on videos and images of variable durations, resolu…

jQuery 元素操作

文章目录 1. jQuery 样式操作1.1 操作 css 方法1.2 设置类样式方法*案例--tab栏切换 1.3 类操作和className 区别 2. jQuery 效果2.1 显示隐藏效果2.2 滑动效果事件切换动画队列及其停止排队方法 3.3 淡入淡出效果利用渐进方式调整透明度*案例--高亮突出显示 3.4 自定义动画 an…