IT评测·应用市场-qidao123.com技术社区

标题: arkTS开辟鸿蒙OS应用(登录页面实现,毗连数据库) [打印本页]

作者: 勿忘初心做自己    时间: 2024-6-21 13:13
标题: arkTS开辟鸿蒙OS应用(登录页面实现,毗连数据库)
前言

喜好的朋友可在抖音、小红书、微信公众号、哔哩哔哩搜索“淼学派对”。知乎搜索“编程淼”。
前端架构


Toubu.ets

  1. import router from '@ohos.router'
  2. @Component
  3. export struct Header{
  4.   build(){
  5.   //   标题部分
  6.     Row({space:5}){
  7.       Image($r('app.media.fanhui'))
  8.         .width(20)
  9.         .onClick(() =>{
  10.           router.back()
  11.         })
  12.       Blank()
  13.       Image($r('app.media.shuaxin'))
  14.         .width(30)
  15.     }
  16.     .width('98%')
  17.     .height(30)
  18.   }
  19. }
复制代码
Index.ets

  1. import  axios  from '@ohos/axios'
  2. import router from '@ohos.router'
  3. @Entry
  4. @Component
  5. struct Index {
  6.   // 上传数据
  7.   @State zhanghao: string = ''
  8.   @State mima: string = ''
  9.   @State zhanghao_find:string =''
  10.   @State mima_find:string =''
  11.   build() {
  12.       Column() {
  13.         Text('龙年千帆启鸿蒙')
  14.           .margin({top:70})
  15.           .fontWeight(FontWeight.Bold)
  16.           .fontSize(30)
  17.         Image($r('app.media.icon'))
  18.           .width(150)
  19.           .margin({top:50,bottom:20})
  20.         // 账号登录
  21.         TextInput({placeholder:'账号'})
  22.           .margin(20)
  23.           .height(50)
  24.           .onChange(value =>{
  25.             console.log(value)
  26.             this.zhanghao_find = value
  27.           })
  28.           .backgroundColor('#36D2')
  29.         TextInput({placeholder:'密码'})
  30.           .margin({left:20,right:20,bottom:25})
  31.           .height(50)
  32.           .onChange(value =>{
  33.             console.log(value)
  34.             this.mima_find = value
  35.           })
  36.           .backgroundColor('#36D2')
  37.         Button('登录')
  38.           .width(200)
  39.           .onClick(()=>{
  40.             axios({
  41.               method: "get",
  42.               url: 'http://localhost:3000/find/'+this.zhanghao_find+ '/' + this.mima_find,
  43.             }).then(res => {
  44.               // console.info('result:' + JSON.stringify(res.data));
  45.               console.info('result:' + JSON.stringify(res.data));
  46.                 router.pushUrl({
  47.                   url: 'pages/NewApp_one',
  48.                 })
  49.             }).catch(error => {
  50.               console.error(error);
  51.             })
  52.           })
  53.         Row(){
  54.           Text('注册')
  55.             .margin({right:5})
  56.             .onClick( () =>{
  57.               {
  58.                 router.pushUrl({
  59.                   url: 'pages/zhuce',
  60.                 })
  61.               }
  62.             })
  63.           Text('|')
  64.           Text('忘记密码')
  65.             .margin({left:5})
  66.             .onClick( () =>{
  67.               {
  68.                 router.pushUrl({
  69.                   url: 'pages/WangjiMima',
  70.                 })
  71.               }
  72.             })
  73.         }.margin(20)
  74.       }
  75.       .width('100%')
  76.     .height('100%')
  77.   }
  78. }
复制代码
NewApp_one.ets

  1. @Entry
  2. @Component
  3. struct NewApp_one {
  4.   @State message: string = 'app主页'
  5.   build() {
  6.     Row() {
  7.       Column() {
  8.         Text(this.message)
  9.           .fontSize(50)
  10.           .fontWeight(FontWeight.Bold)
  11.       }
  12.       .width('100%')
  13.     }
  14.     .height('100%')
  15.   }
  16. }
复制代码
WangjiMima.ets

  1. import { Header } from '../components/Toubu'
  2. import  axios  from '@ohos/axios'
  3. import router from '@ohos.router'
  4. @Entry
  5. @Component
  6. struct Index {
  7.   // 上传数据
  8.   @State zhanghao: string = ''
  9.   @State mima: string = ''
  10.   build() {
  11.     Column() {
  12.       Header()
  13.         .margin(20)
  14.       TextInput({placeholder:'原账号'})
  15.         .margin(20)
  16.         .height(50)
  17.         .onChange(value =>{
  18.           console.log(value)
  19.           this.zhanghao = value
  20.         })
  21.         .backgroundColor('#36D2')
  22.       TextInput({placeholder:'新密码'})
  23.         .margin({ left:20,right:20,bottom:20 })
  24.         .height(50)
  25.         .onChange(value =>{
  26.           console.log(value)
  27.           this.mima = value
  28.         })
  29.         .backgroundColor('#36D2')
  30.       Button('修改密码')
  31.         .width(200)
  32.         .onClick(()=>{
  33.           axios({
  34.             method: "post",
  35.             url: 'http://localhost:3000/upd',
  36.             data:{
  37.               zhanghao:this.zhanghao,
  38.               newmima:this.mima
  39.             },
  40.           }).then(res => {
  41.             console.info('result:' + JSON.stringify(res.data));
  42.             {
  43.               router.pushUrl({
  44.                 url: 'pages/NewApp_one',
  45.               })
  46.             }
  47.           }).catch(error => {
  48.             console.error(error);
  49.           })
  50.         })
  51.     }
  52.     .width('100%')
  53.     .height('100%')
  54.   }
  55. }
