Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信小程序教程04:API(完结) #31

Open
chencl1986 opened this issue Mar 18, 2019 · 0 comments
Open

微信小程序教程04:API(完结) #31

chencl1986 opened this issue Mar 18, 2019 · 0 comments

Comments

@chencl1986
Copy link
Owner

阅读更多系列文章请访问我的GitHub博客,示例代码请访问这里

该教程示例代码:/lesson04/pages/index/index.js、/lesson04/pages/index/index.wxml

wx.showLoading

wx.showLoading用来显示一个loading提示框,但它不提供duration设置,需主动调用 wx.hideLoading 才能关闭提示框,可以通过title属性显示loading文字。

wx.showLoading({
  title: 'loading'
})
setTimeout(function () {
  wx.hideLoading()
}, 2000)

wx.showToast

wx.showToast方法用来显示一个提示框,title属性可指定显示内容,icon属性指定显示的图标类型,为none时即不显示图标,duration属性用来设置提示框显示的时长。

  wx.showToast({
    title: 'toast',
    icon: 'none',
    duration: 1000
  })

wx.showActionSheet

wx.showActionSheet方法用来显示操作菜单,在itemList属性中指定按钮文案,最大长度为6。

wx.showActionSheet方法有3种回调方式,分别为success(选中菜单时)、fail(点击取消按钮和背景色)、complete(弹窗收起时),都会传入结果参数,如{errMsg: "showActionSheet:ok", tapIndex: 0}或{errMsg: "showActionSheet:ok", tapIndex: 0}。

wx.showShareMenu

wx.showShareMenu方法用于显示当前页的转发按钮,它接受一个withShareTicket参数,为true时会生成一个分享id,可以在小程序后台追踪分享结果。

  wx.showShareMenu({
    withShareTicket: true
  })

wx.getUserInfo

wx.getUserInfo用于获取用户信息,可以在success回调中获取用户信息。

wx.getUserInfo({
  success(res) {
    console.log(res)
  }
})

wx.login

wx.login方法与wx.getUserInfo方法不同,它的success回调数据中的code属性是用户的token,如{errMsg: "login:ok", code: "081qfzoj2LluFC0feOlj2gQboj2qfzo5"}。

token每次请求返回都不同,需要将token传到服务端,由服务端通过code2Session接口获取到用户的session_key、openid和unionid,其中是否返回unionid需要看用户的设置。

code2Session请求地址为:

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

其中需要的APPID和SECRET,可以在小程序的开发设置页面中找到。

code2Session的返回值中,session_key主要用来验证用户身份,请求用户的信息,用户每次登录时,session_key的值都不同。

openid是仅在当前小程序中唯一的,当前用户的id,也就是说openid在其他小程序中是不同的。

unionid指的是开发者的多个公众号和小程序等应用中,对同一个用户的唯一标识。https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html

wx.scanCode

wx.scanCode方法是用来调用微信的扫码功能,可用scanType属性指定扫码类型,在success回调中接收扫码结果。

wx.scanCode({
  scanType: ['barCode', 'qrCode'],
  success(res) {
    console.log(res)  // {errMsg: "scanCode:ok", result: "google", scanType: "QR_CODE", charSet: "UTF-8"}
  }
})

wx.setKeepScreenOn

wx.setKeepScreenOn方法用来使屏幕保持常量状态,可将keepScreenOn参数设置为true,开启保持常量。

wx.setKeepScreenOn({
  setKeepScreenOn: true
})

wx.setScreenBrightness

wx.setScreenBrightness可以设置屏幕亮度,参数value属性由0~1表示亮度,
0最暗,1最亮。

wx.setScreenBrightness({
  value: 1
})

wx.getBatteryInfo

wx.getBatteryInfo可用来获取手机点亮,它还有一个同步版本wx.getBatteryInfoSync,在success回调中可以获取数据。

wx.getBatteryInfo({
  success(res) {
    console.log(res)  // {isCharging: false, level: 38, errMsg: "getBatteryInfo:ok"}
  }
})

wx.connectWifi

wx.connectWifi用来链接WIFI,需要传入WIFI的SSID和password。

wx.connectWifi({
  SSID: '',
  password: '',
  success(res) {
    console.log(res.errMsg)
  }
})

wx.startSoterAuthentication

wx.startSoterAuthentication可用来调用微信的生物认证。可在requestAuthModes中传入认证方式,目前仅支持fingerPrint指纹识别。challenge挑战因子属性用于校验调用者身份,将作为 resultJSON 的一部分返回给调用者。

wx.startSoterAuthentication({
  requestAuthModes: ['fingerPrint'],
  challenge: '123456',
  authContent: '请用指纹解锁',
  success(res) {
    wx.showToast({
      title: JSON.stringify(res),
      icon: 'none'
    })
  },
  fail(res) {
    wx.showToast({
      title: JSON.stringify(res),
      icon: 'none',
      duration: 100000
    })
  },
})

wx.downloadFile

wx.downloadFile将会由客户端直接发起一个 HTTPS GET 请求,可通过url属性指定一个下载地址,通过filePath指定下载后的存储路径。

wx.downloadFile({
  url: 'https://cn.bing.com/sa/simg/hpc26i_2x.png',
  filePath: 'hpc26i_2x.png',
  success(res) {
    toast(JSON.stringify(res))
  },
  fail(res) {
    toast(JSON.stringify(res))
  },
})

wx.startBluetoothDevicesDiscovery

wx.startBluetoothDevicesDiscovery方法用来搜索附近的蓝牙设备,在services属性中可以设置要搜索的设备类型。

由于该方法比较消耗系统资源,搜索并链接成功后可以调用wx.stopBluetoothDevicesDiscovery关闭搜索。

wx.startBluetoothDevicesDiscovery({
  success(res) {
    toast(JSON.stringify(res))
    wx.stopBluetoothDevicesDiscovery()
  },
  fail(res) {
    toast(JSON.stringify(res))
  },
})

wx.request

wx.request用于网络请求,注意需要将请求的域名设置为合法域名,否则请求会被拦截,在测试时可以在开发工具中选择不校验合法域名。

wx.request({
  url: 'https://cn.bing.com/search',
  data: { q: 'abc', safe: 'off' },
  method: 'get',
  dataType: 'text',
  success(res) {
    toast(JSON.stringify(res))
  },
  fail(res) {
    toast(JSON.stringify(res))
  }
})

结束语

至此,微信小程序教程完结,在我看来,小程序更像是Vue+React+React Native的混合体,有Vue或React相关开发经验的话入门很简单。

但小程序基于微信平台,能够实现传统Web开发不具备的很多功能,而且能够在微信生态圈内发展,作为前端是不能不掌握的重要技能。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant