鸿蒙项目二—— 注册和登录

news2025/2/8 15:05:57

此部分和上篇文章是连续剧 ,如果需要,请查看

一、注册

import http from '@ohos.net.http';
@Entry
@Component
struct Reg {

  // 定义数据:
  @State username: string = "";
  @State userpass: string = "";
  @State userpass2: string = "";
  @State usernameColor: number = 0x000000;
  @State userpassColor: number = 0x000000;
  @State userpass2Color: number = 0x000000;

  // 表单验证是否成功:
  formIsSuccess() {
    return this.username.trim() !== "" && this.userpass.trim() !== "" && this.userpass2.trim() !== "" && this.userpass === this.userpass2
  }

  regSave() {
    //   1、非空验证
    if (this.username.trim() == "") {
      console.log("用户名不能为空")
      this.usernameColor = 0xff0000;
      return;
    }

    if (this.userpass.trim() == "") {
      console.log("密码不能为空")
      this.userpassColor = 0xff0000;
      return;
    }
    if (this.userpass2.trim() == "") {
      console.log("确认密码不能为空")
      this.userpass2Color = 0xff0000;
      return;
    }
    //   2、密码是否一致

    //   3、发送请求,进行注册
    const httpRequest = http.createHttp();
    httpRequest.request("http://localhost:3000/vips", {
      method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
      // 开发者根据自身业务需要添加header字段
      header: {
        'Content-Type': 'application/json'
      },
      // 当使用POST请求时此字段用于传递内容
      extraData: {
          username:this.username,
          userpass:this.userpass
      },
      connectTimeout: 60000, // 可选,默认为60s
      readTimeout: 60000, // 可选,默认为60s
    },(err,data)=>{
        if(!err){
          console.log("data.result",data.result)
          console.log("data.responseCode",data.responseCode)
          if(data.responseCode==201){
            console.log("注册成功")
          }
        }
    })
  }

  build() {
    Column() {
      Row() {
        Image($r("app.media.back")).width(50).height(30).margin(20)
      }
      .width("100%")
      .height(60)

      Blank().height(50)
      Row() {
        Text("欢迎您的注册")
          .fontSize(40)
          .fontWeight(FontWeight.Bold)
      }

      Row() {
        Text("用户名:").fontSize(20).fontWeight(400)
        TextInput({ placeholder: "请输入用户名", text: this.username }).width(260).height(40)
          .placeholderColor(this.usernameColor)
          .onChange((val: string) => {
            this.username = val;
          })
      }.width("100%")
      .height(60)
      .margin({
        top: 20
      })

      Row() {
        Text("密    码:").fontSize(20).fontWeight(400)
        TextInput({ placeholder: "请输入密码", text: this.userpass })
          .type(InputType.Password)
          .width(260)
          .height(40)
          .placeholderColor(this.userpassColor)
          .onChange((val: string) => {
            this.userpass = val;
          })
      }.width("100%")
      .height(60)
      .margin({
        top: 10
      })

      Row() {
        Text("确认密码:").fontSize(20).fontWeight(400)
        TextInput({ placeholder: "请输入确认密码", text: this.userpass2 })
          .type(InputType.Password)
          .width(260)
          .height(40)
          .placeholderColor(this.userpass2Color)
          .onChange((val: string) => {
            this.userpass2 = val;
          })
      }.width("100%")
      .height(60)
      .margin({
        top: 10
      })

      Row() {
        if (this.userpass !== this.userpass2) {
          Text("两次密码不一致").fontSize(20).fontColor(Color.Red);
        }
      }

      Button("注册")
        .width("90%")
        .height(60)
        .margin({
          top: 10
        })
        .fontSize(30)
        .fontWeight(600)
        .enabled(this.formIsSuccess())
        .onClick(() => {
          this.regSave();
        })

      Blank().height(100)
      Row() {
        Text("--第三方账号登录--")
      }.margin({
        bottom: 20
      })

      Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
        Image($r("app.media.qq")).width(40).height(40)
        Image($r("app.media.weixin")).width(40).height(40)
        Image($r("app.media.weibo")).width(40).height(40)
      }.width("100%")
      .height(60)

    }
    .width('100%')
  }
}

二、登录

本地存储用户信息的文件:

`utils/globaldata.ts`

export const storage:LocalStorage = new LocalStorage();


页面代码:


import http from '@ohos.net.http';
import router from '@ohos.router';
import {storage} from '../utils/globaldata';

interface IParams{
  from:string,
  data:string
}

@Entry
@Component
struct Login {

  // 定义数据:
  @State username: string = "";
  @State userpass: string = "";
  @State phone:string="";

  @State usernameColor: number = 0x000000;
  @State userpassColor: number = 0x000000;

  // 表单验证是否成功:
  formIsSuccess() {
    return this.username.trim() !== "" && this.userpass.trim() !== "";
  }

