1.路由5種跳轉方式
(1)wx.switchTab :只能跳轉到導航頁,並關閉其他的導航頁
(1)wx.reLaunch :關閉所有頁面,打開到應用內的某個頁面
(1)wx.redirectTo :關閉當前頁面,跳轉到應用內的某個頁面。但是不允許跳轉到 導航 頁面
(1)wx.navigateTo :只保留當前頁面,跳轉到應用內的某個頁面。但是不能跳到 tabbar 頁面(最多10層)
(1)wx.navigateBack :返回上一頁,可以返回多級頁面
wx.navigateTo({
url: 'test?id=1'
})
wx.navigateBack({
delta: 2
})
2.彈窗提示
(標簽)
wx.showToast({
title: '功能暫未開放!',
icon: 'none', success
duration: 2000
})
(選擇窗)
wx.showModal({
title: '提示',
content: '這是一個模態彈窗',
success (res) {
if (res.confirm) {
console.log('用戶點擊確定')
} else if (res.cancel) {
console.log('用戶點擊取消')
}
}
})
(加載中)
wx.showLoading({
title: '加載中',
})
setTimeout(function () {
wx.hideLoading()
}, 2000)
3.分享
onLoad: function (options) {
this.getDate(options.id)
wx.showShareMenu({
// 要求小程序返回分享目標信息
withShareTicket: true
})
},
/* 轉發*/
onShareAppMessage: function (ops) {
if (ops.from === 'button') {
// 來自頁面內轉發按鈕
console.log(ops.target)
}
return {
title: '轉發dom',
path: `pages/index/index`,
success: function (res) {
// 轉發成功
console.log("轉發成功:" + JSON.stringify(res));
var shareTickets = res.shareTickets;
},
fail: function (res) {
// 轉發失敗
console.log("轉發失敗:" + JSON.stringify(res));
}
}
}
4.生成canvas圖片
var that = this;
var unit = that.data.screenWidth / 375
//2. canvas繪制文字和圖片
const ctx = wx.createCanvasContext('share');
var bgImgPath = that.data.shareImgSrc;
ctx.drawImage(bgImgPath, 0, 0, 375, 375); //導入圖片
ctx.setFontSize(20)
ctx.setFillStyle('#000')
ctx.fillText('生成文字在圖片上', 20, 110)
ctx.stroke()
ctx.draw(false, function () {
// 3. canvas畫布轉成圖片
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: 375,
height: 375,
destWidth: 375,
destHeight: 375,
canvasId: 'share',
success: function (res) {
console.log(res);
that.setData({
shareImgSrc: res.tempFilePath
})
if (!res.tempFilePath) {
wx.showModal({
title: '提示',
content: '圖片繪制中,請稍后重試',
showCancel: false
})
}
},
fail: function (res) {
console.log(res)
}
})
});
5.保存圖片到相冊
click: function () {
let that = this
wx.saveImageToPhotosAlbum({
filePath: that.data.shareImgSrc,
success(res) {
wx.showModal({
title: '圖片保存成功',
content: '圖片成功保存到相冊了,去發圈噻~',
showCancel: false,
confirmText: '好噠',
confirmColor: '#72B9C3',
success: function (res) {
if (res.confirm) {
console.log('用戶點擊確定');
}
that.setData({
canvasHidden: true
})
}
})
}
})
}
6.數據緩存
wx.setStorageSync('key', 'value')
wx.getStorageSync('key')
7.返回上一頁,刷新頁面
onShow(){
this.onLoad()
}
