• 小程序api(常用)


    wx.getSysteminfoSync()

    同步获取系统信息

    brandstring设备品牌
    modelstring设备型号。新机型刚推出一段时间会显示unknown,微信会尽快进行适配。
    pixelRationumber设备像素比
    screenWidthnumber屏幕宽度,单位px
    screenHeightnumber屏幕高度,单位px
    windowWidthnumber可使用窗口宽度,单位px
    windowHeightnumber可使用窗口高度,单位px
    statusBarHeightnumber状态栏的高度,单位px
    languagestring微信设置的语言
    versionstring微信版本号
    systemstring操作系统及版本

    用法:

    1. onLoad(options) {
    2. wx.getSystemInfo({
    3. success(res) {
    4. console.log(res.model)
    5. console.log(res.pixelRatio)
    6. console.log(res.windowWidth)
    7. console.log(res.windowHeight)
    8. console.log(res.language)
    9. console.log(res.version)
    10. console.log(res.platform)
    11. console.log(res)
    12. }
    13. })
    14. }

    wx.request()

    网络请求

    urlstring必须开发者服务器接口地址
    datastring/object/ArrayBuffer请求的参数
    headerObject设置请求的 header,header 中不能设置 Referer。
    content-type 默认为 application/json
    methodstring默认GETHTTP 请求方法

    用法:

    1. wx.request({
    2. // 请求的地址如果一http开头直接用url不是http开头添加我们 baseUrL
    3. url: url,
    4. method: option.method || "GET", //请求的方法 默认get
    5. data: option.data, //post出入的参数
    6. header,
    7. success(res) {
    8. // 请求成功
    9. resolve(res.data);
    10. },
    11. fail(err) {
    12. // 04 对错误进行处理
    13. wx.showToast({
    14. title: "加载失败",
    15. icon: "none"
    16. })
    17. // 请求失败
    18. reject(err);
    19. },
    20. complete() {
    21. // 关闭加载提示
    22. wx.hideToast();
    23. }
    24. })

     

     wx.downloadFile下载文件

    urlstring常用必填下载资源的 url
    headerObject不常用HTTP 请求的 Header,Header 中不能设置 Referer
    timeoutnumber不常用超时时间,单位为毫秒
    filePathstring指定文件下载后存储的路径 (本地路径)
    successfunction接口调用成功的回调函数
    failfunction接口调用失败的回调函数
    completefunction接口调用结束的回调函数(调用成功、失败都会执行)

     success 回调函数

    tempFilePathstring临时文件路径 (本地路径)。没传入 filePath 指定文件存储路径时会返回,下载后的文件会存储到一个临时文件
    filePathstring用户文件路径 (本地路径)。传入 filePath 时会返回,跟传入的 filePath 一致
    statusCodenumber开发者服务器返回的 HTTP 状态码
    1. wx.downloadFile({
    2. url: this.data.pic,
    3. success(res) {
    4. console.log(res);
    5. //把临时文件保存到相册(需要用户授权)
    6. wx.saveImageToPhotosAlbum({
    7. filePath: res.tempFilePath,
    8. success() {
    9. //提示保存成功
    10. wx.showToast({
    11. title: "下载图片成功",
    12. icon: "none"
    13. })
    14. }
    15. })
    16. }
    17. })

     

    wx.uploadFile上传

    wx.chooseMedia 选择图片或者视频

    wx.chooseImage 从相册选择图片

    urlstring必需开发者服务器地址
    filePathstring必需要上传文件资源的路径 (本地路径)
    namestring必需文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容
    headerObjectHTTP 请求 Header,Header 中不能设置 Referer
    successfunction接口调用成功的回调函数
    failfunction接口调用失败的回调函数
    completefunction接口调用结束的回调函数(调用成功、失败都会执行)
    1. upImg() {
    2. var that = this
    3. //选择图片
    4. //wx.chooseImage({
    5. //选择媒体
    6. wx.chooseMedia({
    7. count: 1, //媒体数量
    8. success(res) {
    9. console.log(res);
    10. //获取选择的第0个图片临时地址
    11. var tempFile = res.tempFiles[0].tempFilePath;
    12. wx.uploadFile({
    13. filePath: tempFile,
    14. name: 'file',
    15. url: 'http://xxxx.com/ajax/file.php',
    16. success: res => {
    17. console.log(res);
    18. // 转换为js对象
    19. var data = JSON.parse(res.data);
    20. // 更新图片信息
    21. that.setData({
    22. pic: "http://xxxx.com" + data.pic
    23. })
    24. }
    25. })
    26. }
    27. })
    28. },

    wx.showModal模态框

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

    wx.showToast提示

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

    wx.showLoading 加载提示

    1. wx.showLoading({
    2. title: '加载中...',
    3. })
    4. setTimeout(() => {
    5. wx.hideLoading()
    6. }, 2000)

    wx.setNavigationBarTitle标题栏文本

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

     wx.setNavigationBarColor标题颜色

    1. wx.setNavigationBarColor({
    2. backgroundColor: '#ff0000',
    3. frontColor: '#ffffff',
    4. animation: {
    5. duration: 400,
    6. timingFunc: "easeIn"
    7. }
    8. })

    wx.getUserProfile获取用户信息

    1. wx.getUserProfile({
    2. desc: '需要获取您的昵称',
    3. success: res => {
    4. console.log(res);
    5. //更新本地用户信息
    6. that.setData({
    7. "userInfo": res.userInfo
    8. })
    9. //存储用户信息到本地
    10. wx.setStorageSync('userInfo', res.userInfo)
    11. }
    12. })

  • 相关阅读:
    7-1 找“重点”
    Linux 下进程间通讯之管道
    最新整理的linux基础命令大全 有需要的收藏
    02 开闭原则
    《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程
    【Spark】RDD、DataFram、DataSet的比较与使用
    The Missing Semester of Your CS Education(计算机教育中缺失的一课)
    Swift开发基础06-闭包
    D. Tournament Countdown(交互题)
    本地docker注册证书docker login连接到harbor仓库、利用shell脚本将大量镜像pull、tag、push到私有harbor仓库
  • 原文地址:https://blog.csdn.net/m0_51024444/article/details/127851998