electricity_bill_calc_wx/utils/index.js
2024-11-26 09:41:22 +08:00

257 lines
5.6 KiB
JavaScript

/** 每次系统加载的时候判断是否有发版 */
export function checkUpgrade() {
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
if (res.hasUpdate) {
// 弹出提示框,提示用户更新并重启小程序
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
showCancel: false,
confirmText: '马上重启',
content: '新版本已经上线,需要您重启小程序以应用新版本。',
success: function (res) {
if (res.confirm) {
// 调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
}
});
});
// 新版本下载失败时弹出提示框
updateManager.onUpdateFailed(function () {
wx.showModal({
title: '更新失败',
content: '新版本下载失败,请删除当前小程序,重新搜索打开。',
});
});
}
});
} else { // 如果用户微信版本过低,则弹出提示框
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
});
}
}
/** 根据不同环境获取不同参数 */
export function getConfigByEnv() {
const result = wx.getAccountInfoSync();
const { envVersion } = result.miniProgram;
let api = ""
switch (envVersion) {
// 开发版
case 'develop':
api = "http://localhost:8000"
// api = "https://zgd.hbhcbn.com/api3"
// api = "https://zgd.hbhcbn.com/api3"
// api = "https://zgd.hbhcbn.com/wxApi"
// api = "http://127.0.0.1:4523/m1/4143821-0-default"
break;
// 体验版
case 'trial':
api = "https://zgd.hbhcbn.com/wxApi"
// api = "https://zgd.hbhcbn.com/api3"
break;
// 正式版
case 'release':
api = "https://zgd.hbhcbn.com/api3"
break;
}
return { api, }
}
/** 获取app.js中的globalData数据 */
export function getGlobalData() {
const app = getApp();
return app.globalData;
}
/**
* 节流
* @param fn 需要节流的函数
* @param interval 时间
*/
export function throttle(fn, interval = 500) {
let flag = false;
return function() {
if(!flag) {
flag = true
fn.apply(this, arguments);
setTimeout(() => {
flag = false
}, interval)
}
}
}
/** 将微信弹框确认异步变成同步 */
export function showModal({
title, content
}) {
return new Promise((resolve) => {
wx.showModal({
title,
content,
complete: (res) => {
if (res.cancel) {
resolve(false)
}
if (res.confirm) {
resolve(true)
}
}
})
})
}
/** 获取可选年份 */
export function getYears() {
const years = [];
const currentYear = new Date().getFullYear();
const minYear = 2024;
for(let i = currentYear; i >= minYear; i--) {
years.push({name: i, id: i});
}
return years;
}
/**
* 防抖
* @param fn 需要防抖的函数
* @param interval 时间
*/
function debounce(fn, interval) {
var timer;
var gapTime = interval || 500
return function() {
clearTimeout(timer)
var that = this;
timer = setTimeout(function (){
fn.call(that);
}, gapTime);
}
}
export const showLoading = async (options = {}) => {
const { title } = options;
return new Promise((resolve, reject) => {
wx.showLoading({
title: title || '加载中',
success: () => {
resolve()
}
})
})
}
export const hideLoading = async () => {
return new Promise((resolve, reject) => {
wx.hideLoading({
noConflict: true,
success: () => {
resolve()
}
})
})
}
export const alertInfo = (message) => {
return wx.showToast({
title: message,
icon: 'none',
})
}
export const alertSuccess = (message) => {
return wx.showToast({
title: message,
icon: 'success'
})
}
export const alertError = (message) => {
return wx.showToast({
title: message,
icon: 'error'
})
}
export const loadingFunc = async callback => {
if (typeof callback !== "function") {
return
}
await showLoading()
try {
await callback()
} catch(err) {
} finally {
await hideLoading()
}
}
export const wxLogin = () => {
return new Promise((resolve, reject) => {
wx.login({
success (res) {
if (res.code) {
resolve(res.code)
return
}
reject()
},
fail: err => {
reject(err)
}
})
})
}
export const wxModal = (data) => {
const { title = '提示', content } = data;
return new Promise((resolve, reject) => {
wx.showModal({
title,
content,
success (res) {
if (res.confirm) {
resolve()
} else if (res.cancel) {
reject()
}
}
})
})
}
export const getPixelRatio = () => {
let pixelRatio = 0
wx.getSystemInfo({
success: function (res) {
pixelRatio = res.pixelRatio
},
fail: function () {
pixelRatio = 0
}
})
return pixelRatio
}
export function isValidPhoneNumber(phoneNumber) {
return /^1\d{10}$/.test(phoneNumber);
}
/** 获取当前页面url */
export function getPageUrl() {
const pages = getCurrentPages() //获取加载的页面
const currentPage = pages[pages.length-1] //获取当前页面的对象
const url = currentPage.route //当前页面url
return url
}
/** 根据文件路径获取文件名 */
export function getFileName(path) {
return path.slice(path.lastIndexOf("/") + 1, -1)
}