小程序api(常用)

wx.getSysteminfoSync()

同步获取系统信息

brand string 设备品牌
model string 设备型号。新机型刚推出一段时间会显示unknown,微信会尽快进行适配。
pixelRatio number 设备像素比
screenWidth number 屏幕宽度,单位px
screenHeight number 屏幕高度,单位px
windowWidth number 可使用窗口宽度,单位px
windowHeight number 可使用窗口高度,单位px
statusBarHeight number 状态栏的高度,单位px
language string 微信设置的语言
version string 微信版本号
system string 操作系统及版本

用法:

 onLoad(options) {
    wx.getSystemInfo({
      success(res) {
        console.log(res.model)
        console.log(res.pixelRatio)
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        console.log(res.language)
        console.log(res.version)
        console.log(res.platform)
        console.log(res)
      }
    })
}

小程序api(常用)_第1张图片

wx.request()

网络请求

url string 必须 开发者服务器接口地址
data string/object/ArrayBuffer 请求的参数
header Object 设置请求的 header,header 中不能设置 Referer。
content-type 默认为 application/json
method string 默认GET HTTP 请求方法

用法:

   wx.request({
      // 请求的地址如果一http开头直接用url不是http开头添加我们 baseUrL
      url: url,
      method: option.method || "GET", //请求的方法 默认get
      data: option.data, //post出入的参数
      header,
      success(res) {
        // 请求成功
        resolve(res.data);
      },
      fail(err) {
        // 04 对错误进行处理
        wx.showToast({
          title: "加载失败",
          icon: "none"
        })
        // 请求失败
        reject(err);
      },
      complete() {
        // 关闭加载提示
        wx.hideToast();
      }
    })

 

 wx.downloadFile下载文件

url string 常用 必填 下载资源的 url
header Object 不常用 HTTP 请求的 Header,Header 中不能设置 Referer
timeout number 不常用 超时时间,单位为毫秒
filePath string 指定文件下载后存储的路径 (本地路径)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

 success 回调函数

tempFilePath string 临时文件路径 (本地路径)。没传入 filePath 指定文件存储路径时会返回,下载后的文件会存储到一个临时文件
filePath string 用户文件路径 (本地路径)。传入 filePath 时会返回,跟传入的 filePath 一致
statusCode number 开发者服务器返回的 HTTP 状态码
 wx.downloadFile({
      url: this.data.pic,
      success(res) {
        console.log(res);
        //把临时文件保存到相册(需要用户授权)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success() {
            //提示保存成功
            wx.showToast({
              title: "下载图片成功",
              icon: "none"
            })
          }
        })
      }
    })

 

wx.uploadFile上传

wx.chooseMedia 选择图片或者视频

wx.chooseImage 从相册选择图片

url string 必需 开发者服务器地址
filePath string 必需 要上传文件资源的路径 (本地路径)
name string 必需 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容
header Object HTTP 请求 Header,Header 中不能设置 Referer
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
upImg() {
    var that = this
    //选择图片
    //wx.chooseImage({
    //选择媒体
    wx.chooseMedia({
      count: 1, //媒体数量
      success(res) {
        console.log(res);
        //获取选择的第0个图片临时地址
        var tempFile = res.tempFiles[0].tempFilePath;
        wx.uploadFile({
          filePath: tempFile,
          name: 'file',
          url: 'http://xxxx.com/ajax/file.php',
          success: res => {
            console.log(res);
            // 转换为js对象
            var data = JSON.parse(res.data);
            // 更新图片信息
            that.setData({
              pic: "http://xxxx.com" + data.pic
            })
          }
        })
      }
    })
  },

wx.showModal模态框

 wx.showModal({
      title: '需要观看广告',
      content: '每天使用两次',
    })

wx.showToast提示

 wx.showToast({
      title: '你好',
    })

wx.showLoading 加载提示

 wx.showLoading({
      title: '加载中...',
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 2000)

wx.setNavigationBarTitle标题栏文本

 wx.setNavigationBarTitle({
      title: 'api讲解',
    })

 wx.setNavigationBarColor标题颜色

 wx.setNavigationBarColor({
      backgroundColor: '#ff0000',
      frontColor: '#ffffff',
      animation: {
        duration: 400,
        timingFunc: "easeIn"
      }
    })

wx.getUserProfile获取用户信息

 wx.getUserProfile({
      desc: '需要获取您的昵称',
      success: res => {
        console.log(res);
        //更新本地用户信息
        that.setData({
          "userInfo": res.userInfo
        })
        //存储用户信息到本地
        wx.setStorageSync('userInfo', res.userInfo)
      }
    })

你可能感兴趣的:(小程序,前端,javascript)