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

news/2024/7/21 12:16:45 标签: 前端, harmonyos

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

一、注册

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.niftyadmin.cn/n/5286600.html

相关文章

Java中的内部类、枚举

内部类、枚举 内部类成员内部类静态内部类局部内部类(不重要)匿名内部类(重要)什么是匿名内部类使用场景 枚举类什么是枚举类枚举类的特点枚举类提供的一些额外API拓展:抽象枚举使用枚举类实现单例设计模式 常见应用场…

Java 实现 HTTP 请求的 4 种方式

在日常工作和学习中,有很多地方都需要发送HTTP请求,本文以Java为例,总结发送HTTP请求的多种方式 使用 HttpURLConnection 类 HttpURLConnection 是 Java 标准库中用来发送 HTTP 请求和接收 HTTP 响应的类。 它预先定义了一些方法&#xff…

求职小程序列表基础配置-移动端通用列表模块配置教程(1)

求职小程序列表基础配置-移动端通用列表模块配置教程(1) 移动端通用列表页开发指南 准备工作 注册多八多AIIDE账号: 访问多八多AIIDE官网并注册新账号。完成邮箱和手机号的验证。 创建移动应用: 登录后,在工作台新建一个移动应用。填写应用名称,选择“…

.gitignore加入文件后 还会有记录。怎么处理?

.gitignore文件仅用于告诉Git哪些文件或目录应该被忽略,以不将其纳入版本控制。当你在.gitignore文件中添加了node_modules时,Git将不会跟踪或记录这个目录下的任何文件变化。 然而,如果你在之前的提交中已经将node_modules目录纳入版本控制&…

Java多线程技术五——单例模式与多线程

1 概述 本章的知识点非常重要。在单例模式与多线程技术相结合的过程中,我们能发现很多以前从未考虑过的问题。这些不良的程序设计如果应用在商业项目中将会带来非常大的麻烦。本章的案例也充分说明,线程与某些技术相结合中,我们要考虑的事情会…

test-03-java 单元测试框架 testNG 入门介绍 junit/junit5/testNG 详细对比

拓展阅读 test-01-java 单元测试框架 junit 入门介绍 test-02-java 单元测试框架 junit5 入门介绍 test-03-java 单元测试框架 testNG 入门介绍 junit/junit5/testNG 详细对比 test assert-01-Google Truth 断言 test 系统学习-03-TestNG Spock testng 入门使用教程 开源…

Codeforces Round 917 (Div. 2)

Codeforces Round 917 (Div. 2) Codeforces Round 917 (Div. 2) A. Least Product 题意: 给出整数数组a,现在可以执行任意次数以下操作:任意选择数组a的一个元素 a i a_i ai​,若 a i a_i ai​>0可以任意替换为[0, a i a_i…

vue 导出 HTML 结构为 Word 文档(.docx)-支持表格、css样式、图片

在 Web 开发中,有时我们希望用户能够将网页上的 HTML 内容保存为 Word 文档,以便更方便地分享和打印。本文将介绍如何使用 html-docx-js 和 file-saver 这两个 JavaScript 库,实现将 HTML 结构导出为 Word 文档的功能。 工具简介 1. html-d…