《仿盒马》app开发技术分享-- 扫一扫功能(35)

举报
yd_215151764 发表于 2025/06/30 10:35:35 2025/06/30
【摘要】 ## 技术栈Appgallery connect## 开发准备随着app的逐渐完善,我们现在需要在细节处做更多的打磨,在首页我们添加了很多静态的按钮和组件,现在我们开始对这些组件进行功能的添加,这次首先实现的是首页头部的扫一扫功能,扫一扫我们实现扫码后跳转商品详情页## 功能分析要实现扫一扫的功能,我们有两种选择,首先是zxing,然后是scankit,这里我们选择使用scankit,因为它...


## 技术栈

Appgallery connect
## 开发准备
随着app的逐渐完善,我们现在需要在细节处做更多的打磨,在首页我们添加了很多静态的按钮和组件,现在我们开始对这些组件进行功能的添加,这次首先实现的是首页头部的扫一扫功能,扫一扫我们实现扫码后跳转商品详情页


## 功能分析
要实现扫一扫的功能,我们有两种选择,首先是zxing,然后是scankit,这里我们选择使用scankit,因为它针对多种复杂扫码场景做了识别优化,提升扫码成功率与用户体验,我们自己要处理的内容就会少很多,我们主要扫码内容就是商品的id,通过扫描商品id对应的二维码,携带id跳转到对应的商品详情页面,查询出对应的商品详情展示


## 代码实现
首先我们在二维码扫描的按钮添加事件回调

```c
 private  onSearchClick?: () => void
  Image($r('app.media.scan'))
        .width(24)
        .height(24)
        .margin({ left: 12 })
        .onClick(()=>{
          this.onSearchClick!()
        })

```
然后我们在引用组件的地方调用Scankit

```c
   CommonSearchBar({onSearchClick:()=>{
                      let options: scanBarcode.ScanOptions = {
                        scanTypes: [scanCore.ScanType.ALL],
                        enableMultiMode: true,
                        enableAlbum: true
                      };
                      try {
                        scanBarcode.startScanForResult(getContext(this), options).then((result: scanBarcode.ScanResult) => {
                          hilog.info(0x0001, '[Scan CPSample]', `Succeeded in getting ScanResult by promise with options, result is ${JSON.stringify(result)}`);
                          if (result.originalValue!=null) {
                            let product: ProductDetailModel = {
                              id: Number(result.originalValue)
                            };
                            router.pushUrl({
                              url: 'pages/component/ProductDetailsPage',
                              params: product
                            }, (err) => {
                              if (err) {
                                console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`);
                                return;
                              }
                              console.info('Invoke pushUrl succeeded.');
                            });
                          }

                        }).catch((error: BusinessError) => {
                          hilog.error(0x0001, '[Scan CPSample]',
                            `Failed to get ScanResult by promise with options. Code:${error.code}, message: ${error.message}`);
                        });
                      } catch (error) {
                        hilog.error(0x0001, '[Scan CPSample]',
                          `Failed to start the scanning service. Code:${error.code}, message: ${error.message}`);
                      }
                  }})
```
在回调中我们获取了扫码的内容,把扫码内容传递到商品详情页

```c
//先新建一个传递参数的实体
export class ProductDetailModel {
  id: number = 0;
}

//商品详情页根据获取的id查询对应的商品
  let databaseZone = cloudDatabase.zone('default');
   let product = await router.getParams() as ProductDetailModel;
    console.info('Received params:',product);
    let condition1 = new cloudDatabase.DatabaseQuery(home_product_list);
    condition1.equalTo("id",product.id)
    let productDetail = await databaseZone.query(condition1);
    let json = JSON.stringify(productDetail)
    let list:HomeProductList[]= JSON.parse(json)
    this.productParams=list[0]
    hilog.error(0x0000, 'testTag', `Failed to query data, code: ${this.productParams}`);

```
到这里就实现了扫码进入商品详情的功能

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。