微信小程序

300 阅读4分钟

1.分享到朋友圈

将小程序页面分享到朋友圈。适用于内容型页面的分享,不适用与有较多交互的页面分享。
仅支持Android平台

1-1:设置分享状态

小程序页面默认不可被分享到朋友圈,开发者需主动设置“分享到朋友圈”。页面允许被分享到朋友圈,需要满足两个条件:

  • 首先,页面需设置允许“发送给朋友”【onShareAppMessage】
  • 满足条件 1 后,页面需设置允许“分享到朋友圈”,同时可自定义标题、分享图等。【onShareTimeline】 满足上述两个条件的页面,可被分享到朋友圈。

1-2:onShareAppMessage(object object)

监听用户点击页面内转发按钮(button组件open-type='share')或右上角菜单“转发”按钮的行为,并自定义转发内容。
注意:只有定义了此事件处理函数,右上角菜单才会显示“转发”按钮

函数参数

  • from:String:转发事件来源(button页面内转发按钮,menu右上角转发菜单)
  • target:Object:如果from值是button,则target是触发这次转发事件的button,否则为undefined
  • webViewURl:String:页面中包含web-view组件时,返回当前web-view的url

返回值

此事件处理函数需要return一个Object,用于自定义转发内容

  • title:转发标题,默认是当前小程序名称
  • path:转发路径,默认页面path,必须是以/开头的完整路径
  • imageUrl:自定义图片路径,默认截图

示例

Page({
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      path: '/page/user?id=123'
    }
  }
})

1-3:onShareTimeline()

本接口为Beta版本,暂只在Android平台支持
监听右上角菜单“分享到朋友圈”按钮的行为,并自定义分享内容
注意:只有定义了此事件处理函数,右上角才会显示“分享到朋友圈”按钮

返回值

事件处理函数返回一个Object,用于自定义分享内容,不支持自定义页面路径

  • title:自定义标题,即朋友圈列表页上显示的标题,默认是当前小程序名称
  • query:自定义页面路径中携带的参数,如path?a=1&b=2的?后面的部分,默认是当前页面路径携带的参数
  • imageUrl:自定义图片路径,默认是小程序logo

示例

Page({
  onShareTimeLine: function(){
    return {
      title: '自定义转发标题',
      query: 'id=123&name="share"'
      imageUrl:....
    }
  }
})

2.获取相机权限

  • getSetting获取当前用户的设置【根据返回值确定授权情况】
  • authorize向用户发起授权请求
  • openSetting调起客户端小程序设置界面,返回用户设置的操作结果【就是一个黑页面】
  wx.getSetting({
    success(res) {
      if(typeof res.authSetting['scope.camera'] === 'undefined'){
        wx.authorize({
            scope: 'scope.camera',
            success(){
              wx.navigateTo({
                url: `/pages/phootdistin/index?interType=${type}`
              })
            }
        })
      }else if(res.authSetting['scope.camera']){
          wx.navigateTo({
            url: `/pages/phootdistin/index?interType=${type}`
          })
      }else{
        wx.openSetting({
          success (res) {
          }
        })      
      }
    }
  })

3.在微信小程序中使用image-cropper

一款高性能的小程序图片裁剪插件,支持旋转、设置尺寸 以下示例实现简单的拍照,然后裁剪图片的功能

3.1下载image-cropper

github下载地址

3.2在json文件中引入组件

将下载的文件放在微信小程序的Component文件夹中

"usingComponents": {
     "image-cropper": "/components/image-cropper/image-cropper"
},
"navigationBarTitleText": "裁剪图片",

3.3在wxml文件中使用组件

<image-cropper id="image-cropper" limit_move="{{true}}" disable_rotate="{{true}}" width="{{width}}" height="{{height}}" imgSrc="{{src}}" bindload="cropperload" bindimageload="loadimage" bindtapcut="clickcut"></image-cropper>

3.4添加js

Page({
      data: {
          src:'',
          width:250,//宽度
          height: 250,//高度
      },
      onLoad: function (options) {
        wx.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: (res)=> {
            const tempFilePaths = res.tempFilePaths;
            //开始裁剪
            this.setData({
              src:tempFilePaths
            });
            // //获取到image-cropper实例
            this.cropper = this.selectComponent("#image-cropper");
            wx.showLoading({
                title: '加载中'
            })
          },
          fail(err){
            wx.navigateBack({
              delta: 1,
            })
          }
        })
      },
      cropperload(e){
          console.log("cropper初始化完成");
      },
      loadimage(e){
          console.log("图片加载完成",e.detail);
          wx.hideLoading();
          //重置图片角度、缩放、位置
          this.cropper.imgReset();
      },
      clickcut(e) {
          console.log(e.detail);
          //点击裁剪框阅览图片
          wx.previewImage({
              current: e.detail.url, // 当前显示图片的http链接
              urls: [e.detail.url] // 需要预览的图片http链接列表
          })
      },
})

有个地方应注意下,就是获取截取后的图片,需要调用此插件的一个函数才可以取得:

//在carman项目中,将这段代码放在点击确定的方法中
this.cropper.getImg((obj) => {
    // 这里就是想要截取的图片传给后台的虚拟路径
    console.log(obj.url)
})

4.在微信小程序手写步骤条

效果图
该项目结合了iview中的图标。 使用了微信小程序progcess组件

4.1html文件

<view class="bar-view">
  <progress percent="{{Steps*percent}}" class="progress" stroke-width="3" color="#4AB3ED" backgroundColor="#E6EEFE" style="width:{{progress}}%"/>
  <view class="steps_all">
    <view class="steps_one_box" wx:for="{{StepsList}}" wx:key="index">
      <view wx:if="{{Steps>index}}" class="steps-success">
        <i-icon type="right"  color="#fff"/>
      </view>
      <view wx:if="{{Steps==index}}" class="steps_img">
          <i-icon type="more" color="#fff" size="28"  />
      </view>
      <view wx:if="{{Steps<index}}" class="steps-no">
        <i-icon type="right"  color="#fff"/>
      </view>
      <view class="{{Steps>=index?'steps_wenzi':'steps_wenzi2'}}" >{{StepsList[index]}}</view>
    </view>
  </view>
</view>

4.2js文件

//设置当前完成步数
Steps:2,
// 当步骤为五步时步骤名不可超过五个汉字
  StepsList: ["第一步", "第二步", "第三步", "第四步", "第五步"],
//步骤为五步时
progress:80,
percent:25,