微信小程序私密消息

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)
      }
    })
相关推荐
icebreaker14 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker14 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
大米饭消灭者3 天前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro
FliPPeDround4 天前
Vitest Environment UniApp:让 uni-app E2E 测试变得前所未有的简单
微信小程序·e2e·前端工程化
FliPPeDround4 天前
微信小程序自动化的 AI 新时代:wechat-devtools-mcp 智能方案
微信小程序·ai编程·mcp
吴声子夜歌5 天前
小程序——布局示例
小程序
码云数智-大飞5 天前
如何创建自己的小程序,码云数智与有赞平台对比
微信小程序
luffy54595 天前
微信小程序页面使用类似filter函数的wxs语法
微信小程序·小程序
Slow菜鸟5 天前
微信小程序开发(二)目录结构完全指南
微信小程序·小程序
攀登的牵牛花5 天前
给女朋友写了个轻断食小程序:去老丈人家也是先动筷了
前端·微信小程序