当前位置:网站首页>再次搞定 Ali 云函数计算 FC
再次搞定 Ali 云函数计算 FC
2022-08-04 10:06:00 【InfoQ】
1. 前言
2. 创建函数计算应用/服务/函数
2.1 选择服务模板:

2.2 立即创建:

2.2.1 部署类型:
2.2.2 触发方式:

2.2.3 服务名&函数名:
2.2.4 等待创建:


2.3 必要的配置修改:
2.3.1 自定义域名:


2.3.2 环境变量配置及函数中读取:
env
// 控制器
async getEnv() {
const { ctx } = this;
ctx.body = process.env.APPID;
}
// 路由配置
router.get('/env', controller.home.getEnv);

2.3.3 函数触发器支持 POST:


3. 移植WxSDK 授权代码:
git clonne [email protected]:OSpoon/wechat4node.git
3.1 必要依赖安装:
3.1.1 crypto-js:
service
3.1.2 egg-cors:
// plugin.ts
const plugin: EggPlugin = {
cors: {
enable: true,
package: 'egg-cors',
},
};
// config.default.ts
config.cors = {
origin: '*',
allowMethods: 'GET, PUT, POST,DELETE, PATCH',
};
3.2 关闭 CSRF 安全策略:
// config.default.ts
config.security = {
csrf: {
enable: false, // 关闭csrf
},
};
3.3 移植路由配置:
- checkOrigin:在微信公众平台主动发起的服务验证时使用;
- token:公众号相关 API 授权信息获取接口;
- ticket:公众号相关 API 票据数据获取接口;
- signature:公众号 JS-SDK 在初始化使用的授权信息获取接口;
router.get('/checkOrigin', controller.home.checkOrigin);
router.get('/token', controller.home.token);
router.get('/ticket', controller.home.ticket);
router.post('/signature', controller.home.signature);
3.4 移植控制器:
service
async checkOrigin() {
const { ctx } = this;
const { signature, timestamp, nonce, echostr } = ctx.query;
const result = await ctx.service.weChat.checkSignature(
signature,
timestamp,
nonce,
);
ctx.body = result ? echostr : '';
}
async token() {
const { ctx } = this;
ctx.body = await ctx.service.weChat.getToken();
}
async ticket() {
const { ctx } = this;
ctx.body = await ctx.service.weChat.getTicket();
}
async signature() {
const { ctx } = this;
const { url } = ctx.request.body;
ctx.body = await ctx.service.weChat.genSignature(url);
}
3.5 移植服务层:
service/WeChat.js
3.5.1 常量 APPID 和 APPSECRET 改为使用环境变量读取:
const APPID = process.env.APPID;
const APPSECRET = process.env.APPSECRET;
3.5.1 对象缓存应用:
access_token
ticket
access_token
ticket
const cache = {
token: {
access_token: undefined,
expires_in: 0,
last_time: 0,
},
ticket: {
ticket: undefined,
expires_in: 0,
last_time: 0,
},
};
export default class WeChat extends Service {}
4. 验证云函数执行情况:
4.1 创建 Vue 项目:
- 执行
yarn create vite
创建;
- 添加 js-sdk 脚本到 index.html:
<script src="http://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
- 安装axios完成接口数据交换;
4.2 移植前端代码:
<script setup>
import { ref } from "vue";
import axios from "axios";
const message = ref("");
const status = ref("");
axios
.post(`http://wx.it200.cn/signature`, {
url: location.href.split("#")[0],
})
.then((res) => {
const { appId, nonceStr, signature, timestamp } = res.data;
console.log("[ res ] >", appId, nonceStr, signature, timestamp);
wx.config({
debug: true, // 开启调试模式,调用的所有 api 的返回值会在客户端 alert 出来,若要查看传入的参数,可以在 pc 端打开,参数信息会通过 log 打出,仅在 pc 端时才会打印。
appId, // 必填,公众号的唯一标识
timestamp, // 必填,生成签名的时间戳
nonceStr, // 必填,生成签名的随机串
signature, // 必填,签名
jsApiList: ["updateAppMessageShareData"], // 必填,需要使用的 JS 接口列表
});
wx.error((res) => {
message.value = res.errMsg;
});
wx.ready(() => {
wx.checkJsApi({
jsApiList: ["updateAppMessageShareData"], // 需要检测的 JS 接口列表,所有 JS 接口列表见附录2,
success: (res) => {
wx.updateAppMessageShareData({
title: "我的掘金", // 分享标题
desc: "我在掘金输出前端知识~", // 分享描述
link: "https://juejin.cn/user/3966693685871694", // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致
imgUrl:
"https://p9-passport.byteacctimg.com/img/user-avatar/71ca4682501063257d8413ff726105a0~300x300.images", // 分享图标
success: function () {
status.value = "设置成功";
},
});
},
});
});
});
</script>
<template>
<h3 v-if="message">{{ message }}</h3>
<h3 v-else>
点击右上角=>分享给朋友
<h5>{{ status }}</h5>
</h3>
</template>
4.3 重新配置环境变量和触发器:
s.yaml
4.4 验证云函数执行:

5. 总结
边栏推荐
猜你喜欢
随机推荐
Four common methods of network attacks and their protection
移动端 开源低代码工具 beeware 和 kivy
LeetCode中等题之旋转图像
在测试集上训练,还能中CVPR?这篇IEEE批判论文是否合理?
罗克韦尔AB PLC RSLogix5000中定时器指令使用方法介绍
密码字典生成工具pydictor/john
双重for循环案例以及while循环和do while循环案例
matlab练习程序(多线段交点)
Cloud function to achieve automatic website check-in configuration details [Web function/Nodejs/cookie]
关于技术学习的6个观点
SVG 的 path 属性绘制图形
学习在php中将特大数字转成带有千/万/亿为单位的字符串
Could you please talk about how the website is accessed?[Interview questions in the web field]
MindSpore:Ascend运行出现问题
Techwiz OLED:OLED器件的发光效率
leetcode动态规划经典例题——53.最大子数组和
MySQL core SQL: SQL structured query statements, library, table operation, CRUD
MySQL:面试问的范式设计
iMeta | 德国国家肿瘤中心顾祖光发表复杂热图(ComplexHeatmap)可视化方法
Acwing 3208. Z字形扫描 偏移量+扩展图