163 lines
4.0 KiB
TypeScript
163 lines
4.0 KiB
TypeScript
import Taro from '@tarojs/taro'
|
||
import Qs from 'qs'
|
||
|
||
/**
|
||
* 跳转
|
||
* @param path
|
||
* @param params
|
||
* @param type false 跳转普通页面,true 跳转tabbar页面
|
||
*/
|
||
type ParamLink = 'navigateTo' | 'switchTab' | 'reLaunch' | 'redirectTo'
|
||
export const goLink = (path: string = '', params = null, way: ParamLink = 'navigateTo') => {
|
||
if (path) {
|
||
// let params_str = Qs_.stringify(params || {}, { encode: false })
|
||
// path = params_str ? path + '?' + params_str : path
|
||
path = setUrlQuery({ url: path, query: params })
|
||
Taro[way]({ url: path })
|
||
}
|
||
}
|
||
const setUrlQuery = (options: { url: string; query: object | null }) => {
|
||
let { url, query } = options
|
||
if (!url) return ''
|
||
if (query) {
|
||
let queryArr: any[] = []
|
||
for (const key in query) {
|
||
if (query.hasOwnProperty(key)) {
|
||
queryArr.push(`${key}=${query[key]}`)
|
||
}
|
||
}
|
||
if (url.indexOf('?') !== -1) {
|
||
url = `${url}&${queryArr.join('&')}`
|
||
} else {
|
||
url = `${url}?${queryArr.join('&')}`
|
||
}
|
||
}
|
||
return url
|
||
}
|
||
/**
|
||
* 判断对象为空
|
||
* @param object
|
||
* @returns
|
||
*/
|
||
export const isEmptyObject = (object: any) => {
|
||
if (object == undefined || object == null || Number.isNaN(object)) {
|
||
return true
|
||
} else {
|
||
if (object.constructor == Object) {
|
||
return Reflect.ownKeys(object).length == 0
|
||
} else if (object.constructor == Array) {
|
||
return object.length == 0
|
||
} else if (object.constructor == String) {
|
||
return object == ''
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
/**
|
||
* 表单检索
|
||
* @param data
|
||
* @param rules = {
|
||
account: [{
|
||
message: "请输入正确的用户名",
|
||
// regex: /\d/, // 正则匹配规则
|
||
// validator: (value:any, rule:any)=>{ // 自定义验证,返回true表示匹配到了(错误)
|
||
// return false;
|
||
// }
|
||
}],
|
||
password: [{
|
||
message: "请输入正确的密码",
|
||
// regex: /\d/
|
||
}]
|
||
};
|
||
* @param message
|
||
* @returns
|
||
*/
|
||
export const retrieval = (data: any, rules?: Object, message: string = '请填写完信息') => {
|
||
return new Promise((resolve, reject) => {
|
||
if (rules) {
|
||
const keys = Reflect.ownKeys(rules)
|
||
const result = keys.some((key: any) => {
|
||
for (let item of (rules as any)[key]) {
|
||
let _res = false
|
||
if (item.validator) {
|
||
if (item.validator(data[key], item)) {
|
||
_res = true
|
||
}
|
||
} else if (item.regex) {
|
||
if (!item.regex.test(data[key])) {
|
||
_res = true
|
||
}
|
||
} else {
|
||
if (isEmptyObject(data[key])) {
|
||
_res = true
|
||
}
|
||
}
|
||
message = item.message
|
||
return _res
|
||
}
|
||
})
|
||
if (result) {
|
||
reject(message)
|
||
}
|
||
} else {
|
||
const keys = Reflect.ownKeys(data)
|
||
if (keys.some((key: any) => isEmptyObject(data[key]))) {
|
||
reject(message)
|
||
}
|
||
}
|
||
resolve(null)
|
||
})
|
||
}
|
||
/**
|
||
* toast提示
|
||
*/
|
||
export const alert = {
|
||
success(title: string) {
|
||
Taro.showToast({
|
||
title,
|
||
icon: 'success',
|
||
})
|
||
},
|
||
error(title: string) {
|
||
Taro.showToast({
|
||
title,
|
||
icon: 'error',
|
||
})
|
||
},
|
||
loading(title: string, mask: true | false = false) {
|
||
Taro.showToast({
|
||
title,
|
||
icon: 'loading',
|
||
mask,
|
||
})
|
||
},
|
||
none(title: string) {
|
||
Taro.showToast({
|
||
title,
|
||
icon: 'none',
|
||
})
|
||
},
|
||
showLoading(title: string, mask: true | false = true) {
|
||
Taro.showLoading({ title, mask })
|
||
},
|
||
hideLoading() {
|
||
Taro.hideLoading()
|
||
},
|
||
}
|
||
|
||
// 金额千位分割符
|
||
export const formatKbPrice = (number: string) => {
|
||
const ret = Array.from(number)
|
||
.reverse()
|
||
.reduce((result: string[], next, i, arr) => {
|
||
if ((i + 1) % 3 === 0 && i + 1 !== arr.length) {
|
||
result.push(next, ',')
|
||
return result
|
||
}
|
||
result.push(next)
|
||
return result
|
||
}, [])
|
||
return ret.reverse().join('')
|
||
}
|