《仿盒马》app开发技术分享-- 设置安全锁(51)

简介: 上一节我们实现了提现页面以及部分组件的业务逻辑,那么我们在提现这一步为了更多的安全层面的考虑,设置了一个安全锁,用户只要开启了安全锁,那么每次的提现,都需要把本地的密码提交到云端核对安全锁的内容才可以执行后续的提现步骤,如果不能解锁,那么后续的内容都无法实现,这更好的保护了用户的财产安全

技术栈

Appgallery connect

开发准备

上一节我们实现了提现页面以及部分组件的业务逻辑,那么我们在提现这一步为了更多的安全层面的考虑,设置了一个安全锁,用户只要开启了安全锁,那么每次的提现,都需要把本地的密码提交到云端核对安全锁的内容才可以执行后续的提现步骤,如果不能解锁,那么后续的内容都无法实现,这更好的保护了用户的财产安全

功能分析

要实现这个功能首先我们需要在个人页面添加一个安全入口,然后在安全锁设置页面开启安全锁的按钮,当没有开启安全锁的时候,点击开启按钮会出现一个安全锁的弹窗,在弹窗里设置对应的密码,设置成功后保存对应的内容提交到云端,进行保存,方便后续的校验,关闭安全锁则在云端删除对应的内容

代码实现

首先我们直接在个人页面的列表中添加上对应的数据

  new SectionLine("安全",
        "设置安全锁",
        $r('app.media.anquan'),
        false),

然后给这条数据添加对应的点击事件

if (item.title=='安全'){
   
        if (this.user!=null) {
   
          router.pushUrl({url:'pages/view/LockPage'})

        }else {
   
          showToast("请先登录")

        }
      }

接下来创建安全锁对应的表

{
   
  "objectTypeName": "verify_info",
  "fields": [
    {
   "fieldName": "id", "fieldType": "Integer", "notNull": true, "belongPrimaryKey": true},
    {
   "fieldName": "open_lock", "fieldType": "Boolean"},
    {
   "fieldName": "lock_str", "fieldType": "String"},
    {
   "fieldName": "user_id", "fieldType": "Integer"}

  ],
  "indexes": [
    {
   "indexName": "field1Index", "indexList": [{
   "fieldName":"id","sortType":"ASC"}]}
  ],
  "permissions": [
    {
   "role": "World", "rights": ["Read", "Upsert", "Delete"]},
    {
   "role": "Authenticated", "rights": ["Read", "Upsert", "Delete"]},
    {
   "role": "Creator", "rights": ["Read", "Upsert", "Delete"]},
    {
   "role": "Administrator", "rights": ["Read", "Upsert", "Delete"]}
  ]
}

创建对应的安全锁页面,在页面中设置好对应的参数

 @State user: User|null=null
  @State flag:boolean=false
  @State lockInfo:VerifyInfo|null=null
  @State lock_ui_visibility:boolean=false

 Column({
   space:10}) {
   
      CommonTopBar({
    title: "安全锁", alpha: 0, titleAlignment: TextAlign.Center ,backButton:true})
      Row(){
   
        Text("安全锁")
          .fontColor(Color.Black)
          .fontSize(16)
        Image(this.lock_ui_visibility?$r('app.media.kai'):$r('app.media.guan'))
          .width(60)
          .height(30)
          .onClick(()=>{
   

          })
      }
      .width('100%')
      .justifyContent(FlexAlign.SpaceBetween)
      .padding({
   left:10,right:10})
      Divider().width('100%').height(0.8)
        .color("#e6e6e6")
      .width('100%')

    }.width('100%').height('100%').backgroundColor(Color.White)

接下来我们创建对应的安全锁设置弹窗并整理好对应的逻辑

import {
    LengthUnit } from '@kit.ArkUI';

@Preview
@CustomDialog
export struct LockDialog {
   
