专栏名称: 安信可科技
全球领先的联网模组、智能家居等物联网硬件方案提供商。
目录
相关文章推荐
青年文摘  ·  商场一层的“温柔陷阱”,专坑年轻人 ·  2 天前  
洞见  ·  一个人最大的无能:抱怨家人 ·  5 天前  
深夜书屋  ·  终于等来了,绝版增订重印 ·  3 天前  
青年文摘  ·  “林黛玉倒拔垂杨柳”,好笑在哪里? ·  4 天前  
51好读  ›  专栏  ›  安信可科技

如何用爱星物联平台做自己专属的美食食谱?

安信可科技  · 公众号  ·  · 2024-04-30 23:08

正文

爱星物联平台提供了很多 API,想要开发一个独立业务,又不想要和平台提供 API 项目糅合在一起?那小伙伴们可以考虑增加新的自定义接口服务和业务服务。


今天来试试弄一个自己的业务 API 服务,用来实现厨房智能小家电里常用的食谱模块吧。


创建服务:

1、搭建新的业务 API 服务,需要用到 iot_demo_api_service 模板;

2、搭建新的业务服务,需要用到 iot_demo_service 模板;

我们先将模板目录 Copy 出来,修改里面的项目名称,这里我将项目名称定义为 iot_recipes_api_service、iot_recipes_service ,这个两个服务专门用于给厨房类家电提供制作食谱相关业务接口;


数据库表创建:

初步设计有如下数据表:

食谱类型表
食谱信息表
食材表
食材类型表
食谱步骤表
食谱步骤食材关联表
食谱评论评分表
食谱赞、踩、收藏表


业务代码生成:

使用代码目录的代码生成工具,生成食谱业务的所有代码

在生成之前,我们先编辑下代码生成工具的配置文件


编辑 gen.bat 文件



执行 gen.bat,实现食谱模块的所有代码生成

将生成所得的 convert、handler、service 目录直接复制到 iot_recipes_service 服务的根目录,注意 handler 目录有一个 handler 注册的方法需要手写,你需要打开手动编写下,将所有 handler 进行注册;



进入到 iot_model 目录,创建目录 db_recipes,生成和存放食谱相关的 model 和 orm,创建 gentoo.bat 文件,添加如下代码:


执行 gentoo.bat,得到生成后的 model 和 orm

经过如上操作,基础功能的添删改查就完成。



App api 接口实现:

APP 中增加食谱类型表、食谱信息表、食材表、食材类型表、食谱步骤表数据查询接口,增加食谱评论评分表、食谱赞、踩、收藏表维护功能



//食谱




    
appappi.GET("/recipesInfo/detail/:id", apis.RecipesInfocontroller.GetRecipesInfoDetail)appappi.GET("/recipesInfo/list", apis.RecipesInfocontroller.QueryRecipesInfoList)
//食谱类型appappi.GET("/recipesType/list", apis.RecipesTypecontroller.QueryRecipesTypeList)
//食材appappi.GET("/food/detail/:id", apis.Foodcontroller.GetFoodDetail)appappi.GET("/food/list", apis.Foodcontroller.QueryFoodList)
//食材类型appappi.GET("/foodType/list", apis.FoodTypecontroller.QueryFoodTypeList)
//增加食谱评论评分表appappi.GET("/recipesComment/list", apis.RecipesInfocontroller.QueryRecipesCommentList)appappi.GET("/recipesComment/add", apis.RecipesInfocontroller.AddRecipesComment)
//食谱赞、踩、收藏表appappi.GET("/recipesComment/setGood", apis.RecipesCommentcontroller.SetGood)appappi.GET("/recipesComment/setBad", apis.RecipesCommentcontroller.SetBad)appappi.GET("/recipesComment/setLike", apis.RecipesCommentcontroller.SetBad)





Cloud api 接口实现(添加到 iot_cloud_api_service 服务):

APP 中增加食谱类型表、食谱信息表、食材表、食材类型表、食谱步骤表配置功能接口,增加食谱评论评分表、食谱赞、踩、收藏表查询功能;


