123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- export default {
- //身份证验证
- isCardNo(card) {
- // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
- var reg =
- /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/;
- if (reg.test(card) === false) {
- console.log(card);
- return false;
- }
- return true
- },
- // 金额显示变化
- getMoneyStyle(value = 0) {
- if (typeof value == 'string') {
- value = (+value).toFixed(2)
- }
- if (typeof value == 'number') {
- value = value.toFixed(2)
- }
- // 将字符串转为数组
- let n = value.split("");
- // 反转数组并复制循环处理
- let arr = n.reverse().map(function(e, ind, ar) {
- // 判断当前下标是否为3的整数倍数且不为最后一个下标
- if (ind % 3 == 0 && ind / 3 > 1 && ind != ar.length) {
- return e + ','
- } else {
- return e
- }
- })
- // 反转数组回复原来排序并合并回字符串
- arr = arr.reverse().join('')
- return arr;
- },
- // 计算倒计时时间
- timeComputed(time) {
- // 获取当前时间
- const actTime = (new Date()).getTime();
- // 获取到期时间
- let stopTime = time - actTime;
- // 判断是否小于0
- if (stopTime < 0) {
- stopTime = stopTime * -1
- }
- let day = Math.floor(stopTime / 1000 / 60 / 60 / 24) //获取剩余天数
- let hours = Math.floor((stopTime / 1000 / 60 / 60) % 24); //获取剩余小时数
- let minutes = Math.floor((stopTime / 1000 / 60) % 60); //获取分钟
- let seconds = Math.floor((stopTime / 1000) % 60); //获取秒数
- return {
- hours, //倒计时小时数
- minutes, //倒计时分钟数
- seconds, //倒计时秒数
- day //倒计时天数
- }
- },
- // 调用打开地图方法
- openMap(e) {
- const that = this
- return new Promise((resolve, reject) => {
- wx.getSetting({
- success(res) {
- //这里判断是否有地位权限
- if (!res.authSetting['scope.userLocation']) {
- wx.showModal({
- title: '提示',
- content: '请求获取位置权限',
- success: function(res) {
- if (res.confirm == false) {
- // 授权失败
- reject()
- return false;
- }
- wx.openSetting({
- success(res) {
- //如果再次拒绝则返回页面并提示
- if (!res.authSetting[
- 'scope.userLocation']) {
- wx.showToast({
- title: '此功能需获取位置信息,请重新设置',
- duration: 3000,
- icon: 'none'
- })
- } else {
- //允许授权,调用地图
- resolve()
- }
- }
- })
- }
- })
- } else {
- //如果有定位权限,调用地图
- resolve()
- }
- }
- })
- })
- },
- //时间戳转换成时间
- getTime(time, type) {
- const num = 13 - (time + '').length;
- let l = 1; //倍数
- for (let i = 0; i < num; i++) {
- l += '0';
- }
- // 重新解析为数字
- l = parseInt(l)
- const date = new Date(parseInt(time) * l);
- const year = date.getFullYear();
- const mon = date.getMonth() + 1;
- const day = date.getDate();
- const hours = date.getHours();
- const minu = date.getMinutes();
- const sec = date.getSeconds();
- return year + '-' + mon + '-' + day + ' ' + (hours < 10 ? `0${hours}` : hours) + ':' + (minu < 10 ? `0${minu}` :
- minu) + ':' + (sec < 10 ? `0${sec}` : sec);
- },
- //除法函数,用来得到精确的除法结果
- //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
- //调用:$h.Div(arg1,arg2)
- //返回值:arg1除以arg2的精确结果
- Div: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var t1 = 0,
- t2 = 0,
- r1, r2;
- try {
- t1 = arg1.toString().split(".")[1].length;
- } catch (e) {}
- try {
- t2 = arg2.toString().split(".")[1].length;
- } catch (e) {}
- r1 = Number(arg1.toString().replace(".", ""));
- r2 = Number(arg2.toString().replace(".", ""));
- return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
- },
- //加法函数,用来得到精确的加法结果
- //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
- //调用:$h.Add(arg1,arg2)
- //返回值:arg1加上arg2的精确结果
- Add: function(arg1, arg2) {
- arg2 = parseFloat(arg2);
- var r1, r2, m;
- try {
- r1 = arg1.toString().split(".")[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split(".")[1].length
- } catch (e) {
- r2 = 0
- }
- m = Math.pow(100, Math.max(r1, r2));
- return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
- },
- //减法函数,用来得到精确的减法结果
- //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
- //调用:$h.Sub(arg1,arg2)
- //返回值:arg1减去arg2的精确结果
- Sub: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var r1, r2, m, n;
- try {
- r1 = arg1.toString().split(".")[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split(".")[1].length
- } catch (e) {
- r2 = 0
- }
- m = Math.pow(10, Math.max(r1, r2));
- //动态控制精度长度
- n = (r1 >= r2) ? r1 : r2;
- return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
- },
- //乘法函数,用来得到精确的乘法结果
- //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
- //调用:$h.Mul(arg1,arg2)
- //返回值:arg1乘以arg2的精确结果
- Mul: function(arg1, arg2) {
- arg1 = parseFloat(arg1);
- arg2 = parseFloat(arg2);
- var m = 0,
- s1 = arg1.toString(),
- s2 = arg2.toString();
- try {
- m += s1.split(".")[1].length
- } catch (e) {}
- try {
- m += s2.split(".")[1].length
- } catch (e) {}
- return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
- },
- /**
- * opt object | string
- * to_url object | string
- * 例:
- * this.Tips('/pages/test/test'); 跳转不提示
- * this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转
- * this.Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上
- * tab=1 一定时间后跳转至 table上
- * tab=2 一定时间后跳转至非 table上
- * tab=3 一定时间后返回上页面
- * tab=4 关闭所有页面跳转至非table上
- * tab=5 关闭当前页面跳转至table上
- */
- Tips(opt, to_url) {
- if (typeof opt == 'string') {
- to_url = opt;
- opt = {};
- }
- let title = opt.title || '',
- icon = opt.icon || 'none',
- endtime = opt.endtime || 2000,
- success = opt.success;
- if (title) uni.showToast({
- title: title,
- icon: icon,
- duration: endtime,
- success
- })
- if (to_url != undefined) {
- if (typeof to_url == 'object') {
- let tab = to_url.tab || 1,
- url = to_url.url || '';
- switch (tab) {
- case 1:
- //一定时间后跳转至 table
- setTimeout(function() {
- uni.switchTab({
- url: url
- })
- }, endtime);
- break;
- case 2:
- //跳转至非table页面
- setTimeout(function() {
- uni.navigateTo({
- url: url,
- })
- }, endtime);
- break;
- case 3:
- //返回上页面
- setTimeout(function() {
- // #ifndef H5
- uni.navigateBack({
- delta: parseInt(url),
- })
- // #endif
- // #ifdef H5
- history.back();
- // #endif
- }, endtime);
- break;
- case 4:
- //关闭当前所有页面跳转至非table页面
- setTimeout(function() {
- uni.reLaunch({
- url: url,
- })
- }, endtime);
- break;
- case 5:
- //关闭当前页面跳转至非table页面
- setTimeout(function() {
- uni.redirectTo({
- url: url,
- })
- }, endtime);
- break;
- }
- } else if (typeof to_url == 'function') {
- setTimeout(function() {
- to_url && to_url();
- }, endtime);
- } else {
- //没有提示时跳转不延迟
- setTimeout(function() {
- uni.navigateTo({
- url: to_url,
- })
- }, title ? endtime : 0);
- }
- }
- }
- }
|