  loginCheck() {
    //   1、非空验证
    if (this.username.trim() == "") {
      console.log("用户名不能为空")
      this.usernameColor = 0xff0000;
      return;
    }

    if (this.userpass.trim() == "") {
      console.log("密码不能为空")
      this.userpassColor = 0xff0000;
      return;
    }

    //3、发送请求,进行登录
    const httpRequest = http.createHttp();

    // get请求,给后端传递数据的方式一:
    // 请求地址?属性名1=属性值1&属性名2=属性值2
    httpRequest.request(`http://localhost:3000/vips?username=${this.username}&userpass=${this.userpass}`,(err,data)=>{
      // !err 只是表示请求响应没有问题,不代表是否获取到了数据
      if(!err){
        console.log("data.result",data.result)
        const arr = JSON.parse(data.result as string)
        if(arr.length===1){
          console.log("登录成功");
          // 保存登录的相关信息(如:用户名,电话号码)
          storage.setOrCreate("username",this.username);
          storage.setOrCreate("phone",this.phone);
          console.log("from",(router.getParams() as IParams).from)
          // 跳转到我的页面。
          // router.back();
          router.pushUrl({
            url:(router.getParams() as IParams).from,
            params:{
              data:(router.getParams() as IParams).data
            }
          })
        }else{
          console.log("登录失败,用户名或密码不正确")
        }
      }else{
        console.log("网络出错")
      }
    })
  }

  build() {
    Column() {
      Row() {
        Image($r("app.media.back")).width(50).height(30).margin(20).onClick(()=>{
          router.back();
        })
      }
      .width("100%")
      .height(60)

      Blank().height(50)
      Row() {
        Text("亲,请登录")
          .fontSize(40)
          .fontWeight(FontWeight.Bold)
      }

      Row() {
        Text("用户名:").fontSize(20).fontWeight(400)
        TextInput({ placeholder: "请输入用户名", text: this.username }).width(260).height(40)
          .placeholderColor(this.usernameColor)
          .onChange((val: string) => {
            this.username = val;
          })
      }.width("100%")
      .height(60)
      .margin({
        top: 20
      })

      Row() {
        Text("手机号码:").fontSize(20).fontWeight(400)
        TextInput({ placeholder: "请输入手机号", text: this.phone }).width(260).height(40)
          .placeholderColor(this.usernameColor)
          .onChange((val: string) => {
            this.phone = val;
          })
      }.width("100%")
      .height(60)
      .margin({
        top: 20
      })

      Row() {
        Text("密    码:").fontSize(20).fontWeight(400)
        TextInput({ placeholder: "请输入密码", text: this.userpass })
          .type(InputType.Password)
          .width(260)
          .height(40)
          .placeholderColor(this.userpassColor)
          .onChange((val: string) => {
            this.userpass = val;
          })
      }.width("100%")
      .height(60)
      .margin({
        top: 10
      })

      Button("登录")
        .width("90%")
        .height(60)
        .margin({
          top: 10
        })
        .fontSize(30)
        .fontWeight(600)
        .enabled(this.formIsSuccess())
        .onClick(() => {
          this.loginCheck();
        })

      Blank().height(100)
      Row() {
        Text("--第三方账号登录--")
      }.margin({
        bottom: 20
      })

      Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
        Image($r("app.media.qq")).width(40).height(40)
        Image($r("app.media.weixin")).width(40).height(40)
        Image($r("app.media.weibo")).width(40).height(40)
      }.width("100%")
      .height(60)

    }
    .width('100%')
  }
}


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

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

相关文章

Java@RequestParam注解和@RequestBody注解接收参数

目录 Java后端接收数据 第一章、后端不写任何注解情况下接收参数1.1)后端不写注解postman发出get请求1.2)后端不写注解postman发出post请求 第二章、后端写RequestParam注解接收参数2.1)postman发出post请求2.2)postman发出get请求…

腾讯云上mysql连接不上

腾讯云服务器默认没开放,3306端口。 1.去腾讯云控制台 2.找到自己的服务器 3选择防火墙 4.添加规则 至此完事了。

Redis数据结构(常用5+4种特殊数据类型)

1、Redis 数据类型以及使用场景分别是什么? Redis 提供了丰富的数据类型,常见的有五种数据类型:String(字符串),Hash(哈希),List(列表)&#xff…

面试复盘2——测试开发——一面+二面

前言 本文主要用于个人复盘学习,因此为保障公平,所以本文不指出公司名,题目编号只是为了自己区别而已。对待面经,望读者还是更多从其中学习总结,而不是去碰原题。 面试岗位信息 测试开发工程师,秋招但需…

Spring Boot3 Web开发技术

前期回顾 springboot项目常见的配置文件类型有哪些?哪种类型的优先级最高 yml properties yaml 读取配置文件里的数据用什么注解? value restful风格 RESTful 风格与传统的 HTTP 请求方式相比,更加简洁,安全,能隐…

基于AR+地图导航的景区智慧导览设计

