【Serverless】快速集成云函數HarmonyOS( 二 )


?
5、配置云函數創建云函數
1.         開通云函數服務后,在云函數界面,點擊“+創建函數” 。

【Serverless】快速集成云函數HarmonyOS

文章插圖
?
2.         在創建函數中,完成函數定義 , 包括函數基本信息,函數部署信息及函數代碼等內容的填寫 。
【Serverless】快速集成云函數HarmonyOS

文章插圖
?
3.         在handler.js的輸入框中輸入以下代碼,來實現加法計算,并點擊右上角“保存”按鈕 。
let myHandler = function(event, context) {var sum;if (event.body) {var reqBody = JSON.parse(event.body)var number1=reqBody.number1var number2=reqBody.number2sum = number1+number2}else {sum = 0}var obj={"result":sum}var res = new context.HTTPResponse(obj, {"res-type": "context.env","faas-content-type": "json"}, "application/json", "200");//send info logcontext.logger.info("this is message of debug log");//send info logcontext.logger.info("this is message of error log");//send error logcontext.logger.error("Test error log");//send responsecontext.callback(res);};module.exports.myHandler = myHandler;
【Serverless】快速集成云函數HarmonyOS

文章插圖
測試函數
1.         您可以通過兩種方式進入函數測試頁面 。
  • 點擊函數詳情界面右上角的“測試”按鈕 。
  • 在Cloud Functions主界面上左側導航欄點擊“函數”,在函數頁面點擊“測試”頁簽 。
    【Serverless】快速集成云函數HarmonyOS

    文章插圖
    ?
2.         選擇剛剛創建的函數及其版本,在事件中輸入如下代碼:
{"number1": 0,"number2": 0}
【Serverless】快速集成云函數HarmonyOS

文章插圖
3.         在執行結果中查看結果,與結果一致:
【Serverless】快速集成云函數HarmonyOS

文章插圖
?
添加觸發器
1.         在函數列表中點擊函數名稱進入函數詳情頁面 。如果是函數別名,則進入函數別名配置頁面 。
2.         點擊配置頁簽下的“+添加觸發器” 。
3.         在“配置觸發器”區域配置“觸發器類型”等信息,此處以HTTP觸發器類型和POST請求方式為例 。
【Serverless】快速集成云函數HarmonyOS

文章插圖
?
4.         完成后點擊“添加”并點擊“保存” 。
5.         將詳細信息中的觸發URL的后綴保存 , 作為客戶端請求時的觸發器標識 。
【Serverless】快速集成云函數HarmonyOS

文章插圖
?
6、界面設計本次Codelab中您可以在DevEco Studio工程中創建一個布局頁面,參照下圖進行UI設計,具備兩個數字的輸入 , 云函數返回求和結果的展示功能即可 。
【Serverless】快速集成云函數HarmonyOS

文章插圖
?
布局文件代碼如下:
<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><DirectionalLayoutohos:width="match_parent"ohos:height="50vp"ohos:alignment="center"ohos:orientation="horizontal"><TextFieldohos:id="$+id:editText_number1"ohos:width="120vp"ohos:height="match_content"ohos:hint="$string:mainability_input_number"ohos:max_text_lines="1"ohos:text_size="20vp"/></DirectionalLayout><DirectionalLayoutohos:width="match_parent"ohos:height="50vp"ohos:alignment="center"ohos:orientation="horizontal"><TextFieldohos:id="$+id:editText_number2"ohos:width="120vp"ohos:height="match_content"ohos:hint="$string:mainability_input_number"ohos:max_text_lines="1"ohos:text_size="20vp" /></DirectionalLayout><DirectionalLayoutohos:width="match_parent"ohos:height="50vp"ohos:alignment="center"ohos:orientation="horizontal"><Buttonohos:id="$+id:btn_add"ohos:width="match_content"ohos:height="match_content"ohos:text_size="20vp"ohos:text="$string:mainability_add_number"/></DirectionalLayout><DirectionalLayoutohos:width="match_parent"ohos:height="50vp"ohos:alignment="center"ohos:orientation="horizontal"><Textohos:width="match_content"ohos:height="match_content"ohos:text="$string:mainability_number_res"ohos:text_size="20vp" /><Textohos:id="$+id:textView_result"ohos:width="match_content"ohos:height="match_content"ohos:text_size="20vp"/></DirectionalLayout></DirectionalLayout>

推薦閱讀