微信小程序私密消息

1.小程序默认是没有转发功能,除非你实现页面的onShareAppMessage函数

2.通过wx.showShareMenu(Object object)可以控制弹出分享的时候显示什么按钮,注意这个函数不是弹出分享窗口

3.参考官方api文档,设置为私密消息:

wx.updateShareMenu(Object object)
https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.updateShareMenu.html

javascript 复制代码
wx.updateShareMenu({
    withShareTicket: true,
    isPrivateMessage: true, 
    activityId: activityId,
})

其中activityId参考api文档从服务端获取

经过这一步后分享出去的小程序就都不能再二次转发。

4.验证私密消息

wx.authPrivateMessage(Object object)
https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.authPrivateMessage.html

javascript 复制代码
wx.authPrivateMessage({
  shareTicket: 'xxxxxx',
  success(res) {
    console.log('authPrivateMessage success', res)
    // res
    // {
    //   errMsg: 'authPrivateMessage:ok'
    //   valid: true
    //   iv: 'xxxx',
    //   encryptedData: 'xxxxxx'
    // }
  },
  fail(res) {
    console.log('authPrivateMessage fail', res)
  }
})

验证正确就会进入success,错误进入fail

问题:这里有个参数shareTicket,从哪里拿?

方法1:

javascript 复制代码
App({
  onLaunch(options) {
    console.log("onLaunch",options)
    }
})

app.js的onLaunch

方法2:

Object wx.getEnterOptionsSync()

Object wx.getEnterOptionsSync() | 微信开放文档

javascript 复制代码
    const xx = wx.getEnterOptionsSync();
    console.log("xx",xx)
    wx.authPrivateMessage({
      shareTicket: xx.shareTicket,
      success(res) {
        console.log('authPrivateMessage success', res)
        // res
        // {
        //   errMsg: 'authPrivateMessage:ok'
        //   valid: true
        //   iv: 'xxxx',
        //   encryptedData: 'xxxxxx'
        // }
      },
      fail(res) {
        console.log('authPrivateMessage fail', res)
      }
    })
相关推荐
Byte_Me7 小时前
从东南亚出发:小程序容器技术如何助力 App 快速打入全球市场?
小程序
gongzemin10 小时前
微信第三方平台的配置
微信·微信小程序·资讯
一念杂记16 小时前
【实战系列】30分钟开发微信小程序登录&注册&绑定功能
前端·后端·微信小程序
小白_ysf17 小时前
uniapp开发微信小程序(新旧版本对比:授权手机号登录、授权头像和昵称)
微信小程序·小程序·uni-app
kilito_0117 小时前
uniapp 微信小程序 列表点击分享 不同的信息
微信小程序·小程序·uni-app
二饭17 小时前
微信小程序动态切换窗口主题色
微信小程序·小程序
白日依山尽yy17 小时前
Vue、微信小程序、Uniapp 面试题整理最新整合版
vue.js·微信小程序·uni-app
新手小黑吖17 小时前
vscode开发微信小程序
vscode·微信小程序
2501_9160137418 小时前
移动端 WebView 调试实战,多平台行为差异排查与统一调试流程
android·ios·小程序·https·uni-app·iphone·webview