The Open Capability of WeChat Mini Programs

1. Get network status

We know that there are several ways for mobile phones to connect to the Internet: Wifi, 2G, 3G, 4G, including the coming 5G soon. There are different usage habits when using Internet services. For applications that consume more data such as video playback, some restrictions also need to be imposed. If the user is not in the WIFI state, some prompts need to be given to the user.

Page({
  // 点击“预览文档”的按钮,触发tap回调
  tap: function() {
    wx.getNetworkType({
      success: function(res) {
        // networkType字段的有效值:
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'wifi') {
          // 从网络上下载文档
          wx.downloadFile({
            url:' https://lark-temp.oss-cn-hangzhou.aliyuncs.com/__temp/464110/docx/bb5d8e7d-97bc-406b-be94-ca2b4724c0e6.docx?OSSAccessKeyId=LTAI4GKnqTWmz2X8mzA1Sjbv&Expires=1644944885&Signature=qUYOtNWZOaopjkEFbcKpvgmn%2B1U%3D',
            success: function (res) {

              // 下载成功之后进行预览文档
              wx.openDocument({
                filePath: res.tempFilePath            
})
            }
          })
        } else {
          wx.showToast({ title: '当前为非Wifi环境' })
        }
      }
    })
  }
})

2. Scanning ability

In order to reduce user input, we can encode complex information into a QR code, use the API of the host environment wx.scanCode to scan WeChat, and after the user scans the code, the success callback of wx. The string information corresponding to the dimension code.

 

Page({
  // 点击“扫码订餐”的按钮,触发tapScan回调
  tapScan: function() {
    // 调用wx.login获取微信登录凭证
    wx.scanCode({
      success: function(res) {
        var num = res.result // 获取到的num就是餐桌的编号
      }
    })
  }
})

3. Obtain current WeChat user information

    WeChat also provides many developed APIs, some of which involve the acquisition of user privacy. Regarding the calls of these APIs, it is necessary to explicitly declare these operations on the interface through the button button, such as obtaining user information.

 

<open-data type="groupName" open-gid="xxxxxx"></open-data>
<open-data type="userAvatarUrl"></open-data>
<open-data type="userGender" lang="zh_CN"></open-data>

 4.wx.canIUse()

Determine whether the API, callback, parameters, components, etc. of the applet are available in the current version. The return value is boolean.

// 组件的属性
wx.canIUse('text.selectable')
wx.canIUse('button.open-type.contact')
// 对象的属性或方法
wx.canIUse('console.log')
wx.canIUse('Image.src')
// wx接口参数、回调或者返回值
wx.canIUse('showToast.object.image')
wx.canIUse('request.object.method.GET')

5.wx.getUserProfile 

Get user information. An authorization window will pop up for each request, and userInfo will be returned after the user agrees.

 6. Small program sharing API 

Add the function onShareAppMessage to the Page constructor to generate a share button in the top menu bar

 

Page({  onShareAppMessage(){    
    // 我们要记录转发的记录    
    return {      
    // 分享的标题     
     title:'网友热议',      
    // 分享的封面, 默认为当前页面的截图      
    imageUrl:"/assets/icon/index.png",      
    // 点击分享跳转的路径,默认是当前路径,也可以自定义配置,添加额外的跳转参数      path:"/pages/index/index?userId=10&entry=share"    }  },  // 省略了别的属性  ...}) 

 

Guess you like

Origin blog.csdn.net/weixin_53583255/article/details/127560789