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

news2024/9/22 21:35:22

【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.coloradmin.cn/o/1536634.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

linux系统----------MySQL索引浅探索

目录 一、数据库索引介绍 二、索引的作用 索引的副作用 (缺点) 三、创建索引的原则依据 四、索引的分类和创建 4.1普通索引 4.1.1直接创建索引 4.1.2修改表方式创建 4.1.3创建表的时候指定索引 4.2唯一索引 4.2.1直接创建唯一索引 4.2.2修改表方式创建 4.2.3创建表…

Linux——du, df命令查看磁盘空间使用情况

一、实现原理&#xff1a; df 命令的全称是Disk Free &#xff0c;显而易见它是统计磁盘中空闲的空间&#xff0c;也即空闲的磁盘块数。它是通过文件系统磁盘块分配图进行计算出的。 du 命令的全称是 Disk Used &#xff0c;统计磁盘有已经使用的空间。它是直接统计各文件各目…

Qt QGraphicsView移动、缩放

原链接 首先需要明白&#xff0c;view在整个视图框架中的角色是用于显示scene的&#xff0c;所以决定了如何展示scene&#xff0c;包括scale()函数&#xff0c;用于放大缩小所展示的scene&#xff1b;centerOn()函数&#xff0c;决定scene的中心在何方。所有的操作&#xff0c…

移动app测试的好处简析,有必要选择第三方软件测试机构吗?

移动app测试是指对移动应用程序进行全面、系统和深入的检查和验证&#xff0c;以确保其功能、性能和稳定性达到预期要求。在移动应用市场日益竞争激烈的今天&#xff0c;进行移动app测试是至关重要的。 一、移动app测试的好处&#xff1a;   1、具有确保应用质量的作用。通过…

集简云新增“AI图像生成与识别”功能:实现智能图像识别与理解场景

自OpenAI发布GPT-4V以来&#xff0c;也掀起了各大企业对于多模态大模型的研究热潮。和以往的生图模型相比&#xff0c;多模态模型已突破文本限制&#xff0c;图像理解和识别能力尤为突出。 本周&#xff0c;集简云上线AI图像识别与问答功能&#xff0c;集成OpenAI和Anthropic两…

复制浏览器请求到Postman

目录 1.复制链接 2.导入到Postman 1.复制链接 F12打开开发者模式 2.导入到Postman 如上图所示&#xff0c;参数及cookie等信息都被导入进来。

VUE自己项目做的时候遇到的疑惑问题

晚上还在疑惑为什么下面还有一个一模一样的 早上起来&#xff0c;神清气爽&#xff0c;想了一下。原来是我用了两个路由出口

C语言复杂度(个人笔记)

时间复杂度主要衡量一个算法的运行快慢. 空间复杂度主要衡量一个算法运行所需要的额外空间. 时间复杂度 算法中的基本操作的执行次数&#xff0c;为算法的时间复杂度. 只需要大概执行次数&#xff0c;我们使用大O的渐进表示法。(看谁对数学表达式的影响最大) 空间复杂度 是…

学习C++是否有必要学习Boost库?

C作为一门强大且灵活的编程语言&#xff0c;在软件开发领域有着广泛的应用。而在C的学习过程中&#xff0c;Boost库是一个经常被提及的重要资源。那么&#xff0c;对于C的学习者而言&#xff0c;是否有必要投入精力去学习Boost库呢&#xff1f;本文将就此问题展开详尽讨论。 一…

LVGL:拓展部件——键盘 lv_keyboard

一、概述 此控件特点&#xff1a; 特殊Button矩阵&#xff1a;lv_keyboard 本质上是一个经过定制的按钮矩阵控件。每个按钮都可以独立触发事件或响应。预定义的键映射&#xff1a;lv_keyboard 自带了一套预设的按键布局和对应的字符映射表&#xff0c;开发者可以根据需要选择…

js实现hash路由原理

