uniapp commonly used APIs

1. Dynamically set the title  uni.setNavigationBarTitle

uni.setNavigationBarTitle({
				title: '标题'
			})

2. Get the current location  uni.getLocation   and open the map  uni.chooseLocation

// 获取位置信息
			getDotLocation () {
				uni.getLocation({
					type: "wgs84",
					success: (res) => {
						uni.chooseLocation({
							latitude: res.latitude, // 纬度
							longitude: res.longitude, // 经度
							success: (data) => {
								if (data.errMsg === 'chooseLocation:ok') {
									this.formData.address = data.address
									this.formData.longitude = data.longitude
									this.formData.latitude = data.latitude
								} else {
									this.$util.toastTopage('获取位置失败', 'error')
								}
							},
							fail: () => this.$util.toastTopage('获取位置失败', 'error')
						})
					},
					fail: () => {
						this.$util.modal({
							c: '为了正常使用,请授权「位置信息」- 「使用小程序时允许」'
						}, () => wx.openSetting())
					}
				})
			},

3.  Store the data in the local cache  uni.setStorageSync()   to get the local cache  uni.getStorageSync()

// 手机号需存进缓存
uni.setStorageSync('phoneNumber', 18866668888)
// 读取手机号
const phone =  uni.getStorageSync('phoneNumber')
console.log(phone) // 18866668888

4. Preview the image uni.previewImage()  

uni.previewImage({
    current: 0,     // 当前显示图片的索引值
	urls: photoList,    // 需要预览的图片列表,photoList要求必须是数组
	loop:true,          // 是否可循环预览
})

5. Save to album  uni.saveImageToPhotosAlbum() 

<view class="footer" >
	<view class="footer-item" @click="savePosterTap(传图片的地址过去)">
		<u-icon name="download" color="#82848a" size="22"></u-icon>
		<text class="text">保存至相册</text>
	</view>
</view>
 /**
			 * 保存图片
			 */

            savePosterTap(url) {
				this.imgSrc = url
				this.saveImage();
			},
			saveImage() {
				let that = this;
				// 向用户发起授权请求
				uni.authorize({
					scope: 'scope.writePhotosAlbum',
					success: () => {
						// 已授权
						that.downLoadImg();
					},
					fail: () => {
						// 拒绝授权,获取当前设置
						uni.getSetting({
							success: (result) => {
								if (!result.authSetting['scope.writePhotosAlbum']) {
									that.isAuth()
								}
							}
						});
					}
				})
			},
			/**
			 * 下载资源,保存图片到系统相册
			 */
			downLoadImg() {
				uni.showLoading({
					title: '加载中'
				});
				uni.downloadFile({
					url: this.imgSrc, // 保存的图片地址
					success: (res) => {
						uni.hideLoading();
						console.log(res, 'res');
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									uni.showToast({
										title: "保存成功",
										icon: "none"
									});
								},
								fail: function() {
									uni.showToast({
										title: "保存失败,请稍后重试",
										icon: "none"
									});
								}
							});
						}
					},
					fail: (err) => {
						console.log(err, 'err');
						uni.showToast({
							title: "请退出小程序,重新打开",
							icon: "none"
						});
					}
				})
			},
			/*
			 * 引导用户开启权限
			 */
			isAuth() {
				uni.showModal({
					content: '由于您还没有允许保存图片到您相册里,无法进行保存,请点击确定允许授权',
					success: (res) => {
						if (res.confirm) {
							uni.openSetting({
								success: (result) => {
									console.log(result.authSetting);
								}
							});
						}
					}
				});
			},

Guess you like

Origin blog.csdn.net/cwb_2120/article/details/130336887