随着科技的飞速发展,智慧旅游已经成为现代旅游业的一个重要趋势。在这个背景下,景区智慧导览作为智慧旅游的核心组成部分,正逐渐受到越来越多游客的青睐。本文将深入探讨地图导航软件在景区智慧导览中的应用,并分析其为游客和景区…

pytorch中池化函数详解

1 池化概述 1.1 什么是池化 池化层是卷积神经网络中常用的一个组件,池化层经常用在卷积层后边,通过池化来降低卷积层输出的特征向量,避免出现过拟合的情况。池化的基本思想就是对不同位置的特征进行聚合统计。池化层主要是模仿人的视觉系统…

整数规划-割平面法

整数规划-割平面法 割平面法思想Gomorys割平面法原理实例 谨以此博客作为学习期间的记录。 割平面法思想 在之前,梳理了分支定界法的流程:分支定界法 除了分支定界法,割平面法也是求解整数规划的另一个利器。 我们已经知道,线性规划的可行域…

XStream 反序列化漏洞 CVE-2021-39144 已亲自复现

XStream 反序列化漏洞 CVE-2021-39144 已亲自复现 漏洞名称漏洞描述影响版本 漏洞复现环境搭建 修复建议总结 漏洞名称 漏洞描述 在Unmarshalling Time处包含用于重新创建前一对象的类型信息。XStream基于这些类型的信息创建新实例。攻击者可以控制输入流并替换或注入对象&am…

LeetCode-回文链表(234)

题目描述: 给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。 因为这一题是受到876题求链表中间节点的启发,所以在这里也加一下。 876.链表的中间结点…

机器视觉系统选型-避免畸变

在定位及高精度测量的系统中,镜头畸变的影响尤其重要 • 使用远心镜头 • 进行系统标定

二维码智慧门牌管理系统:提升社区管理智能化水平

文章目录 前言一、全方位信息录入与查询二、公安权限账户访问的公安大数据后台三、社区工作人员申请权限安装录入软件四、业主通过移动终端扫描标准地址二维码门牌自主申报录入五、系统的价值 前言 在数字化时代,社区管理面临着更新流动人口信息、准确录入六实相关…

119. 杨辉三角 II(Java)

给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。 在「杨辉三角」中,每个数是它左上方和右上方的数的和。 示例 1: 输入: rowIndex 3 输出: [1,3,3,1]示例 2: 输入: rowIndex 0 输出: [1]示例 3: 输入: rowIndex 1 输出: [1,1]提示…

xlua源码分析(四) lua访问C#的值类型

xlua源码分析(四) lua访问C#的值类型 上一节我们主要探讨了C#是如何使用interface和delegate访问lua层的table和function的,本节我们跟着Examples 05_NoGc,来看看xlua是如何实现lua层无gc访问C#的值类型的。 首先例子中用到的lua…

LH7904D 太阳能警示灯 0.4W×2

应用范围: 可安装在电线杆,路灯,围挡,交 通护栏及各种杆式固体等场所起警示作用。 产品特点: 采用进口PS材质; 光控无开关,白天不闪,昏暗环境自动闪烁,无需手动操作,省时省事; …

Hive04_DDL操作

Hive DDL操作 1 DDL 数据定义 1.1 创建数据库 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path] [WITH DBPROPERTIES (property_nameproperty_value, ...)];[IF NOT EXISTS] :判断是否存在 [COMMENT database_c…

冒泡排序(C语言)

void BubbleSort(int arr[], int len) {int i, j, temp;for (i 0; i < len; i){for (j len - 1; j > i; j--){if (arr[j] > arr[j 1]){temp arr[j];arr[j] arr[j 1];arr[j 1] temp;}}} } 优化&#xff1a; 设置标志位flag&#xff0c;如果发生了交换flag设置…

C语言学习day10:三目运算符

三目运算符&#xff1a; if语句&#xff1a; if (a>b) {printf("a大\n");}else {printf("b大\n");} 三目&#xff1a; 表达式1&#xff1f;表达式2&#xff1a;表达式3&#xff1a; 代码&#xff1a; int a 10; int b 20; int c; 解释如果a>b则…

YOLOv8改进 | 主干篇 | RevColV1可逆列网络(特征解耦助力小目标检测)

一、本文介绍 本文给大家带来的是主干网络RevColV1&#xff0c;翻译过来就是可逆列网络去发表于ICLR2022&#xff0c;其是一种新型的神经网络设计(和以前的网络结构的传播方式不太一样)&#xff0c;由多个子网络&#xff08;列&#xff09;通过多级可逆连接组成。这种设计允许…

W5500-EVB-Pico评估版介绍

文章目录 1 概述2 板载资源2.1 硬件规格2.2 硬件规格2.3 工作条件 3 参考资料3.2 原理图3.3 尺寸图 (单位 : mm)3.4 参考例程 4 硬件协议栈优势 1 概述 W5500-EVB-Pico是基于树莓派RP2040和完全硬连线TCP/IP控制器W5500的微控制器开发板-基本上与树莓派Pico板相同&#xff0c;但…