一、简单的上下布局&#xff0c;点击左侧导航&#xff0c;中间内容跟对变化&#xff0c;主要技术使用js检测路由的onhashchange事件 效果图 二、话不多说&#xff0c;直接上代码 <!DOCTYPE html> <html lang"zh"><head><meta charset"…

[Java、Android面试]_11_线程的启动方式和区别

文章目录 1. 继承Thread类2. 实现Runnable接口3. 实现Callable接口4. 使用Executor框架4. 四者的区别 本人今年参加了很多面试&#xff0c;也有幸拿到了一些大厂的offer&#xff0c;整理了众多面试资料&#xff0c;后续还会分享众多面试资料。 整理成了面试系列&#xff0c;由于…

Qt教程 — 3.5 深入了解Qt 控件:Display Widgets部件(1)

目录 1 Display Widgets简介 2 如何使用Display Widgets部件 2.1 QLabel组件-显示图像或文本 2.2 QCalendarWidget组件-日历简单的使用 2.3 QLCDNumber组件-控件作时钟的显示 2.4 QProgressBar组件-模拟手机电池充电 2.5 QFrame组件-绘制水平/垂直线 Display Widgets将分…

【机器学习】基于树种算法优化的BP神经网络分类预测(TSA-BP)

目录 1.原理与思路2.设计与实现3.结果预测4.代码获取 1.原理与思路 【智能算法应用】智能算法优化BP神经网络思路【智能算法】树种优化算法&#xff08;TSA&#xff09;原理及实现 2.设计与实现 数据集&#xff1a; 多输入多输出&#xff1a;样本特征24&#xff0c;标签类别…

【vue-小知识】var、let 和 const之间的区别

文章目录 结论1、重复定义变量名var&#xff1a;允许重复定义变量名let和const&#xff1a;不可以重复定义变量名 2、修改值var&#xff1a;允许修改值let&#xff1a;允许修改值const&#xff1a;不允许修改值&#xff0c;会报错 3、变量提升var : 支持变量提升let和const&…

[ C++ ] STL---stack与queue

目录 stack简介 stack的常用接口 queue简介 queue的常用接口 stack的模拟实现 queue的模拟实现 stack简介 1. stack是具有后进先出操作的一种容器适配器&#xff0c;其只能从容器的一端进行元素的插入与删除操作&#xff1b; 2. stack是作为容器适配器被实现的&#xff0…

使用Barrier对齐ConstraintLayout几个控件的最高的一个

前提就是想让一个控件X&#xff0c;对齐A&#xff0c;B&#xff0c;C等控件最高的位置&#xff0c;直接看图。 看&#xff0c;由于name的一行&#xff0c;或者2行&#xff0c;会导致email行的高度&#xff0c;可能比image块高&#xff0c;也可能比image快矮。 那么&#xff…

【STC8A8K64D4开发板】第2-17讲:PCA实现数模转换(DAC)

第2-17讲&#xff1a;PCA实现数模转换&#xff08;DAC&#xff09; 学习目的了解DAC数模转换原理及RC积分电路原理。掌握STC8A8K64D4系列单片机实现DAC功能的硬件和软件设计。 DAC简介 DAC (全称是Digital to Analog Convertor)数模转换器是一种将数字信号转换为模拟信号&a…

vue3+threejs新手从零开发卡牌游戏(七):创建卡组

在开始前先优化下之前的代码&#xff1a; 在之前hand/p1.vue中为了定位 utils文件夹下新建common.ts&#xff0c;将一些公用方法提取出来放在这里&#xff1a; 在game/Cards.ts中&#xff0c;我们调整下卡牌的厚度&#xff0c;由原来的0.02改为0.005&#xff0c;原因是之前的…

ZHUTI主提2024夏季系列 —「逐·行」

ZHUTI主提全新发布2024夏季「逐行」系列&#xff0c;聚焦当下人与自然的关系&#xff0c;以衣为载体&#xff0c;秉承东方哲学的艺术理念&#xff0c;将美学艺术与主流时尚设计融合&#xff0c;赋予当代时装表达新方向&#xff0c;共创现代女性之美。 取自然之意境&#xff0c…