小程序各种功能代码片段整理---持续更新

*—

—*

目录引导:

  1. 轮播功能
  2. 小程序客服会话功能
  3. 转发/分享功能 小卡片设置
  4. 获取微信用户的头像和名称(不弹窗的方法)
  5. 新页面跳转(子页面返回)
  6. 更改单个页面顶部导航栏的名字
  7. Canvas导出图片
  8. 小程序弹窗提示 wx.showToast()
  9. 九宫格方式上传图片(预览并删除)
  10. 底部导航
  11. 小程序MD5加密写法(支持加密中文)
  12. 锚点效果
  13. 导航栏吸顶效果
  14. 获取屏幕当前高度并赋值给某个view
  15. 图片裁剪功能
  16. 解决 分享出去的页面没有返回按钮的方法
  17. 点击按钮返回上一页并传参
  18. tab切换功能
  19. 使用setData修改data中子对象的属性值
  20. 小程序验证手机号、60秒验证码(正则)
  21. 获取点击的列表的index

1、轮播功能

<swiper indicator-dots="{
   {
   indicatorDots}}" autoplay="{
   {
   autoplay}}" interval="{
   {
   interval}}" duration="{
   {
   duration}}" circular="{
   {
   duration}}" current="{
   {
   swiperCurrent}}" bindchange="swiperChange" class="swiper">
  <block wx:for="{
   {
   imgUrls}}" wx:key="unique">
    <swiper-item>
      <image src="{
   {
   item}}" class="img" bindtap="swipclick" />
    </swiper-item>
  </block>
</swiper>
/* swiper {
    height: 421.5rpx;
} */
swiper-item image {
    width: 100%;
    height: 100%;
}
.swiper-container{
  width: 100%;
  position: relative;
}
.swiper-container .swiper{
  height: 300rpx;
}
.swiper-container .swiper .img{
  width: 100%;
  height: 100%;
}
const app = getApp()
Page({
  data: {
    swiperCurrent: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,//自动切换时间间隔
    duration: 800,//滑动动画时长
    circular: true,//是否采用衔接滑动
    imgUrls: [
      '../../img/index/1.jpeg',
      '../../img/index/2.jpeg',
      '../../img/index/3.jpeg'
    ]
  },
  //轮播图的切换事件
  swiperChange: function (e) {
   
    this.setData({
      swiperCurrent: e.detail.current
    })
    //console.log(e.detail.current);
  },
  //点击指示点切换
  chuangEvent: function (e) {
   
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },
  //点击图片触发事件
  swipclick: function (e) {
   
    console.log(this.data.swiperCurrent);
    wx.switchTab({
      url: this.data.links[this.data.swiperCurrent]
    })
  },
})

2、小程序客服会话功能

<button class="kf_button" open-type="contact" session-from="weapp">
    客服按钮
</button>

微信平台 - 小程序绑定客服页面
微信平台 - 小程序客服会话窗口

3、转发/分享功能 小卡片设置

<button data-name="shareBtn" open-type="share" plain="true">转发</button>

PS: 添加plain=”true”后button的边框样式可自定义 ↓ ↓

button[plain]{ 
  border:0 
} 
 //转发
  onShareAppMessage: function (options) {
   
      var that = this;
      // 设置菜单中的转发按钮触发转发事件时的转发内容
      var shareObj = {
          title: "这是一个标题!",        // 默认是小程序的名称(可以写slogan等)
          //path: '/page/index/index/user?id=123',        // 默认是当前页面,必须是以‘/’开头的完整路径
          imageUrl: '../../img/xiaochengxu-share.jpg',     //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
          success: function (res) {
   
              // 转发成功之后的回调
              if (res.errMsg == 'shareAppMessage:ok') {
              }
          },
          fail: function (res) {
   
              // 转发失败之后的回调
              if (res.errMsg == 'shareAppMessage:fail cancel') {
                  // 用户取消转发
                       console.log("用户取消转发");
              } else if (res.errMsg == 'shareAppMessage:fail') {
                  // 转发失败,其中 detail message 为详细失败信息
        
  • 11
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的self-attention代码片段,用于对输入序列进行自注意力计算: ``` class SelfAttention(nn.Module): def __init__(self, input_dim, hidden_dim): super(SelfAttention, self).__init__() self.query_linear = nn.Linear(input_dim, hidden_dim) self.key_linear = nn.Linear(input_dim, hidden_dim) self.value_linear = nn.Linear(input_dim, hidden_dim) def forward(self, x): # 计算query、key、value向量 query = self.query_linear(x) key = self.key_linear(x) value = self.value_linear(x) # 计算注意力分数 scores = torch.matmul(query, key.transpose(-2, -1)) scores = scores / math.sqrt(query.size(-1)) # 计算注意力权重 attention_weights = nn.functional.softmax(scores, dim=-1) # 计算加权和 weighted_values = torch.matmul(attention_weights, value) output = weighted_values.sum(dim=-2) return output ``` 下面是一个简单的cross-attention代码片段,用于计算query序列和key-value序列之间的跨注意力: ``` class CrossAttention(nn.Module): def __init__(self, query_dim, key_dim, value_dim, hidden_dim): super(CrossAttention, self).__init__() self.query_linear = nn.Linear(query_dim, hidden_dim) self.key_linear = nn.Linear(key_dim, hidden_dim) self.value_linear = nn.Linear(value_dim, hidden_dim) def forward(self, query, key, value): # 计算query、key、value向量 query = self.query_linear(query) key = self.key_linear(key) value = self.value_linear(value) # 计算注意力分数 scores = torch.matmul(query, key.transpose(-2, -1)) scores = scores / math.sqrt(query.size(-1)) # 计算注意力权重 attention_weights = nn.functional.softmax(scores, dim=-1) # 计算加权和 weighted_values = torch.matmul(attention_weights, value) output = weighted_values.sum(dim=-2) return output ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值