更新记录

1.0.3(2021-05-24)

更新项目名称

1.0.2(2021-05-24)

更新了项目简介

1.0.1(2021-05-24)

测试加密云函数

查看更多

平台兼容性

阿里云 腾讯云 支付宝云
×
Vue2 Vue3
×
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.1.0 × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × ×

云函数类插件通用教程

使用云函数类插件的前提是:使用HBuilderX 2.9+


拍照识花的项目搭建

项目模板概述:

1. 基于Unicloud研发,部署简单;

2. 基于百度图像识别API,使用简单;

3. 配套植物识别主题UI,用户体验好;

4. 更多功能,持续更新;

后续规划

1.集成地图SDK

2.新增我的上传管理功能

3.集成分享点赞功能

效果图: https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled.png

真实项目体验:

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled.png

第一步

创建了unicloud 的服务空间列表

https://unicloud.dcloud.net.cn/home

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled.png

4aQAqLN8xdfv4UpUxlDIr2Hc
9YNGePhk7yfbpDeLiXkiX8rFGz3tTA8G

第二步

创建百度AI https://console.bce.baidu.com/ai/?_=1621755182746#/ai/imagerecognition/app/create

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%201.png

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%202.png

创建uni项目

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%203.png

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%204.png

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%205.png

完成初始化后

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%206.png

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%207.png

package.json

{
  "dependencies": {
    "axios": "^0.21.1",
    "dayjs": "^1.10.4"
  }
}
//云函数目录下新建package.json
npm install 

云函数代码


'use strict';
const axios = require('axios')
const dayjs = require('dayjs')
exports.main = async (event, context) => {
    const db = uniCloud.database();
    const dbCmd = db.command
    let appId = '4aQAqLN8xdfv4UpUxlDIr2Hc';
    let tokens = await db.collection('baiduToken').where({
        appId
    })
        .get();
    if (tokens.data.length && dayjs().valueOf() < tokens.data[0].expireTimestamp) {
        return {
            access_token: tokens.data[0].access_token
        }
    } else {
        let result = await axios(
            {
                method: "GET",
                url: 'https://aip.baidubce.com/oauth/2.0/token',
                params: {
                    grant_type: "client_credentials",
                    client_id: appId,
                    client_secret: '9YNGePhk7yfbpDeLiXkiX8rFGz3tTA8G',
                },
            })
        let res = await db.collection('baiduToken').where({
            appId
        }).remove()
        let tokens = await db.collection('baiduToken')
            .add({
                appId,
                expireTimestamp: dayjs().valueOf() + result.data.expires_in,
                access_token: result.data.access_token
            });
        return {
            access_token: result.data.access_token
        }
    }
};

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%208.png

数据库创建

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%209.png

{
  "bsonType": "object",
  "required": [],
  "permission": {
    "read": false,
    "create": false,
    "update": false,
    "delete": false
  },
  "properties": {
    "access_token": {
      "bsonType": "string"
    },
    "appId": {
      "bsonType": "string"
    },
    "_id": {
      "description": "ID,系统自动生成"
    },
    "expireTimestamp": {
      "bsonType": "int",
      "description": "过期时间"
    }
  }
}

页面样式调整

略过

https://nongmo-plant.oss-cn-beijing.aliyuncs.com/chajiandasai/Untitled%2010.png

页面代码略过

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问