//食谱webapi.GET("/recipesInfo/detail/:id", apis.RecipesInfocontroller.GetRecipesInfoDetail)webapi.GET("/recipesInfo/list", apis.RecipesInfocontroller.QueryRecipesInfoList)webapi.POST("/recipesInfo/add", apis.RecipesInfocontroller.AddRecipesInfo)webapi.POST("/recipesInfo/edit", apis.RecipesInfocontroller.EditRecipesInfo)webapi.POST("/recipesInfo/publish", apis.RecipesInfocontroller.PublishRecipesInfo)webapi.POST("/recipesInfo/delete/:id", apis.RecipesInfocontroller.DeleteRecipesInfo)
//食谱类型webapi.GET("/recipesType/detail/:id", apis.RecipesTypecontroller.GetRecipesTypeDetail)webapi.GET("/recipesType/list", apis.RecipesTypecontroller.QueryRecipesTypeList)webapi.POST("/recipesType/add", apis.RecipesTypecontroller.AddRecipesType)webapi.POST("/recipesType/edit", apis.RecipesTypecontroller.EditRecipesType)webapi.POST("/recipesType/delete/:id", apis.RecipesTypecontroller.DeleteRecipesType)
//食材webapi.GET("/food/detail/:id", apis.Foodcontroller.GetFoodDetail)webapi.GET("/food/list", apis.Foodcontroller.QueryFoodList)webapi.POST("/food/add", apis.Foodcontroller.AddFood)webapi.POST("/food/edit", apis.Foodcontroller.EditFood)webapi.POST("/food/delete/:id", apis.Foodcontroller.DeleteFood)
//食材类型webapi.GET("/foodType/detail/:id", apis.FoodTypecontroller.GetFoodTypeDetail)webapi.GET("/foodType/list", apis.FoodTypecontroller.QueryFoodTypeList)webapi.POST("/foodType/add", apis.FoodTypecontroller.AddFoodType)webapi.POST("/foodType/edit", apis.FoodTypecontroller.EditFoodType)webapi.POST("/foodType/delete/:id", apis.FoodTypecontroller.DeleteFoodType)
//食谱步骤webapi.GET("/recipesSteps/list", apis.RecipesStepscontroller.QueryRecipesStepsList)webapi.POST("/recipesSteps/add", apis.RecipesStepscontroller.AddRecipesSteps)webapi.POST("/recipesSteps/edit", apis.RecipesStepscontroller.EditRecipesSteps)webapi.POST("/recipesSteps/delete/:id", apis.RecipesStepscontroller.DeleteRecipesSteps)
//食谱步骤食材关联表webapi.GET("/recipesStepRe/list", apis.RecipesStepRecontroller.QueryRecipesStepList)webapi.POST("/recipesStepRe/add", apis.RecipesStepRecontroller.AddRecipesStep)webapi.POST("/recipesStepRe/delete/:id", apis.RecipesStepRecontroller.DeleteRecipesStep)
//食材步骤关联webapi.GET("/recipesStepRe/list", apis.RecipesInfocontroller.QueryRecipesStepReList)webapi.POST("/recipesStepRe/add", apis.RecipesInfocontroller.AddRecipesStepRe)webapi.POST("/recipesStepRe/delete/:id", apis.RecipesInfocontroller.DeleteRecipesStepRe)
//增加食谱评论评分表webapi.GET("/recipesComment/list", apis.RecipesInfocontroller.QueryRecipesCommentList)webapi.GET("/recipesComment/repley", apis.RecipesInfocontroller.AddRecipesComment)


前端功能实现:

开放平台增加食谱管理、食谱类型管理、食材管理、食材类型管理、食材统计等功能页面;

前端大部分组件使用的 ant-design-vue 的原生组件,直接参照官方文档就可以开发了;另外框架自定义了 ChartCard、ColorPicker、DemoStep、detailChange、Editor、GlobalHeader、IconFont、IconSelector、ImgCutterDialog、MultiTab、NoData、NProgress、PreviewModal、SelectLang、tableCard、uploadButton、uploadCard、VueQrCode 组件,你可以参照已实现示例进行使用;


APP 开发:

因为平台提供的开源版 APP 我没有拿到源代码,无法直接修改,于是我想到一个非常特别的方式来实现 APP 的功能,就是我创建了一个食谱的产品,然后编写了一套食谱的控制面板,这个食谱的产品默认添加给每一个注册用户,然后食谱面板中是完整的食谱 APP 功能。

于是我写了一个食谱的 H5 项目,作为产品的方式默认引入到 APP 中, 当然你可以使用官方提供的面板的模板进行改造,这样可以使用到于原生交互和设备控制的功能。


接下来家庭数据获取,为每一个用户提供一个默认食谱面板入口;

找到:

iot_app_api_service/controls/user/services/home_service.go,在大概 196 行的位置增加默认食谱面板的引用,以产品面板的方式加载食谱模块。

注意:deviceList 中的设备类型修改 3,这样就不会出现开关图标了。


完美!!

我让爱星物联开源 APP







请到「今天看啥」查看全文