  @State passwords: Number[]=[];
  @Link lock_ui_visibility:boolean
  @State message: string = '绘制您的密码图案!';
  public callback:(passwords:string)=>void=():void=>{
   }
  private patternLockController: PatternLockController = new PatternLockController();
  controller: CustomDialogController;
  build() {
   
    Column({
   space:20}) {
   
      Text(this.message).textAlign(TextAlign.Center).margin(20).fontSize(20)
        .fontColor(Color.Black)
      PatternLock(this.patternLockController)
        .sideLength(300)
        .circleRadius(9)
        .pathStrokeWidth(5)
        .activeColor('#707070')
        .selectedColor('#707070')
        .pathColor('#707070')
        .backgroundColor('#F5F5F5')
        .autoReset(true)
        .activateCircleStyle({
   
          color: '#707070',
          radius: {
    value: 16, unit: LengthUnit.VP },
          enableWaveEffect: true
        })
        .onDotConnect((index: number) => {
   
          console.log("onDotConnect index: " + index);
        })
        .onPatternComplete((input: Array<number>) => {
   
          if (input.length < 5) {
   
            this.message = '图案连接数不能小于5';
            return;
          }
          if (this.passwords.length > 0) {
   
            if (this.passwords.toString() === input.toString()) {
   
              this.passwords = input;
              this.message = '图案码设置完成';
              this.patternLockController.setChallengeResult(PatternLockChallengeResult.CORRECT);
              const str: string = JSON.stringify(this.passwords);
              this.callback(str)
              this.controller.close()

            } else {
   
              this.message = '图案码核对失败,请确认后重新绘制.';
              this.patternLockController.setChallengeResult(PatternLockChallengeResult.WRONG);
            }
          } else {
   
            this.passwords = input;
            this.message = "请再次绘制图案码.";
          }
        })
    }
    .borderRadius({
   topLeft:20,topRight:20})
    .justifyContent(FlexAlign.Start)
    .backgroundColor(Color.White)
    .height(500)
    .width('100%')
  }
}

接下来我们在页面内调用

  private dialogController: CustomDialogController = new CustomDialogController({
   
    builder: LockDialog({
   
     lock_ui_visibility:this.lock_ui_visibility,
      callback: async (str:string)=>{
   
        //在这里提交表单信息,成功插入之后关闭弹窗,修改安全锁按钮的状态,重新查询数据赋值lockInfo
        showToast(str)
        let info=new verify_info()
        info.id=Math.floor(Math.random() * 1000000)
        info.open_lock=true
        info.lock_str=str
        info.user_id=this.user!.user_id
        let num = await databaseZone.upsert(info);
        if (num>0) {
   
          this.lock_ui_visibility=true
          this.initLockInfo()
        }

      }
    }),
    alignment: DialogAlignment.Bottom,
    customStyle:false
  });

因为我们提交数据需要用户信息,首次进入页面也需要加载对应的状态所以还需要进行数据查询


  async initLockInfo(){
   
    const value = await StorageUtils.getAll('user');
    if (value != "") {
   
      this.user = JSON.parse(value)
    }
    let databaseZone = cloudDatabase.zone('default');
    let condition = new cloudDatabase.DatabaseQuery(verify_info);
    condition.equalTo("user_id", this.user?.user_id)
    let listData = await databaseZone.query(condition);
    let json = JSON.stringify(listData)
    let data: VerifyInfo[] = JSON.parse(json)
    if (data.length>0) {
   
      this.lock_ui_visibility=true
      this.lockInfo=data[0]
    }else {
   
      this.lock_ui_visibility=false

    }
  }

在生命周期方法内调用

  async aboutToAppear(): Promise<void> {
   


this.initLockInfo()

  }

实现开关的业务逻辑

```css
build() {
Column({space:10}) {
CommonTopBar({ title: "安全锁", alpha: 0, titleAlignment: TextAlign.Center ,backButton:true})
Row(){
Text("安全锁")
.fontColor(Color.Black)
.fontSize(16)
Image(this.lock_ui_visibility?$r('app.media.kai'):$r('app.media.guan'))
.width(60)
.height(30)
.onClick(async ()=>{
if (this.lock_ui_visibility) {
let info=new verify_info()
info.id=this.lockInfo!.id
let num = await databaseZone.delete(info);
if (num>0) {
this.lock_ui_visibility=false
}
}else {
this.dialogController.open()
}
})
}
.width('100%')
.justifyContent(FlexAlign.SpaceBetween)
.padding({left:10,right:10})
Divider().width('100%').height(0.8)
.color("#e6e6e6")
.width('100%')

}.width('100%').height('100%').backgroundColor(Color.White)

}

相关文章
|
6月前
《仿盒马》app开发技术分享-- 确认订单页(数据展示)(29)
上一节我们实现了地址的添加,那么有了地址之后我们接下来的重点就可以放到订单生成上了,我们在购物车页面,点击结算会跳转到一个 订单确认页面,在这个页面我们需要有地址选择、加购列表展示、价格计算、优惠计算、商品数量展示等信息。
190 3
|
6月前
|
存储
《仿盒马》app开发技术分享-- 订单结合优惠券结算(60)
上一节我们已经实现了优惠券的选择,并且成功的把券后的价格也展示给用户,不能使用的优惠券我们也用友好的方式告知用户,这一节我们来实现优惠券内容的下一步,优惠券内容结合订单进行结算提交
145 13
|
6月前
|
存储
《仿盒马》app开发技术分享-- 优惠券展示页(57)
上一节我们实现了优惠券的领取功能,并且在云端已经成功查询出优惠券信息,那么我们需要来实现一个优惠券展示的页面来向用户展示当前账号下的优惠券信息,辅助用户更好的去购买需要的商品,因为优惠券会有多种状态,在展示时也要注意不同状态的区分如何处理
141 9
|
6月前
|
JSON 数据挖掘 数据格式
《仿盒马》app开发技术分享-- 分类左侧列表(17)
上一节我们实现了分类页面的顶部导航栏全选弹窗列表,并实现了跟顶部列表的点击选中联动效果,这一节我们要实现的功能是,分类模块的左侧列表,它同样也需要跟弹窗列表的点击,顶部列表的点击有联动的效果
118 4
|
6月前
|
定位技术 API
《仿盒马》app开发技术分享-- 原生地图展示(26)
上一节我们实现了获取当前用户的位置,并且成功的拿到了经纬度,这一节我们就要根据拿到的经纬度,结合我们其他的知识点来实现地图的展示。
169 4
|
6月前
|
数据库
《仿盒马》app开发技术分享-- 回收订单页功能完善(45)
上一节我们实现了订单的待取件、已取消状态展示,并且成功实现了修改订单状态后的列表刷新,实现了云端数据的修改,这一节我们来实现订单页剩下的两个板块的业务逻辑,分别是运输中、已完成状态下的列表展示以及订单状态的修改
142 1
|
6月前
《仿盒马》app开发技术分享-- 回收金查询页面(48)
上一节我们实现了查看当前账号下的预收益,以及当下收益,并且展示了已完成订单的列表,现在我们可以针对收益来做更多的内容了,在之前的开发中我们在个人中心页面实现了一个静态的金额展示,后续我们将会在这里展示当前账号的总金额,点击当前账号金额进入回收金查询页面,在这个页面我们将会对该账号的回收金进行一系列的操作
142 1
|
6月前
《仿盒马》app开发技术分享-- 兑换商品数据插入(67)
上一节我们实现了积分列表的展示,我们可以更直观的查看当前用户积分的收支情况,但是现在我们只有积分收入并没有消费的地方,所以现在我们开始着手积分兑换相关的内容。这一节我们来实现积分兑换商品的内容
99 0
|
6月前
|
API 数据库
《仿盒马》app开发技术分享-- 我的积分页(63)
上一节我们实现了个人中心页面的业务逻辑优化,成功的在用户登陆退出状态下展示对应的组件内容,这一节我们来实现app中另外一个比较重要的模块---积分模块。
123 0
|
6月前
《伴时匣》app开发技术分享--表单提交准备(4)
上一节我们实现了用户登录功能,现在我们进入首页,可以开始准备着手发布我们的日期计划了,在这之前我们先实现信息表的创建。在首页实现一个标题栏,一个悬浮的按钮。
138 0