Files
xk-doctor-wx/common/js/util.js
2024-09-19 12:57:55 +08:00

301 lines
7.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
let systemInfo = uni.getSystemInfoSync();
/**
* 更新小程序
*/
function updateMiniapp() {
const updateManager = uni.getUpdateManager();
updateManager.onCheckForUpdate(function(res) {
// 请求完新版本信息的回调
});
updateManager.onUpdateReady(function(res) {
uni.showModal({
title: "更新提示",
content: "新版本已经准备好,是否重启应用?",
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
},
});
});
updateManager.onUpdateFailed(function(res) {
// 新的版本下载失败
});
}
/**
* 获取AppInfo
*/
async function getAppInfo() {
const accountInfo = await uni.getAccountInfoSync();
const appId = await accountInfo.miniProgram.appId;
return appId;
}
/**
* 获取code
*/
async function getCode() {
return new Promise((resolve, reject) => {
uni.getProvider({
service: "oauth",
success: (res) => {
// 平台来源
uni.login({
provider: res.provider[0],
scopes: ["auth_base"],
success(loginRes) {
let code = loginRes.code;
resolve(code);
},
fail(err) {
uni.showModal({
title: err.errMsg,
showCancel: false,
});
},
complete() {},
});
},
});
});
}
//校验手机号有效性
const checkPhoneNumber = mobile => {
//验证手机号真实性
if (mobile == null || mobile == undefined) {
wx.showToast({
title: '手机号不能为空',
icon: 'none'
})
return false
}
// 匹配所有号码(手机卡 + 数据卡 + 上网卡)
if (!(/^(?:\+?86)?1(?:3\d{3}|5[^4\D]\d{2}|8\d{3}|7(?:[01356789]\d{2}|4(?:0\d|1[0-2]|9\d))|9[01356789]\d{2}|6[2567]\d{2}|4(?:[14]0\d{3}|[68]\d{4}|[579]\d{2}))\d{6}$/
.test(mobile))) {
wx.showToast({
title: '手机号格式错误',
icon: 'none'
})
return false
}
return true
}
let m_checkProv = (val) => {
var pattern = /^[1-9][0-9]/;
var provs = {
11: "北京",
12: "天津",
13: "河北",
14: "山西",
15: "内蒙古",
21: "辽宁",
22: "吉林",
23: "黑龙江 ",
31: "上海",
32: "江苏",
33: "浙江",
34: "安徽",
35: "福建",
36: "江西",
37: "山东",
41: "河南",
42: "湖北 ",
43: "湖南",
44: "广东",
45: "广西",
46: "海南",
50: "重庆",
51: "四川",
52: "贵州",
53: "云南",
54: "西藏 ",
61: "陕西",
62: "甘肃",
63: "青海",
64: "宁夏",
65: "新疆",
71: "台湾",
81: "香港",
82: "澳门"
};
if (pattern.test(val)) {
if (provs[val]) {
return true;
}
}
return false;
};
let m_checkDate = (val) => {
var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
if (pattern.test(val)) {
var year = val.substring(0, 4);
var month = val.substring(4, 6);
var date = val.substring(6, 8);
var date2 = new Date(year + "-" + month + "-" + date);
if (date2 && date2.getMonth() == (parseInt(month) - 1)) {
return true;
}
}
return false;
};
let m_checkCode = (val) => {
var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
var code = val.substring(17);
if (p.test(val)) {
var sum = 0;
for (var i = 0; i < 17; i++) {
sum += val[i] * factor[i];
}
if (parity[sum % 11] == code.toUpperCase()) {
return true;
}
}
return false;
};
function m_by_checkID(val = '') {
if (m_checkCode(val) && val) {
var date = val.substring(6, 14);
if (m_checkDate(date)) {
if (m_checkProv(val.substring(0, 2))) {
return true;
}
}
}
return false;
};
function trim(str, pos = 'both') {
if (pos == 'both') {
return str.replace(/^\s+|\s+$/g, "");
} else if (pos == "left") {
return str.replace(/^\s*/, '');
} else if (pos == 'right') {
return str.replace(/(\s*$)/g, "");
} else if (pos == 'all') {
return str.replace(/\s+/g, "");
} else {
return str;
}
}
/**
* 样式转换
* 对象转字符串,或者字符串转对象
* @param {Object | String} 需要转换的目标
* @param {String} 转换的目的object-转为对象string-转为字符串
*/
function addStyle(customStyle, target = 'object') {
// 字符串转字符串,对象转对象情形,直接返回
if (uni.$u.test.empty(customStyle) || typeof(customStyle) === 'object' && target === 'object' || target ===
'string' &&
typeof(customStyle) === 'string') {
return customStyle
}
// 字符串转对象
if (target === 'object') {
// 去除字符串样式中的两端空格(中间的空格不能去掉比如padding: 20px 0如果去掉了就错了),空格是无用的
customStyle = trim(customStyle)
// 根据";"将字符串转为数组形式
const styleArray = customStyle.split(';')
const style = {}
// 历遍数组,拼接成对象
for (let i = 0; i < styleArray.length; i++) {
// 'font-size:20px;color:red;',如此最后字符串有";"的话会导致styleArray最后一个元素为空字符串这里需要过滤
if (styleArray[i]) {
const item = styleArray[i].split(':')
style[trim(item[0])] = trim(item[1])
}
}
return style
}
// 这里为对象转字符串形式
let string = ''
for (const i in customStyle) {
// 驼峰转为中划线的形式否则css内联样式无法识别驼峰样式属性名
const key = i.replace(/([A-Z])/g, '-$1').toLowerCase()
string += `${key}:${customStyle[i]};`
}
// 去除两端空格
return trim(string)
}
// 导航栏高度
function navbarHeight(height = '') {
// #ifdef APP-PLUS || H5
return height ? height : 44;
// #endif
// #ifdef MP
// 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
// 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
// return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
let he = systemInfo.platform == 'ios' ? 44 : 48;
return height ? height : he;
// #endif
}
// 导航栏高度
function navbarHeight(height = '') {
// #ifdef APP-PLUS || H5
return height ? height : 44;
// #endif
// #ifdef MP
// 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
// 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
// return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
let he = systemInfo.platform == 'ios' ? 44 : 48;
return height ? height : he;
// #endif
}
function getAge(identityCard) {
var len = (identityCard + "").length;
if (len == 0) {
return '';
} else {
if ((len != 15) && (len != 18)) //身份证号码只能为15位或18位其它不合法
{
return '';
}
}
var strBirthday = "";
if (len == 18) //处理18位的身份证号码从号码中得到生日和性别代码
{
strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(
12, 2);
}
if (len == 15) {
strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard
.substr(10, 2);
}
//时间字符串里,必须是“/”
var birthDate = new Date(strBirthday);
var nowDateTime = new Date();
var age = nowDateTime.getFullYear() - birthDate.getFullYear();
//再考虑月、天的因素;.getMonth()获取的是从0开始的这里进行比较不需要加1
if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() &&
nowDateTime.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
module.exports = {
getAge,
updateMiniapp,
getAppInfo,
getCode,
checkPhoneNumber,
m_by_checkID,
addStyle,
navbarHeight
};