《仿盒马》app开发技术分享-- 首页活动配置(5)

## 技术栈

Appgallery connect

## 开发准备

上一篇文章中我们实现了项目端云一体化首页部分模块动态配置,实现了对模块模块的后端控制显示和隐藏,这能让我们的app更加的灵活,也能应对更多的情况。现在我们来对配置模块进行完善,除了已有的模块以外,我们还有一些banner ,活动入口等模块,这些模块的数据并不多,所以我们也归纳到配置中去实现。并且我们在配置表中添加了一些不同的id,我们只需要根据相对应的id 去查询对应的表就可以了

**代码实现**

实现横幅海报,商品活动入口

创建海报横幅表

{

  "objectTypeName": "home_poster",

  "fields": [

    {"fieldName": "id", "fieldType": "Integer", "notNull": true, "belongPrimaryKey": true},

    {"fieldName": "poster_id", "fieldType": "Integer", "notNull": true, "defaultValue": 0},

    {"fieldName": "url", "fieldType": "String"},

    {"fieldName": "router", "fieldType": "String"}

  ],

  "indexes": [

    {"indexName": "posterIdIndex", "indexList": [{"fieldName":"poster_id","sortType":"ASC"}]}

  ],

  "permissions": [

    {"role": "World", "rights": ["Read"]},

    {"role": "Authenticated", "rights": ["Read", "Upsert"]},

    {"role": "Creator", "rights": ["Read", "Upsert", "Delete"]},

    {"role": "Administrator", "rights": ["Read", "Upsert", "Delete"]}

  ]

}

创建商品活动入口表

{

  "objectTypeName": "home_good_center",

  "fields": [

    {"fieldName": "id", "fieldType": "Integer", "notNull": true, "belongPrimaryKey": true},

    {"fieldName": "good_left_id", "fieldType": "Integer", "notNull": true, "defaultValue": 0},

    {"fieldName": "title", "fieldType": "String"},

    {"fieldName": "url", "fieldType": "String"}

  ],

  "indexes": [

    {"indexName": "goodLeftIdIndex", "indexList": [{"fieldName":"good_left_id","sortType":"ASC"}]}

  ],

  "permissions": [

    {"role": "World", "rights": ["Read"]},

    {"role": "Authenticated", "rights": ["Read", "Upsert"]},

    {"role": "Creator", "rights": ["Read", "Upsert", "Delete"]},

    {"role": "Administrator", "rights": ["Read", "Upsert", "Delete"]}

  ]

}

分别填充数据

海报

{

  "cloudDBZoneName": "default",

  "objectTypeName": "home_poster",

  "objects": [

    {

      "id": 10,

      "poster_id": 1,

      "url": "在线图片链接",

      "router": "string1"

    }

  ]

}

商品活动入口

{

  "cloudDBZoneName": "default",

  "objectTypeName": "home_good_center",

  "objects": [

    {

      "id": 10,

      "good_left_id": 1,

      "title": "生鲜严选",

      "url": "在线图片链接"

    },

    {

      "id": 20,

      "good_left_id": 1,

      "title": "西购新品",

      "url": "在线图片链接"

    },

    {

      "id": 30,

      "good_left_id": 1,

      "title": "今日推荐",

      "url": "在线图片链接"

    }

  ]

}

都填充完成后,我们把数据提交到云端,然后进行配置类的同步

接下来我们进行数据查询,因为我们在配置表中添加了id ,所以我们要查询出对应id的活动入口。

  @State homeActivity:HomeActivitySetting[]=[]//首页活动配置

  @State homeGoodCenter:HomeGoodCenter[]=[]//商品活动入口

let listData3 = await databaseZone.query(condition3);

      let json3 = JSON.stringify(listData3)

      let data3:HomeActivitySetting[]= JSON.parse(json3)

      this.homeActivity=data3

      hilog.error(0x0000, 'testTag', `Failed to query data, code: ${this.homeActivity}`);

      let list5 = await databaseZone.query(home_good);

      home_good.equalTo("good_left_id",data3[0].good_left_id);

      let json5 = JSON.stringify(list5)

      let data5:HomeGoodCenter[]= JSON.parse(json5)

      this.homeGoodCenter=data5

      hilog.error(0x0000, 'testTag', `Failed to query data, code: ${this.homeGoodCenter}`);

然后我们修改一下商品活动入口的内容

import { HomeGoodCenter } from "../entity/HomeGoodCenter"

@Component

@Preview

export struct SpecialColumn {

  @Link  goodInfo: HomeGoodCenter[]

  build() {

    Column(){

      List({space:10}){

        ForEach(this.goodInfo,(data:HomeGoodCenter)=>{

          ListItem(){

            Column(){

              Text(data.title)

                .fontSize(16)

                .fontWeight(FontWeight.Bold)

                .fontColor(Color.Black)

              Blank()

              Image(data.url)

                .width('28%')

                .height(90)

                .margin({ bottom: 8 })

                .objectFit(ImageFit.Cover)

            }

            .borderRadius(5)

            .backgroundColor("#ffeedeb8")

            .padding(5)

          }

        })

      }

      .listDirection(Axis.Horizontal)

    }

    .margin({top:10})

  }

}

在首页进行调用

  SpecialColumn({goodInfo:this.homeGoodCenter})

到这里我们就实现了活动配置相关的内容

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容