123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- function formatTime(time) {
- if (typeof time !== 'number' || time < 0) {
- return time
- }
- var hour = parseInt(time / 3600)
- time = time % 3600
- var minute = parseInt(time / 60)
- time = time % 60
- var second = time
- return ([hour, minute, second]).map(function (n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }).join(':')
- }
- function formatLocation(longitude, latitude) {
- if (typeof longitude === 'string' && typeof latitude === 'string') {
- longitude = parseFloat(longitude)
- latitude = parseFloat(latitude)
- }
- longitude = longitude.toFixed(2)
- latitude = latitude.toFixed(2)
- return {
- longitude: longitude.toString().split('.'),
- latitude: latitude.toString().split('.')
- }
- }
- var dateUtils = {
- UNITS: {
- '年': 31557600000,
- '月': 2629800000,
- '天': 86400000,
- '小时': 3600000,
- '分钟': 60000,
- '秒': 1000
- },
- humanize: function (milliseconds) {
- var humanize = '';
- for (var key in this.UNITS) {
- if (milliseconds >= this.UNITS[key]) {
- humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
- break;
- }
- }
- return humanize || '刚刚';
- },
- format: function (dateStr) {
- var date = this.parse(dateStr)
- var diff = Date.now() - date.getTime();
- if (diff < this.UNITS['天']) {
- return this.humanize(diff);
- }
- var _format = function (number) {
- return (number < 10 ? ('0' + number) : number);
- };
- return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
- _format(date.getHours()) + ':' + _format(date.getMinutes());
- },
- parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
- var a = str.split(/[^0-9]/);
- return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
- }
- };
- var getQuery=function(name){
- return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
- }
- /* 登录判断 */
- var persistLogin=function(_this) {
- disableTurnPage();
- let storageState = uni.getStorageSync('storage_state');
- //console.info('_this.$store',_this.$store);
- if(_this.$store.state){
- if (!_this.$store.state.isLogin) { //缓存中有值,重新赋值
-
- if (storageState.isLogin) {
- console.info("缓存中storageState isLogin为真有值重新给状态赋值");
- setState(_this,storageState);
-
-
- } else { //缓存中没值,跳转到登录页面
- //console.info("当前页面路径:" + _this.route);
- console.info("缓存中storageState isLogin为假");
- if (_this.route !== "/" && _this.route.toLocaleLowerCase() !== "/pages/login/login") {
- console.info('util重新登录');
- uni.navigateTo({
- url: '/'
- });
- }
- }
- } else {
- console.info("_this.$store.state.isLogin为真")
- setStateFromStorage(_this,storageState);
- }
-
- }
- else{
- console.info('_this.$store.state=null');
- setStateFromStorage(_this);
- }
- }
- function setStateFromStorage(_this,storageState){
- if (storageState) {
- let obj = _this.$store.state;
- for (let key in obj) {
- if (obj[key]) {
- //设置状态中的各种值
- if (key == 'token') {
- _this.$store.commit('setToken', storageState.token);
- }
- if (key == 'avatar') {
- _this.$store.commit('setAvatar', storageState.avatar);
- }
- if (key == 'name') {
- _this.$store.commit('setName', storageState.name);
- }
- if (key == 'introduction') {
- _this.$store.commit('setIntroduction', storageState.introduction);
- }
- if (key == 'roles') {
- _this.$store.commit('setRoles', storageState.roles);
- }
- if (key == 'user') {
- _this.$store.commit('setUser', storageState.user);
- }
- if (key == 'stationName') {
- _this.$store.commit('setStationName', storageState.stationName);
- }
- if (key == 'stationCode') {
- _this.$store.commit('setStationCode', storageState.stationCode);
- }
- if (key == 'departmentName') {
- _this.$store.commit('setDepartmentName', storageState.departmentName);
- }
- if (key == 'departmentCode') {
- _this.$store.commit('setDepartmentCode', storageState.departmentCode);
- }
- if (key == 'projectCode') {
- _this.$store.commit('setProjectCode', storageState.projectCode);
- }
-
- }
- }
-
- }
- else{
- //console.info("当前页面路径:" + _this.route);
- if (_this.route !== "/" && _this.route.toLocaleLowerCase() !== "/pages/login/login") {
- console.info('util重新登录');
- uni.navigateTo({
- url: '/'
- });
- }
- }
- }
- /* 设置状态值 */
- function setState(_this,storageState){
- //设置状态中的各种值
- _this.$store.commit('setIsLogin', storageState.isLogin);
- _this.$store.commit('setToken', storageState.tnToken);
- _this.$store.commit('setAvatar', storageState.avatar);
- _this.$store.commit('setName', storageState.name);
- _this.$store.commit('setIntroduction', storageState.introduction);
- _this.$store.commit('setRoles', storageState.roles);
- _this.$store.commit('setUser', storageState.user);
- _this.$store.commit('setStationName', storageState.stationName);
- _this.$store.commit('setStationCode', storageState.stationCode);
- _this.$store.commit('setDepartmentName', storageState.departmentName);
- _this.$store.commit('setDepartmentCode', storageState.departmentCode);
- _this.$store.commit('setProjectCode', storageState.projectCode);
- }
- /* 获取状态值 */
- function getState(_this,key){
- let storageState=uni.getStorageSync("storage_state");
- console.info('storageState',storageState);
- if(key==='isLogin'){
- if(!_this.$store.state.isLogin){
- if(storageState){
- _this.$store.commit('setIsLogin', storageState.isLogin);
- }
-
- }
- return _this.$store.state.isLogin;
- }
- if(key==='userCode'){
- if(!_this.$store.state.user||!_this.$store.state.user.userCode){
- if(storageState){
- _this.$store.commit('setUser', storageState.user);
- }
-
- }
- return _this.$store.state.user.userCode;
- }
- if (key == 'projectCode') {
- if(!_this.$store.state.projectCode){
- if(storageState){
- _this.$store.commit('setProjectCode', storageState.projectCode);
- }
-
- }
- return _this.$store.state.projectCode;
- }
- return "";
- }
- /* 日期格式 */
- var getDate=function(type) {
- const date = new Date();
- let year = date.getFullYear();
- let month = date.getMonth() + 1;
- let day = date.getDate();
- if (type === 'start') {
- year = year - 60;
- } else if (type === 'end') {
- year = year + 2;
- }
- month = month > 9 ? month : '0' + month;;
- day = day > 9 ? day : '0' + day;
- return `${year}-${month}-${day}`;
- }
- /* 禁用左右翻页 */
- function disableTurnPage(){
- document.addEventListener("touchstart",function(e){
- startX = e.touches[0].pageX;
-
- startY = e.touches[0].pageY;
-
- });
- document.addEventListener('touchmove', function(event) {
- //console.info('左右滑动',event);
- //当屏幕有多个touch或者页面被缩放过,就不执行move操作
- if (event.targetTouches.length > 1 || event.scale && event.scale !== 1) return;
- var touch = event.targetTouches[0];
-
- endPos = {
- x: touch.pageX - startX,
- y: touch.pageY - startY
- };
- isScrolling = Math.abs(endPos.x) < Math.abs(endPos.y) ? 1 : 0; //isScrolling为1时,表示纵向滑动,0为横向滑动
- if (isScrolling === 0) { //横向滑动
- // alert(0);
- event.preventDefault(); //阻止触摸事件的默认行为,即阻止滚屏
-
- }
- }, { passive: false });
- }
- /**
- * 判断变量是否为空,
- * @param {[type]} param 变量
- * @return {Boolean} 为空返回true,否则返回false。
- */
- function isEmpty(param){
- if(param){
- var param_type = typeof(param);
- if(param_type == 'object'){
- //要判断的是【对象】或【数组】或【null】等
- if(typeof(param.length) == 'undefined'){
- if(JSON.stringify(param) == "{}"){
- return true;//空值,空对象
- }
- }else if(param.length == 0){
- return true;//空值,空数组
- }
- }else if(param_type == 'string'){
- //如果要过滤空格等字符
- var new_param = param.trim();
- if(new_param.length == 0){
- //空值,例如:带有空格的字符串" "。
- return true;
- }
- }else if(param_type == 'boolean'){
- if(!param){
- return true;
- }
- }else if(param_type== 'number'){
- if(!param){
- return true;
- }
- }
- return false;//非空值
- }else{
- //空值,例如:
- //(1)null
- //(2)可能使用了js的内置的名称,例如:var name=[],这个打印类型是字符串类型。
- //(3)空字符串''、""。
- //(4)数字0、00等,如果可以只输入0,则需要另外判断。
- return true;
- }
- }
- module.exports = {
- formatTime: formatTime,
- formatLocation: formatLocation,
- dateUtils: dateUtils,
- getQuery:getQuery , //获取url的某个参数
- persistLogin:persistLogin,
- getDate:getDate,
- setState:setState, //自storage中获取状态值,重新给状态赋值
- getState:getState,
- isEmpty:isEmpty
-
- }
|