小程序-常用API

前言:
这里基本都是微信的API 在微信开发工具中都是有提示的,但并不支持JS提示,所以一定是要具备相应的JS基础。
更新Json数据

var self = this
self.setData({
	hasLogin: true
})
that.update()

网络请求

wx.request({
      url: '',
      data: {},
      method: 'GET',
      header: { 'content-type': 'application/json' },
      success: function (res) {
	      var datas= res.data
      },
      fail: function () {},
      complete: function () {}
    })

加载

//显示加载框
wx.showLoading({
	title: '加载中',
})
//隐藏加载框
wx.hideLoading()

bindtap点击事件传值
1.data-tag自定义参数传参

<text data-tag="{{item.tag}}" id="{{item.tag}}" bindtap='checkPage'>{{item.value}}</text>

//实现
checkPage: function(event) {
	var newTagId = event.target.id;
	    var tag = event.target.dataset.tag;
    console.log("newTagId = " + newTagId)
    console.log("tag = " + tag)
}

跳转

 wx.navigateTo({
    url: './addresume/addresume?name=' + name + '&openId=' + openId,
   })
wx.navigateTo({
  url: '../welfare/addresume/addresume',
})

PS:注意url 两者的文件位置关系,以上两种写法都可达到目的
拓展分析:‘./’ 为当前目录下 ‘…/’则为当前目录的上级目录 ,’/'则为当前目录的下的某个目录

pages/welfare/welfare.js
pages/welfare/addresume/addresume.js

toast

wx.showToast({
	title: '复制成功',
	duration: 1500,
})

对话框

wx.showModal({
    title: '复制该链接',
    content: '完成后请手动打开浏览器,是否继续?' + link,
    success: function (res) {
      if (res.confirm) {
        wx.setClipboardData({
          data: link,
          success: function () {
            wx.showToast({
              title: '复制成功',
              duration: 1500,
            })

          },
          fail: function () {
            wx.showToast({
              title: '复制失败',
              icon: 'none',
              duration: 500,
            })
          }
        })
      } else if (res.cancel) {
        wx.showToast({
          title: '取消复制',
          icon: 'none',
          duration: 500,
        })
      }
    }
  })

try catch

try {
  //在这里运行代码
    抛出错误
} catch(err) {
  //在这里处理错误
    console.log(err)
}

获取用户信息

https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html

打开附件

wx.downloadFile({       //下载附件
      url: 'https://www.sz800800.cn/ZENGXING/0109...18.16.docx',
      fileType:'docx',
      success: function (res) {
        console.log(res)  
        var filePath = res.tempFilePath
        wx.openDocument({    //打开附件
          filePath: filePath,
          success: function (res) {
            console.log(res)
          },
          complete:function(e){
            console.log(e)
          }
        })
      }
    })
  }

打开文档

wx.showLoading({
      title: '正在加载中...',
    })
    wx.downloadFile({
      url: item.downLoadUrl,
      success: function(res) {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function(res) {
            wx.hideLoading()()
          }
        })
      }
    })

猜你喜欢

转载自blog.csdn.net/qq_20330595/article/details/82704668