|
|
@@ -1,77 +1,103 @@
|
|
|
-import CONSTANT from '@/constant'
|
|
|
-import { mobileReg } from '@/utils/tools/validate'
|
|
|
-import { getMiniAppUrl, isWxBrowser } from '@/utils/common'
|
|
|
+import CONSTANT from "@/constant";
|
|
|
+import { mobileReg } from "@/utils/tools/validate";
|
|
|
+import { getMiniAppUrl, isWxBrowser } from "@/utils/common";
|
|
|
// 是否为空
|
|
|
-const isEmpty = (val) => {
|
|
|
+const isEmpty = val => {
|
|
|
if (val instanceof Array) {
|
|
|
- if (val.length === 0) return true
|
|
|
+ if (val.length === 0) return true;
|
|
|
} else if (val instanceof Object) {
|
|
|
- if (!Object.keys(val).length) return true
|
|
|
+ if (!Object.keys(val).length) return true;
|
|
|
} else {
|
|
|
- if (val === 'null' || val === null || val === 'undefined' || val === undefined || val === '') return true
|
|
|
- return false
|
|
|
+ if (
|
|
|
+ val === "null" ||
|
|
|
+ val === null ||
|
|
|
+ val === "undefined" ||
|
|
|
+ val === undefined ||
|
|
|
+ val === ""
|
|
|
+ )
|
|
|
+ return true;
|
|
|
+ return false;
|
|
|
}
|
|
|
- return false
|
|
|
-}
|
|
|
-const numberFormat =(number,type)=> {
|
|
|
- if(number > 10000) {
|
|
|
- let num = (number / 10000 + '').split('.')
|
|
|
- return `${num[0]}.${(''+ num[1]).slice(0,1)} 万`
|
|
|
- }else {
|
|
|
- return number
|
|
|
- }
|
|
|
-}
|
|
|
+ return false;
|
|
|
+};
|
|
|
+/**
|
|
|
+ * 深拷贝对象
|
|
|
+ * @param {要拷贝对象} obj
|
|
|
+ */
|
|
|
+export const copyObject = obj => {
|
|
|
+ let str,
|
|
|
+ newobj = Array.isArray(obj) === true ? [] : {};
|
|
|
+ if (typeof obj !== "object") {
|
|
|
+ return;
|
|
|
+ } else if (JSON) {
|
|
|
+ (str = JSON.stringify(obj)), //系列化对象
|
|
|
+ (newobj = JSON.parse(str)); //还原
|
|
|
+ } else {
|
|
|
+ for (let i in obj) {
|
|
|
+ newobj[i] = typeof obj[i] === "object" ? copyObject(obj[i]) : obj[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return newobj;
|
|
|
+};
|
|
|
+const numberFormat = (number, type) => {
|
|
|
+ if (number > 10000) {
|
|
|
+ let num = (number / 10000 + "").split(".");
|
|
|
+ return `${num[0]}.${("" + num[1]).slice(0, 1)} 万`;
|
|
|
+ } else {
|
|
|
+ return number;
|
|
|
+ }
|
|
|
+};
|
|
|
// 截取图片上传字符串
|
|
|
-const imgSubstr = (val) => {
|
|
|
- if (isEmpty(val)) return []
|
|
|
+const imgSubstr = val => {
|
|
|
+ if (isEmpty(val)) return [];
|
|
|
if (isArray(val)) {
|
|
|
return val.map(e => {
|
|
|
- let index = e.indexOf('/uploads/')
|
|
|
- e = e.substring(index)
|
|
|
- return e
|
|
|
- })
|
|
|
+ let index = e.indexOf("/uploads/");
|
|
|
+ e = e.substring(index);
|
|
|
+ return e;
|
|
|
+ });
|
|
|
} else {
|
|
|
- let index = val.indexOf('/uploads/')
|
|
|
- return val.substring(index)
|
|
|
+ let index = val.indexOf("/uploads/");
|
|
|
+ return val.substring(index);
|
|
|
}
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
// 是否为数组
|
|
|
-const isArray = (arr) => {
|
|
|
- return (typeof arr == 'object') && arr.constructor == Array
|
|
|
-}
|
|
|
+const isArray = arr => {
|
|
|
+ return typeof arr == "object" && arr.constructor == Array;
|
|
|
+};
|
|
|
|
|
|
// 是否为字符串
|
|
|
-const isString = (str) => {
|
|
|
- return (typeof str == 'string') && str.constructor == String
|
|
|
-}
|
|
|
+const isString = str => {
|
|
|
+ return typeof str == "string" && str.constructor == String;
|
|
|
+};
|
|
|
|
|
|
// 是否为对象
|
|
|
-const isObject = (obj) => {
|
|
|
- return (typeof obj == 'object') && obj.constructor == Object
|
|
|
-}
|
|
|
+const isObject = obj => {
|
|
|
+ return typeof obj == "object" && obj.constructor == Object;
|
|
|
+};
|
|
|
|
|
|
// 是否为数字
|
|
|
-const isNumber = (num) => {
|
|
|
- return (typeof num == 'number') && num.constructor == Number
|
|
|
-}
|
|
|
+const isNumber = num => {
|
|
|
+ return typeof num == "number" && num.constructor == Number;
|
|
|
+};
|
|
|
|
|
|
// 是否为日期类型
|
|
|
-const isDate = (date) => {
|
|
|
- return (typeof date == 'object') && date.constructor == Date
|
|
|
-}
|
|
|
+const isDate = date => {
|
|
|
+ return typeof date == "object" && date.constructor == Date;
|
|
|
+};
|
|
|
|
|
|
// 是否为函数
|
|
|
-const isFunction = (obj) => {
|
|
|
- return (typeof obj == 'object') && obj.constructor == Function
|
|
|
-}
|
|
|
+const isFunction = obj => {
|
|
|
+ return typeof obj == "object" && obj.constructor == Function;
|
|
|
+};
|
|
|
|
|
|
-const checkMobile = (num) => {
|
|
|
- return mobileReg.test(num)
|
|
|
-}
|
|
|
-const checkName = (str) => {
|
|
|
- return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str)
|
|
|
-}
|
|
|
+const checkMobile = num => {
|
|
|
+ return mobileReg.test(num);
|
|
|
+};
|
|
|
+const checkName = str => {
|
|
|
+ return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str);
|
|
|
+};
|
|
|
|
|
|
/** *
|
|
|
* 检查登录
|
|
|
@@ -79,18 +105,20 @@ const checkName = (str) => {
|
|
|
* @parmas isFrist 为true时开头为&
|
|
|
*/
|
|
|
export async function getCheckLogin() {
|
|
|
- let status = false
|
|
|
- let token = uni.getStorageSync('token')
|
|
|
+ let status = false;
|
|
|
+ let token = uni.getStorageSync("token");
|
|
|
if (token) {
|
|
|
- status = true
|
|
|
+ status = true;
|
|
|
} else {
|
|
|
// #ifdef H5
|
|
|
if (isWxBrowser()) {
|
|
|
- let option = JSON.parse(uni.getStorageSync('currentQuery')) || {}
|
|
|
- console.log('option', option)
|
|
|
+ let option = JSON.parse(uni.getStorageSync("currentQuery")) || {};
|
|
|
+ console.log("option", option);
|
|
|
// console.log('option', getMiniAppUrl(option, true))
|
|
|
// return false
|
|
|
- location.href = `${CONSTANT.hostUrl}/auth/prepare?url=${encodeURIComponent(getMiniAppUrl(option, true))}`
|
|
|
+ location.href = `${CONSTANT.hostUrl}/auth/prepare?url=${encodeURIComponent(
|
|
|
+ getMiniAppUrl(option, true)
|
|
|
+ )}`;
|
|
|
} else {
|
|
|
// uni.showToast({
|
|
|
// title: '请用微信浏览器打开!',
|
|
|
@@ -102,13 +130,13 @@ export async function getCheckLogin() {
|
|
|
// #ifndef H5
|
|
|
// #endif
|
|
|
}
|
|
|
- return status
|
|
|
+ return status;
|
|
|
}
|
|
|
|
|
|
export const floatFormat = (f, n) => {
|
|
|
- let m = Math.pow(10, n)
|
|
|
- return parseInt(f * m, 10) / m
|
|
|
-}
|
|
|
+ let m = Math.pow(10, n);
|
|
|
+ return parseInt(f * m, 10) / m;
|
|
|
+};
|
|
|
|
|
|
/** *
|
|
|
* 对象参数转为url参数
|
|
|
@@ -119,22 +147,25 @@ export const parse = (query, isFrist = false) => {
|
|
|
let str = Object.keys(query)
|
|
|
.filter(key => !isEmpty(query[key]))
|
|
|
.reduce((result, key) => {
|
|
|
- const value = query[key]
|
|
|
+ const value = query[key];
|
|
|
// in查询特殊处理
|
|
|
if (Array.isArray(value) && !isEmpty(value)) {
|
|
|
- return `${result}&${value.reduce((val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`, '')}`
|
|
|
+ return `${result}&${value.reduce(
|
|
|
+ (val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`,
|
|
|
+ ""
|
|
|
+ )}`;
|
|
|
}
|
|
|
|
|
|
// between查询做特殊处理
|
|
|
- if (typeof value === 'object' && !isEmpty(value)) {
|
|
|
- const [start, end] = value
|
|
|
- return `${result}&${key}[]=${start}&${key}[]=${end}`
|
|
|
+ if (typeof value === "object" && !isEmpty(value)) {
|
|
|
+ const [start, end] = value;
|
|
|
+ return `${result}&${key}[]=${start}&${key}[]=${end}`;
|
|
|
}
|
|
|
|
|
|
- return `${result}&${key}=${value}`
|
|
|
- }, '')
|
|
|
- return isFrist ? str : str.replace(/^&/, '?')
|
|
|
-}
|
|
|
+ return `${result}&${key}=${value}`;
|
|
|
+ }, "");
|
|
|
+ return isFrist ? str : str.replace(/^&/, "?");
|
|
|
+};
|
|
|
|
|
|
/** *
|
|
|
* 全局时间转换
|
|
|
@@ -142,42 +173,55 @@ export const parse = (query, isFrist = false) => {
|
|
|
* @parmas fmt 默认转换的时间格式
|
|
|
* @parmas all 为true时time为13位时间戳
|
|
|
*/
|
|
|
-export const $formatDate = (num, fmt = 'YYYY-MM-DD HH:mm', all) => {
|
|
|
- num = all ? num : num * 1000
|
|
|
- let date = new Date()
|
|
|
- date.setTime(num)
|
|
|
+export const $formatDate = (num, fmt = "YYYY-MM-DD HH:mm", all) => {
|
|
|
+ num = all ? num : num * 1000;
|
|
|
+ let date = new Date();
|
|
|
+ date.setTime(num);
|
|
|
let o = {
|
|
|
- 'M+': date.getMonth() + 1,
|
|
|
- 'D+': date.getDate(),
|
|
|
- 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
|
|
|
- 'H+': date.getHours(),
|
|
|
- 'm+': date.getMinutes(),
|
|
|
- 's+': date.getSeconds(),
|
|
|
- 'q+': Math.floor((date.getMonth() + 3) / 3),
|
|
|
- 'S': date.getMilliseconds()
|
|
|
- }
|
|
|
+ "M+": date.getMonth() + 1,
|
|
|
+ "D+": date.getDate(),
|
|
|
+ "h+": date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
|
|
|
+ "H+": date.getHours(),
|
|
|
+ "m+": date.getMinutes(),
|
|
|
+ "s+": date.getSeconds(),
|
|
|
+ "q+": Math.floor((date.getMonth() + 3) / 3),
|
|
|
+ S: date.getMilliseconds()
|
|
|
+ };
|
|
|
let week = {
|
|
|
- '0': '\u65e5',
|
|
|
- '1': '\u4e00',
|
|
|
- '2': '\u4e8c',
|
|
|
- '3': '\u4e09',
|
|
|
- '4': '\u56db',
|
|
|
- '5': '\u4e94',
|
|
|
- '6': '\u516d'
|
|
|
- }
|
|
|
+ "0": "\u65e5",
|
|
|
+ "1": "\u4e00",
|
|
|
+ "2": "\u4e8c",
|
|
|
+ "3": "\u4e09",
|
|
|
+ "4": "\u56db",
|
|
|
+ "5": "\u4e94",
|
|
|
+ "6": "\u516d"
|
|
|
+ };
|
|
|
if (/(Y+)/.test(fmt)) {
|
|
|
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
|
|
+ fmt = fmt.replace(
|
|
|
+ RegExp.$1,
|
|
|
+ (date.getFullYear() + "").substr(4 - RegExp.$1.length)
|
|
|
+ );
|
|
|
}
|
|
|
if (/(E+)/.test(fmt)) {
|
|
|
- fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
|
|
|
+ fmt = fmt.replace(
|
|
|
+ RegExp.$1,
|
|
|
+ (RegExp.$1.length > 1
|
|
|
+ ? RegExp.$1.length > 2
|
|
|
+ ? "\u661f\u671f"
|
|
|
+ : "\u5468"
|
|
|
+ : "") + week[date.getDay() + ""]
|
|
|
+ );
|
|
|
}
|
|
|
for (let k in o) {
|
|
|
- if (new RegExp('(' + k + ')').test(fmt)) {
|
|
|
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
|
|
|
+ if (new RegExp("(" + k + ")").test(fmt)) {
|
|
|
+ fmt = fmt.replace(
|
|
|
+ RegExp.$1,
|
|
|
+ RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
- return fmt
|
|
|
-}
|
|
|
+ return fmt;
|
|
|
+};
|
|
|
|
|
|
/** *
|
|
|
* 全局公共按钮跳转
|
|
|
@@ -186,58 +230,58 @@ export const $formatDate = (num, fmt = 'YYYY-MM-DD HH:mm', all) => {
|
|
|
* @parmas item.query 为跳转携带的参数
|
|
|
* @parmas item.type 为跳转调用的函数 1为navigateTo, 2为redirectTo, 3为reLaunch, 4为switchTab
|
|
|
*/
|
|
|
-export const pageTo = (item) => {
|
|
|
-
|
|
|
+export const pageTo = item => {
|
|
|
if (isNumber(item)) {
|
|
|
uni.navigateBack({
|
|
|
delta: item
|
|
|
- })
|
|
|
+ });
|
|
|
}
|
|
|
if (isString(item)) {
|
|
|
uni.navigateTo({
|
|
|
url: item
|
|
|
- })
|
|
|
- return false
|
|
|
+ });
|
|
|
+ return false;
|
|
|
}
|
|
|
if (item.funtion) {
|
|
|
- item.funtion()
|
|
|
+ item.funtion();
|
|
|
}
|
|
|
if (item.url) {
|
|
|
- let query = item.query ? parse(item.query) : ''
|
|
|
- let allUrl = `${item.url}${query}`
|
|
|
+ let query = item.query ? parse(item.query) : "";
|
|
|
+ let allUrl = `${item.url}${query}`;
|
|
|
if (item.type == 2) {
|
|
|
uni.redirectTo({
|
|
|
url: allUrl
|
|
|
- })
|
|
|
+ });
|
|
|
} else if (item.type == 3) {
|
|
|
uni.reLaunch({
|
|
|
url: allUrl
|
|
|
- })
|
|
|
+ });
|
|
|
} else if (item.type == 4) {
|
|
|
- console.log('switchTab', item.query)
|
|
|
- uni.setStorageSync('switchTabQuery', item.query)
|
|
|
+ console.log("switchTab", item.query);
|
|
|
+ uni.setStorageSync("switchTabQuery", item.query);
|
|
|
uni.switchTab({
|
|
|
url: item.url
|
|
|
- })
|
|
|
+ });
|
|
|
} else {
|
|
|
uni.navigateTo({
|
|
|
url: allUrl
|
|
|
- })
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+};
|
|
|
/** *
|
|
|
* 是否登录
|
|
|
*/
|
|
|
export const isLogin = () => {
|
|
|
- let token = uni.getStorageSync('token')
|
|
|
+ let token = uni.getStorageSync("token");
|
|
|
if (token) {
|
|
|
- return true
|
|
|
+ return true;
|
|
|
}
|
|
|
- return false
|
|
|
-}
|
|
|
+ return false;
|
|
|
+};
|
|
|
export default {
|
|
|
isEmpty,
|
|
|
+ copyObject,
|
|
|
imgSubstr,
|
|
|
isArray,
|
|
|
parse,
|
|
|
@@ -249,4 +293,4 @@ export default {
|
|
|
isLogin,
|
|
|
getCheckLogin,
|
|
|
numberFormat
|
|
|
-}
|
|
|
+};
|