| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- // import wxJs from "jweixin-module"
- // import jWeixin from "wecomjsdk"
- // import {
- // getWxJsSign
- // } from "@/api/weChat";
- let tools = {}
- /**
- * 大小判断
- * @param a
- * @param b
- * @returns {number}
- */
- tools.sortNumber = function(a, b) {
- return a - b;
- }
- tools.formatDateAgo = (dateStr)=>{
- const now = new Date();
- const target = new Date(dateStr);
- const diff = (now - target) / 1000 / 60; // 转换为分钟
-
- if (diff < 1) {
- return '刚刚';
- } else if (diff < 60) {
- return parseInt(diff) + '分钟前';
- } else if (diff < 60 * 24) {
- return parseInt(diff / 60) + '小时前';
- } else if (diff < 60 * 24 * 30) {
- return parseInt(diff / 60 / 24) + '天前';
- } else if (diff < 60 * 24 * 30 * 12) {
- return parseInt(diff / 60 / 24 / 30) + '个月前';
- } else {
- return parseInt(diff / 60 / 24 / 30 / 12) + '年前';
- }
- }
- tools.leftClick = function() {
- let pages = getCurrentPages();
- if (pages.length > 1) {
- uni.navigateBack({
- delta: 1
- })
- } else {
- uni.reLaunch({
- url: '/pages/index/index'
- });
- }
- }
- //邮箱/手机***过滤
- tools.starPhone = function(value, type) {
- const start = value.slice(0, 3)
- const end = value.slice(-type)
- return `${start}****${end}`
- }
- // 验证密码格式
- //只能由字母、数字组成,6-12位
- tools.passwordInput = function(val) {
- const emailReg =
- /^(?=.*\d)(?=.*[A-z])[\da-zA-Z]{6,12}$/;
- if (!emailReg.test(val)) {
- // this.error("密码格式错误")
- return;
- } else {
- return true;
- }
- }
- //验证手机格式
- tools.phoneInput = function(val) {
- const emailReg =
- /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
- if (!emailReg.test(val)) {
- // this.error("手机格式错误")
- return;
- } else {
- return true;
- }
- }
- /**
- * 保留两位小数
- * @param num
- * @returns {string}
- */
- tools.twoFloating = function(num) {
- // 获取两位小数
- let price = "";
- price = num * 1;
- price = String(price).split(".")[1];
- if (price !== undefined && price.length === 1) {
- price = `.${price}0`;
- } else {
- price === undefined ? (price = ".00") : (price = `.${price}`);
- }
- return price;
- }
- tools.formatDecimal = function(num, decimal) {
- num = num.toString()
- let index = num.indexOf('.')
- if (index !== -1) {
- num = num.substring(0, decimal + index + 1)
- } else {
- num = num.substring(0)
- }
- return parseFloat(num).toFixed(decimal)
- }
- /**
- * 错误提示
- * @param msg
- */
- tools.error = function(msg) {
- uni.showToast({
- 'title': msg,
- 'icon': 'error',
- 'mask': true,
- 'duration': 1500
- })
- }
- /**
- * 成功提示
- * @param msg
- */
- tools.success = function(msg) {
- uni.showToast({
- 'title': msg,
- 'icon': 'success',
- 'mask': true,
- 'duration': 1500
- })
- }
- /**
- * 显示Loading
- */
- tools.showLoading = function() {
- uni.showLoading({
- title: '加载中...',
- mask: true
- });
- }
- /**
- * 关闭Loading
- */
- tools.hideLoading = function() {
- uni.hideLoading();
- }
- /**
- * 获取时间戳(毫秒)
- * @returns {number}
- */
- tools.getTime = function() {
- return new Date().getTime();
- }
- tools.updateVersion = function(sysVersion, appUrl) {
- let app_version = plus.runtime.version;
- console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
- console.log(app_version < sysVersion)
- if (app_version < sysVersion) {
- uni.showLoading({
- title: '更新中……'
- })
- uni.downloadFile({ //执行下载
- url: appUrl, //下载地址
- success: (downloadResult) => { //下载成功
- uni.hideLoading();
- if (downloadResult.statusCode === 200) {
- uni.showModal({
- title: '',
- content: '更新成功,确定现在重启吗?',
- confirmText: '重启',
- confirmColor: '#EE8F57',
- success: function(res) {
- if (res.confirm === true) {
- plus.runtime.install( //安装
- downloadResult.tempFilePath, {
- force: true
- },
- function(res) {
- tools.success('更新成功,重启中')
- plus.runtime.restart();
- }
- );
- }
- }
- });
- }
- }
- });
- } else {
- // tools.success('你已是最新版本')
- }
- }
- /**
- * uniapp html图片显示控制
- * @param str
- * @returns {*}
- */
- tools.imgDeal = function(str) {
- console.log(str)
- if (str === null || str === undefined) {
- return '';
- } else {
- return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
- }
- }
- /**
- * 获取平台类型 1:微信,2:支付宝
- * @returns {boolean|number}
- */
- tools.platformType = function() {
- let ua = window.navigator.userAgent.toLowerCase();
- if (ua.indexOf('micromessenger') != -1) {
- return 1;
- } else if (ua.indexOf('alipay') != -1) {
- return 2;
- } else {
- return 0;
- }
- }
- /**
- * 获取开发平台
- * @returns {string}
- */
- tools.getPlatform = function() {
- let platForm = undefined;
- // #ifdef H5
- platForm = 'H5';
- //#endif
- // #ifdef APP-PLUS
- platForm = 'APP';
- //#endif
- // #ifdef APP-PLUS-NVUE
- platForm = 'APP';
- //#endif
- // #ifdef APP-NVUE
- platForm = 'APP';
- //#endif
- // #ifdef MP-WEIXIN
- platForm = 'MP-WEIXIN'; // 小程序
- //#endif
- // #ifdef MP-ALIPAY
- platForm = 'MP-ALIPAY';
- //#endif
- // #ifdef MP-BAIDU
- platForm = 'MP-BAIDU';
- //#endif
- // #ifdef MP-TOUTIAO
- platForm = 'MP-TOUTIAO';
- //#endif
- // #ifdef MP-LARK
- platForm = 'MP-LARK';
- //#endif
- // #ifdef MP-QQ
- platForm = 'MP-QQ';
- //#endif
- // #ifdef MP-KUAISHOU
- platForm = 'MP-KUAISHOU';
- //#endif
- // #ifdef QUICKAPP-WEBVIEW
- platForm = 'QUICKAPP-WEBVIEW';
- //#endif
- return platForm;
- }
- /**
- * 记录用户登录信息
- * @param data
- * @param type
- */
- tools.setLoginData = function(data) {
- uni.setStorageSync('m_id', data.m_id)
- uni.setStorageSync('token', data.token)
- tools.success('登陆成功')
- setTimeout(() => {
- uni.reLaunch({
- url: '/pages/index/index'
- });
- }, 1500)
- }
- /**
- * 微信JSDK授权
- * @param jsApiList
- * @returns {Promise<*>}
- */
- // tools.wxInitialize = async function(jsApiList) {
- // return await getWxJsSign({
- // 'url': window.location.href,
- // 'type': 1
- // }).then((ret) => {
- // if (ret.code === 1) {
- // wxJs.config({
- // beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
- // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
- // appId: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
- // timestamp: ret.data.timestamp * 1, // 必填,生成签名的时间戳
- // nonceStr: ret.data.noncestr, // 必填,生成签名的随机串
- // signature: ret.data.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
- // jsApiList: jsApiList, // 必填,需要使用的JS接口列表
- // success: function(res) {
- // console.log(res)
- // console.log('config:---------------OK')
- // tools.success('成功')
- // },
- // fail: function(res) {
- // console.log('config-error')
- // console.log(res)
- // }
- // });
- // return true;
- // } else {
- // return false;
- // }
- // })
- // }
- /**
- * 企业微信信息授权
- * @param jsApiList
- * @returns {Promise<unknown>}
- */
- // tools.wxAgentInitialize = async function(jsApiList) {
- // return await getWxJsSign({
- // 'url': window.location.href,
- // 'type': 2
- // }).then((ret) => {
- // if (ret.code === 1) {
- // jWeixin.agentConfig({
- // corpid: ret.data.appId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
- // agentid: ret.data.agentid, // 必填,企业微信的应用id (e.g. 1000247)
- // timestamp: ret.data.timestamp * 1, // 必填,生成签名的时间戳
- // nonceStr: ret.data.noncestr, // 必填,生成签名的随机串
- // signature: ret.data.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
- // jsApiList: jsApiList, // 必填,需要使用的JS接口列表
- // success: function(res) {
- // console.log('agentConfig:---------------OK')
- // console.log(res)
- // },
- // fail: function(res) {
- // console.log('agentConfig-error')
- // console.log(res)
- // if (res.errMsg.indexOf('function not exist') > -1) {
- // alert('版本过低请升级')
- // }
- // }
- // });
- // return true;
- // } else {
- // return false;
- // }
- // })
- // }
- tools.download=function (url){
- window.open(url)
- }
- export default tools
|