复制代码
zhuce.ets

  1. import { Header } from '../components/Toubu'
  2. import  axios  from '@ohos/axios'
  3. import router from '@ohos.router'
  4. @Entry
  5. @Component
  6. struct Index {
  7.   // 上传数据
  8.   @State zhanghao: string = ''
  9.   @State mima: string = ''
  10.   @State zhanghao_find:string =''
  11.   @State mima_find:string =''
  12.   build() {
  13.     Column() {
  14.       Header()
  15.         .margin(20)
  16.       TextInput({placeholder:'注册账号'})
  17.         .margin(20)
  18.         .height(50)
  19.         .onChange(value =>{
  20.           console.log(value)
  21.           this.zhanghao = value
  22.         })
  23.         .backgroundColor('#36D2')
  24.       TextInput({placeholder:'注册密码'})
  25.         .margin({ left:20,right:20,bottom:20 })
  26.         .height(50)
  27.         .onChange(value =>{
  28.           console.log(value)
  29.           this.mima = value
  30.         })
  31.         .backgroundColor('#36D2')
  32.       Button('注册并登录')
  33.         .width(200)
  34.         .onClick(()=>{
  35.           axios({
  36.             method: "post",
  37.             url: 'http://localhost:3000/publish',
  38.             data:{
  39.               zhanghao:this.zhanghao,
  40.               mima:this.mima
  41.             },
  42.           }).then(res => {
  43.             console.info('result:' + JSON.stringify(res.data));
  44.               router.pushUrl({
  45.                 url: 'pages/NewApp_one',
  46.               })
  47.           }).catch(error => {
  48.             console.error(error);
  49.           })
  50.         })
  51.     }
  52.     .width('100%')
  53.     .height('100%')
  54.   }
  55. }
复制代码
后端代码node.js

  1. const express = require('express');
  2. const app = express();
  3. const { users } = require('./db');
  4. app.use(express.urlencoded({ extended: true }));
  5. app.use(express.json())
  6. // 注册账号
  7. app.post("/publish", async (req, res) => {
  8.     try {
  9.         const { zhanghao, mima } = req.body;
  10.         await users.create({
  11.             zhanghao, mima
  12.         });
  13.         res.send("success")
  14.     } catch (error) {
  15.         res.send(error, "error")
  16.     }
  17. })
  18. // 注销账号
  19. app.post("/del", async (req, res) => {
  20.     console.log(req.body.zhanghao)
  21.     try {
  22.         const { zhanghao } = req.body;
  23.         // 使用 deleteOne 删除指定 name 的数据
  24.         const result = await users.deleteOne({ zhanghao });
  25.         if (result.deletedCount === 1) {
  26.             res.send("success");
  27.         } else {
  28.             res.send("未找到匹配的记录");
  29.         }
  30.     } catch (error) {
  31.         res.send(error, "error");
  32.     }
  33. })
  34. // 修改账号密码
  35. app.post("/upd", async (req, res) => {
  36.     try {
  37.         const { zhanghao, newmima } = req.body;
  38.         // 使用 updateOne 更新指定 name 的数据记录的 nianling 字段
  39.         const result = await users.updateOne({ zhanghao }, { $set: { mima: newmima } });
  40.         res.json({ message: "密码更新成功!", result });
  41.     } catch (error) {
  42.         res.status(500).json({ error: error.message });
  43.     }
  44. });
  45. // 账号登录
  46. app.get("/find/:zhanghao/:mima", async (req, res) => {
  47.     try {
  48.         const zhanghao = req.params.zhanghao;
  49.         const mima = req.params.mima;
  50.         // 使用 find 查询所有匹配指定 name 的数据记录
  51.         const results = await users.find({ zhanghao, mima });
  52.         if (results.length > 0) {
  53.             // 如果找到匹配的记录,则返回所有匹配的记录
  54.             res.json({ data: results, message: "登录成功!" });
  55.         } else {
  56.             res.status(404).json({ message: "未找到匹配的记录" });
  57.         }
  58.     } catch (error) {
  59.         res.status(500).json({ message: "服务器内部错误" });
  60.     }
  61. });
  62. app.listen(3000, () => {
  63.     console.log('server running')
  64. })
复制代码
效果图



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4