知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
微信小程序設(shè)備信息和版本更新
發(fā)表時(shí)間:2021-2-28
發(fā)布人:葵宇科技
瀏覽次數(shù):57
封裝until.js
// 獲取小程序以及設(shè)備信息
function get(success, fail=undefined) {
// 設(shè)備信息
const info = {
// 系統(tǒng)設(shè)備信息原對(duì)象
info: {},
// 是否為iOS
isIOS: false,
// 是否為iphoneX XR XS...等iOS系統(tǒng)全面屏手機(jī)
isIphoneX: false,
// 是否為安卓
isAndroid: false,
// 是否為Mac
isMac: false,
// 是否為Windows
isWindows: false,
// 設(shè)備像素比 (px 與 rpx 的轉(zhuǎn)換比例, 公式:px * pixelRatio = rpx)
pixelRatio: 1,
// 屏幕寬度
screenWidth: 0,
screenWidthRPX: 0,
// 屏幕高度
screenHeight: 0,
screenHeightRPX: 0,
// 狀態(tài)欄高度
statusBarHeight: 0,
statusBarHeightRPX: 0,
// 導(dǎo)航欄高度(不包括狀態(tài)欄,單純的導(dǎo)航欄高度)
navigationBarHeight: 0,
navigationBarHeightRPX: 0,
// 導(dǎo)航欄高度(包括狀態(tài)欄,整個(gè)導(dǎo)航欄高度)
navigationHeight: 0,
navigationHeightRPX: 0,
// 底部TabBar菜單欄高度
tabBarHeight: 0,
tabBarHeightRPX: 0
}
// 加載系統(tǒng)信息進(jìn)行更換
wx.getSystemInfo({
// 獲取成功
success: (res) => {
// 右上角菜單膠囊范圍
let menuButtonRect = wx.getMenuButtonBoundingClientRect()
// 狀態(tài)欄默認(rèn)高度
const statusBarHeight = 20
// 導(dǎo)航欄默認(rèn)高度
const navigationBarHeight = 44
// TabBar默認(rèn)高度
const tabBarHeight = 48
// 記錄原始數(shù)據(jù)
info.info = res
// 是否為iOS
info.isIOS = (res.system.indexOf('iOS') !== -1)
// 是否為iOS系統(tǒng)全面屏手機(jī)
if (info.isIOS) {
// 如果為 iOS 且安全區(qū)域上面超過 默認(rèn)狀態(tài)欄高度 則為 X 系列
info.isIphoneX = (res.safeArea.top > statusBarHeight)
}
// 是否為安卓
info.isAndroid = (res.system.indexOf('Android') !== -1)
// 是否為Mac
info.isMac = (res.system.indexOf('macOS') !== -1)
// 是否為Windows
info.isWindows = (res.system.indexOf('Windows') !== -1)
// 設(shè)備像素比(750 / 屏幕寬度)
// 系統(tǒng)給成的 res.pixelRatio 值不對(duì),所以使用自己換算出來的比例
info.pixelRatio = 750 / res.windowWidth
// 屏幕寬度
info.screenWidth = res.screenWidth
// 屏幕寬度 - RPX
info.screenWidthRPX = info.screenWidth * info.pixelRatio
// 屏幕高度
info.screenHeight = res.screenHeight
// 屏幕高度 - RPX
info.screenHeightRPX = info.screenHeight * info.pixelRatio
// 狀態(tài)欄高度
info.statusBarHeight = Math.max(res.statusBarHeight, statusBarHeight)
// 狀態(tài)欄高度 - RPX
info.statusBarHeightRPX = info.statusBarHeight * info.pixelRatio
// 導(dǎo)航欄高度
const menuBarHeight = (menuButtonRect.top - info.statusBarHeight) * 2 + menuButtonRect.height
info.navigationBarHeight = Math.max(menuBarHeight, navigationBarHeight)
// 導(dǎo)航欄高度 - 如果為奇數(shù)則轉(zhuǎn)成偶數(shù)
if (info.navigationBarHeight % 2) { info.navigationBarHeight += 1 }
// 導(dǎo)航欄高度 - RPX
info.navigationBarHeightRPX = info.navigationBarHeight * info.pixelRatio
// 導(dǎo)航欄高度
info.navigationHeight = (info.statusBarHeight + info.navigationBarHeight)
// 導(dǎo)航欄高度 - RPX
info.navigationHeightRPX = info.navigationHeight * info.pixelRatio
// 底部TabBar菜單欄高度
info.tabBarHeight = Math.max(info.screenHeight - info.navigationHeight - res.windowHeight, tabBarHeight)
// 底部TabBar菜單欄高度 - 如果為奇數(shù)則轉(zhuǎn)成偶數(shù)
if (info.tabBarHeight % 2) { info.tabBarHeight += 1 }
// 底部TabBar菜單欄高度 - RPX
info.tabBarHeightRPX = info.tabBarHeight * info.pixelRatio
console.log(info)
// 返回
if (success) { success(info) }
},
// 獲取失敗
fail: (err) => {
if (fail) { fail(err) }
}
})
}
// 檢查小程序版本并更新
function update(success=undefined, fail=undefined) {
// 檢查是否支持版本更新
if (wx.canIUse('getUpdateManager')) {
// 獲取版本更新對(duì)象
var updateManager = wx.getUpdateManager()
// 檢測是否有新版本
updateManager.onCheckForUpdate((res) => {
// 有新版本
if (res.hasUpdate) {
// 更新成功回調(diào)
updateManager.onUpdateReady((res) => {
// 有回調(diào)實(shí)現(xiàn)
if (success) {
// 自己寫提示,返回版本更新對(duì)象,方便外部使用
success(updateManager, res)
} else {
// 使用內(nèi)部更新提示
wx.showModal({
title: '更新提示',
content: '新版本已經(jīng)準(zhǔn)備好,是否重啟應(yīng)用?',
success: (res) => {
// 確定重啟,在 onUpdateReady 回調(diào)中使用 applyUpdate 強(qiáng)制小程序重啟使用新版本。
if (res.confirm) { updateManager.applyUpdate() }
}
})
}
})
// 更新失敗回調(diào)
updateManager.onUpdateFailed((err) => {
// 有回調(diào)實(shí)現(xiàn)
if (fail) {
// 自己寫提示
fail(err)
} else {
// 使用內(nèi)部更新提示
wx.showModal({
title: '更新提示',
content: '新版本下載失敗,請(qǐng)檢查網(wǎng)絡(luò)!',
showCancel: false
})
}
})
} else {
// 無新版本
}
})
}
}
// 導(dǎo)出使用
module.exports = {
// 獲取小程序以及設(shè)備信息
get,
// 檢查小程序版本并更新
update
}
app.js引用
// 導(dǎo)入 until.js
const system = require("./utils.js")
// 小程序主入口
App({
// 設(shè)備信息存放,設(shè)備信息推薦存放到 app.js 文件中作為全局參數(shù)使用
// 然后到每個(gè)頁面通過 const app = getApp() 獲取使用即可 app.systemInfo
systemInfo: {},
// 啟動(dòng)函數(shù)
onLaunch: function (options) {
// 檢查版本更新,推薦放這里,也可以放到手動(dòng)觸發(fā)的地方調(diào)用
system.update()
// 獲取設(shè)備信息
system.get((info) => {
// 記錄設(shè)備信息
this.systemInfo = info
})
}
})
相關(guān)案例查看更多
相關(guān)閱讀
- .net網(wǎng)站
- 網(wǎng)站優(yōu)化公司
- 云南網(wǎng)站建設(shè)哪家好
- 前端
- 小程序模板開發(fā)公司
- 區(qū)塊鏈
- 網(wǎng)站開發(fā)哪家好
- 智慧農(nóng)貿(mào)市場
- 云南旅游網(wǎng)站建設(shè)
- 昆明網(wǎng)站開發(fā)
- 云南做百度小程序的公司
- 云南網(wǎng)站建設(shè)費(fèi)用
- 退款
- 云南軟件開發(fā)
- 云南建設(shè)廳官方網(wǎng)站
- 網(wǎng)站建設(shè)選
- web
- 生成海報(bào)
- 網(wǎng)站建設(shè)方案 doc
- 云南小程序開發(fā)費(fèi)用
- 云南網(wǎng)站建設(shè)開發(fā)
- 報(bào)廢車
- 云南網(wǎng)站維護(hù)
- 云南網(wǎng)站建設(shè)靠譜公司
- 小程序商城
- 云南網(wǎng)站建設(shè)首選公司
- 昆明小程序代建
- 網(wǎng)絡(luò)公司聯(lián)系方式
- 模版消息
- 紅河小程序開發(fā)