【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

推薦閱讀