随手精灵小程序,单独为了获取WIFI信息加强围栏告警功能能力
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3KB

  1. // 云函数入口文件
  2. const cloud = require('wx-server-sdk')
  3. cloud.init()
  4. // 云函数入口函数
  5. exports.main = async (event, context) => {
  6. console.log(event)
  7. switch (event.action) {
  8. case 'requestSubscribeMessage': {
  9. return requestSubscribeMessage(event)
  10. }
  11. case 'sendSubscribeMessage': {
  12. return sendSubscribeMessage(event)
  13. }
  14. case 'getWXACode': {
  15. return getWXACode(event)
  16. }
  17. case 'getOpenData': {
  18. return getOpenData(event)
  19. }
  20. default: {
  21. return
  22. }
  23. }
  24. }
  25. async function requestSubscribeMessage(event) {
  26. // 此处为模板 ID,开发者需要到小程序管理后台 - 订阅消息 - 公共模板库中添加模板,
  27. // 然后在我的模板中找到对应模板的 ID,填入此处
  28. return '请到管理后台申请模板 ID 然后在此替换' // 如 'N_J6F05_bjhqd6zh2h1LHJ9TAv9IpkCiAJEpSw0PrmQ'
  29. }
  30. async function sendSubscribeMessage(event) {
  31. const { OPENID } = cloud.getWXContext()
  32. const { templateId } = event
  33. const sendResult = await cloud.openapi.subscribeMessage.send({
  34. touser: OPENID,
  35. templateId,
  36. miniprogram_state: 'developer',
  37. page: 'pages/openapi/openapi',
  38. // 此处字段应修改为所申请模板所要求的字段
  39. data: {
  40. thing1: {
  41. value: '咖啡',
  42. },
  43. time3: {
  44. value: '2020-01-01 00:00',
  45. },
  46. }
  47. })
  48. return sendResult
  49. }
  50. async function getWXACode(event) {
  51. // 此处将获取永久有效的小程序码,并将其保存在云文件存储中,最后返回云文件 ID 给前端使用
  52. const wxacodeResult = await cloud.openapi.wxacode.get({
  53. path: 'pages/openapi/openapi',
  54. })
  55. const fileExtensionMatches = wxacodeResult.contentType.match(/\/([^/]+)/)
  56. const fileExtension = (fileExtensionMatches && fileExtensionMatches[1]) || 'jpg'
  57. const uploadResult = await cloud.uploadFile({
  58. // 云文件路径,此处为演示采用一个固定名称
  59. cloudPath: `wxacode_default_openapi_page.${fileExtension}`,
  60. // 要上传的文件内容可直接传入图片 Buffer
  61. fileContent: wxacodeResult.buffer,
  62. })
  63. if (!uploadResult.fileID) {
  64. throw new Error(`upload failed with empty fileID and storage server status code ${uploadResult.statusCode}`)
  65. }
  66. return uploadResult.fileID
  67. }
  68. async function getOpenData(event) {
  69. return cloud.getOpenData({
  70. list: event.openData.list,
  71. })
  72. }