小程序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)}})
}
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开头添加我们 baseUrLurl: url,method: option.method || "GET", //请求的方法 默认getdata: 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)}})
小程序api(常用)相关推荐
- 微信小程序原生常用语法 1
微信小程序的教程 学习目标 如何创建小程序项目 小程序项目的基本组成结构 小程序页面由几部分组成 小程序中常见的组件如何使用 小程序如何进行协同开发与发布 小程序的宿主环境 小程序标准开发模式 1.申 ...
- 一起学习微信小程序api
微信小程序api介绍 小程序API 是小程序开发的核心,通过 API 开发者可以获得微信底层封装的高级特性,如网络请求.文件操作.设备信息.地理定位.本地存储等. 小程序的 API 以全局对象 wx ...
- 微信小程序开发常用的npm包
本篇主要介绍在微信小程序中使用 npm 包以及使用微信开发者工具开发微信小程序时最常用的 Vant Weapp 小程序 UI 组件库.miniprogram-api-promise 将 API Pro ...
- 微信小程序api概述
微信小程序api概述 1.api 1.1 网络api 1.2 缓存api 1.3 界面api 2 开放能力-获取用户信息 2.1 通过API获取用户信息 3 图片与文件 3.1 下载图片 3.2 上传 ...
- 小程序 长按api_微信小程序API相关知识科普
微信小程序API(Application Programming Interface),即应用程序编程接口.API是一种接口函数,把函数封装起来,给开发者,这样好多的功能就不需要你去实现了,只要会调用 ...
- 微信小程序上传接口php,微信小程序API 上传、下载
微信小程序API 上传.下载 wx.uploadFile(OBJECT) 将本地资源上传到开发者服务器.如页面通过 wx.chooseImage 等接口获取到一个本地资源的临时文件路径后,可通过此接口 ...
- 微信小程序api接口调用用封装
微信小程序api接口调用用封装 1. 在 02-项目 下新建request目录及index.js文件 1.1 02-项目\request\index.js 1.2.index.js // 同时发送异步 ...
- 微信小程序API之showActionSheet(操作菜单)
微信小程序API之showActionSheet(操作菜单) wxml: <button bindtap="cd01">操作菜单01</button> &l ...
- 微信小程序API之showModal(Loding...)
微信小程序API之showModal(Loding-) wxml: <button bindtap="ts01">显示 Loading 加载框</button&g ...
- 微信小程序API之video
微信小程序API之video wxml: <button bindtap="tz">跳转tab页面</button><navigator url=&q ...
最新文章
- poj2352(树状数组)
- DFS、栈、双向队列:CF264A- Escape from Stones
- tt桌球瞄准器手机版_新年换台新手机!各价位段哪些机型值得入手?
- 简明python教程购买-简明Python3教程 6.基础
- PAT (Advanced Level) 1010 Radix(二分+模拟)
- 2015蓝桥杯省赛---java---A---3(九数分三组)
- 选购工业交换机时,工业交换机的IP等级多少比较合适?
- 国际:如何识别真正的程序员
- 高程5.7单体内置对象 5.8小结
- JVM内存模型、逃逸分析以及发生GC的时机
- selenium之 chromedriver与chrome版本映射表(更新至v2.43)
- 6.4Spring环境配置等级和优先级
- 《JavaScript征途》广泛征集读后感创作者,参与有奖!
- 移动通信-多径效应,多普勒效应,菲涅尔区,阴影效应,快衰落,慢衰落
- OA升级及二次开发方案
- 日本python程序员工资_年轻程序员赴日本工作有前途吗?
- python爬虫学习之爬取169图片网站
- 计算机编程语言排行榜—TIOBE世界编程语言排行榜(2021年11月份最新版)
- 硅谷丛林的故事 EDA篇
- 阿里云华北1235、华东1、华东2和华南1分别对应哪些城市?地域节点物理数据中心在哪?... 1