openapi.security.msgSecCheck在环境共享下无法调用的解决方案

小程序A,将云开发环境共享给小程序B,在小程序B里调用共享的云函数,里面调用openapi.security.msgSecCheck做文本检测,接口返回errCode:43104,openapi.security.msgSecCheck:fail The openid does not match the appid rid: 63636e51-10c81478-705b4947

由于是共享环境,所以入参openid只能拿到小程序B的openid,我没法拿到A的openid啊。但调用的云函数默认用的是小程序A的appid,所以造成了不匹配。

============解决方法====================

const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const opt = {}
exports.main = async event => {
  const wxc = cloud.getWXContext()
  opt.appid = wxc.FROM_APPID || wxc.APPID//获取环境或共享环境的访问端小程序appid
  if (event.action == 'security.imgSecCheck') return await imgSecCheck(event)//大图片安全检查
  if (event.action == 'xxx') return await xxx(event)//其他特殊处理
  return await cloud.openapi(opt)[event.action](event.body || {})
}
async function imgSecCheck(event) {
  let res = await cloud.downloadFile({
    fileID: event.fileID,
  })
  return await cloud.openapi(opt).security.imgSecCheck({
    media: {
      contentType: "image/png",
      value: res.fileContent
    }
  })
}

======

前端示例

app.cloud.callFunction({
//app.cloud是小程序当前环境的cloud,在app.js中初始化,可能是wx.cloud,也可能是共享环境的cloud
      name: 'openapi',
      data: {
        action: 'wxacode.getUnlimited',
        body: {
          scene,
          width: 280
        },
      }
    })