【Serverless】云函数微信小程序( 三 )


【Serverless】云函数微信小程序

文章插图
【Serverless】云函数微信小程序

文章插图
?
代码如下:
index.wxml:
<view class="container"><view><form catchsubmit="formSubmit" catchreset="formReset"><text>AGC-function-Demo</text><text>\n</text><text>\n</text><view class="weui-cell__bd" style="margin: 30rpx 0"><input class="weui-input" name="httpTrigger" placeholder="input your HttpTrigger" /></view><view class="weui-cell__bd" style="margin: 30rpx 0"><input class="weui-input" name="body" placeholder="input your Body" /></view><text>\n</text><view class="btn-area"><button style="margin: 30rpx 0" type="primary" formType="submit" data-type="run">run</button></view></form></view><view class="text-box"><text class="text-content">{{functionRes}}</text></view></view>
【Serverless】云函数微信小程序

文章插图
【Serverless】云函数微信小程序

文章插图
index.wxss:
/**index.wxss**/.container {padding: 0 0;}.userinfo {color: #aaa;}.userinfo-avatar {overflow: hidden;width: 128rpx;height: 128rpx;margin: 20rpx;border-radius: 50%;}.section_switch {display: flex;align-items: center;justify-content: space-around;}.text-box {margin: 20rpx;}.text-content {word-break: break-all;}
【Serverless】云函数微信小程序

文章插图
【Serverless】云函数微信小程序

文章插图
云函数开发云函数最主要的功能就是在端侧触发云测的函数,首先调用wrap接口通过触发器标识指定需要触发的函数,然后调用call接口将函数的入参传入并触发函数 。
formSubmit: function (e) {console.log(e);var _a = e.detail.value, httpTrigger = _a.httpTrigger, body = _a.body;switch (e.detail.target.dataset.type) {case 'run':this.run(httpTrigger, body);break;default:break;}},run: function (httpTrigger, body) {var _this = this;var functionCallable = agconnect.function().wrap(httpTrigger);functionCallable.call(body).then(function (res) {_this.setData({functionRes: JSON.stringify(res.getValue())});});}
【Serverless】云函数微信小程序

文章插图
【Serverless】云函数微信小程序

文章插图
在初始化时输入刚刚复制的应用配置信息
var agConnectConfig = {//应用配置信息}//初始化agcagconnect.instance().configInstance(agConnectConfig);
【Serverless】云函数微信小程序

文章插图
【Serverless】云函数微信小程序

文章插图
打包测试1.打开微信开发者工具 , 选择编译 。
2. 打开页面后再HttpTrigger中填写之前在创建触发器时获取的触发器标识,再Function Body中添加触发的入参 。
【Serverless】云函数微信小程序

文章插图
【Serverless】云函数微信小程序

文章插图
?
3.点击run后查看result 。
打印结果显示2022是虎年,说明测试成功
【Serverless】云函数微信小程序

文章插图
【Serverless】云函数微信小程序

文章插图
?
恭喜您祝贺您 , 您已经成功地构建了您的第一个应用程序,并学到了:
  • 如何将方法函数添加为AppGallery Connect的云函数 。
  • 如何测试添加的云函数 。
  • 如何集成AppGallery Connect云函数的SDK并在本地调用云函数 。
参考文件
  • 详细的云函数配置开发指南请参见云函数服务开发指南 。
  • 云函数的相关API介绍请参见云函数API参考 。
欲了解更多更全技术文章 , 欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

推荐阅读