You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

315 lines
7.6 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { login, getUserInfo } from '@/common/js/api/homePage/login.js'
export default {
/**
* 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: function(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);
}
}
},
/**
* 移除数组中的某个数组并组成新的数组返回
* @param array array 需要移除的数组
* @param int index 需要移除的数组的键值
* @param string | int 值
* @return array
*
*/
ArrayRemove: function(array, index, value) {
const valueArray = [];
if (array instanceof Array) {
for (let i = 0; i < array.length; i++) {
if (typeof index == 'number' && array[index] != i) {
valueArray.push(array[i]);
} else if (typeof index == 'string' && array[i][index] != value) {
valueArray.push(array[i]);
}
}
}
return valueArray;
},
/*
* 合并数组
*/
SplitArray(list, sp) {
if (typeof list != 'object') return [];
if (sp === undefined) sp = [];
for (var i = 0; i < list.length; i++) {
sp.push(list[i]);
}
return sp;
},
trim(str) {
return String.prototype.trim.call(str);
},
toStringValue: function(obj) {
if (obj instanceof Array) {
var arr = [];
for (var i = 0; i < obj.length; i++) {
arr[i] = toStringValue(obj[i]);
}
return arr;
} else if (typeof obj == 'object') {
for (var p in obj) {
obj[p] = toStringValue(obj[p]);
}
} else if (typeof obj == 'number') {
obj = obj + '';
}
return obj;
},
showModal: (title="",infor="", fnSure = () =>{}, fnCancel= () =>{}) => {
uni.showModal({
title,
content: infor,
showCancel: true, // 不显示取消按钮
success: function (res) {
if (res.confirm) {
fnSure()
} else if (res.cancel) {
fnCancel()
}
}
});
},
// 验证是否已登录
checkLogin: ()=>{
let userNum = uni.getStorageSync('userNum');
if(!userNum){
return false //未登陆
}else{
return true //已登录
}
},
// 获取地址
getAdress: (fn = () =>{})=>{
return new Promise((resolve,reject) =>{
uni.getLocation({
type: 'wgs84',
success: function (res) {
console.log('获取位置成功:', res)
getApp().globalData.latitude = res.latitude;
getApp().globalData.longitude = res.longitude;
},
fail:function(err){
//点击取消
if(err.errMsg.indexOf('auth denied') > -1 || err.errMsg.indexOf('auth deny') > -1 ){
console.log('用户点击取消授权:',err)
}
//手机未开启GPS定位
else if(err.errMsg.indexOf('ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') > -1){
console.log('用户手机未开启GPS',err)
}
else{
console.log(err)
}
reject()
}
})
})
},
setStorage: (key, data)=>{
uni.setStorageSync({
key,
data,
success: function() {
console.log(key + "已储存");
},
fail: function() {
console.log(key + "未储存");
}
});
},
/**
* 格式化时间
* @param {number} seconds 秒
* @param {String} connect 连接符 非必填
* 格式 默认 *时*分*秒
* connect 为 == 时 :分 :秒
*/
formatTime : (seconds, connect) =>{
if (seconds) {
let result = '';
let hour = connect ? connect : '时';
let minute = connect ? connect : '分'
let second = connect ? '' : '秒';
let _seconds = parseInt(seconds % 60) > 9 ? parseInt(seconds % 60) : '0' + parseInt(seconds % 60);
let _mins = parseInt(seconds % 3600 / 60) > 9 ? parseInt(seconds % 3600 / 60) : '0' + parseInt(seconds % 3600 / 60);
let _hour = parseInt(seconds / 3600) > 9 ? parseInt(seconds / 3600) : '0' + parseInt(seconds / 3600);
if(_hour > 0) {
result = `${_hour}${hour}${padZero(_mins)}${minute}${padZero(_seconds)}${second}`
}else if(_mins > 0) {
result = `00${hour}${padZero(_mins)}${minute}${padZero(_seconds)}${second}`
}else{
result = `00${hour}00${minute}${padZero(_seconds)}${second}`
}
return result;
}
},
formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let convertLocale = date.getTime();
let nowOffset = new Date().getTimezoneOffset() * 60 * 1000; // 当前时间与utc时间差
let nowUtc = convertLocale;
let aValue = new Date(nowUtc); // 格式化UTC时间
let o = {
'Y+': aValue.getFullYear(),
'M+': aValue.getMonth() + 1,
'd+': aValue.getDate(),
'h+': aValue.getHours(),
'm+': aValue.getMinutes(),
's+': aValue.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padZero(str));
}
}
return fmt;
},
// 登录
loginFun: (username, password)=>{
return new Promise((resolve,reject) =>{
login({
account: username,
password: password,
tenantId:0
}).then(res =>{
if(res.code == 200){
resolve(res)
}else{
uni.showToast({
title: res.message,
icon: 'none',
})
reject(res)
}
})
})
},
// 获取用户信息
getUserInfoFun: (username, password)=>{
return new Promise((resolve,reject) =>{
getUserInfo().then(res =>{
if(res.code == 200){
resolve(res)
}else{
reject(res)
}
})
})
}
}
function padZero (num) {
return new RegExp(/^\d$/g).test(num) ? `0${num}` : num;
}